| 1 |
# test code for the script module when using winrm connection |
| 2 |
# (c) 2014, Chris Church <[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 |
- name: run setup to allow skipping OS-specific tests |
| 20 |
setup: |
| 21 |
gather_subset: min |
| 22 |
|
| 23 |
- name: get tempdir path |
| 24 |
raw: $env:TEMP |
| 25 |
register: tempdir |
| 26 |
|
| 27 |
- name: set script path dynamically |
| 28 |
set_fact: |
| 29 |
test_win_script_filename: "{{ tempdir.stdout_lines[0] }}/testing_win_script.txt" |
| 30 |
|
| 31 |
- name: run simple test script |
| 32 |
script: test_script.ps1 |
| 33 |
register: test_script_result |
| 34 |
|
| 35 |
- name: check that script ran |
| 36 |
assert: |
| 37 |
that: |
| 38 |
- "test_script_result.rc == 0" |
| 39 |
- "'Woohoo' in test_script_result.stdout" |
| 40 |
- (ansible_connection == 'ssh') | ternary(test_script_result.stderr is defined, not test_script_result.stderr) |
| 41 |
- "test_script_result is not failed" |
| 42 |
- "test_script_result is changed" |
| 43 |
|
| 44 |
- name: run test script that takes arguments including a unicode char |
| 45 |
script: test_script_with_args.ps1 /this /that /Ӧther |
| 46 |
register: test_script_with_args_result |
| 47 |
|
| 48 |
- name: check that script ran and received arguments and returned unicode |
| 49 |
assert: |
| 50 |
that: |
| 51 |
- "test_script_with_args_result.rc == 0" |
| 52 |
- "test_script_with_args_result.stdout | length > 0" |
| 53 |
- "test_script_with_args_result.stdout_lines[0] == '/this'" |
| 54 |
- "test_script_with_args_result.stdout_lines[1] == '/that'" |
| 55 |
- "test_script_with_args_result.stdout_lines[2] == '/Ӧther'" |
| 56 |
- (ansible_connection == 'ssh') | ternary(test_script_with_args_result.stderr is defined, not test_script_with_args_result.stderr) |
| 57 |
- "test_script_with_args_result is not failed" |
| 58 |
- "test_script_with_args_result is changed" |
| 59 |
|
| 60 |
# Bug: https://github.com/ansible/ansible/issues/32850 |
| 61 |
- name: set fact of long string |
| 62 |
set_fact: |
| 63 |
long_string: "{{ lookup('pipe', 'printf \"a%.0s\" {1..1000}') }}" |
| 64 |
|
| 65 |
- name: run test script with args that exceed the stdin buffer |
| 66 |
script: test_script_with_args.ps1 {{ long_string }} |
| 67 |
register: test_script_with_large_args_result |
| 68 |
|
| 69 |
- name: check that script ran and received arguments correctly |
| 70 |
assert: |
| 71 |
that: |
| 72 |
- test_script_with_large_args_result.rc == 0 |
| 73 |
- (ansible_connection == 'ssh') | ternary(test_script_with_large_args_result.stderr is defined, not test_script_with_large_args_result.stderr) |
| 74 |
- test_script_with_large_args_result is not failed |
| 75 |
- test_script_with_large_args_result is changed |
| 76 |
|
| 77 |
- name: check that script ran and received arguments correctly with winrm output |
| 78 |
assert: |
| 79 |
that: |
| 80 |
- test_script_with_large_args_result.stdout == long_string + "\r\n" |
| 81 |
when: ansible_connection != 'psrp' |
| 82 |
|
| 83 |
- name: check that script ran and received arguments correctly with psrp output |
| 84 |
assert: |
| 85 |
that: |
| 86 |
- test_script_with_large_args_result.stdout == long_string |
| 87 |
when: ansible_connection == 'psrp' |
| 88 |
|
| 89 |
- name: run test script that takes parameters passed via splatting |
| 90 |
script: test_script_with_splatting.ps1 @{ This = 'this'; That = '{{ test_win_script_value }}'; Other = 'other'} |
| 91 |
register: test_script_with_splatting_result |
| 92 |
|
| 93 |
- name: check that script ran and received parameters via splatting |
| 94 |
assert: |
| 95 |
that: |
| 96 |
- "test_script_with_splatting_result.rc == 0" |
| 97 |
- "test_script_with_splatting_result.stdout | length > 0" |
| 98 |
- "test_script_with_splatting_result.stdout_lines[0] == 'this'" |
| 99 |
- "test_script_with_splatting_result.stdout_lines[1] == test_win_script_value" |
| 100 |
- "test_script_with_splatting_result.stdout_lines[2] == 'other'" |
| 101 |
- (ansible_connection == 'ssh') | ternary(test_script_with_splatting_result.stderr is defined, not test_script_with_splatting_result.stderr) |
| 102 |
- "test_script_with_splatting_result is not failed" |
| 103 |
- "test_script_with_splatting_result is changed" |
| 104 |
|
| 105 |
- name: run test script that takes splatted parameters from a variable |
| 106 |
script: test_script_with_splatting.ps1 {{ test_win_script_splat }} |
| 107 |
register: test_script_with_splatting2_result |
| 108 |
|
| 109 |
- name: check that script ran and received parameters via splatting from a variable |
| 110 |
assert: |
| 111 |
that: |
| 112 |
- "test_script_with_splatting2_result.rc == 0" |
| 113 |
- "test_script_with_splatting2_result.stdout | length > 0" |
| 114 |
- "test_script_with_splatting2_result.stdout_lines[0] == 'THIS'" |
| 115 |
- "test_script_with_splatting2_result.stdout_lines[1] == 'THAT'" |
| 116 |
- "test_script_with_splatting2_result.stdout_lines[2] == 'OTHER'" |
| 117 |
- (ansible_connection == 'ssh') | ternary(test_script_with_splatting2_result.stderr is defined, not test_script_with_splatting2_result.stderr) |
| 118 |
- "test_script_with_splatting2_result is not failed" |
| 119 |
- "test_script_with_splatting2_result is changed" |
| 120 |
|
| 121 |
- name: run test script that has errors |
| 122 |
script: test_script_with_errors.ps1 |
| 123 |
register: test_script_with_errors_result |
| 124 |
ignore_errors: true |
| 125 |
|
| 126 |
- name: check that script ran but failed with errors |
| 127 |
assert: |
| 128 |
that: |
| 129 |
- "test_script_with_errors_result.rc == 1" |
| 130 |
- "not test_script_with_errors_result.stdout" |
| 131 |
- test_script_with_errors_result.stderr is search("Oh noes I has an error\\r\\nAt ansible\.builtin\.script\.test_script_with_errors\.ps1") |
| 132 |
- "test_script_with_errors_result is failed" |
| 133 |
- "test_script_with_errors_result is changed" |
| 134 |
|
| 135 |
- name: cleanup test file if it exists |
| 136 |
raw: Remove-Item "{{ test_win_script_filename }}" -Force |
| 137 |
ignore_errors: true |
| 138 |
|
| 139 |
- name: run test script that creates a file |
| 140 |
script: test_script_creates_file.ps1 {{ test_win_script_filename }} |
| 141 |
args: |
| 142 |
creates: "{{ test_win_script_filename }}" |
| 143 |
register: test_script_creates_file_result |
| 144 |
|
| 145 |
- name: check that script ran and indicated a change |
| 146 |
assert: |
| 147 |
that: |
| 148 |
- "test_script_creates_file_result.rc == 0" |
| 149 |
- "not test_script_creates_file_result.stdout" |
| 150 |
- (ansible_connection == 'ssh') | ternary(test_script_creates_file_result.stderr is defined, not test_script_creates_file_result.stderr) |
| 151 |
- "test_script_creates_file_result is not failed" |
| 152 |
- "test_script_creates_file_result is changed" |
| 153 |
|
| 154 |
- name: run test script that creates a file again |
| 155 |
script: test_script_creates_file.ps1 {{ test_win_script_filename }} |
| 156 |
args: |
| 157 |
creates: "{{ test_win_script_filename }}" |
| 158 |
register: test_script_creates_file_again_result |
| 159 |
|
| 160 |
- name: check that the script did not run since the remote file exists |
| 161 |
assert: |
| 162 |
that: |
| 163 |
- "test_script_creates_file_again_result is not failed" |
| 164 |
- "test_script_creates_file_again_result is not changed" |
| 165 |
- "test_script_creates_file_again_result is skipped" |
| 166 |
|
| 167 |
- name: run test script that removes a file |
| 168 |
script: test_script_removes_file.ps1 {{ test_win_script_filename }} |
| 169 |
args: |
| 170 |
removes: "{{ test_win_script_filename }}" |
| 171 |
register: test_script_removes_file_result |
| 172 |
|
| 173 |
- name: check that the script ran since the remote file exists |
| 174 |
assert: |
| 175 |
that: |
| 176 |
- "test_script_removes_file_result.rc == 0" |
| 177 |
- "not test_script_removes_file_result.stdout" |
| 178 |
- (ansible_connection == 'ssh') | ternary(test_script_removes_file_result.stderr is defined, not test_script_removes_file_result.stderr) |
| 179 |
- "test_script_removes_file_result is not failed" |
| 180 |
- "test_script_removes_file_result is changed" |
| 181 |
|
| 182 |
- name: run test script that removes a file again |
| 183 |
script: test_script_removes_file.ps1 {{ test_win_script_filename }} |
| 184 |
args: |
| 185 |
removes: "{{ test_win_script_filename }}" |
| 186 |
register: test_script_removes_file_again_result |
| 187 |
|
| 188 |
- name: check that the script did not run since the remote file does not exist |
| 189 |
assert: |
| 190 |
that: |
| 191 |
- "test_script_removes_file_again_result is not failed" |
| 192 |
- "test_script_removes_file_again_result is not changed" |
| 193 |
- "test_script_removes_file_again_result is skipped" |
| 194 |
|
| 195 |
- name: skip batch tests on 6.0 (UTF8 codepage prevents it from working, see https://github.com/ansible/ansible/issues/21915) |
| 196 |
block: |
| 197 |
- name: run simple batch file |
| 198 |
script: test_script.bat |
| 199 |
register: test_batch_result |
| 200 |
|
| 201 |
- name: check that batch file ran |
| 202 |
assert: |
| 203 |
that: |
| 204 |
- "test_batch_result.rc == 0" |
| 205 |
- "'batch' in test_batch_result.stdout" |
| 206 |
- (ansible_connection == 'ssh') | ternary(test_batch_result.stderr is defined, not test_batch_result.stderr) |
| 207 |
- "test_batch_result is not failed" |
| 208 |
- "test_batch_result is changed" |
| 209 |
|
| 210 |
- name: run simple batch file with .cmd extension |
| 211 |
script: test_script.cmd |
| 212 |
register: test_cmd_result |
| 213 |
|
| 214 |
- name: check that batch file with .cmd extension ran |
| 215 |
assert: |
| 216 |
that: |
| 217 |
- "test_cmd_result.rc == 0" |
| 218 |
- "'cmd extension' in test_cmd_result.stdout" |
| 219 |
- (ansible_connection == 'ssh') | ternary(test_cmd_result.stderr is defined, not test_cmd_result.stderr) |
| 220 |
- "test_cmd_result is not failed" |
| 221 |
- "test_cmd_result is changed" |
| 222 |
|
| 223 |
- name: run simple batch file with .bat extension that fails |
| 224 |
script: fail.bat |
| 225 |
ignore_errors: true |
| 226 |
register: test_batch_result |
| 227 |
|
| 228 |
- name: check that batch file with .bat extension reported failure |
| 229 |
assert: |
| 230 |
that: |
| 231 |
- test_batch_result.rc == 1 |
| 232 |
- test_batch_result.stdout | length > 0 |
| 233 |
- test_batch_result.stderr | length > 0 |
| 234 |
- test_batch_result is failed |
| 235 |
- test_batch_result is changed |
| 236 |
when: not ansible_distribution_version.startswith('6.0') |
| 237 |
|
| 238 |
- name: run test script that takes a boolean parameter |
| 239 |
script: test_script_bool.ps1 $false # use false as that can pick up more errors |
| 240 |
register: test_script_bool_result |
| 241 |
|
| 242 |
- name: check that the script ran and the parameter was treated as a boolean |
| 243 |
assert: |
| 244 |
that: |
| 245 |
- test_script_bool_result.stdout_lines[0] == 'System.Boolean' |
| 246 |
- test_script_bool_result.stdout_lines[1] == 'False' |
| 247 |
|
| 248 |
- name: run test script that uses envvars |
| 249 |
script: test_script_with_env.ps1 |
| 250 |
environment: |
| 251 |
taskenv: task |
| 252 |
register: test_script_env_result |
| 253 |
|
| 254 |
- name: ensure that script ran and that environment var was passed |
| 255 |
assert: |
| 256 |
that: |
| 257 |
- test_script_env_result is successful |
| 258 |
- test_script_env_result.stdout_lines[0] == 'task' |
| 259 |
|
| 260 |
# check mode |
| 261 |
- name: Run test script that creates a file in check mode |
| 262 |
script: test_script_creates_file.ps1 {{ test_win_script_filename }} |
| 263 |
args: |
| 264 |
creates: "{{ test_win_script_filename }}" |
| 265 |
check_mode: yes |
| 266 |
register: test_script_creates_file_check_mode |
| 267 |
|
| 268 |
- name: Get state of file created by script |
| 269 |
win_stat: |
| 270 |
path: "{{ test_win_script_filename }}" |
| 271 |
register: create_file_stat |
| 272 |
|
| 273 |
- name: Assert that a change was reported but the script did not make changes |
| 274 |
assert: |
| 275 |
that: |
| 276 |
- test_script_creates_file_check_mode is changed |
| 277 |
- not create_file_stat.stat.exists |
| 278 |
|
| 279 |
- name: Run test script that creates a file |
| 280 |
script: test_script_creates_file.ps1 {{ test_win_script_filename }} |
| 281 |
args: |
| 282 |
creates: "{{ test_win_script_filename }}" |
| 283 |
|
| 284 |
- name: Run test script that removes a file in check mode |
| 285 |
script: test_script_removes_file.ps1 {{ test_win_script_filename }} |
| 286 |
args: |
| 287 |
removes: "{{ test_win_script_filename }}" |
| 288 |
check_mode: yes |
| 289 |
register: test_script_removes_file_check_mode |
| 290 |
|
| 291 |
- name: Get state of file removed by script |
| 292 |
win_stat: |
| 293 |
path: "{{ test_win_script_filename }}" |
| 294 |
register: remove_file_stat |
| 295 |
|
| 296 |
- name: Assert that a change was reported but the script did not make changes |
| 297 |
assert: |
| 298 |
that: |
| 299 |
- test_script_removes_file_check_mode is changed |
| 300 |
- remove_file_stat.stat.exists |
| 301 |
|
| 302 |
- name: run test script with become that outputs 2 lines |
| 303 |
script: test_script_whoami.ps1 |
| 304 |
register: test_script_result_become |
| 305 |
become: yes |
| 306 |
become_user: SYSTEM |
| 307 |
become_method: runas |
| 308 |
|
| 309 |
- name: check that the script ran and we get both outputs on new lines |
| 310 |
assert: |
| 311 |
that: |
| 312 |
- test_script_result_become.stdout_lines[0]|lower == 'nt authority\\system' |
| 313 |
- test_script_result_become.stdout_lines[1] == 'finished' |
| 314 |
|
| 315 |
- name: run test script with become set by vars |
| 316 |
script: test_script_whoami.ps1 |
| 317 |
register: test_script_result_become_vars |
| 318 |
vars: |
| 319 |
ansible_become: yes |
| 320 |
ansible_become_user: SYSTEM |
| 321 |
ansible_become_method: runas |
| 322 |
|
| 323 |
- name: check that the script ran and we get both outputs on new lines |
| 324 |
assert: |
| 325 |
that: |
| 326 |
- test_script_result_become_vars.stdout_lines[0]|lower == 'nt authority\\system' |
| 327 |
- test_script_result_become_vars.stdout_lines[1] == 'finished' |
| 328 |
|
| 329 |
- name: run script that emits stderr from sub process |
| 330 |
script: test_script_with_native_stderr.ps1 |
| 331 |
register: script_stderr |
| 332 |
|
| 333 |
- name: check that script ran and emitted stderr |
| 334 |
assert: |
| 335 |
that: |
| 336 |
- script_stderr.rc == 0 |
| 337 |
- script_stderr.stdout == "" |
| 338 |
# SSH includes debug output in stderr and includes a trailing newline |
| 339 |
# Use a simple search to ensure the expected stderr is present but ignoring any extra output |
| 340 |
- script_stderr.stderr == "stderr 1\r\nstderr 2\r\n" |
| 341 |
|
| 342 |
- name: run script with non-ASCII contents |
| 343 |
script: test_script_unicode.ps1 |
| 344 |
register: script_unicode |
| 345 |
|
| 346 |
- name: assert run script with non-ASCII contents |
| 347 |
assert: |
| 348 |
that: |
| 349 |
- script_unicode.stdout | trim == 'ünicode' |
| 350 |
|
| 351 |
- name: run PowerShell script with specific executable |
| 352 |
script: test_psversion.ps1 |
| 353 |
args: |
| 354 |
executable: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe |
| 355 |
register: script_with_exe |
| 356 |
|
| 357 |
- name: check that script with executable ran |
| 358 |
assert: |
| 359 |
that: |
| 360 |
- script_with_exe.rc == 0 |
| 361 |
- "'5.1' in script_with_exe.stdout" |
| 362 |
- (ansible_connection == 'ssh') | ternary(script_with_exe.stderr is defined, not script_with_exe.stderr) |
| 363 |
- script_with_exe is not failed |
| 364 |
- script_with_exe is changed |
| 365 |
|
| 366 |
- name: run PowerShell script that is quoted |
| 367 |
script: >- |
| 368 |
"test_script_with_args.ps1" 123 abc 'def ghi' "jkl mno" |
| 369 |
register: script_with_quotes |
| 370 |
|
| 371 |
- name: check that script with quotes ran |
| 372 |
assert: |
| 373 |
that: |
| 374 |
- script_with_quotes.rc == 0 |
| 375 |
- script_with_quotes.stdout_lines | count == 4 |
| 376 |
- script_with_quotes.stdout_lines[0] == '123' |
| 377 |
- script_with_quotes.stdout_lines[1] == 'abc' |
| 378 |
- script_with_quotes.stdout_lines[2] == 'def ghi' |
| 379 |
- script_with_quotes.stdout_lines[3] == 'jkl mno' |
| 380 |
- (ansible_connection == 'ssh') | ternary(script_with_quotes.stderr is defined, not script_with_quotes.stderr) |
| 381 |
- script_with_quotes is not failed |
| 382 |
- script_with_quotes is changed |