2014-02-23 20:51:26 +00:00
|
|
|
# Test code for the command and shell modules.
|
|
|
|
# (c) 2014, Richard Isaacson <richard.c.isaacson@gmail.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/>.
|
|
|
|
|
|
|
|
- set_fact: output_dir_test={{output_dir}}/test_command_shell
|
|
|
|
|
|
|
|
- name: make sure our testing sub-directory does not exist
|
|
|
|
file: path="{{ output_dir_test }}" state=absent
|
|
|
|
|
|
|
|
- name: create our testing sub-directory
|
|
|
|
file: path="{{ output_dir_test }}" state=directory
|
|
|
|
|
|
|
|
- name: prep our test script
|
|
|
|
copy: src=test.sh dest="{{ output_dir_test }}" mode=0755
|
|
|
|
|
|
|
|
- name: prep our test script
|
|
|
|
copy: src=create_afile.sh dest="{{ output_dir_test }}" mode=0755
|
|
|
|
|
|
|
|
- name: prep our test script
|
|
|
|
copy: src=remove_afile.sh dest="{{ output_dir_test }}" mode=0755
|
|
|
|
|
2016-08-19 17:23:14 +00:00
|
|
|
- name: locate bash
|
|
|
|
shell: which bash
|
|
|
|
register: bash
|
|
|
|
|
|
|
|
- name: locate sha1sum/shasum
|
|
|
|
shell: which sha1sum || which shasum
|
|
|
|
register: sha1sum
|
|
|
|
|
2014-02-23 20:51:26 +00:00
|
|
|
##
|
|
|
|
## command
|
|
|
|
##
|
|
|
|
|
|
|
|
- name: execute the test.sh script via command
|
|
|
|
command: "{{output_dir_test | expanduser}}/test.sh"
|
|
|
|
register: command_result0
|
|
|
|
|
|
|
|
- name: assert that the script executed correctly
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- "command_result0.rc == 0"
|
|
|
|
- "command_result0.stderr == ''"
|
|
|
|
- "command_result0.stdout == 'win'"
|
|
|
|
|
|
|
|
# executable
|
|
|
|
|
|
|
|
# FIXME doesn't have the expected stdout.
|
|
|
|
|
|
|
|
#- name: execute the test.sh script with executable via command
|
2016-08-19 17:23:14 +00:00
|
|
|
# command: "{{output_dir_test | expanduser}}/test.sh executable={{ bash.stdout }}"
|
2014-02-23 20:51:26 +00: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
|
|
|
|
command: ./test.sh chdir="{{output_dir_test | expanduser}}"
|
|
|
|
register: command_result2
|
|
|
|
|
|
|
|
- name: assert that the script executed correctly with chdir
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- "command_result2.rc == 0"
|
|
|
|
- "command_result2.stderr == ''"
|
|
|
|
- "command_result2.stdout == 'win'"
|
|
|
|
|
|
|
|
# creates
|
|
|
|
|
|
|
|
- name: verify that afile.txt is absent
|
|
|
|
file: path={{output_dir_test}}/afile.txt state=absent
|
|
|
|
|
|
|
|
- name: create afile.txt with create_afile.sh via command
|
2014-12-14 22:39:17 +00:00
|
|
|
command: "{{output_dir_test | expanduser}}/create_afile.sh {{output_dir_test | expanduser}}/afile.txt creates={{output_dir_test | expanduser}}/afile.txt"
|
2014-02-23 20:51:26 +00:00
|
|
|
|
|
|
|
- name: verify that afile.txt is present
|
|
|
|
file: path={{output_dir_test}}/afile.txt state=file
|
|
|
|
|
2014-12-14 22:40:04 +00:00
|
|
|
- name: re-run previous command using creates with globbing
|
|
|
|
command: "{{output_dir_test | expanduser}}/create_afile.sh {{output_dir_test | expanduser}}/afile.txt creates={{output_dir_test | expanduser}}/afile.*"
|
|
|
|
register: command_result3
|
|
|
|
|
|
|
|
- name: assert that creates with globbing is working
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- "command_result3.changed != True"
|
|
|
|
|
2014-02-23 20:51:26 +00:00
|
|
|
# removes
|
|
|
|
|
|
|
|
- name: remove afile.txt with remote_afile.sh via command
|
2014-12-14 22:39:17 +00:00
|
|
|
command: "{{output_dir_test | expanduser}}/remove_afile.sh {{output_dir_test | expanduser}}/afile.txt removes={{output_dir_test | expanduser}}/afile.txt"
|
2014-02-23 20:51:26 +00:00
|
|
|
|
|
|
|
- name: verify that afile.txt is absent
|
|
|
|
file: path={{output_dir_test}}/afile.txt state=absent
|
|
|
|
|
2014-12-14 22:40:04 +00:00
|
|
|
- name: re-run previous command using removes with globbing
|
|
|
|
command: "{{output_dir_test | expanduser}}/remove_afile.sh {{output_dir_test | expanduser}}/afile.txt removes={{output_dir_test | expanduser}}/afile.*"
|
|
|
|
register: command_result4
|
|
|
|
|
|
|
|
- name: assert that removes with globbing is working
|
2014-02-23 20:51:26 +00:00
|
|
|
assert:
|
|
|
|
that:
|
2014-12-14 22:40:04 +00:00
|
|
|
- "command_result4.changed != True"
|
2014-02-23 20:51:26 +00:00
|
|
|
|
|
|
|
##
|
|
|
|
## shell
|
|
|
|
##
|
|
|
|
|
|
|
|
- name: execute the test.sh script
|
|
|
|
shell: "{{output_dir_test | expanduser}}/test.sh"
|
|
|
|
register: shell_result0
|
|
|
|
|
|
|
|
- name: assert that the script executed correctly
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- "shell_result0.rc == 0"
|
|
|
|
- "shell_result0.stderr == ''"
|
|
|
|
- "shell_result0.stdout == 'win'"
|
|
|
|
|
|
|
|
# executable
|
|
|
|
|
|
|
|
# FIXME doesn't pass the expected stdout
|
|
|
|
|
|
|
|
#- name: execute the test.sh script
|
2016-08-19 17:23:14 +00:00
|
|
|
# shell: "{{output_dir_test | expanduser}}/test.sh executable={{ bash.stdout }}"
|
2014-02-23 20:51:26 +00: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
|
|
|
|
|
|
|
|
- name: execute the test.sh script with chdir
|
|
|
|
shell: ./test.sh chdir="{{output_dir_test | expanduser}}"
|
|
|
|
register: shell_result2
|
|
|
|
|
|
|
|
- name: assert that the shell executed correctly with chdir
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- "shell_result2.rc == 0"
|
|
|
|
- "shell_result2.stderr == ''"
|
|
|
|
- "shell_result2.stdout == 'win'"
|
|
|
|
|
|
|
|
# creates
|
|
|
|
|
|
|
|
- name: verify that afile.txt is absent
|
|
|
|
file: path={{output_dir_test}}/afile.txt state=absent
|
|
|
|
|
|
|
|
- name: execute the test.sh script with chdir
|
|
|
|
shell: "{{output_dir_test | expanduser}}/test.sh > {{output_dir_test | expanduser}}/afile.txt creates={{output_dir_test | expanduser}}/afile.txt"
|
|
|
|
|
|
|
|
- name: verify that afile.txt is present
|
|
|
|
file: path={{output_dir_test}}/afile.txt state=file
|
|
|
|
|
2014-12-14 22:39:17 +00:00
|
|
|
# multiline
|
2014-08-04 14:31:30 +00:00
|
|
|
|
2015-07-14 04:23:17 +00:00
|
|
|
- name: remove test file previously created
|
|
|
|
file: path={{output_dir_test | expanduser}}/afile.txt state=absent
|
|
|
|
|
2014-08-04 14:31:30 +00:00
|
|
|
- name: execute a shell command using a literal multiline block
|
2014-08-08 16:54:38 +00:00
|
|
|
args:
|
2016-08-19 17:23:14 +00:00
|
|
|
executable: "{{ bash.stdout }}"
|
2014-08-04 14:31:30 +00:00
|
|
|
shell: |
|
2014-09-04 14:35:41 +00:00
|
|
|
echo this is a \
|
|
|
|
"multiline echo" \
|
2014-08-04 14:31:30 +00:00
|
|
|
"with a new line
|
2014-08-08 16:54:38 +00:00
|
|
|
in quotes" \
|
2016-08-19 17:23:14 +00:00
|
|
|
| {{ sha1sum.stdout }} \
|
2014-08-08 16:54:38 +00:00
|
|
|
| tr -s ' ' \
|
2014-08-04 14:31:30 +00:00
|
|
|
| cut -f1 -d ' '
|
2014-08-08 16:54:38 +00:00
|
|
|
echo "this is a second line"
|
2014-12-14 22:39:17 +00:00
|
|
|
register: shell_result5
|
2014-08-04 14:31:30 +00:00
|
|
|
|
2014-12-14 22:39:17 +00:00
|
|
|
- debug: var=shell_result5
|
2014-08-04 14:31:30 +00:00
|
|
|
|
|
|
|
- name: assert the multiline shell command ran as expected
|
|
|
|
assert:
|
|
|
|
that:
|
2014-12-14 22:39:17 +00:00
|
|
|
- "shell_result5.changed"
|
|
|
|
- "shell_result5.stdout == '5575bb6b71c9558db0b6fbbf2f19909eeb4e3b98\nthis is a second line'"
|
2014-10-15 02:12:35 +00:00
|
|
|
|
|
|
|
- name: execute a shell command using a literal multiline block with arguments in it
|
|
|
|
shell: |
|
2016-08-19 17:23:14 +00:00
|
|
|
executable="{{ bash.stdout }}"
|
2014-10-15 02:12:35 +00:00
|
|
|
creates={{output_dir_test | expanduser}}/afile.txt
|
|
|
|
echo "test"
|
2014-12-14 22:39:17 +00:00
|
|
|
register: shell_result6
|
2014-10-15 02:12:35 +00:00
|
|
|
|
|
|
|
- name: assert the multiline shell command with arguments in it run as expected
|
|
|
|
assert:
|
|
|
|
that:
|
2014-12-14 22:39:17 +00:00
|
|
|
- "shell_result6.changed"
|
|
|
|
- "shell_result6.stdout == 'test'"
|
2014-10-15 02:12:35 +00:00
|
|
|
|
|
|
|
- name: remove the previously created file
|
|
|
|
file: path={{output_dir_test}}/afile.txt state=absent
|