1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
|
# Test code for the script module and action_plugin.
# (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
##
- set_fact:
remote_tmp_dir_test: "{{ remote_tmp_dir }}/test_script"
- name: make sure our testing sub-directory does not exist
file:
path: "{{ remote_tmp_dir_test }}"
state: absent
- name: create our testing sub-directory
file:
path: "{{ remote_tmp_dir_test }}"
state: directory
##
## script
##
- name: Required one of free-form and cmd
script:
ignore_errors: yes
register: script_required
- name: assert that the script fails if neither free-form nor cmd is given
assert:
that:
- script_required.failed
- "'one of the following' in script_required.msg"
- 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'"
- 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
- 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
- name: verify that afile.txt is absent
file:
path: "{{ remote_tmp_dir_test }}/afile.txt"
state: absent
- name: create afile.txt with create_afile.sh via command
script: create_afile.sh {{ remote_tmp_dir_test | expanduser }}/afile.txt
args:
creates: "{{ remote_tmp_dir_test | expanduser }}/afile.txt"
register: _create_test1
- name: Check state of created file
stat:
path: "{{ remote_tmp_dir_test | expanduser }}/afile.txt"
register: _create_stat1
- name: Run create_afile.sh again to ensure it is skipped
script: create_afile.sh {{ remote_tmp_dir_test | expanduser }}/afile.txt
args:
creates: "{{ remote_tmp_dir_test | expanduser }}/afile.txt"
register: _create_test2
- name: Assert that script report a change, file was created, second run was skipped
assert:
that:
- _create_test1 is changed
- _create_stat1.stat.exists
- _create_test2 is skipped
# removes
- name: verify that afile.txt is present
file:
path: "{{ remote_tmp_dir_test }}/afile.txt"
state: file
- name: remove afile.txt with remote_afile.sh via command
script: remove_afile.sh {{ remote_tmp_dir_test | expanduser }}/afile.txt
args:
removes: "{{ remote_tmp_dir_test | expanduser }}/afile.txt"
register: _remove_test1
- name: Check state of removed file
stat:
path: "{{ remote_tmp_dir_test | expanduser }}/afile.txt"
register: _remove_stat1
- name: Run remote_afile.sh again to enure it is skipped
script: remove_afile.sh {{ remote_tmp_dir_test | expanduser }}/afile.txt
args:
removes: "{{ remote_tmp_dir_test | expanduser }}/afile.txt"
register: _remove_test2
- name: Assert that script report a change, file was removed, second run was skipped
assert:
that:
- _remove_test1 is changed
- not _remove_stat1.stat.exists
- _remove_test2 is skipped
# async
- name: verify that afile.txt is absent
file:
path: "{{ remote_tmp_dir_test }}/afile.txt"
state: absent
- name: test task failure with async param
script: /some/script.sh
async: 2
ignore_errors: true
register: script_result3
- name: assert task with async param failed
assert:
that:
- script_result3 is failed
- script_result3.msg == "This action (script) does not support async."
# check mode
- name: Run script to create a file in check mode
script: create_afile.sh {{ remote_tmp_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: "{{ remote_tmp_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:
- _check_mode_test is not changed
- _check_mode_test is skipped
- not _afile_stat.stat.exists
- name: Run script to create a file
script: create_afile.sh {{ remote_tmp_dir_test | expanduser }}/afile2.txt
- name: Run script to create a file in check mode with 'creates' argument
script: create_afile.sh {{ remote_tmp_dir_test | expanduser }}/afile2.txt
args:
creates: "{{ remote_tmp_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 message was returned
assert:
that:
- _check_mode_test2 is skipped
- '_check_mode_test2.msg == (remote_tmp_dir_test | expanduser) + "/afile2.txt exists, matching creates option"'
- name: Remove afile2.txt
file:
path: "{{ remote_tmp_dir_test | expanduser }}/afile2.txt"
state: absent
- name: Run script to remove a file in check mode with 'removes' argument
script: remove_afile.sh {{ remote_tmp_dir_test | expanduser }}/afile2.txt
args:
removes: "{{ remote_tmp_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:
- _check_mode_test3 is skipped
- '_check_mode_test3.msg == (remote_tmp_dir_test | expanduser) + "/afile2.txt does not exist, matching removes option"'
# executable
- name: Run script with shebang omitted
script: no_shebang.py
args:
executable: "{{ ansible_python_interpreter }}"
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
|