Debian Codegraph @

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

raw
1 # Test code for the command and shell modules.
2
3 # Copyright: (c) 2014, Richard Isaacson <[email protected]>
4 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5
6 - name: use command with unsupported executable arg
7 command: ls /dev/null
8 args:
9 executable: /bogus
10 register: executable
11
12 - name: assert executable warning was reported
13 assert:
14 that:
15 - executable.stdout == '/dev/null'
16 - executable.warnings | length() == 1
17 - "'no longer supported' in executable.warnings[0]"
18
19 - name: use command with no command
20 command:
21 args:
22 chdir: /
23 register: no_command
24 ignore_errors: true
25
26 - name: assert executable fails with no command
27 assert:
28 that:
29 - no_command is failed
30 - no_command.msg is contains 'one of the following is required'
31
32 - name: use argv
33 command:
34 argv:
35 - echo
36 - testing
37 register: argv_command
38 ignore_errors: true
39
40 - name: assert executable works with argv
41 assert:
42 that:
43 - "argv_command.stdout == 'testing'"
44
45 - name: use argv and command string
46 command: echo testing
47 args:
48 argv:
49 - echo
50 - testing
51 register: argv_and_string_command
52 ignore_errors: true
53
54 - name: assert executable fails with both argv and command string
55 assert:
56 that:
57 - argv_and_string_command is failed
58 - argv_and_string_command.msg is contains 'parameters are mutually exclusive'
59
60 - set_fact:
61 remote_tmp_dir_test: "{{ remote_tmp_dir }}/test_command_shell"
62
63 - name: make sure our testing sub-directory does not exist
64 file:
65 path: "{{ remote_tmp_dir_test }}"
66 state: absent
67
68 - name: create our testing sub-directory
69 file:
70 path: "{{ remote_tmp_dir_test }}"
71 state: directory
72
73 - name: prep our test script
74 copy:
75 src: test.sh
76 dest: "{{ remote_tmp_dir_test }}"
77 mode: '0755'
78
79 - name: prep our test script
80 copy:
81 src: create_afile.sh
82 dest: "{{ remote_tmp_dir_test }}"
83 mode: '0755'
84
85 - name: prep our test script
86 copy:
87 src: remove_afile.sh
88 dest: "{{ remote_tmp_dir_test }}"
89 mode: '0755'
90
91 - name: locate bash
92 shell: which bash
93 register: bash
94
95 ##
96 ## command
97 ##
98
99 - name: execute the test.sh script via command
100 command: "{{ remote_tmp_dir_test }}/test.sh"
101 register: command_result0
102
103 - name: assert that the script executed correctly
104 assert:
105 that:
106 - command_result0.rc == 0
107 - command_result0.stderr == ''
108 - command_result0.stdout == 'win'
109
110 # executable
111
112 # FIXME doesn't have the expected stdout.
113
114 #- name: execute the test.sh script with executable via command
115 # command: "{{remote_tmp_dir_test }}/test.sh executable={{ bash.stdout }}"
116 # register: command_result1
117 #
118 #- name: assert that the script executed correctly with command
119 # assert:
120 # that:
121 # - "command_result1.rc == 0"
122 # - "command_result1.stderr == ''"
123 # - "command_result1.stdout == 'win'"
124
125 # chdir
126
127 - name: execute the test.sh script with chdir via command
128 command: ./test.sh
129 args:
130 chdir: "{{ remote_tmp_dir_test }}"
131 register: command_result2
132
133 - name: Check invalid chdir
134 command: echo
135 args:
136 chdir: "{{ remote_tmp_dir }}/nope"
137 ignore_errors: yes
138 register: chdir_invalid
139
140 - name: assert that the script executed correctly with chdir
141 assert:
142 that:
143 - command_result2.rc == 0
144 - command_result2.stderr == ''
145 - command_result2.stdout == 'win'
146 - chdir_invalid is failed
147 - chdir_invalid.msg is search('Unable to change directory')
148
149 # creates
150
151 - name: verify that afile.txt is absent
152 file:
153 path: "{{ remote_tmp_dir_test }}/afile.txt"
154 state: absent
155
156 - name: create afile.txt with create_afile.sh via command (check mode)
157 command: "{{ remote_tmp_dir_test }}/create_afile.sh {{remote_tmp_dir_test }}/afile.txt"
158 args:
159 creates: "{{ remote_tmp_dir_test }}/afile.txt"
160 register: check_mode_result
161 check_mode: yes
162
163 - assert:
164 that:
165 - check_mode_result.changed
166 - "'skipped' not in check_mode_result"
167
168 - name: verify that afile.txt still does not exist
169 stat:
170 path: "{{remote_tmp_dir_test}}/afile.txt"
171 register: stat_result
172 failed_when: stat_result.stat.exists
173
174 - name: create afile.txt with create_afile.sh via command
175 command: "{{ remote_tmp_dir_test }}/create_afile.sh {{remote_tmp_dir_test }}/afile.txt"
176 args:
177 creates: "{{ remote_tmp_dir_test }}/afile.txt"
178
179 - name: verify that afile.txt is present
180 file:
181 path: "{{ remote_tmp_dir_test }}/afile.txt"
182 state: file
183
184 - name: re-run previous command using creates with globbing (check mode)
185 command: "{{ remote_tmp_dir_test }}/create_afile.sh {{ remote_tmp_dir_test }}/afile.txt"
186 args:
187 creates: "{{ remote_tmp_dir_test }}/afile.*"
188 register: check_mode_result
189 check_mode: yes
190
191 - assert:
192 that:
193 - not check_mode_result.changed
194 - "'skipped' not in check_mode_result"
195
196 - name: re-run previous command using creates with globbing
197 command: "{{ remote_tmp_dir_test }}/create_afile.sh {{ remote_tmp_dir_test }}/afile.txt"
198 args:
199 creates: "{{ remote_tmp_dir_test }}/afile.*"
200 register: command_result3
201
202 - name: assert that creates with globbing is working
203 assert:
204 that:
205 - command_result3 is not changed
206
207 # removes
208
209 - name: remove afile.txt with remote_afile.sh via command (check mode)
210 command: "{{ remote_tmp_dir_test }}/remove_afile.sh {{ remote_tmp_dir_test }}/afile.txt"
211 args:
212 removes: "{{ remote_tmp_dir_test }}/afile.txt"
213 register: check_mode_result
214 check_mode: yes
215
216 - assert:
217 that:
218 - check_mode_result.changed
219 - "'skipped' not in check_mode_result"
220
221 - name: verify that afile.txt still exists
222 stat:
223 path: "{{remote_tmp_dir_test}}/afile.txt"
224 register: stat_result
225 failed_when: not stat_result.stat.exists
226
227 - name: remove afile.txt with remote_afile.sh via command
228 command: "{{ remote_tmp_dir_test }}/remove_afile.sh {{ remote_tmp_dir_test }}/afile.txt"
229 args:
230 removes: "{{ remote_tmp_dir_test }}/afile.txt"
231
232 - name: verify that afile.txt is absent
233 file: path={{remote_tmp_dir_test}}/afile.txt state=absent
234
235 - name: re-run previous command using removes with globbing (check mode)
236 command: "{{ remote_tmp_dir_test }}/remove_afile.sh {{ remote_tmp_dir_test }}/afile.txt"
237 args:
238 removes: "{{ remote_tmp_dir_test }}/afile.*"
239 register: check_mode_result
240 check_mode: yes
241
242 - assert:
243 that:
244 - not check_mode_result.changed
245 - "'skipped' not in check_mode_result"
246
247 - name: re-run previous command using removes with globbing
248 command: "{{ remote_tmp_dir_test }}/remove_afile.sh {{ remote_tmp_dir_test }}/afile.txt"
249 args:
250 removes: "{{ remote_tmp_dir_test }}/afile.*"
251 register: command_result4
252
253 - name: assert that removes with globbing is working
254 assert:
255 that:
256 - command_result4.changed != True
257
258 - name: pass stdin to cat via command
259 command: cat
260 args:
261 stdin: 'foobar'
262 register: command_result5
263
264 - name: assert that stdin is passed
265 assert:
266 that:
267 - command_result5.stdout == 'foobar'
268
269 - name: send to stdin literal multiline block
270 command: "{{ ansible_python.executable }} -c 'import hashlib, sys; print(hashlib.sha1((sys.stdin.buffer if hasattr(sys.stdin, \"buffer\") else sys.stdin).read()).hexdigest())'"
271 args:
272 stdin: |-
273 this is the first line
274 this is the second line
275
276 this line is after an empty line
277 this line is the last line
278 register: command_result6
279
280 - name: assert the multiline input was passed correctly
281 assert:
282 that:
283 - "command_result6.stdout == '9cd0697c6a9ff6689f0afb9136fa62e0b3fee903'"
284
285 - name: check default var expansion
286 command: /bin/sh -c 'echo "\$TEST"'
287 environment:
288 TEST: z
289 register: command_result7
290
291 - name: assert vars were expanded
292 assert:
293 that:
294 - command_result7.stdout == '\\z'
295
296 - name: check disabled var expansion
297 command: /bin/sh -c 'echo "\$TEST"'
298 args:
299 expand_argument_vars: false
300 environment:
301 TEST: z
302 register: command_result8
303
304 - name: assert vars were not expanded
305 assert:
306 that:
307 - command_result8.stdout == '$TEST'
308
309 ##
310 ## shell
311 ##
312
313 - name: Execute the test.sh script
314 shell: "{{ remote_tmp_dir_test }}/test.sh"
315 register: shell_result0
316
317 - name: Assert that the script executed correctly
318 assert:
319 that:
320 - shell_result0 is changed
321 - shell_result0.cmd == remote_tmp_dir_test + '/test.sh'
322 - shell_result0.rc == 0
323 - shell_result0.stderr == ''
324 - shell_result0.stdout == 'win'
325
326 # executable
327
328 # FIXME doesn't pass the expected stdout
329
330 #- name: execute the test.sh script
331 # shell: "{{remote_tmp_dir_test }}/test.sh executable={{ bash.stdout }}"
332 # register: shell_result1
333 #
334 #- name: assert that the shell executed correctly
335 # assert:
336 # that:
337 # - "shell_result1.rc == 0"
338 # - "shell_result1.stderr == ''"
339 # - "shell_result1.stdout == 'win'"
340
341 # chdir
342
343 - name: Execute the test.sh script with chdir
344 shell: ./test.sh
345 args:
346 chdir: "{{ remote_tmp_dir_test }}"
347 register: shell_result2
348
349 - name: Assert that the shell executed correctly with chdir
350 assert:
351 that:
352 - shell_result2 is changed
353 - shell_result2.cmd == './test.sh'
354 - shell_result2.rc == 0
355 - shell_result2.stderr == ''
356 - shell_result2.stdout == 'win'
357
358 # creates
359
360 - name: Verify that afile.txt is absent
361 file:
362 path: "{{ remote_tmp_dir_test }}/afile.txt"
363 state: absent
364
365 - name: Execute the test.sh script with chdir
366 shell: "{{ remote_tmp_dir_test }}/test.sh > {{ remote_tmp_dir_test }}/afile.txt"
367 args:
368 chdir: "{{ remote_tmp_dir_test }}"
369 creates: "{{ remote_tmp_dir_test }}/afile.txt"
370
371 - name: Verify that afile.txt is present
372 file:
373 path: "{{ remote_tmp_dir_test }}/afile.txt"
374 state: file
375
376 # multiline
377
378 - name: Remove test file previously created
379 file:
380 path: "{{ remote_tmp_dir_test }}/afile.txt"
381 state: absent
382
383 - name: Execute a shell command using a literal multiline block
384 args:
385 executable: "{{ bash.stdout }}"
386 shell: |
387 echo this is a \
388 "multiline echo" \
389 "with a new line
390 in quotes" \
391 | {{ ansible_python.executable }} -c 'import hashlib, sys; print(hashlib.sha1((sys.stdin.buffer if hasattr(sys.stdin, "buffer") else sys.stdin).read()).hexdigest())'
392 echo "this is a second line"
393 register: shell_result5
394
395 - name: Assert the multiline shell command ran as expected
396 assert:
397 that:
398 - shell_result5 is changed
399 - shell_result5.rc == 0
400 - shell_result5.cmd == 'echo this is a "multiline echo" "with a new line\nin quotes" | ' + ansible_python.executable + ' -c \'import hashlib, sys; print(hashlib.sha1((sys.stdin.buffer if hasattr(sys.stdin, "buffer") else sys.stdin).read()).hexdigest())\'\necho "this is a second line"\n'
401 - shell_result5.stdout == '5575bb6b71c9558db0b6fbbf2f19909eeb4e3b98\nthis is a second line'
402
403 - name: Execute a shell command using a literal multiline block with arguments in it
404 shell: |
405 executable="{{ bash.stdout }}"
406 creates={{ remote_tmp_dir_test }}/afile.txt
407 echo "test"
408 register: shell_result6
409
410 - name: Assert the multiline shell command with arguments in it run as expected
411 assert:
412 that:
413 - shell_result6 is changed
414 - shell_result6.rc == 0
415 - shell_result6.cmd == 'echo "test"\n'
416 - shell_result6.stdout == 'test'
417
418 - name: Execute a shell command using a multiline block where whitespaces matter
419 shell: |
420 cat <<EOF
421 One
422 Two
423 Three
424 EOF
425 register: shell_result7
426
427 - name: Assert the multiline shell command outputs with whitespaces preserved
428 assert:
429 that:
430 - shell_result7 is changed
431 - shell_result7.rc == 0
432 - shell_result7.cmd == 'cat <<EOF\nOne\n Two\n Three\nEOF\n'
433 - shell_result7.stdout == 'One\n Two\n Three'
434
435 - name: execute a shell command with no trailing newline to stdin
436 shell: cat > {{remote_tmp_dir_test }}/afile.txt
437 args:
438 stdin: test
439 stdin_add_newline: no
440
441 - name: make sure content matches expected
442 copy:
443 dest: "{{remote_tmp_dir_test }}/afile.txt"
444 content: test
445 register: shell_result7
446 failed_when:
447 - shell_result7 is failed or
448 shell_result7 is changed
449
450 - name: execute a shell command with trailing newline to stdin
451 shell: cat > {{remote_tmp_dir_test }}/afile.txt
452 args:
453 stdin: test
454 stdin_add_newline: yes
455
456 - name: make sure content matches expected
457 copy:
458 dest: "{{remote_tmp_dir_test }}/afile.txt"
459 content: |
460 test
461 register: shell_result8
462 failed_when:
463 - shell_result8 is failed or
464 shell_result8 is changed
465
466 - name: execute a shell command with trailing newline to stdin, default
467 shell: cat > {{remote_tmp_dir_test }}/afile.txt
468 args:
469 stdin: test
470
471 - name: make sure content matches expected
472 copy:
473 dest: "{{remote_tmp_dir_test }}/afile.txt"
474 content: |
475 test
476 register: shell_result9
477 failed_when:
478 - shell_result9 is failed or
479 shell_result9 is changed
480
481 - name: remove the previously created file
482 file:
483 path: "{{ remote_tmp_dir_test }}/afile.txt"
484 state: absent
485
486 - name: test check mode skip message
487 command:
488 cmd: "true"
489 check_mode: yes
490 register: result
491
492 - name: assert check message exists
493 assert:
494 that:
495 - "'Command would have run if not in check mode' in result.msg"
496 - result.skipped
497 - not result.changed
498
499 - name: test check mode creates/removes message
500 command:
501 cmd: "true"
502 creates: yes
503 check_mode: yes
504 register: result
505
506 - name: assert check message exists
507 assert:
508 that:
509 - "'Command would have run if not in check mode' in result.msg"
510 - "'skipped' not in result"
511 - result.changed
512
513 - name: command symlink handling
514 block:
515 - name: Create target folders
516 file:
517 path: '{{remote_tmp_dir}}/www_root/site'
518 state: directory
519
520 - name: Create symlink
521 file:
522 path: '{{remote_tmp_dir}}/www'
523 state: link
524 src: '{{remote_tmp_dir}}/www_root'
525
526 - name: check parent using chdir
527 shell: dirname "$PWD"
528 args:
529 chdir: '{{remote_tmp_dir}}/www/site'
530 register: parent_dir_chdir
531
532 - name: check parent using cd
533 shell: cd "{{remote_tmp_dir}}/www/site" && dirname "$PWD"
534 register: parent_dir_cd
535
536 - name: check expected outputs
537 assert:
538 that:
539 - parent_dir_chdir.stdout != parent_dir_cd.stdout
540 # These tests use endswith, to get around /private/tmp on macos
541 - 'parent_dir_cd.stdout.endswith(remote_tmp_dir ~ "/www")'
542 - 'parent_dir_chdir.stdout.endswith(remote_tmp_dir ~ "/www_root")'
543
544 - name: Set print error command for Python 3
545 set_fact:
546 print_error_command: print(msg, file=sys.stderr)
547
548 - name: run command with strip
549 command: '{{ ansible_python_interpreter }} -c "import sys; msg=''hello \n \r''; print(msg); {{ print_error_command }}"'
550 register: command_strip
551
552 - name: run command without strip
553 command: '{{ ansible_python_interpreter }} -c "import sys; msg=''hello \n \r''; print(msg); {{ print_error_command }}"'
554 args:
555 strip_empty_ends: no
556 register: command_no_strip
557
558 - name: Verify strip behavior worked as expected
559 assert:
560 that:
561 - command_strip.stdout == 'hello \n '
562 - command_strip.stderr == 'hello \n '
563 - command_no_strip.stdout== 'hello \n \r\n'
564 - command_no_strip.stderr == 'hello \n \r\n'
565
566 - name: run shell with expand_argument_vars
567 shell: echo 'hi'
568 args:
569 expand_argument_vars: false
570 register: shell_expand_failure
571 ignore_errors: true
572
573 - name: assert shell with expand_arguments_vars failed
574 assert:
575 that:
576 - shell_expand_failure is failed
577 - "shell_expand_failure.msg == 'Unsupported parameters for (shell) module: expand_argument_vars'"
578
579 - name: Run command that backgrounds, to ensure no hang
580 shell: '{{ role_path }}/scripts/yoink.sh &'
581 delegate_to: localhost
582 timeout: 5
583
584 - name: Run command that does not exist
585 command: commandthatdoesnotexist --would-be-awkward
586 register: res
587 failed_when: res.stdout != '' or res.stderr != ''