diff options
author | Matt Clay <matt@mystile.com> | 2016-10-13 09:09:25 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-10-13 09:09:25 -0700 |
commit | 75e4645ee70434e2706845ca9fe96d5823616eea (patch) | |
tree | 360f27f11eeb33ba993ae3f30764589b3981e99c /test/integration/targets/conditionals | |
parent | 374e4348e4d76cff7c0e6e41cb2368799c0ae443 (diff) | |
download | ansible-75e4645ee70434e2706845ca9fe96d5823616eea.tar.gz |
Migrate Linux CI roles to test targets. (#17997)
Diffstat (limited to 'test/integration/targets/conditionals')
-rw-r--r-- | test/integration/targets/conditionals/tasks/main.yml | 309 | ||||
-rw-r--r-- | test/integration/targets/conditionals/vars/main.yml | 17 |
2 files changed, 326 insertions, 0 deletions
diff --git a/test/integration/targets/conditionals/tasks/main.yml b/test/integration/targets/conditionals/tasks/main.yml new file mode 100644 index 0000000000..0908b910fb --- /dev/null +++ b/test/integration/targets/conditionals/tasks/main.yml @@ -0,0 +1,309 @@ +# test code for conditional statements +# (c) 2014, James Cammarata <jcammarata@ansible.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/>. + +- name: test conditional '==' + shell: echo 'testing' + when: 1 == 1 + register: result + +- name: assert conditional '==' ran + assert: + that: + - "result.changed == true" + - "result.stdout == 'testing'" + - "result.rc == 0" + +- name: test bad conditional '==' + shell: echo 'testing' + when: 0 == 1 + register: result + +- name: assert bad conditional '==' did NOT run + assert: + that: + - "result.skipped == true" + +- name: test conditional '!=' + shell: echo 'testing' + when: 0 != 1 + register: result + +- name: assert conditional '!=' ran + assert: + that: + - "result.changed == true" + - "result.stdout == 'testing'" + - "result.rc == 0" + +- name: test bad conditional '!=' + shell: echo 'testing' + when: 1 != 1 + register: result + +- name: assert bad conditional '!=' did NOT run + assert: + that: + - "result.skipped == true" + +- name: test conditional 'in' + shell: echo 'testing' + when: 1 in [1,2,3] + register: result + +- name: assert conditional 'in' ran + assert: + that: + - "result.changed == true" + - "result.stdout == 'testing'" + - "result.rc == 0" + +- name: test bad conditional 'in' + shell: echo 'testing' + when: 1 in [7,8,9] + register: result + +- name: assert bad conditional 'in' did NOT run + assert: + that: + - "result.skipped == true" + +- name: test conditional 'not in' + shell: echo 'testing' + when: 0 not in [1,2,3] + register: result + +- name: assert conditional 'not in' ran + assert: + that: + - "result.changed == true" + - "result.stdout == 'testing'" + - "result.rc == 0" + +- name: test bad conditional 'not in' + shell: echo 'testing' + when: 1 not in [1,2,3] + register: result + +- name: assert bad conditional 'not in' did NOT run + assert: + that: + - "result.skipped == true" + +- name: test conditional 'is defined' + shell: echo 'testing' + when: test_bare is defined + register: result + +- name: assert conditional 'is defined' ran + assert: + that: + - "result.changed == true" + - "result.stdout == 'testing'" + - "result.rc == 0" + +- name: test bad conditional 'is defined' + shell: echo 'testing' + when: foo_asdf_xyz is defined + register: result + +- name: assert bad conditional 'is defined' did NOT run + assert: + that: + - "result.skipped == true" + +- name: test conditional 'is not defined' + shell: echo 'testing' + when: foo_asdf_xyz is not defined + register: result + +- name: assert conditional 'is not defined' ran + assert: + that: + - "result.changed == true" + - "result.stdout == 'testing'" + - "result.rc == 0" + +- name: test bad conditional 'is not defined' + shell: echo 'testing' + when: test_bare is not defined + register: result + +- name: assert bad conditional 'is not defined' did NOT run + assert: + that: + - "result.skipped == true" + +- name: test bad conditional 'is undefined' + shell: echo 'testing' + when: test_bare is undefined + register: result + +- name: assert bad conditional 'is undefined' did NOT run + assert: + that: + - "result.skipped == true" + +- name: test bare conditional + shell: echo 'testing' + when: test_bare + register: result + +- name: assert bare conditional ran + assert: + that: + - "result.changed == true" + - "result.stdout == 'testing'" + - "result.rc == 0" + +- name: test conditional using a variable + shell: echo 'testing' + when: test_bare_var == 123 + register: result + +- name: assert conditional using a variable ran + assert: + that: + - "result.changed == true" + - "result.stdout == 'testing'" + - "result.rc == 0" + +- name: test good conditional based on nested variables + shell: echo 'testing' + when: test_bare_nested_good + register: result + +- name: assert good conditional based on nested var ran + assert: + that: + - "result.changed == true" + - "result.stdout == 'testing'" + - "result.rc == 0" + +- name: test bad conditional based on nested variables + shell: echo 'testing' + when: test_bare_nested_bad + register: result + +- name: assert that the bad nested conditional did NOT run + assert: + that: + - "result.skipped == true" + +#----------------------------------------------------------------------- +# proper booleanification tests (issue #8629) + +- name: set fact to string 'false' + set_fact: bool_test1=false + +- name: set fact to string 'False' + set_fact: bool_test2=False + +- name: set fact to a proper boolean using complex args + set_fact: + bool_test3: false + +- name: "test boolean value 'false' string using 'when: var'" + command: echo 'hi' + when: bool_test1 + register: result + +- name: assert that the task did not run for 'false' + assert: + that: + - "result.skipped == true" + +- name: "test boolean value 'false' string using 'when: not var'" + command: echo 'hi' + when: not bool_test1 + register: result + +- name: assert that the task DID run for not 'false' + assert: + that: + - "result.changed" + +- name: "test boolean value of 'False' string using 'when: var'" + command: echo 'hi' + when: bool_test2 + register: result + +- name: assert that the task did not run for 'False' + assert: + that: + - "result.skipped == true" + +- name: "test boolean value 'False' string using 'when: not var'" + command: echo 'hi' + when: not bool_test2 + register: result + +- name: assert that the task DID run for not 'False' + assert: + that: + - "result.changed" + +- name: "test proper boolean value of complex arg using 'when: var'" + command: echo 'hi' + when: bool_test3 + register: result + +- name: assert that the task did not run for proper boolean false + assert: + that: + - "result.skipped == true" + +- name: "test proper boolean value of complex arg using 'when: not var'" + command: echo 'hi' + when: not bool_test3 + register: result + +- name: assert that the task DID run for not false + assert: + that: + - "result.changed" + +- set_fact: skipped_bad_attribute=True +- block: + - name: test a with_items loop using a variable with a missing attribute + debug: var=item + with_items: "{{cond_bad_attribute.results | default('')}}" + register: result + - set_fact: skipped_bad_attribute=False + - name: assert the task was skipped + assert: + that: + - skipped_bad_attribute + when: cond_bad_attribute is defined and 'results' in cond_bad_attribute + +- name: test a with_items loop skipping a single item + debug: var=item + with_items: "{{cond_list_of_items.results}}" + when: item != 'b' + register: result + +- debug: var=result + +- name: assert only a single item was skipped + assert: + that: + - result.results|length == 3 + - result.results[1].skipped + +- name: test complex templated condition + debug: msg="it works" + when: vars_file_var in things1|union([vars_file_var]) diff --git a/test/integration/targets/conditionals/vars/main.yml b/test/integration/targets/conditionals/vars/main.yml new file mode 100644 index 0000000000..a02b87168e --- /dev/null +++ b/test/integration/targets/conditionals/vars/main.yml @@ -0,0 +1,17 @@ +--- +# foo is a dictionary that will be used to check that +# a conditional passes a with_items loop on a variable +# with a missing attribute (ie. foo.results) +cond_bad_attribute: + bar: a + +cond_list_of_items: + results: + - a + - b + - c + +things1: + - 1 + - 2 +vars_file_var: 321 |