2014-02-24 18:08:21 +01:00
|
|
|
# Test code for the script module and action_plugin.
|
2014-02-24 17:57:22 +01:00
|
|
|
# (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/>.
|
|
|
|
|
|
|
|
##
|
|
|
|
## prep
|
|
|
|
##
|
|
|
|
|
2017-11-14 00:33:44 +01:00
|
|
|
- set_fact:
|
|
|
|
output_dir_test: "{{ output_dir }}/test_script"
|
2014-02-24 17:57:22 +01:00
|
|
|
|
|
|
|
- name: make sure our testing sub-directory does not exist
|
2017-11-14 00:33:44 +01:00
|
|
|
file:
|
|
|
|
path: "{{ output_dir_test }}"
|
|
|
|
state: absent
|
2014-02-24 17:57:22 +01:00
|
|
|
|
|
|
|
- name: create our testing sub-directory
|
2017-11-14 00:33:44 +01:00
|
|
|
file:
|
|
|
|
path: "{{ output_dir_test }}"
|
|
|
|
state: directory
|
2014-02-24 17:57:22 +01:00
|
|
|
|
|
|
|
##
|
|
|
|
## script
|
|
|
|
##
|
|
|
|
|
|
|
|
- name: execute the test.sh script via command
|
|
|
|
script: test.sh
|
|
|
|
register: script_result0
|
|
|
|
|
|
|
|
- name: assert that the script executed correctly
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- "script_result0.rc == 0"
|
|
|
|
- "script_result0.stdout == 'win'"
|
|
|
|
|
2017-11-14 00:33:44 +01:00
|
|
|
- name: Execute a script with a space in the path
|
|
|
|
script: "'space path/test.sh'"
|
|
|
|
register: _space_path_test
|
|
|
|
tags:
|
|
|
|
- spacepath
|
|
|
|
|
|
|
|
- name: Assert that script with space in path ran successfully
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- _space_path_test is success
|
|
|
|
- _space_path_test.stdout == 'Script with space in path'
|
|
|
|
tags:
|
|
|
|
- spacepath
|
|
|
|
|
|
|
|
- name: Execute a script with arguments including a unicode character
|
|
|
|
script: test_with_args.sh -this -that -Ӧther
|
|
|
|
register: unicode_args
|
2014-02-24 17:57:22 +01:00
|
|
|
|
2017-11-14 00:33:44 +01:00
|
|
|
- name: Assert that script with unicode character ran successfully
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- unicode_args is success
|
|
|
|
- unicode_args.stdout_lines[0] == '-this'
|
|
|
|
- unicode_args.stdout_lines[1] == '-that'
|
|
|
|
- unicode_args.stdout_lines[2] == '-Ӧther'
|
|
|
|
|
|
|
|
# creates
|
2014-02-24 17:57:22 +01:00
|
|
|
- name: verify that afile.txt is absent
|
2017-11-14 00:33:44 +01:00
|
|
|
file:
|
|
|
|
path: "{{ output_dir_test }}/afile.txt"
|
|
|
|
state: absent
|
2014-02-24 17:57:22 +01:00
|
|
|
|
|
|
|
- name: create afile.txt with create_afile.sh via command
|
2017-11-14 00:33:44 +01:00
|
|
|
script: create_afile.sh {{ output_dir_test | expanduser }}/afile.txt
|
|
|
|
args:
|
|
|
|
creates: "{{ output_dir_test | expanduser }}/afile.txt"
|
|
|
|
register: _create_test1
|
|
|
|
|
|
|
|
- name: Check state of created file
|
|
|
|
stat:
|
|
|
|
path: "{{ output_dir_test | expanduser }}/afile.txt"
|
|
|
|
register: _create_stat1
|
|
|
|
|
|
|
|
- name: Run create_afile.sh again to ensure it is skipped
|
|
|
|
script: create_afile.sh {{ output_dir_test | expanduser }}/afile.txt
|
|
|
|
args:
|
|
|
|
creates: "{{ output_dir_test | expanduser }}/afile.txt"
|
|
|
|
register: _create_test2
|
|
|
|
|
|
|
|
- name: Assert that script report a change, file was created, second run was skipped
|
|
|
|
assert:
|
|
|
|
that:
|
2017-11-27 23:58:08 +01:00
|
|
|
- _create_test1 is changed
|
2017-11-14 00:33:44 +01:00
|
|
|
- _create_stat1.stat.exists
|
2017-11-27 23:58:08 +01:00
|
|
|
- _create_test2 is skipped
|
2014-02-24 17:57:22 +01:00
|
|
|
|
|
|
|
|
|
|
|
# removes
|
2017-11-14 00:33:44 +01:00
|
|
|
- name: verify that afile.txt is present
|
|
|
|
file:
|
|
|
|
path: "{{ output_dir_test }}/afile.txt"
|
|
|
|
state: file
|
2014-02-24 17:57:22 +01:00
|
|
|
|
|
|
|
- name: remove afile.txt with remote_afile.sh via command
|
2017-11-14 00:33:44 +01:00
|
|
|
script: remove_afile.sh {{ output_dir_test | expanduser }}/afile.txt
|
|
|
|
args:
|
|
|
|
removes: "{{ output_dir_test | expanduser }}/afile.txt"
|
|
|
|
register: _remove_test1
|
|
|
|
|
|
|
|
- name: Check state of removed file
|
|
|
|
stat:
|
|
|
|
path: "{{ output_dir_test | expanduser }}/afile.txt"
|
|
|
|
register: _remove_stat1
|
|
|
|
|
|
|
|
- name: Run remote_afile.sh again to enure it is skipped
|
|
|
|
script: remove_afile.sh {{ output_dir_test | expanduser }}/afile.txt
|
|
|
|
args:
|
|
|
|
removes: "{{ output_dir_test | expanduser }}/afile.txt"
|
|
|
|
register: _remove_test2
|
|
|
|
|
|
|
|
- name: Assert that script report a change, file was removed, second run was skipped
|
2014-02-24 17:57:22 +01:00
|
|
|
assert:
|
|
|
|
that:
|
2017-11-27 23:58:08 +01:00
|
|
|
- _remove_test1 is changed
|
2017-11-14 00:33:44 +01:00
|
|
|
- not _remove_stat1.stat.exists
|
2017-11-27 23:58:08 +01:00
|
|
|
- _remove_test2 is skipped
|
2017-11-14 00:33:44 +01:00
|
|
|
|
2017-04-21 07:06:01 +02:00
|
|
|
|
|
|
|
# async
|
2017-11-14 00:33:44 +01:00
|
|
|
- name: verify that afile.txt is absent
|
|
|
|
file:
|
|
|
|
path: "{{ output_dir_test }}/afile.txt"
|
|
|
|
state: absent
|
2017-04-21 07:06:01 +02:00
|
|
|
|
2017-11-14 00:33:44 +01:00
|
|
|
- name: test task failure with async param
|
2017-04-21 07:06:01 +02:00
|
|
|
script: /some/script.sh
|
|
|
|
async: 2
|
|
|
|
ignore_errors: true
|
|
|
|
register: script_result3
|
|
|
|
|
|
|
|
- name: assert task with async param failed
|
|
|
|
assert:
|
|
|
|
that:
|
2017-11-27 23:58:08 +01:00
|
|
|
- script_result3 is failed
|
2017-04-21 22:17:12 +02:00
|
|
|
- script_result3.msg == "async is not supported for this task."
|
2017-11-14 00:33:44 +01:00
|
|
|
|
|
|
|
|
|
|
|
# check mode
|
|
|
|
- name: Run script to create a file in check mode
|
|
|
|
script: create_afile.sh {{ output_dir_test | expanduser }}/afile2.txt
|
|
|
|
check_mode: yes
|
|
|
|
register: _check_mode_test
|
|
|
|
|
|
|
|
- debug:
|
|
|
|
var: _check_mode_test
|
|
|
|
verbosity: 2
|
|
|
|
|
|
|
|
- name: Get state of file created by script
|
|
|
|
stat:
|
|
|
|
path: "{{ output_dir_test | expanduser }}/afile2.txt"
|
|
|
|
register: _afile_stat
|
|
|
|
|
|
|
|
- debug:
|
|
|
|
var: _afile_stat
|
|
|
|
verbosity: 2
|
|
|
|
|
|
|
|
- name: Assert that a change was reported but the script did not make changes
|
|
|
|
assert:
|
|
|
|
that:
|
2017-11-27 23:58:08 +01:00
|
|
|
- _check_mode_test is changed
|
2017-11-14 00:33:44 +01:00
|
|
|
- not _afile_stat.stat.exists
|
|
|
|
|
|
|
|
- name: Run script to create a file
|
|
|
|
script: create_afile.sh {{ output_dir_test | expanduser }}/afile2.txt
|
|
|
|
|
|
|
|
- name: Run script to create a file in check mode with 'creates' argument
|
|
|
|
script: create_afile.sh {{ output_dir_test | expanduser }}/afile2.txt
|
|
|
|
args:
|
|
|
|
creates: "{{ output_dir_test | expanduser }}/afile2.txt"
|
|
|
|
register: _check_mode_test2
|
|
|
|
check_mode: yes
|
|
|
|
|
|
|
|
- debug:
|
|
|
|
var: _check_mode_test2
|
|
|
|
verbosity: 2
|
|
|
|
|
|
|
|
- name: Assert that task was skipped and mesage was returned
|
|
|
|
assert:
|
|
|
|
that:
|
2017-11-27 23:58:08 +01:00
|
|
|
- _check_mode_test2 is skipped
|
2018-01-16 06:15:04 +01:00
|
|
|
- '_check_mode_test2.msg == "{{ output_dir_test | expanduser }}/afile2.txt exists, matching creates option"'
|
2017-11-14 00:33:44 +01:00
|
|
|
|
|
|
|
- name: Remove afile2.txt
|
|
|
|
file:
|
|
|
|
path: "{{ output_dir_test | expanduser }}/afile2.txt"
|
|
|
|
state: absent
|
|
|
|
|
|
|
|
- name: Run script to remove a file in check mode with 'removes' argument
|
|
|
|
script: remove_afile.sh {{ output_dir_test | expanduser }}/afile2.txt
|
|
|
|
args:
|
|
|
|
removes: "{{ output_dir_test | expanduser }}/afile2.txt"
|
|
|
|
register: _check_mode_test3
|
|
|
|
check_mode: yes
|
|
|
|
|
|
|
|
- debug:
|
|
|
|
var: _check_mode_test3
|
|
|
|
verbosity: 2
|
|
|
|
|
|
|
|
- name: Assert that task was skipped and message was returned
|
|
|
|
assert:
|
|
|
|
that:
|
2017-11-27 23:58:08 +01:00
|
|
|
- _check_mode_test3 is skipped
|
2018-01-16 06:15:04 +01:00
|
|
|
- '_check_mode_test3.msg == "{{ output_dir_test | expanduser }}/afile2.txt does not exist, matching removes option"'
|
2018-05-25 21:40:48 +02:00
|
|
|
|
|
|
|
# executable
|
|
|
|
|
|
|
|
- name: Run script with shebang omitted
|
|
|
|
script: no_shebang.py
|
|
|
|
args:
|
2019-03-29 09:19:51 +01:00
|
|
|
executable: "{{ ansible_python_interpreter }}"
|
2018-05-25 21:40:48 +02:00
|
|
|
register: _shebang_omitted_test
|
|
|
|
tags:
|
|
|
|
- noshebang
|
|
|
|
|
|
|
|
- name: Assert that script with shebang omitted succeeded
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- _shebang_omitted_test is success
|
|
|
|
- _shebang_omitted_test.stdout == 'Script with shebang omitted'
|
|
|
|
tags:
|
|
|
|
- noshebang
|