From 1b4ba5431b1070c63b9069d310f42e16f302db10 Mon Sep 17 00:00:00 2001 From: Michael DeHaan <michael@ansible.com> Date: Thu, 20 Feb 2014 13:25:19 -0500 Subject: [PATCH] Update makefile, add files for variable blending tests. --- tests_new/integration/Makefile | 3 +- tests_new/integration/group_vars/all | 9 +++ tests_new/integration/group_vars/local | 1 + tests_new/integration/host_vars/all | 1 + tests_new/integration/host_vars/testhost | 5 ++ tests_new/integration/non_destructive.yml | 2 +- .../roles/test_var_blending/defaults/main.yml | 4 + .../roles/test_var_blending/files/foo.txt | 77 +++++++++++++++++++ .../roles/test_var_blending/meta/main.yml | 3 + .../roles/test_var_blending/tasks/main.yml | 36 +++++++++ .../roles/test_var_blending/templates/foo.j2 | 77 +++++++++++++++++++ .../roles/test_var_blending/vars/main.yml | 4 + .../test_var_blending/vars/more_vars.yml | 1 + tests_new/integration/vars_file.yml | 5 ++ 14 files changed, 226 insertions(+), 2 deletions(-) create mode 100644 tests_new/integration/group_vars/all create mode 100644 tests_new/integration/group_vars/local create mode 100644 tests_new/integration/host_vars/all create mode 100644 tests_new/integration/host_vars/testhost create mode 100644 tests_new/integration/roles/test_var_blending/defaults/main.yml create mode 100644 tests_new/integration/roles/test_var_blending/files/foo.txt create mode 100644 tests_new/integration/roles/test_var_blending/meta/main.yml create mode 100644 tests_new/integration/roles/test_var_blending/tasks/main.yml create mode 100644 tests_new/integration/roles/test_var_blending/templates/foo.j2 create mode 100644 tests_new/integration/roles/test_var_blending/vars/main.yml create mode 100644 tests_new/integration/roles/test_var_blending/vars/more_vars.yml create mode 100644 tests_new/integration/vars_file.yml diff --git a/tests_new/integration/Makefile b/tests_new/integration/Makefile index 0ae635d480..9e80b0d79d 100644 --- a/tests_new/integration/Makefile +++ b/tests_new/integration/Makefile @@ -3,5 +3,6 @@ all: non_destructive destructive # amazon rackspace non_destructive: ansible-playbook non_destructive.yml -i inventory -e @integration_config.yml -v $(TEST_FLAGS) -non_destructive: +destructive: ansible-playbook destructive.yml -i inventory -e @integration_config.yml -v $(TEST_FLAGS) + diff --git a/tests_new/integration/group_vars/all b/tests_new/integration/group_vars/all new file mode 100644 index 0000000000..30aa3d6d56 --- /dev/null +++ b/tests_new/integration/group_vars/all @@ -0,0 +1,9 @@ +a: 999 +b: 998 +c: 997 +d: 996 +uno: 1 +dos: 2 +tres: 3 +etest: 'from group_vars' +inventory_beats_default: 'narf' diff --git a/tests_new/integration/group_vars/local b/tests_new/integration/group_vars/local new file mode 100644 index 0000000000..8feb93fc99 --- /dev/null +++ b/tests_new/integration/group_vars/local @@ -0,0 +1 @@ +tres: 'three' diff --git a/tests_new/integration/host_vars/all b/tests_new/integration/host_vars/all new file mode 100644 index 0000000000..e9789525a9 --- /dev/null +++ b/tests_new/integration/host_vars/all @@ -0,0 +1 @@ +etest: 'from hostvars' diff --git a/tests_new/integration/host_vars/testhost b/tests_new/integration/host_vars/testhost new file mode 100644 index 0000000000..accb6b4715 --- /dev/null +++ b/tests_new/integration/host_vars/testhost @@ -0,0 +1,5 @@ +a: 1 +b: 2 +c: 3 +d: 4 +role_var_beats_inventory: 'should_not_see_this' diff --git a/tests_new/integration/non_destructive.yml b/tests_new/integration/non_destructive.yml index 181030b1e6..5c200858f1 100644 --- a/tests_new/integration/non_destructive.yml +++ b/tests_new/integration/non_destructive.yml @@ -20,5 +20,5 @@ - { role: test_git, tags: test_git } - { role: test_hg, tags: test_hg } - { role: test_changed_when, tags: test_changed_when } - - { role: test_var_blending, tags: test_var_blending } + - { role: test_var_blending, parameterized_beats_default: 1234, tags: test_var_blending } diff --git a/tests_new/integration/roles/test_var_blending/defaults/main.yml b/tests_new/integration/roles/test_var_blending/defaults/main.yml new file mode 100644 index 0000000000..671a1271e5 --- /dev/null +++ b/tests_new/integration/roles/test_var_blending/defaults/main.yml @@ -0,0 +1,4 @@ +etest: "from role defaults" +role_var_beats_default: "shouldn't see this" +parameterized_beats_default: "shouldn't see this" +inventory_beats_default: "shouldn't see this" diff --git a/tests_new/integration/roles/test_var_blending/files/foo.txt b/tests_new/integration/roles/test_var_blending/files/foo.txt new file mode 100644 index 0000000000..a1c0d7591b --- /dev/null +++ b/tests_new/integration/roles/test_var_blending/files/foo.txt @@ -0,0 +1,77 @@ +The value of groups_tree_var = 4000. +This comes from host, not the parents or grandparents. + +The value of the grandparent variable grandparent_var is +not overridden and is = 2000 + +The value of the parent variable is not overriden and +is = 6000 + +The variable 'overridden_in_parent' is set in the parent +and grandparent, so the parent wins. It's value is = 1000. + +The values of 'uno', 'dos', and 'tres' are set in group_vars/all but 'tres' is +set to the value of 'three' in group_vars/local, which should override it. + +uno = 1 +dos = 2 +tres = three + +The values of 'a', 'b', 'c', and 'd' are set in host_vars/local and should not +be clobbered by values that are also set in group_vars. + +a = 1 +b = 2 +c = 3 +d = 4 + +The value of 'badwolf' is set via the include_vars plugin. + +badwolf = badwolf + +The value of 'winter' is set via the main.yml in the role. + +winter = coming + +Here's an arbitrary variable set as vars_files in the playbook. + +vars_file_var = 321 + +And vars. + +vars = 123 + +Variables about other hosts can be looked up via hostvars. This includes +facts but here we'll just access a variable defined in the groups. + +999 + +Ansible has pretty basic precedence rules for variable overriding. We already have +some tests above about group order. Here are a few more. + + * -e variables always win + * then comes "most everything else" + * then comes variables defined in inventory + * then "role defaults", which are the most "defaulty" and lose in priority to everything. + +Given the above rules, here's a test that a -e variable overrides inventory, +and also defaults, and role vars. + +etest = from -e + +Now a test to make sure role variables can override inventory variables. + +role_var_beats_inventory = chevron 5 encoded + +Role variables should also beat defaults. + +role_var_beats_default = chevron 6 encoded + +But defaults are lower priority than inventory, so inventory should win. + +inventory_beats_default = narf + +That's the end of the precedence tests for now, but more are welcome. + + + diff --git a/tests_new/integration/roles/test_var_blending/meta/main.yml b/tests_new/integration/roles/test_var_blending/meta/main.yml new file mode 100644 index 0000000000..1050c23ce3 --- /dev/null +++ b/tests_new/integration/roles/test_var_blending/meta/main.yml @@ -0,0 +1,3 @@ +dependencies: + - prepare_tests + diff --git a/tests_new/integration/roles/test_var_blending/tasks/main.yml b/tests_new/integration/roles/test_var_blending/tasks/main.yml new file mode 100644 index 0000000000..606d80764c --- /dev/null +++ b/tests_new/integration/roles/test_var_blending/tasks/main.yml @@ -0,0 +1,36 @@ +# test code +# (c) 2014, Michael DeHaan <michael.dehaan@gmail.com> + +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see <http://www.gnu.org/licenses/>. + +- include_vars: more_vars.yml + +- name: deploy a template that will use variables at various levels + template: src=foo.j2 dest={{output_dir}}/foo.templated + register: template_result + +- name: copy known good into place + copy: src=foo.txt dest={{output_dir}}/foo.txt + +- name: compare templated file to known good + shell: diff {{output_dir}}/foo.templated {{output_dir}}/foo.txt + register: diff_result + +- name: verify templated file matches known good + assert: + that: + - 'diff_result.stdout == ""' + diff --git a/tests_new/integration/roles/test_var_blending/templates/foo.j2 b/tests_new/integration/roles/test_var_blending/templates/foo.j2 new file mode 100644 index 0000000000..d3361db343 --- /dev/null +++ b/tests_new/integration/roles/test_var_blending/templates/foo.j2 @@ -0,0 +1,77 @@ +The value of groups_tree_var = {{ groups_tree_var }}. +This comes from host, not the parents or grandparents. + +The value of the grandparent variable grandparent_var is +not overridden and is = {{ grandparent_var }} + +The value of the parent variable is not overriden and +is = {{ parent_var }} + +The variable 'overridden_in_parent' is set in the parent +and grandparent, so the parent wins. It's value is = {{ overridden_in_parent }}. + +The values of 'uno', 'dos', and 'tres' are set in group_vars/all but 'tres' is +set to the value of 'three' in group_vars/local, which should override it. + +uno = {{ uno }} +dos = {{ dos }} +tres = {{ tres }} + +The values of 'a', 'b', 'c', and 'd' are set in host_vars/local and should not +be clobbered by values that are also set in group_vars. + +a = {{ a }} +b = {{ b }} +c = {{ c }} +d = {{ d }} + +The value of 'badwolf' is set via the include_vars plugin. + +badwolf = {{ badwolf }} + +The value of 'winter' is set via the main.yml in the role. + +winter = {{ winter }} + +Here's an arbitrary variable set as vars_files in the playbook. + +vars_file_var = {{ vars_file_var }} + +And vars. + +vars = {{ vars_var }} + +Variables about other hosts can be looked up via hostvars. This includes +facts but here we'll just access a variable defined in the groups. + +{{ hostvars['testhost2']['a'] }} + +Ansible has pretty basic precedence rules for variable overriding. We already have +some tests above about group order. Here are a few more. + + * -e variables always win + * then comes "most everything else" + * then comes variables defined in inventory + * then "role defaults", which are the most "defaulty" and lose in priority to everything. + +Given the above rules, here's a test that a -e variable overrides inventory, +and also defaults, and role vars. + +etest = {{ etest }} + +Now a test to make sure role variables can override inventory variables. + +role_var_beats_inventory = {{ role_var_beats_inventory }} + +Role variables should also beat defaults. + +role_var_beats_default = {{ role_var_beats_default }} + +But defaults are lower priority than inventory, so inventory should win. + +inventory_beats_default = {{ inventory_beats_default }} + +That's the end of the precedence tests for now, but more are welcome. + + + diff --git a/tests_new/integration/roles/test_var_blending/vars/main.yml b/tests_new/integration/roles/test_var_blending/vars/main.yml new file mode 100644 index 0000000000..1bb08bf8ac --- /dev/null +++ b/tests_new/integration/roles/test_var_blending/vars/main.yml @@ -0,0 +1,4 @@ +winter: coming +etest: 'from role vars' +role_var_beats_inventory: 'chevron 5 encoded' +role_var_beats_default: 'chevron 6 encoded' diff --git a/tests_new/integration/roles/test_var_blending/vars/more_vars.yml b/tests_new/integration/roles/test_var_blending/vars/more_vars.yml new file mode 100644 index 0000000000..bd9592b561 --- /dev/null +++ b/tests_new/integration/roles/test_var_blending/vars/more_vars.yml @@ -0,0 +1 @@ +badwolf: badwolf diff --git a/tests_new/integration/vars_file.yml b/tests_new/integration/vars_file.yml new file mode 100644 index 0000000000..bd162327d2 --- /dev/null +++ b/tests_new/integration/vars_file.yml @@ -0,0 +1,5 @@ +# this file is here to support testing vars_files in the blending tests only. +# in general define test data in the individual role: +# roles/role_name/vars/main.yml + +vars_file_var: 321