From 59bacd1518ef2659b70fc983671dc8b4c7bac437 Mon Sep 17 00:00:00 2001
From: Tristan Cacqueray <tdecacqu@redhat.com>
Date: Thu, 12 Sep 2019 19:45:55 +0000
Subject: [PATCH] fetch-sphinx-output: introduce zuul_use_fetch_output

This change enables using fetch-sphinx-output role along with
the fetch-output role. By default the role still synchronizes
artifacts back to the executor.

Change-Id: I81c8a2faf566be694dbf1c59cbbd157dc4df5146
---
 roles/fetch-sphinx-output/README.rst          |  8 +++++
 roles/fetch-sphinx-output/defaults/main.yaml  |  2 ++
 roles/fetch-sphinx-output/tasks/main.yaml     |  8 +++++
 .../python/fetch-sphinx-output.yaml           | 33 +++++++++++++++++++
 zuul-tests.d/python-jobs.yaml                 | 24 ++++++++++++++
 5 files changed, 75 insertions(+)
 create mode 100644 test-playbooks/python/fetch-sphinx-output.yaml

diff --git a/roles/fetch-sphinx-output/README.rst b/roles/fetch-sphinx-output/README.rst
index 3f84dd994..0c854aada 100644
--- a/roles/fetch-sphinx-output/README.rst
+++ b/roles/fetch-sphinx-output/README.rst
@@ -34,3 +34,11 @@ to the log root of the executor.
    :default: {{ zuul.project.src_dir }}
 
    The location of the main working directory of the job.
+
+.. zuul:rolevar:: zuul_use_fetch_output
+   :default: false
+
+   Whether to synchronize files to the executor work dir, or to copy them
+   on the test instance.
+   When set to false, the role synchronizes the file to the executor.
+   When set to true, the job needs to use the fetch-output role later.
diff --git a/roles/fetch-sphinx-output/defaults/main.yaml b/roles/fetch-sphinx-output/defaults/main.yaml
index 33c0a57e2..c9ba69c11 100644
--- a/roles/fetch-sphinx-output/defaults/main.yaml
+++ b/roles/fetch-sphinx-output/defaults/main.yaml
@@ -1,6 +1,8 @@
 ---
 zuul_executor_dest: "{{ zuul.executor.log_root }}"
 zuul_work_dir: "{{ zuul.project.src_dir }}"
+zuul_output_dir: "{{ ansible_user_dir }}/zuul-output"
+zuul_use_fetch_output: "{{ zuul_site_use_fetch_output|default(false) }}"
 sphinx_build_dir: doc/build
 sphinx_output_suffix: ""
 sphinx_output_src: "{{ zuul_work_dir }}/{{ sphinx_build_dir }}/html{{ sphinx_output_suffix }}"
diff --git a/roles/fetch-sphinx-output/tasks/main.yaml b/roles/fetch-sphinx-output/tasks/main.yaml
index d545b01ba..48a4a7b68 100644
--- a/roles/fetch-sphinx-output/tasks/main.yaml
+++ b/roles/fetch-sphinx-output/tasks/main.yaml
@@ -4,6 +4,14 @@
     mode: pull
     src: "{{ sphinx_output_src }}"
     verify_host: true
+  when: not zuul_use_fetch_output
+
+- name: Copy sphinx build html
+  copy:
+    dest: "{{ zuul_output_dir }}/logs/"
+    src: "{{ sphinx_output_src }}"
+    remote_src: true
+  when: zuul_use_fetch_output
 
 - name: Return artifact to Zuul
   zuul_return:
diff --git a/test-playbooks/python/fetch-sphinx-output.yaml b/test-playbooks/python/fetch-sphinx-output.yaml
new file mode 100644
index 000000000..0b161051e
--- /dev/null
+++ b/test-playbooks/python/fetch-sphinx-output.yaml
@@ -0,0 +1,33 @@
+- hosts: all
+  pre_tasks:
+    # Run ensure-output-dirs now as it is not performed speculatively
+    - import_role:
+        name: ensure-output-dirs
+
+    - name: Create fake sphinx output
+      shell: |
+        mkdir -p {{ zuul.project.src_dir }}/doc/build/html
+        echo "<body>Hello</body>" > {{ zuul.project.src_dir }}/doc/build/html/index.html
+
+  tasks:
+    - import_role:
+        name: fetch-sphinx-output
+
+    - import_role:
+        name: fetch-output
+      when: zuul_use_fetch_output
+
+  post_tasks:
+    - name: Check for artifact on the test instance
+      stat:
+        path: "{{ ansible_user_dir }}/zuul-output/logs/html/index.html"
+      register: _test_artifact
+      failed_when: not _test_artifact.stat.exists
+      when: zuul_use_fetch_output
+
+    - name: Check for artifact on the executor
+      stat:
+        path: "{{ zuul.executor.log_root }}/html/index.html"
+      delegate_to: localhost
+      register: _executor_artifact
+      failed_when: not _executor_artifact.stat.exists
diff --git a/zuul-tests.d/python-jobs.yaml b/zuul-tests.d/python-jobs.yaml
index 05f387559..8157ba7c1 100644
--- a/zuul-tests.d/python-jobs.yaml
+++ b/zuul-tests.d/python-jobs.yaml
@@ -458,6 +458,26 @@
     vars:
       zuul_use_fetch_output: false
 
+- job:
+    name: zuul-jobs-test-fetch-sphinx-output
+    description: Test the fetch-sphinx-output
+    files:
+      - roles/ensure-output-dirs/.*
+      - roles/fetch-sphinx-output/.*
+      - roles/fetch-output/.*
+    run: test-playbooks/python/fetch-sphinx-output.yaml
+    vars:
+      zuul_use_fetch_output: true
+
+- job:
+    name: zuul-jobs-test-fetch-sphinx-output-synchronize
+    description: Test the fetch-sphinx-output
+    files:
+      - roles/fetch-sphinx-output/.*
+    run: test-playbooks/python/fetch-sphinx-output.yaml
+    vars:
+      zuul_use_fetch_output: false
+
 - project:
     check:
       jobs:
@@ -503,6 +523,8 @@
         - zuul-jobs-test-fetch-tox-output-synchronize
         - zuul-jobs-test-fetch-subunit-output
         - zuul-jobs-test-fetch-subunit-output-synchronize
+        - zuul-jobs-test-fetch-sphinx-output
+        - zuul-jobs-test-fetch-sphinx-output-synchronize
     gate:
       jobs:
         - zuul-jobs-test-ensure-pip-centos-7
@@ -544,3 +566,5 @@
         - zuul-jobs-test-fetch-tox-output-synchronize
         - zuul-jobs-test-fetch-subunit-output
         - zuul-jobs-test-fetch-subunit-output-synchronize
+        - zuul-jobs-test-fetch-sphinx-output
+        - zuul-jobs-test-fetch-sphinx-output-synchronize