From d2d4abedc5f2b774682fb5e23372b2bdc6ca07b2 Mon Sep 17 00:00:00 2001
From: Paul Belanger <pabelanger@redhat.com>
Date: Thu, 20 Jul 2017 18:48:42 -0400
Subject: [PATCH] Move subunit processing into fetch-testr-output

This logic makes more sense in the fetch-testr-output role. I've also
left a few TODO action for a followup patch.  Specifically, this
still depends some openstack-infra configuration.

Change-Id: Iea8968708e3e80f0d21434ec08fff5e4ee7bdf4d
Signed-off-by: Paul Belanger <pabelanger@redhat.com>
---
 roles/fetch-testr-output/tasks/main.yaml    | 13 ++++----
 roles/fetch-testr-output/tasks/process.yaml | 33 +++++++++++++++++++++
 roles/tox/tasks/main.yaml                   |  8 -----
 3 files changed, 39 insertions(+), 15 deletions(-)
 create mode 100644 roles/fetch-testr-output/tasks/process.yaml

diff --git a/roles/fetch-testr-output/tasks/main.yaml b/roles/fetch-testr-output/tasks/main.yaml
index 02fcb8d21..5e8f0633d 100644
--- a/roles/fetch-testr-output/tasks/main.yaml
+++ b/roles/fetch-testr-output/tasks/main.yaml
@@ -1,13 +1,12 @@
-- name: Register subunit file
+- name: Register testrepository directory
   stat:
-    path: "{{ zuul_work_dir }}/testrepository.subunit"
-  register: p
+    path: "{{ zuul_work_dir }}/.testrepository"
+  register: testrepository_stat
 
-- name: Compress subunit file to archive to logs server
-  archive:
-    path: "{{ p.stat.path }}"
+- name: Process testr artifacts
+  include: process.yaml
   when:
-    - p.stat.exists
+    - testrepository_stat.stat.exists
 
 - name: Collect test-results
   synchronize:
diff --git a/roles/fetch-testr-output/tasks/process.yaml b/roles/fetch-testr-output/tasks/process.yaml
new file mode 100644
index 000000000..801b2ba69
--- /dev/null
+++ b/roles/fetch-testr-output/tasks/process.yaml
@@ -0,0 +1,33 @@
+- name: Generate testrepository.subunit file
+  # TODO(pabelanger): We cannot depend on tox_envlist here!!!
+  shell: ".tox/{{ tox_envlist }}/bin/testr last --subunit > ./testrepository.subunit"
+  args:
+    chdir: "{{ zuul_work_dir }}"
+
+- name: Generate testr_results.html file
+  # TODO(pabelanger): We cannot depend on /usr/os-testr-env here!!!
+  command: "/usr/os-testr-env/bin/subunit2html ./testrepository.subunit testr_results.html"
+  args:
+    chdir: "{{ zuul_work_dir }}"
+
+- name: Register testrepository.subunit file
+  stat:
+    path: "{{ zuul_work_dir }}/testrepository.subunit"
+  register: p
+
+- name: Compress testrepository.subunit file
+  archive:
+    path: "{{ p.stat.path }}"
+  when:
+    - p.stat.exists
+
+- name: Register testr_results.html file
+  stat:
+    path: "{{ zuul_work_dir }}/testr_results.html"
+  register: html
+
+- name: Compress testr_results.html file
+  archive:
+    path: "{{ html.stat.path }}"
+  when:
+    - html.stat.exists
diff --git a/roles/tox/tasks/main.yaml b/roles/tox/tasks/main.yaml
index 5b6e8fd4f..65d09c327 100644
--- a/roles/tox/tasks/main.yaml
+++ b/roles/tox/tasks/main.yaml
@@ -20,14 +20,6 @@
             return
         fi
 
-        if [ -f ".testrepository/0.2" ] ; then
-            cp .testrepository/0.2 ./testrepository.subunit
-        elif [ -f ".testrepository/0" ] ; then
-            $bin_path/testr last --subunit > ./testrepository.subunit
-        fi
-        /usr/os-testr-env/bin/subunit2html ./testrepository.subunit testr_results.html
-        gzip -9 ./testr_results.html
-
         rancount=$($bin_path/testr last | sed -ne 's/Ran \([0-9]\+\).*tests in.*/\1/p')
         if [ -z "$rancount" ] || [ "$rancount" -eq "0" ] ; then
             echo