From b6adb9a8c45c6917839701be82c92087ec0e0724 Mon Sep 17 00:00:00 2001
From: Monty Taylor <mordred@inaugust.com>
Date: Wed, 17 Jan 2018 11:40:19 -0600
Subject: [PATCH] Don't fetch coverage report from failing tests

When test runs fail, coverage reports are not generated. We try to fetch
them anyway, which leads to POST_FAILURE instead of FAILURE.

Add in a check to ensure the test run was successful, then a check for
the coverage dir and an explicit error message so that it's clear to
people that the issue is that coverage wasn't generated after a
successful run, rather than there being an issue with rsync or
something.

Change-Id: I07f942a9df518d6602ee151d82b35ce413769c73
---
 roles/fetch-coverage-output/README.rst        |  2 +-
 .../fetch-coverage-output/defaults/main.yaml  |  2 +-
 roles/fetch-coverage-output/tasks/main.yaml   | 26 ++++++++++++++-----
 3 files changed, 22 insertions(+), 8 deletions(-)

diff --git a/roles/fetch-coverage-output/README.rst b/roles/fetch-coverage-output/README.rst
index 4058566ff..f618203fc 100644
--- a/roles/fetch-coverage-output/README.rst
+++ b/roles/fetch-coverage-output/README.rst
@@ -12,7 +12,7 @@ on the worker to the log root of the executor.
    root.
 
 .. zuul:rolevar:: coverage_output_src
-   :default: {{ zuul.project.canonical_name }}/cover/
+   :default: {{ zuul.project.src_dir }}/cover/
 
    The location on the worker from which to fetch the coverage
    output detail.  By default, the ``cover`` dir of the current
diff --git a/roles/fetch-coverage-output/defaults/main.yaml b/roles/fetch-coverage-output/defaults/main.yaml
index df5ed6854..4363e1f48 100644
--- a/roles/fetch-coverage-output/defaults/main.yaml
+++ b/roles/fetch-coverage-output/defaults/main.yaml
@@ -1,2 +1,2 @@
 zuul_executor_dest: "{{ zuul.executor.log_root }}"
-coverage_output_src: "src/{{ zuul.project.canonical_name }}/cover"
+coverage_output_src: "{{ zuul.project.src_dir }}/cover"
diff --git a/roles/fetch-coverage-output/tasks/main.yaml b/roles/fetch-coverage-output/tasks/main.yaml
index 63a627ad2..ae283171c 100644
--- a/roles/fetch-coverage-output/tasks/main.yaml
+++ b/roles/fetch-coverage-output/tasks/main.yaml
@@ -1,6 +1,20 @@
-- name: Collect coverage details output
-  synchronize:
-    dest: "{{ zuul_executor_dest }}"
-    mode: pull
-    src: "{{ coverage_output_src }}"
-    verify_host: true
+- when: zuul_success | default(true) | bool
+  block:
+
+    - name: Check to see if coverage report exists
+      stat:
+        path: "{{ coverage_output_src }}"
+      register: coverage_report_stat
+
+    - name: Collect coverage details output
+      synchronize:
+        dest: "{{ zuul_executor_dest }}"
+        mode: pull
+        src: "{{ coverage_output_src }}"
+        verify_host: true
+      when: coverage_report_stat.stat.exists
+
+    - name: Fail if coverage report not found
+      fail:
+        msg: "Coverage report was not found even though tests succeeded"
+      when: not coverage_report_stat.stat.exists