From ad0d40f63766222c3779f6a326c1117b3f809fd8 Mon Sep 17 00:00:00 2001 From: Sviatoslav Sydorenko Date: Thu, 30 Aug 2018 22:14:30 +0200 Subject: [PATCH 1/3] Test integration of loop and until options Ref #44741 Ref ansible/proposals#140 --- test/integration/targets/loop-until/aliases | 1 + .../targets/loop-until/tasks/main.yml | 156 ++++++++++++++++++ 2 files changed, 157 insertions(+) create mode 100644 test/integration/targets/loop-until/aliases create mode 100644 test/integration/targets/loop-until/tasks/main.yml diff --git a/test/integration/targets/loop-until/aliases b/test/integration/targets/loop-until/aliases new file mode 100644 index 00000000000000..765b70da7963c6 --- /dev/null +++ b/test/integration/targets/loop-until/aliases @@ -0,0 +1 @@ +shippable/posix/group2 diff --git a/test/integration/targets/loop-until/tasks/main.yml b/test/integration/targets/loop-until/tasks/main.yml new file mode 100644 index 00000000000000..447d5b00c63c0f --- /dev/null +++ b/test/integration/targets/loop-until/tasks/main.yml @@ -0,0 +1,156 @@ +# Test code for integration of until and loop options +# Copyright: (c) 2018, Ansible Project + +# 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 . +- shell: '{{ ansible_python.executable }} -c "import tempfile; print(tempfile.mkstemp()[1])"' + register: tempfilepaths + # 0 to 3: + loop: "{{ range(0, 3 + 1) | list }}" + +- set_fact: + "until_tempfile_path_{{ idx }}": "{{ tmp_file.stdout }}" + loop: "{{ tempfilepaths.results }}" + loop_control: + index_var: idx + loop_var: tmp_file + +- set_fact: + until_tempfile_path_var_names: > + {{ vars | select('match', '^until_tempfile_path_') | list }} + +- name: loop and until with 6 retries + shell: echo "run" >> {{ lookup('vars', tmp_file_var) }} && wc -w < {{ lookup('vars', tmp_file_var) }} | tr -d ' ' + register: runcount + until: runcount.stdout | int == idx + 3 + retries: "{{ idx + 2 }}" + delay: 0.01 + loop: "{{ until_tempfile_path_var_names }}" + loop_control: + index_var: idx + loop_var: tmp_file_var + +- debug: var=runcount + +- assert: + that: item.stdout | int == idx + 3 + loop: "{{ runcount.results }}" + loop_control: + index_var: idx + +- &cleanup-tmp-files + name: Empty tmp files + copy: + content: "" + dest: "{{ lookup('vars', tmp_file_var) }}" + loop: "{{ until_tempfile_path_var_names }}" + loop_control: + index_var: idx + loop_var: tmp_file_var + +- name: loop with specified max retries + shell: echo "run" >> {{ lookup('vars', tmp_file_var) }} + until: 1==0 + retries: 5 + delay: 0.01 + ignore_errors: true + loop: "{{ until_tempfile_path_var_names }}" + loop_control: + index_var: idx + loop_var: tmp_file_var + +- name: validate output + shell: wc -l < {{ lookup('vars', tmp_file_var) }} + register: runcount + loop: "{{ until_tempfile_path_var_names }}" + loop_control: + index_var: idx + loop_var: tmp_file_var + +- assert: + that: item.stdout | int == 6 # initial + 5 retries + loop: "{{ runcount.results }}" + +- *cleanup-tmp-files + +- name: Test failed_when impacting until + shell: echo "run" >> {{ lookup('vars', tmp_file_var) }} + register: failed_when_until + failed_when: True + until: failed_when_until is successful + retries: 3 + delay: 0.5 + ignore_errors: True + loop: "{{ until_tempfile_path_var_names }}" + loop_control: + index_var: idx + loop_var: tmp_file_var + +- name: Get attempts number + shell: wc -l < {{ lookup('vars', tmp_file_var) }} + register: runcount + loop: "{{ until_tempfile_path_var_names }}" + loop_control: + index_var: idx + loop_var: tmp_file_var + +- assert: + that: item.stdout | int == 3 + 1 + loop: "{{ runcount.results }}" + +- *cleanup-tmp-files + +- name: Test changed_when impacting until + shell: echo "run" >> {{ lookup('vars', tmp_file_var) }} + register: changed_when_until + changed_when: False + until: changed_when_until is changed + retries: 3 + delay: 0.5 + ignore_errors: True + loop: "{{ until_tempfile_path_var_names }}" + loop_control: + index_var: idx + loop_var: tmp_file_var + +- name: Get attempts number + shell: wc -l < {{ lookup('vars', tmp_file_var) }} + register: runcount + loop: "{{ until_tempfile_path_var_names }}" + loop_control: + index_var: idx + loop_var: tmp_file_var + +- assert: + that: item.stdout | int == 3 + 1 + loop: "{{ runcount.results }}" + +- *cleanup-tmp-files + +- name: Test access to attempts in changed_when/failed_when + shell: 'true' + register: changed_when_attempts + until: 1 == 0 + retries: 5 + delay: 0.5 + failed_when: changed_when_attempts.attempts > 6 + loop: "{{ runcount.results }}" + +- &wipe-out-tmp-files + file: path="{{ lookup('vars', tmp_file_var) }}" state=absent + loop: "{{ until_tempfile_path_var_names }}" + loop_control: + index_var: idx + loop_var: tmp_file_var From 533b5926a2693f8d87ad51d94bd20a10f92312d7 Mon Sep 17 00:00:00 2001 From: Sviatoslav Sydorenko Date: Sun, 2 Sep 2018 14:07:37 +0200 Subject: [PATCH 2/3] Replace select filter with a more portable thing --- test/integration/targets/loop-until/tasks/main.yml | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/test/integration/targets/loop-until/tasks/main.yml b/test/integration/targets/loop-until/tasks/main.yml index 447d5b00c63c0f..bb3799a30f5733 100644 --- a/test/integration/targets/loop-until/tasks/main.yml +++ b/test/integration/targets/loop-until/tasks/main.yml @@ -22,14 +22,18 @@ - set_fact: "until_tempfile_path_{{ idx }}": "{{ tmp_file.stdout }}" + until_tempfile_path_var_names: > + {{ [ 'until_tempfile_path_' + idx | string ] + until_tempfile_path_var_names | default([]) }} loop: "{{ tempfilepaths.results }}" loop_control: index_var: idx loop_var: tmp_file -- set_fact: - until_tempfile_path_var_names: > - {{ vars | select('match', '^until_tempfile_path_') | list }} +# `select` filter is only available since Jinja 2.7, +# thus tests are failing under CentOS in CI +#- set_fact: +# until_tempfile_path_var_names: > +# {{ vars | select('match', '^until_tempfile_path_') | list }} - name: loop and until with 6 retries shell: echo "run" >> {{ lookup('vars', tmp_file_var) }} && wc -w < {{ lookup('vars', tmp_file_var) }} | tr -d ' ' From 43a8cd53afa4546085150643ec3bdeb551eb4b39 Mon Sep 17 00:00:00 2001 From: Sam Doran Date: Mon, 29 Nov 2021 17:03:13 -0500 Subject: [PATCH 3/3] Add context This is needed for split controller/remote --- test/integration/targets/loop-until/aliases | 1 + 1 file changed, 1 insertion(+) diff --git a/test/integration/targets/loop-until/aliases b/test/integration/targets/loop-until/aliases index 765b70da7963c6..90ea9e12811c08 100644 --- a/test/integration/targets/loop-until/aliases +++ b/test/integration/targets/loop-until/aliases @@ -1 +1,2 @@ shippable/posix/group2 +context/controller