Debian Codegraph @

test/integration/targets/expect/tasks/main.yml

raw
1 # test code for the ping module
2 # (c) 2014, James Cammarata <[email protected]>
3
4 # This file is part of Ansible
5 #
6 # Ansible is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # Ansible is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
18 - name: Install test requirements
19 import_role:
20 name: setup_pexpect
21
22 - name: record the test command files
23 set_fact:
24 test_command_file: "{{ remote_tmp_dir | expanduser }}/test_command.py"
25 test_non_utf8_command_file: "{{ remote_tmp_dir | expanduser }}/test_non_utf8_command.py"
26
27 - name: copy script into output directory
28 copy:
29 src: test_command.py
30 dest: "{{ test_command_file }}"
31 mode: "0444"
32
33 - name: copy non-UTF8 script into output directory
34 copy:
35 src: test_non_utf8_command.py
36 dest: "{{ test_non_utf8_command_file }}"
37 mode: "0444"
38
39 - name: record the output file
40 set_fact: output_file={{remote_tmp_dir}}/foo.txt
41
42 - name: use expect with non-UTF8 output which should fail
43 expect:
44 command: "{{ ansible_python_interpreter }} {{ test_non_utf8_command_file }}"
45 responses:
46 foo: bar
47 register: expect_result
48 ignore_errors: yes
49
50 - name: verify expect with non-UTF8 output failed as expected
51 assert:
52 that:
53 - expect_result is failed
54 - expect_result.msg is contains 'Refusing to deserialize an invalid UTF8 string value' # controller-side failure, not module-side
55
56 - copy:
57 content: "foo"
58 dest: "{{output_file}}"
59
60 - name: test expect
61 expect:
62 command: "{{ansible_python_interpreter}} {{test_command_file}}"
63 responses:
64 foo: bar
65 register: expect_result
66
67 - name: assert expect worked
68 assert:
69 that:
70 - "expect_result.changed == true"
71 - "expect_result.stdout_lines|last == 'foobar'"
72
73 - name: test creates option
74 expect:
75 command: "{{ansible_python_interpreter}} {{test_command_file}}"
76 responses:
77 foo: bar
78 creates: "{{output_file}}"
79 register: creates_result
80
81 - name: assert when creates is provided command is not run
82 assert:
83 that:
84 - "creates_result.changed == false"
85 - "'skipped' in creates_result.stdout"
86
87 - name: test creates option (missing)
88 expect:
89 command: "{{ansible_python_interpreter}} {{test_command_file}}"
90 responses:
91 foo: bar
92 creates: "{{output_file}}.does.not.exist"
93 register: creates_result
94
95 - name: assert when missing creates is provided command is run
96 assert:
97 that:
98 - "creates_result.changed == true"
99 - "creates_result.stdout_lines|last == 'foobar'"
100
101 - name: test removes option
102 expect:
103 command: "{{ansible_python_interpreter}} {{test_command_file}}"
104 responses:
105 foo: bar
106 removes: "{{output_file}}"
107 register: removes_result
108
109 - name: assert when removes is provided command is run
110 assert:
111 that:
112 - "removes_result.changed == true"
113 - "removes_result.stdout_lines|last == 'foobar'"
114
115 - name: test removes option (missing)
116 expect:
117 command: "{{ansible_python_interpreter}} {{test_command_file}}"
118 responses:
119 foo: bar
120 removes: "{{output_file}}.does.not.exist"
121 register: removes_result
122
123 - name: assert when missing removes is provided command is not run
124 assert:
125 that:
126 - "removes_result.changed == false"
127 - "'skipped' in removes_result.stdout"
128
129 - name: test chdir
130 expect:
131 command: "/bin/sh -c 'pwd && sleep 1'"
132 chdir: "{{remote_tmp_dir}}"
133 responses:
134 foo: bar
135 register: chdir_result
136
137 - name: get remote_tmp_dir real path
138 raw: >
139 {{ ansible_python_interpreter }} -c 'import os; os.chdir("{{remote_tmp_dir}}"); print(os.getcwd())'
140 register: remote_tmp_dir_real_path
141
142 - name: assert chdir works
143 assert:
144 that:
145 - chdir_result.stdout | trim == remote_tmp_dir_real_path.stdout | trim
146
147 - name: test timeout option
148 expect:
149 command: "sleep 10"
150 responses:
151 foo: bar
152 timeout: 1
153 ignore_errors: true
154 register: timeout_result
155
156 - name: assert failure message when timeout
157 assert:
158 that:
159 - "timeout_result.msg == 'command exceeded timeout'"
160
161 - name: test echo option
162 expect:
163 command: "{{ansible_python_interpreter}} {{test_command_file}}"
164 responses:
165 foo: bar
166 echo: true
167 register: echo_result
168
169 - name: assert echo works
170 assert:
171 that:
172 - "echo_result.stdout_lines|length == 7"
173 - "echo_result.stdout_lines[-2] == 'foobar'"
174 - "echo_result.stdout_lines[-1] == 'bar'"
175
176 - name: test timeout is valid as null
177 expect:
178 command: "{{ansible_python_interpreter}} {{test_command_file}}"
179 responses:
180 foo: bar
181 echo: true
182 timeout: null # wait indefinitely
183 timeout: 2 # but shouldn't be waiting long
184
185 - name: test response list
186 expect:
187 command: "{{ansible_python_interpreter}} {{test_command_file}} foo foo"
188 responses:
189 foo:
190 - bar
191 - baz
192 register: list_result
193
194 - name: assert list response works
195 assert:
196 that:
197 - "list_result.stdout_lines|length == 7"
198 - "list_result.stdout_lines[-2] == 'foobar'"
199 - "list_result.stdout_lines[-1] == 'foobaz'"
200
201 - name: test no remaining responses
202 expect:
203 command: "{{ansible_python_interpreter}} {{test_command_file}} foo foo"
204 responses:
205 foo:
206 - bar
207 register: list_result
208 ignore_errors: yes
209
210 - name: assert no remaining responses
211 assert:
212 that:
213 - "list_result.failed"
214 - "'No remaining responses' in list_result.msg"
215
216 - name: test no command
217 expect:
218 command: ""
219 responses:
220 foo: bar
221 register: no_command_result
222 ignore_errors: yes
223
224 - name: assert no command
225 assert:
226 that:
227 - "no_command_result.failed"
228 - "no_command_result.msg == 'no command given'"
229 - "no_command_result.rc == 256"
230
231 - name: test non-zero return code
232 expect:
233 command: "ls /does-not-exist"
234 responses:
235 foo: bar
236 register: non_zero_result
237 ignore_errors: yes
238
239 - name: assert non-zero return code
240 assert:
241 that:
242 - "non_zero_result.failed"
243 - "non_zero_result.msg == 'non-zero return code'"
244
245 - name: run tests in venv
246 vars:
247 venv_dir: "{{ output_dir }}/pexpected_venv"
248 venv_python: "{{ venv_dir }}/bin/python"
249 block:
250 - name: ensure venv exists
251 pip:
252 name: coverage
253 virtualenv: "{{ venv_dir }}"
254 virtualenv_command: "{{ ansible_python_interpreter }} -m venv"
255
256 - name: trigger pexpect dependency absence
257 expect:
258 command: "echo expectThePexpected"
259 responses:
260 foo: bar
261 vars:
262 ansible_python_interpreter: "{{ venv_python }}"
263 register: pexpect_dependency_absence
264 ignore_errors: true
265
266 - name: assert pexpect dependency absence
267 assert:
268 that:
269 - pexpect_dependency_absence is failed
270 - pexpect_dependency_absence.msg is contains 'pexpect'
271
272 - name: Install pexpect3.3
273 pip:
274 name: pexpect
275 version: 3.3
276 virtualenv: "{{ venv_dir }}"
277
278 - name: trigger fallback to pexpect3.3 methods
279 expect:
280 command: "echo pexpectIsABitOutOfDate"
281 responses:
282 foo: bar
283 vars:
284 ansible_python_interpreter: "{{ venv_python }}"
285 register: pexpect_version_three_dot_three
286
287 - name: install pexpect3.0
288 pip:
289 name: pexpect
290 version: 3.0
291 virtualenv: "{{ venv_dir }}"
292
293 - name: trigger failure with out of date pexpect version
294 expect:
295 command: "echo pexpectIsTooOutOfDate"
296 responses:
297 foo: bar
298 vars:
299 ansible_python_interpreter: "{{ venv_python }}"
300 register: pexpect_version_out_of_date
301 ignore_errors: true
302
303 - name: assert failure with out of date pexpect
304 assert:
305 that:
306 - pexpect_version_out_of_date is failed
307 - pexpect_version_out_of_date.msg is contains 'Insufficient version of pexpect installed'
308
309 - name: trigger TypeError on timeout value
310 expect:
311 command: "echo piIsNotAnInt"
312 responses:
313 foo: bar
314 timeout: pi
315 register: pexpect_timeout_typeerror
316 ignore_errors: true
317
318 - name: assert timeout triggers TypeError when not an int
319 assert:
320 that:
321 - pexpect_timeout_typeerror is failed
322 - pexpect_timeout_typeerror.msg is contains 'cannot be converted to an int'
323
324 - name: trigger pexpect.ExceptionPexpect
325 expect:
326 command: commandthatdoesntexist
327 responses:
328 foo: bar
329 register: pexpect_exception_pexpect
330 ignore_errors: true
331
332 - name: assert failure for a nonexistent command
333 assert:
334 that:
335 - pexpect_exception_pexpect is failed