From 3d0550a96cb78b8289cd340a59c4ea5f7a966ed2 Mon Sep 17 00:00:00 2001
From: Monty Taylor <mordred@inaugust.com>
Date: Mon, 15 Jan 2018 19:12:55 -0600
Subject: [PATCH] Revert "Revert "Add consolidated role for processing
 subunit""

This reverts commit 9f4efe98e10c9d553dc8a15bd7b84584e1118684.

Compress testr results.html before fetching it

The synchronize command has testr_results.html.gz in the file list, but
we don't ever gzip it so it fails on fetch.

Make the "Check for testr directory" task actually check for
.testrepository and not for .stestr a second time.

The compression bug broke only for people using stestr. For people
using testr, the path bug meant they don't get html reports.

Change-Id: I0cdfc66ee8b046affeb0b071fef38c21cb7a4948
---
 playbooks/unittests/post.yaml                 |  3 +-
 roles/fetch-subunit-output/README.rst         | 12 +++++
 roles/fetch-subunit-output/defaults/main.yaml |  2 +
 roles/fetch-subunit-output/tasks/main.yaml    | 48 +++++++++++++++++++
 roles/fetch-subunit-output/tasks/process.yaml | 22 +++++++++
 5 files changed, 85 insertions(+), 2 deletions(-)
 create mode 100644 roles/fetch-subunit-output/README.rst
 create mode 100644 roles/fetch-subunit-output/defaults/main.yaml
 create mode 100644 roles/fetch-subunit-output/tasks/main.yaml
 create mode 100644 roles/fetch-subunit-output/tasks/process.yaml

diff --git a/playbooks/unittests/post.yaml b/playbooks/unittests/post.yaml
index dcf3709e8..a19836486 100644
--- a/playbooks/unittests/post.yaml
+++ b/playbooks/unittests/post.yaml
@@ -1,4 +1,3 @@
 - hosts: all
   roles:
-    - fetch-testr-output
-    - fetch-stestr-output
+    - fetch-subunit-output
diff --git a/roles/fetch-subunit-output/README.rst b/roles/fetch-subunit-output/README.rst
new file mode 100644
index 000000000..1d9c992bb
--- /dev/null
+++ b/roles/fetch-subunit-output/README.rst
@@ -0,0 +1,12 @@
+Collect subunit outputs
+
+**Role Variables**
+
+.. zuul:rolevar:: zuul_work_dir
+   :default: {{ zuul.project.src_dir }}
+
+   Directory to work in
+
+.. zuul:rolevar:: tox_envlist
+
+   tox environment that was used to run the tests originally.
diff --git a/roles/fetch-subunit-output/defaults/main.yaml b/roles/fetch-subunit-output/defaults/main.yaml
new file mode 100644
index 000000000..2cdab6ceb
--- /dev/null
+++ b/roles/fetch-subunit-output/defaults/main.yaml
@@ -0,0 +1,2 @@
+---
+zuul_work_dir: "{{ zuul.project.src_dir }}"
diff --git a/roles/fetch-subunit-output/tasks/main.yaml b/roles/fetch-subunit-output/tasks/main.yaml
new file mode 100644
index 000000000..a552d3edf
--- /dev/null
+++ b/roles/fetch-subunit-output/tasks/main.yaml
@@ -0,0 +1,48 @@
+- name: Check for stestr directory
+  stat:
+    path: "{{ zuul_work_dir }}/.stestr"
+  register: stestr_stat
+
+- name: Generate stestr subunit file
+  shell:
+    cmd: ".tox/{{ tox_envlist }}/bin/stestr last --subunit > ./testrepository.subunit"
+    chdir: "{{ zuul_work_dir }}"
+  when:
+    - tox_envlist is defined
+    - stestr_stat.stat.exists
+
+- name: Generate stestr subunit file
+  shell:
+    cmd: "stestr last --subunit > ./testrepository.subunit"
+    chdir: "{{ zuul_work_dir }}"
+  when:
+    - tox_envlist is not defined
+    - stestr_stat.stat.exists
+
+- name: Check for testr directory
+  stat:
+    path: "{{ zuul_work_dir }}/.testrepository"
+  register: testr_stat
+  when: not stestr_stat.stat.exists
+
+- name: Generate testrepository.subunit file
+  shell:
+    cmd: ".tox/{{ tox_envlist }}/bin/testr last --subunit > ./testrepository.subunit"
+    chdir: "{{ zuul_work_dir }}"
+  when:
+    - tox_envlist is defined
+    - not stestr_stat.stat.exists
+    - testr_stat.stat.exists
+
+- name: Generate testrepository.subunit file
+  shell:
+    cmd: "testr last --subunit > ./testrepository.subunit"
+    chdir: "{{ zuul_work_dir }}"
+  when:
+    - tox_envlist is not defined
+    - not stestr_stat.stat.exists
+    - testr_stat.stat.exists
+
+- name: Process and fetch subunit results
+  include: process.yaml
+  when: stestr_stat.stat.exists or testr_stat.stat.exists
diff --git a/roles/fetch-subunit-output/tasks/process.yaml b/roles/fetch-subunit-output/tasks/process.yaml
new file mode 100644
index 000000000..b2b6aa013
--- /dev/null
+++ b/roles/fetch-subunit-output/tasks/process.yaml
@@ -0,0 +1,22 @@
+- 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: Compress testrepository files
+  archive:
+    path: "{{ zuul_work_dir }}/{{ item }}"
+  with_items:
+    - testrepository.subunit
+    - testr_results.html
+
+- name: Collect test-results
+  synchronize:
+    dest: "{{ zuul.executor.log_root }}"
+    mode: pull
+    src: "{{ zuul_work_dir }}/{{ item }}"
+    verify_host: true
+  with_items:
+    - "*testr_results.html.gz"
+    - "*testrepository.subunit.gz"