From 022832a1d7d1d0b402d93e68c1f6aca73bb42d89 Mon Sep 17 00:00:00 2001
From: Monty Taylor <mordred@inaugust.com>
Date: Sat, 2 Sep 2017 09:54:19 -0500
Subject: [PATCH] Add UPPER_CONSTRAINTS_FILE file if it exists

Create a parameter called tox_upper_constraints. The parameter points
to a file on the system. If the file exists, it will be passed to the
tox invocation in the UPPER_CONSTRAINTS_FILE environment variable.

This allows a global setting in a base job of a known file to be the
UPPER_CONSTRAINTS_FILE file, or for that to be overridden on a per-job
basis. For people who do not know or care what an
UPPER_CONSTRAINTS_FILE file is, the behavior should be a no-op.

Also, collapse tox_environment and tox_environment_defaults.
zuul already does variable merging on parent and child jobs, so we don't
need to do it in the playbook.

While we're in there, clean up a few small things in the yaml. Notably,
jlk says we should not put variables in task names, and we don't need
quotes around strings if they don't have special characters.

Change-Id: Ibede59ca6f85914952da175c77ea29391ef9a88c
---
 roles/tox/README.rst         | 13 ++++++-------
 roles/tox/defaults/main.yaml |  3 +--
 roles/tox/tasks/main.yaml    | 23 +++++++++++++++++------
 zuul.yaml                    |  7 ++++++-
 4 files changed, 30 insertions(+), 16 deletions(-)

diff --git a/roles/tox/README.rst b/roles/tox/README.rst
index 45fa89abf..45f8a3daa 100644
--- a/roles/tox/README.rst
+++ b/roles/tox/README.rst
@@ -6,12 +6,6 @@ Runs tox for a project
 
    Environment variables to pass in to the tox run.
 
-.. zuul:rolevar:: tox_environment_defaults
-
-   Default environment variables to pass in to the tox run. Intended
-   to allow setting a set of environment variables in a base job but
-   to still allow specific settings on a per-job or per-variant basis.
-
 .. zuul:rolevar:: tox_envlist
    :default: venv
 
@@ -27,7 +21,12 @@ Runs tox for a project
 
    String of extra command line options to pass to tox.
 
+.. zuul:rolevar:: tox_upper_constraints_file
+
+   Path to an upper constraints file. Will be provided to tox via
+   UPPER_CONSTRAINTS_FILE environment variable if it exists.
+
 .. zuul:rolevar:: zuul_work_dir
-   :default: src/{{ zuul.project.canonical_name }}
+   :default: {{ zuul.project.src_dir }}
 
    Directory to run tox in.
diff --git a/roles/tox/defaults/main.yaml b/roles/tox/defaults/main.yaml
index cb70ca0a2..f4b08abd2 100644
--- a/roles/tox/defaults/main.yaml
+++ b/roles/tox/defaults/main.yaml
@@ -1,8 +1,7 @@
 ---
 tox_environment: {}
-tox_environment_defaults: {}
 tox_envlist: venv
 tox_executable: tox
 tox_extra_args: -vv
 
-zuul_work_dir: "src/{{ zuul.project.canonical_name }}"
+zuul_work_dir: "{{ zuul.project.src_dir }}"
diff --git a/roles/tox/tasks/main.yaml b/roles/tox/tasks/main.yaml
index 44f7ed797..edb8acc4a 100644
--- a/roles/tox/tasks/main.yaml
+++ b/roles/tox/tasks/main.yaml
@@ -1,14 +1,25 @@
-- name: Require tox_envlist variable - {{ tox_envlist }}
+- name: Require tox_envlist variable
   fail:
-    msg: "tox_envlist is required for this role"
+    msg: tox_envlist is required for this role
   when: tox_envlist is not defined
 
-- name: Create my_tox_environment variable
+- name: Check to see if the constraints file exists
+  stat:
+    path: "{{ tox_upper_constraints_file }}"
+    get_checksum: false
+    get_mime: false
+    get_md5: false
+  register: stat_results
+  when: tox_upper_constraints_file is defined
+
+- name: Record file location
   set_fact:
-    my_tox_environment: "{{ tox_environment_defaults | combine(tox_environment) }}"
+    tox_upper_constraints_env:
+      UPPER_CONSTRAINTS_FILE: "{{ tox_upper_constraints_file }}"
+  when: not stat_results|skipped and stat_results.stat.exists
 
 - name: Run tox
   args:
-    chdir: "{{ zuul_work_dir | default(omit) }}"
-  environment: "{{ my_tox_environment }}"
+    chdir: "{{ zuul_work_dir }}"
+  environment: "{{ tox_environment|combine(tox_upper_constraints_env|default({})) }}"
   command: "{{ tox_executable }} -e{{ tox_envlist }} {{ tox_extra_args }}"
diff --git a/zuul.yaml b/zuul.yaml
index edbfc0ecb..fbea82b7c 100644
--- a/zuul.yaml
+++ b/zuul.yaml
@@ -34,11 +34,16 @@
       .. zuul:jobvar:: tox_extra_args
 
          String containing extra arguments to append to the tox command line.
+
+      .. zuul:jobvar: tox_upper_constraints_file
+
+         Path to an upper constraints file. Will be provided to tox via
+         UPPER_CONSTRAINTS_FILE environment variable if it exists.
     run: playbooks/tox/run
     pre-run: playbooks/tox/pre
     post-run: playbooks/tox/post
     vars:
-      tox_environment_defaults:
+      tox_environment:
         NOSE_WITH_HTML_OUTPUT: 1
         NOSE_HTML_OUT_FILE: nose_results.html
         NOSE_WITH_XUNIT: 1