| 1 |
# Test code for the script module and action_plugin. |
| 2 |
# (c) 2014, Richard Isaacson <[email protected]> |
| 3 |
|
| 4 |
# This file is part of Ansible |
| 5 |
# |
| 6 |
# Ansible is free software: you can redistribute it and/or modify |
| 7 |
# it under the terms of the GNU General Public License as published by |
| 8 |
# the Free Software Foundation, either version 3 of the License, or |
| 9 |
# (at your option) any later version. |
| 10 |
# |
| 11 |
# Ansible is distributed in the hope that it will be useful, |
| 12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
# GNU General Public License for more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU General Public License |
| 17 |
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. |
| 18 |
|
| 19 |
## |
| 20 |
## prep |
| 21 |
## |
| 22 |
|
| 23 |
- set_fact: |
| 24 |
remote_tmp_dir_test: "{{ remote_tmp_dir }}/test_script" |
| 25 |
|
| 26 |
- name: make sure our testing sub-directory does not exist |
| 27 |
file: |
| 28 |
path: "{{ remote_tmp_dir_test }}" |
| 29 |
state: absent |
| 30 |
|
| 31 |
- name: create our testing sub-directory |
| 32 |
file: |
| 33 |
path: "{{ remote_tmp_dir_test }}" |
| 34 |
state: directory |
| 35 |
|
| 36 |
## |
| 37 |
## script |
| 38 |
## |
| 39 |
|
| 40 |
- name: Required one of free-form and cmd |
| 41 |
script: |
| 42 |
ignore_errors: yes |
| 43 |
register: script_required |
| 44 |
|
| 45 |
- name: assert that the script fails if neither free-form nor cmd is given |
| 46 |
assert: |
| 47 |
that: |
| 48 |
- script_required.failed |
| 49 |
- "'one of the following' in script_required.msg" |
| 50 |
|
| 51 |
- name: execute the test.sh script via command |
| 52 |
script: test.sh |
| 53 |
register: script_result0 |
| 54 |
|
| 55 |
- name: assert that the script executed correctly |
| 56 |
assert: |
| 57 |
that: |
| 58 |
- "script_result0.rc == 0" |
| 59 |
- "script_result0.stdout == 'win'" |
| 60 |
|
| 61 |
- name: Basic non-zero RC |
| 62 |
script: exit_1.sh |
| 63 |
ignore_errors: true |
| 64 |
register: non_zero_rc |
| 65 |
|
| 66 |
- name: Ensure non-zero RC result |
| 67 |
assert: |
| 68 |
that: |
| 69 |
- non_zero_rc is failed |
| 70 |
- non_zero_rc.rc == 1 |
| 71 |
|
| 72 |
- name: Exercise failed_when on non-zero RC |
| 73 |
script: exit_1.sh |
| 74 |
register: non_zero_rc_failed_when |
| 75 |
failed_when: false |
| 76 |
|
| 77 |
- name: Ensure failed_when executed |
| 78 |
assert: |
| 79 |
that: |
| 80 |
- non_zero_rc_failed_when is success |
| 81 |
- non_zero_rc_failed_when.rc == 1 |
| 82 |
- non_zero_rc_failed_when.failed_when_result is false |
| 83 |
|
| 84 |
- name: Execute a script with a space in the path |
| 85 |
script: "'space path/test.sh'" |
| 86 |
register: _space_path_test |
| 87 |
tags: |
| 88 |
- spacepath |
| 89 |
|
| 90 |
- name: Assert that script with space in path ran successfully |
| 91 |
assert: |
| 92 |
that: |
| 93 |
- _space_path_test is success |
| 94 |
- _space_path_test.stdout == 'Script with space in path' |
| 95 |
tags: |
| 96 |
- spacepath |
| 97 |
|
| 98 |
- name: Execute a script with arguments including a unicode character |
| 99 |
script: test_with_args.sh -this -that -Ӧther |
| 100 |
register: unicode_args |
| 101 |
|
| 102 |
- name: Assert that script with unicode character ran successfully |
| 103 |
assert: |
| 104 |
that: |
| 105 |
- unicode_args is success |
| 106 |
- unicode_args.stdout_lines[0] == '-this' |
| 107 |
- unicode_args.stdout_lines[1] == '-that' |
| 108 |
- unicode_args.stdout_lines[2] == '-Ӧther' |
| 109 |
|
| 110 |
# creates |
| 111 |
- name: verify that afile.txt is absent |
| 112 |
file: |
| 113 |
path: "{{ remote_tmp_dir_test }}/afile.txt" |
| 114 |
state: absent |
| 115 |
|
| 116 |
- name: create afile.txt with create_afile.sh via command |
| 117 |
script: create_afile.sh {{ remote_tmp_dir_test | expanduser }}/afile.txt |
| 118 |
args: |
| 119 |
creates: "{{ remote_tmp_dir_test | expanduser }}/afile.txt" |
| 120 |
register: _create_test1 |
| 121 |
|
| 122 |
- name: Check state of created file |
| 123 |
stat: |
| 124 |
path: "{{ remote_tmp_dir_test | expanduser }}/afile.txt" |
| 125 |
register: _create_stat1 |
| 126 |
|
| 127 |
- name: Run create_afile.sh again to ensure it isn't changed |
| 128 |
script: create_afile.sh {{ remote_tmp_dir_test | expanduser }}/afile.txt |
| 129 |
args: |
| 130 |
creates: "{{ remote_tmp_dir_test | expanduser }}/afile.txt" |
| 131 |
register: _create_test2 |
| 132 |
|
| 133 |
- name: Assert that script report a change, file was created, second run wasn't changed |
| 134 |
assert: |
| 135 |
that: |
| 136 |
- _create_test1 is changed |
| 137 |
- _create_stat1.stat.exists |
| 138 |
- _create_test2 is not changed |
| 139 |
- _create_test2.msg is contains "matching creates option" |
| 140 |
|
| 141 |
|
| 142 |
# removes |
| 143 |
- name: verify that afile.txt is present |
| 144 |
file: |
| 145 |
path: "{{ remote_tmp_dir_test }}/afile.txt" |
| 146 |
state: file |
| 147 |
|
| 148 |
- name: remove afile.txt with remote_afile.sh via command |
| 149 |
script: remove_afile.sh {{ remote_tmp_dir_test | expanduser }}/afile.txt |
| 150 |
args: |
| 151 |
removes: "{{ remote_tmp_dir_test | expanduser }}/afile.txt" |
| 152 |
register: _remove_test1 |
| 153 |
|
| 154 |
- name: Check state of removed file |
| 155 |
stat: |
| 156 |
path: "{{ remote_tmp_dir_test | expanduser }}/afile.txt" |
| 157 |
register: _remove_stat1 |
| 158 |
|
| 159 |
- name: Run remote_afile.sh again to enure it isn't changed |
| 160 |
script: remove_afile.sh {{ remote_tmp_dir_test | expanduser }}/afile.txt |
| 161 |
args: |
| 162 |
removes: "{{ remote_tmp_dir_test | expanduser }}/afile.txt" |
| 163 |
register: _remove_test2 |
| 164 |
|
| 165 |
- name: Assert that script report a change, file was removed, second run wasn't changed |
| 166 |
assert: |
| 167 |
that: |
| 168 |
- _remove_test1 is changed |
| 169 |
- not _remove_stat1.stat.exists |
| 170 |
- _remove_test2 is not changed |
| 171 |
- _remove_test2.msg is contains "matching removes option" |
| 172 |
|
| 173 |
|
| 174 |
# async |
| 175 |
- name: verify that afile.txt is absent |
| 176 |
file: |
| 177 |
path: "{{ remote_tmp_dir_test }}/afile.txt" |
| 178 |
state: absent |
| 179 |
|
| 180 |
- name: test task failure with async param |
| 181 |
script: /some/script.sh |
| 182 |
async: 2 |
| 183 |
ignore_errors: true |
| 184 |
register: script_result3 |
| 185 |
|
| 186 |
- name: assert task with async param failed |
| 187 |
assert: |
| 188 |
that: |
| 189 |
- script_result3 is failed |
| 190 |
- script_result3.msg == "This action (script) does not support async." |
| 191 |
|
| 192 |
|
| 193 |
# check mode |
| 194 |
- name: Run script to create a file in check mode |
| 195 |
script: create_afile.sh {{ remote_tmp_dir_test | expanduser }}/afile2.txt |
| 196 |
check_mode: yes |
| 197 |
register: _check_mode_test |
| 198 |
|
| 199 |
- debug: |
| 200 |
var: _check_mode_test |
| 201 |
verbosity: 2 |
| 202 |
|
| 203 |
- name: Get state of file created by script |
| 204 |
stat: |
| 205 |
path: "{{ remote_tmp_dir_test | expanduser }}/afile2.txt" |
| 206 |
register: _afile_stat |
| 207 |
|
| 208 |
- debug: |
| 209 |
var: _afile_stat |
| 210 |
verbosity: 2 |
| 211 |
|
| 212 |
- name: Assert that a change was reported but the script did not make changes |
| 213 |
assert: |
| 214 |
that: |
| 215 |
- _check_mode_test is not changed |
| 216 |
- not _afile_stat.stat.exists |
| 217 |
|
| 218 |
- name: Run script to create a file |
| 219 |
script: create_afile.sh {{ remote_tmp_dir_test | expanduser }}/afile2.txt |
| 220 |
|
| 221 |
- name: Run script to create a file in check mode with 'creates' argument |
| 222 |
script: create_afile.sh {{ remote_tmp_dir_test | expanduser }}/afile2.txt |
| 223 |
args: |
| 224 |
creates: "{{ remote_tmp_dir_test | expanduser }}/afile2.txt" |
| 225 |
register: _check_mode_test2 |
| 226 |
check_mode: yes |
| 227 |
|
| 228 |
- debug: |
| 229 |
var: _check_mode_test2 |
| 230 |
verbosity: 2 |
| 231 |
|
| 232 |
- name: Assert that task was not changed and message was returned |
| 233 |
assert: |
| 234 |
that: |
| 235 |
- _check_mode_test2 is not changed |
| 236 |
- '_check_mode_test2.msg == (remote_tmp_dir_test | expanduser) + "/afile2.txt exists, matching creates option"' |
| 237 |
|
| 238 |
- name: Remove afile2.txt |
| 239 |
file: |
| 240 |
path: "{{ remote_tmp_dir_test | expanduser }}/afile2.txt" |
| 241 |
state: absent |
| 242 |
|
| 243 |
- name: Run script to remove a file in check mode with 'removes' argument |
| 244 |
script: remove_afile.sh {{ remote_tmp_dir_test | expanduser }}/afile2.txt |
| 245 |
args: |
| 246 |
removes: "{{ remote_tmp_dir_test | expanduser }}/afile2.txt" |
| 247 |
register: _check_mode_test3 |
| 248 |
check_mode: yes |
| 249 |
|
| 250 |
- debug: |
| 251 |
var: _check_mode_test3 |
| 252 |
verbosity: 2 |
| 253 |
|
| 254 |
- name: Assert that task was not changed and message was returned |
| 255 |
assert: |
| 256 |
that: |
| 257 |
- _check_mode_test3 is not changed |
| 258 |
- '_check_mode_test3.msg == (remote_tmp_dir_test | expanduser) + "/afile2.txt does not exist, matching removes option"' |
| 259 |
|
| 260 |
# executable |
| 261 |
|
| 262 |
- name: Run script with shebang omitted |
| 263 |
script: no_shebang.py |
| 264 |
args: |
| 265 |
executable: "{{ ansible_python_interpreter }}" |
| 266 |
register: _shebang_omitted_test |
| 267 |
tags: |
| 268 |
- noshebang |
| 269 |
|
| 270 |
- name: Assert that script with shebang omitted succeeded |
| 271 |
assert: |
| 272 |
that: |
| 273 |
- _shebang_omitted_test is success |
| 274 |
- _shebang_omitted_test.stdout == 'Script with shebang omitted' |
| 275 |
tags: |
| 276 |
- noshebang |