2014-02-23 21:51:26 +01:00
# Test code for the command and shell modules.
2018-09-22 00:53:18 +02:00
# Copyright: (c) 2014, Richard Isaacson <richard.c.isaacson@gmail.com>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
2014-02-23 21:51:26 +01:00
2017-09-15 20:57:50 +02:00
- name : use command to execute sudo
command : sudo -h
register : become
- name : assert become warning was reported
assert :
that :
- "become.warnings | length() == 1"
- "'Consider using' in become.warnings[0]"
- name : use command to execute sudo without warnings
command : sudo -h warn=no
register : become
- name : assert become warning was not reported
assert :
that :
- "'warnings' not in become"
- name : use command to execute tar
command : tar --help
register : tar
- name : assert tar warning was reported
assert :
that :
2019-02-13 19:54:36 +01:00
- tar.warnings | length() == 1
- '"Consider using the unarchive module rather than running ' 'tar' '" in tar.warnings[0]'
2017-09-15 20:57:50 +02:00
- name : use command to execute chown
command : chown -h
register : chown
ignore_errors : true
- name : assert chown warning was reported
assert :
that :
2019-02-13 19:54:36 +01:00
- chown.warnings | length() == 1
- '"Consider using the file module with owner rather than running ' 'chown' '" in chown.warnings[0]'
2017-09-15 20:57:50 +02:00
- name : use command with unsupported executable arg
2019-02-13 19:54:36 +01:00
command : ls /dev/null
args :
executable : /bogus
2017-09-15 20:57:50 +02:00
register : executable
- name : assert executable warning was reported
assert :
that :
2019-02-13 19:54:36 +01:00
- executable.stdout == '/dev/null'
- executable.warnings | length() == 1
2017-09-15 20:57:50 +02:00
- "'no longer supported' in executable.warnings[0]"
2019-02-13 19:54:36 +01:00
# The warning isn't on the task since it comes from the action plugin. Not sure
# how to test for that.
#
# - name: Use command with reboot
# command: sleep 2 && /not/shutdown -r now
# ignore_errors: yes
# register: reboot
# - name: Assert that reboot warning was issued
# assert:
# that:
# - '"Consider using the reboot module" in reboot.warnings[0]'
2017-09-15 20:57:50 +02:00
- name : use command with no command
2019-02-13 19:54:36 +01:00
command :
args :
chdir : /
2017-09-15 20:57:50 +02:00
register : no_command
ignore_errors : true
- name : assert executable fails with no command
assert :
that :
2019-02-13 19:54:36 +01:00
- no_command is failed
- no_command.msg == 'no command given'
- no_command.rc == 256
2017-09-15 20:57:50 +02:00
2018-05-16 22:37:39 +02:00
- name : use argv
command :
argv :
- echo
- testing
register : argv_command
ignore_errors : true
- name : assert executable works with argv
assert :
that :
- "argv_command.stdout == 'testing'"
- name : use argv and command string
command : echo testing
args :
argv :
- echo
- testing
register : argv_and_string_command
ignore_errors : true
- name : assert executable fails with both argv and command string
assert :
that :
2019-02-13 19:54:36 +01:00
- argv_and_string_command is failed
- argv_and_string_command.msg == 'only command or argv can be given, not both'
- argv_and_string_command.rc == 256
2018-05-16 22:37:39 +02:00
2019-02-13 19:54:36 +01:00
- set_fact :
output_dir_test : "{{ output_dir }}/test_command_shell"
2014-02-23 21:51:26 +01:00
- name : make sure our testing sub-directory does not exist
2019-02-13 19:54:36 +01:00
file :
path : "{{ output_dir_test }}"
state : absent
2014-02-23 21:51:26 +01:00
- name : create our testing sub-directory
2019-02-13 19:54:36 +01:00
file :
path : "{{ output_dir_test }}"
state : directory
2014-02-23 21:51:26 +01:00
- name : prep our test script
2019-02-13 19:54:36 +01:00
copy :
src : test.sh
dest : "{{ output_dir_test }}"
mode : '0755'
2014-02-23 21:51:26 +01:00
- name : prep our test script
2019-02-13 19:54:36 +01:00
copy :
src : create_afile.sh
dest : "{{ output_dir_test }}"
mode : '0755'
2014-02-23 21:51:26 +01:00
- name : prep our test script
2019-02-13 19:54:36 +01:00
copy :
src : remove_afile.sh
dest : "{{ output_dir_test }}"
mode : '0755'
2014-02-23 21:51:26 +01:00
2016-08-19 19:23:14 +02:00
- name : locate bash
shell : which bash
register : bash
2014-02-23 21:51:26 +01:00
##
## command
##
- name : execute the test.sh script via command
2019-02-13 19:54:36 +01:00
command : "{{ output_dir_test }}/test.sh"
2014-02-23 21:51:26 +01:00
register : command_result0
- name : assert that the script executed correctly
assert :
that :
2019-02-13 19:54:36 +01:00
- command_result0.rc == 0
- command_result0.stderr == ''
- command_result0.stdout == 'win'
2014-02-23 21:51:26 +01:00
# executable
# FIXME doesn't have the expected stdout.
#- name: execute the test.sh script with executable via command
2019-02-13 19:54:36 +01:00
# command: "{{output_dir_test }}/test.sh executable={{ bash.stdout }}"
2014-02-23 21:51:26 +01:00
# register: command_result1
#
#- name: assert that the script executed correctly with command
# assert:
# that:
# - "command_result1.rc == 0"
# - "command_result1.stderr == ''"
# - "command_result1.stdout == 'win'"
# chdir
- name : execute the test.sh script with chdir via command
2019-02-13 19:54:36 +01:00
command : ./test.sh
args :
chdir : "{{ output_dir_test }}"
2014-02-23 21:51:26 +01:00
register : command_result2
- name : assert that the script executed correctly with chdir
assert :
that :
2019-02-13 19:54:36 +01:00
- command_result2.rc == 0
- command_result2.stderr == ''
- command_result2.stdout == 'win'
2014-02-23 21:51:26 +01:00
# creates
- name : verify that afile.txt is absent
2019-02-13 19:54:36 +01:00
file :
path : "{{ output_dir_test }}/afile.txt"
state : absent
2014-02-23 21:51:26 +01:00
- name : create afile.txt with create_afile.sh via command
2019-02-13 19:54:36 +01:00
command : "{{ output_dir_test }}/create_afile.sh {{output_dir_test }}/afile.txt"
args :
creates : "{{ output_dir_test }}/afile.txt"
2014-02-23 21:51:26 +01:00
- name : verify that afile.txt is present
2019-02-13 19:54:36 +01:00
file :
path : "{{ output_dir_test }}/afile.txt"
state : file
2014-02-23 21:51:26 +01:00
2014-12-14 23:40:04 +01:00
- name : re-run previous command using creates with globbing
2019-02-13 19:54:36 +01:00
command : "{{ output_dir_test }}/create_afile.sh {{ output_dir_test }}/afile.txt"
args :
creates : "{{ output_dir_test }}/afile.*"
2014-12-14 23:40:04 +01:00
register : command_result3
- name : assert that creates with globbing is working
assert :
that :
2019-02-13 19:54:36 +01:00
- command_result3 is not changed
2014-12-14 23:40:04 +01:00
2014-02-23 21:51:26 +01:00
# removes
- name : remove afile.txt with remote_afile.sh via command
2019-02-13 19:54:36 +01:00
command : "{{ output_dir_test }}/remove_afile.sh {{ output_dir_test }}/afile.txt"
args :
removes : "{{ output_dir_test }}/afile.txt"
2014-02-23 21:51:26 +01:00
- name : verify that afile.txt is absent
file : path={{output_dir_test}}/afile.txt state=absent
2014-12-14 23:40:04 +01:00
- name : re-run previous command using removes with globbing
2019-02-13 19:54:36 +01:00
command : "{{ output_dir_test }}/remove_afile.sh {{ output_dir_test }}/afile.txt"
args :
removes : "{{ output_dir_test }}/afile.*"
2014-12-14 23:40:04 +01:00
register : command_result4
- name : assert that removes with globbing is working
2014-02-23 21:51:26 +01:00
assert :
that :
2019-02-13 19:54:36 +01:00
- command_result4.changed != True
2014-02-23 21:51:26 +01:00
2017-04-07 13:28:35 +02:00
- name : pass stdin to cat via command
2019-02-13 19:54:36 +01:00
command : cat
2017-04-07 13:28:35 +02:00
args :
stdin : 'foobar'
register : command_result5
- name : assert that stdin is passed
assert :
that :
2019-02-13 19:54:36 +01:00
- command_result5.stdout == 'foobar'
2017-04-07 13:28:35 +02:00
- name : send to stdin literal multiline block
2019-02-13 19:54:36 +01:00
command : "{{ ansible_python.executable }} -c 'import hashlib, sys; print(hashlib.sha1((sys.stdin.buffer if hasattr(sys.stdin, \"buffer\") else sys.stdin).read()).hexdigest())'"
2017-04-07 13:28:35 +02:00
args :
stdin : |-
this is the first line
this is the second line
this line is after an empty line
this line is the last line
register : command_result6
- name : assert the multiline input was passed correctly
assert :
that :
2018-02-10 19:48:44 +01:00
- "command_result6.stdout == '9cd0697c6a9ff6689f0afb9136fa62e0b3fee903'"
2017-04-07 13:28:35 +02:00
2014-02-23 21:51:26 +01:00
##
## shell
##
2018-09-22 00:53:18 +02:00
- name : Execute the test.sh script
2019-02-13 19:54:36 +01:00
shell : "{{ output_dir_test }}/test.sh"
2014-02-23 21:51:26 +01:00
register : shell_result0
2018-09-22 00:53:18 +02:00
- name : Assert that the script executed correctly
2014-02-23 21:51:26 +01:00
assert :
that :
2018-09-22 00:53:18 +02:00
- shell_result0 is changed
2019-02-13 19:54:36 +01:00
- shell_result0.cmd == '{{ output_dir_test }}/test.sh'
2018-09-22 00:53:18 +02:00
- shell_result0.rc == 0
- shell_result0.stderr == ''
- shell_result0.stdout == 'win'
2014-02-23 21:51:26 +01:00
# executable
# FIXME doesn't pass the expected stdout
#- name: execute the test.sh script
2019-02-13 19:54:36 +01:00
# shell: "{{output_dir_test }}/test.sh executable={{ bash.stdout }}"
2014-02-23 21:51:26 +01:00
# register: shell_result1
#
#- name: assert that the shell executed correctly
# assert:
# that:
# - "shell_result1.rc == 0"
# - "shell_result1.stderr == ''"
# - "shell_result1.stdout == 'win'"
# chdir
2018-09-22 00:53:18 +02:00
- name : Execute the test.sh script with chdir
2019-02-13 19:54:36 +01:00
shell : ./test.sh
args :
chdir : "{{ output_dir_test }}"
2014-02-23 21:51:26 +01:00
register : shell_result2
2018-09-22 00:53:18 +02:00
- name : Assert that the shell executed correctly with chdir
2014-02-23 21:51:26 +01:00
assert :
that :
2018-09-22 00:53:18 +02:00
- shell_result2 is changed
- shell_result2.cmd == './test.sh'
- shell_result2.rc == 0
- shell_result2.stderr == ''
- shell_result2.stdout == 'win'
2014-02-23 21:51:26 +01:00
# creates
2018-09-22 00:53:18 +02:00
- name : Verify that afile.txt is absent
2019-02-13 19:54:36 +01:00
file :
path : "{{ output_dir_test }}/afile.txt"
state : absent
2014-02-23 21:51:26 +01:00
2018-09-22 00:53:18 +02:00
- name : Execute the test.sh script with chdir
2019-02-13 19:54:36 +01:00
shell : "{{ output_dir_test }}/test.sh > {{ output_dir_test }}/afile.txt"
args :
chdir : "{{ output_dir_test }}"
creates : "{{ output_dir_test }}/afile.txt"
2014-02-23 21:51:26 +01:00
2018-09-22 00:53:18 +02:00
- name : Verify that afile.txt is present
2019-02-13 19:54:36 +01:00
file :
path : "{{ output_dir_test }}/afile.txt"
state : file
2014-02-23 21:51:26 +01:00
2014-12-14 23:39:17 +01:00
# multiline
2014-08-04 16:31:30 +02:00
2018-09-22 00:53:18 +02:00
- name : Remove test file previously created
2019-02-13 19:54:36 +01:00
file :
path : "{{ output_dir_test }}/afile.txt"
state : absent
2015-07-14 06:23:17 +02:00
2018-09-22 00:53:18 +02:00
- name : Execute a shell command using a literal multiline block
2014-08-08 18:54:38 +02:00
args :
2016-08-19 19:23:14 +02:00
executable : "{{ bash.stdout }}"
2014-08-04 16:31:30 +02:00
shell : |
2014-09-04 16:35:41 +02:00
echo this is a \
"multiline echo" \
2014-08-04 16:31:30 +02:00
"with a new line
2014-08-08 18:54:38 +02:00
in quotes" \
2019-02-13 19:54:36 +01:00
| {{ ansible_python.executable }} -c 'import hashlib, sys; print(hashlib.sha1((sys.stdin.buffer if hasattr(sys.stdin, "buffer") else sys.stdin).read()).hexdigest())'
2014-08-08 18:54:38 +02:00
echo "this is a second line"
2014-12-14 23:39:17 +01:00
register : shell_result5
2014-08-04 16:31:30 +02:00
2018-09-22 00:53:18 +02:00
- name : Assert the multiline shell command ran as expected
2014-08-04 16:31:30 +02:00
assert :
that :
2018-09-22 00:53:18 +02:00
- shell_result5 is changed
- shell_result5.rc == 0
2019-02-13 19:54:36 +01:00
- 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'
2018-09-22 00:53:18 +02:00
- shell_result5.stdout == '5575bb6b71c9558db0b6fbbf2f19909eeb4e3b98\nthis is a second line'
2014-10-15 04:12:35 +02:00
2018-09-22 00:53:18 +02:00
- name : Execute a shell command using a literal multiline block with arguments in it
2014-10-15 04:12:35 +02:00
shell : |
2016-08-19 19:23:14 +02:00
executable="{{ bash.stdout }}"
2019-02-13 19:54:36 +01:00
creates={{ output_dir_test }}/afile.txt
2014-10-15 04:12:35 +02:00
echo "test"
2014-12-14 23:39:17 +01:00
register : shell_result6
2014-10-15 04:12:35 +02:00
2018-09-22 00:53:18 +02:00
- name : Assert the multiline shell command with arguments in it run as expected
assert :
that :
- shell_result6 is changed
- shell_result6.rc == 0
- shell_result6.cmd == 'echo "test"\n'
- shell_result6.stdout == 'test'
- name : Execute a shell command using a multiline block where whitespaces matter
shell : |
cat <<EOF
One
Two
Three
EOF
register : shell_result7
- name : Assert the multiline shell command outputs with whitespaces preserved
2014-10-15 04:12:35 +02:00
assert :
that :
2018-09-22 00:53:18 +02:00
- shell_result7 is changed
- shell_result7.rc == 0
- shell_result7.cmd == 'cat <<EOF\nOne\n Two\n Three\nEOF\n'
- shell_result7.stdout == 'One\n Two\n Three'
2014-10-15 04:12:35 +02:00
2018-10-31 17:53:02 +01:00
- name : execute a shell command with no trailing newline to stdin
2019-02-13 19:54:36 +01:00
shell : cat > {{output_dir_test }}/afile.txt
2018-10-31 17:53:02 +01:00
args :
stdin : test
stdin_add_newline : no
- name : make sure content matches expected
copy :
2019-02-13 19:54:36 +01:00
dest : "{{output_dir_test }}/afile.txt"
2018-10-31 17:53:02 +01:00
content : test
register : shell_result7
failed_when :
- shell_result7 is failed or
shell_result7 is changed
- name : execute a shell command with trailing newline to stdin
2019-02-13 19:54:36 +01:00
shell : cat > {{output_dir_test }}/afile.txt
2018-10-31 17:53:02 +01:00
args :
stdin : test
stdin_add_newline : yes
- name : make sure content matches expected
copy :
2019-02-13 19:54:36 +01:00
dest : "{{output_dir_test }}/afile.txt"
2018-10-31 17:53:02 +01:00
content : |
test
register : shell_result8
failed_when :
- shell_result8 is failed or
shell_result8 is changed
- name : execute a shell command with trailing newline to stdin, default
2019-02-13 19:54:36 +01:00
shell : cat > {{output_dir_test }}/afile.txt
2018-10-31 17:53:02 +01:00
args :
stdin : test
- name : make sure content matches expected
copy :
2019-02-13 19:54:36 +01:00
dest : "{{output_dir_test }}/afile.txt"
2018-10-31 17:53:02 +01:00
content : |
test
register : shell_result9
failed_when :
- shell_result9 is failed or
shell_result9 is changed
2014-10-15 04:12:35 +02:00
- name : remove the previously created file
2019-02-13 19:54:36 +01:00
file :
path : "{{ output_dir_test }}/afile.txt"
state : absent