Add proper check mode support to the script module (#31852)

* Do not run script in check mode

Fixes #30676

* Reformat script integration test

* Add integration tests for check mode of script module

* Fix name on test

* Cleanup temp file

* win_script integration test syntaxt changes

* Add check mode tests for win_script

* Use proper variable in test

* Fail if source file does not exist

* Verify script is accessible and don't copy in check mode

Use shlex to properly split shell arguments, though a path with spaces in it still needs to be quoted in the playbook.
Add note to docs describing such.
Improve error message if file is not found indicating there may be a space in the path.

* Properly encode path now that path is split using shlex

* Allow for spaces in both path and script name

* Add unicode character test to Linux script tests

* Add Linux test for space in path to script
This commit is contained in:
Sam Doran 2017-11-13 18:33:44 -05:00 committed by Matt Davis
parent 474bf208e9
commit ea3638b580
7 changed files with 291 additions and 75 deletions

View file

@ -0,0 +1 @@
Write-Host "Ansible supports spaces in the path to the script."

View file

@ -35,8 +35,8 @@
- "test_script_result.stdout"
- "'Woohoo' in test_script_result.stdout"
- "not test_script_result.stderr"
- "not test_script_result|failed"
- "test_script_result|changed"
- "not test_script_result | failed"
- "test_script_result | changed"
- name: run test script that takes arguments including a unicode char
script: test_script_with_args.ps1 /this /that /Ӧther
@ -51,8 +51,8 @@
- "test_script_with_args_result.stdout_lines[1] == '/that'"
- "test_script_with_args_result.stdout_lines[2] == '/Ӧther'"
- "not test_script_with_args_result.stderr"
- "not test_script_with_args_result|failed"
- "test_script_with_args_result|changed"
- "not test_script_with_args_result | failed"
- "test_script_with_args_result | changed"
- name: run test script that takes parameters passed via splatting
script: test_script_with_splatting.ps1 @{ This = 'this'; That = '{{ test_win_script_value }}'; Other = 'other'}
@ -67,8 +67,8 @@
- "test_script_with_splatting_result.stdout_lines[1] == test_win_script_value"
- "test_script_with_splatting_result.stdout_lines[2] == 'other'"
- "not test_script_with_splatting_result.stderr"
- "not test_script_with_splatting_result|failed"
- "test_script_with_splatting_result|changed"
- "not test_script_with_splatting_result | failed"
- "test_script_with_splatting_result | changed"
- name: run test script that takes splatted parameters from a variable
script: test_script_with_splatting.ps1 {{ test_win_script_splat }}
@ -83,8 +83,8 @@
- "test_script_with_splatting2_result.stdout_lines[1] == 'THAT'"
- "test_script_with_splatting2_result.stdout_lines[2] == 'OTHER'"
- "not test_script_with_splatting2_result.stderr"
- "not test_script_with_splatting2_result|failed"
- "test_script_with_splatting2_result|changed"
- "not test_script_with_splatting2_result | failed"
- "test_script_with_splatting2_result | changed"
- name: run test script that has errors
script: test_script_with_errors.ps1
@ -97,15 +97,17 @@
- "test_script_with_errors_result.rc != 0"
- "not test_script_with_errors_result.stdout"
- "test_script_with_errors_result.stderr"
- "test_script_with_errors_result|failed"
- "test_script_with_errors_result|changed"
- "test_script_with_errors_result | failed"
- "test_script_with_errors_result | changed"
- name: cleanup test file if it exists
raw: Remove-Item "{{test_win_script_filename}}" -Force
raw: Remove-Item "{{ test_win_script_filename }}" -Force
ignore_errors: true
- name: run test script that creates a file
script: test_script_creates_file.ps1 "{{test_win_script_filename}}" creates="{{test_win_script_filename}}"
script: test_script_creates_file.ps1 {{ test_win_script_filename }}
args:
creates: "{{ test_win_script_filename }}"
register: test_script_creates_file_result
- name: check that script ran and indicated a change
@ -114,22 +116,26 @@
- "test_script_creates_file_result.rc == 0"
- "not test_script_creates_file_result.stdout"
- "not test_script_creates_file_result.stderr"
- "not test_script_creates_file_result|failed"
- "test_script_creates_file_result|changed"
- "not test_script_creates_file_result | failed"
- "test_script_creates_file_result | changed"
- name: run test script that creates a file again
script: test_script_creates_file.ps1 "{{test_win_script_filename}}" creates="{{test_win_script_filename}}"
script: test_script_creates_file.ps1 {{ test_win_script_filename }}
args:
creates: "{{ test_win_script_filename }}"
register: test_script_creates_file_again_result
- name: check that the script did not run since the remote file exists
assert:
that:
- "not test_script_creates_file_again_result|failed"
- "not test_script_creates_file_again_result|changed"
- "test_script_creates_file_again_result|skipped"
- "not test_script_creates_file_again_result | failed"
- "not test_script_creates_file_again_result | changed"
- "test_script_creates_file_again_result | skipped"
- name: run test script that removes a file
script: test_script_removes_file.ps1 "{{test_win_script_filename}}" removes="{{test_win_script_filename}}"
script: test_script_removes_file.ps1 {{ test_win_script_filename }}
args:
removes: "{{ test_win_script_filename }}"
register: test_script_removes_file_result
- name: check that the script ran since the remote file exists
@ -138,19 +144,21 @@
- "test_script_removes_file_result.rc == 0"
- "not test_script_removes_file_result.stdout"
- "not test_script_removes_file_result.stderr"
- "not test_script_removes_file_result|failed"
- "test_script_removes_file_result|changed"
- "not test_script_removes_file_result | failed"
- "test_script_removes_file_result | changed"
- name: run test script that removes a file again
script: test_script_removes_file.ps1 "{{test_win_script_filename}}" removes="{{test_win_script_filename}}"
script: test_script_removes_file.ps1 {{ test_win_script_filename }}
args:
removes: "{{ test_win_script_filename }}"
register: test_script_removes_file_again_result
- name: check that the script did not run since the remote file does not exist
assert:
that:
- "not test_script_removes_file_again_result|failed"
- "not test_script_removes_file_again_result|changed"
- "test_script_removes_file_again_result|skipped"
- "not test_script_removes_file_again_result | failed"
- "not test_script_removes_file_again_result | changed"
- "test_script_removes_file_again_result | skipped"
# TODO: these tests fail on 2008 (not even R2) with no output. It's related to the default codepage being UTF8- if we force it back to 437, it works fine.
# Need to figure out a sane place to do that under the new exec wrapper.
@ -165,8 +173,8 @@
# - "test_batch_result.stdout"
# - "'batch' in test_batch_result.stdout"
# - "not test_batch_result.stderr"
# - "not test_batch_result|failed"
# - "test_batch_result|changed"
# - "not test_batch_result | failed"
# - "test_batch_result | changed"
#- name: run simple batch file with .cmd extension
@ -180,8 +188,8 @@
# - "test_cmd_result.stdout"
# - "'cmd extension' in test_cmd_result.stdout"
# - "not test_cmd_result.stderr"
# - "not test_cmd_result|failed"
# - "test_cmd_result|changed"
# - "not test_cmd_result | failed"
# - "test_cmd_result | changed"
- name: run test script that takes a boolean parameter
script: test_script_bool.ps1 $true
@ -205,4 +213,47 @@
# that:
# - test_script_env_result | succeeded
# - test_script_env_result.stdout_lines[0] == 'task'
#
#
# check mode
- name: Run test script that creates a file in check mode
script: test_script_creates_file.ps1 {{ test_win_script_filename }}
args:
creates: "{{ test_win_script_filename }}"
check_mode: yes
register: test_script_creates_file_check_mode
- name: Get state of file created by script
win_stat:
path: "{{ test_win_script_filename }}"
register: create_file_stat
- name: Assert that a change was reported but the script did not make changes
assert:
that:
- test_script_creates_file_check_mode | changed
- not create_file_stat.stat.exists
- name: Run test script that creates a file
script: test_script_creates_file.ps1 {{ test_win_script_filename }}
args:
creates: "{{ test_win_script_filename }}"
- name: Run test script that removes a file in check mode
script: test_script_removes_file.ps1 {{ test_win_script_filename }}
args:
removes: "{{ test_win_script_filename }}"
check_mode: yes
register: test_script_removes_file_check_mode
- name: Get state of file removed by script
win_stat:
path: "{{ test_win_script_filename }}"
register: remove_file_stat
- name: Assert that a change was reported but the script did not make changes
assert:
that:
- test_script_removes_file_check_mode | changed
- remove_file_stat.stat.exists