summaryrefslogtreecommitdiff
path: root/test/integration/targets/win_script/tasks/main.yml
blob: 32b637630afecae4e00927e9947623e774c1c08f (plain)
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
253
254
255
256
257
258
259
# test code for the script module when using winrm connection
# (c) 2014, Chris Church <chris@ninemoreminutes.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/>.

- name: get tempdir path
  raw: $env:TEMP
  register: tempdir

- name: set script path dynamically
  set_fact:
    test_win_script_filename: "{{ tempdir.stdout_lines[0] }}/testing_win_script.txt"

- name: run simple test script
  script: test_script.ps1
  register: test_script_result

- name: check that script ran
  assert:
    that:
      - "test_script_result.rc == 0"
      - "test_script_result.stdout"
      - "'Woohoo' in test_script_result.stdout"
      - "not test_script_result.stderr"
      - "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
  register: test_script_with_args_result

- name: check that script ran and received arguments and returned unicode
  assert:
    that:
      - "test_script_with_args_result.rc == 0"
      - "test_script_with_args_result.stdout"
      - "test_script_with_args_result.stdout_lines[0] == '/this'"
      - "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"

- 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'}
  register: test_script_with_splatting_result

- name: check that script ran and received parameters via splatting
  assert:
    that:
      - "test_script_with_splatting_result.rc == 0"
      - "test_script_with_splatting_result.stdout"
      - "test_script_with_splatting_result.stdout_lines[0] == 'this'"
      - "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"

- name: run test script that takes splatted parameters from a variable
  script: test_script_with_splatting.ps1 {{ test_win_script_splat }}
  register: test_script_with_splatting2_result

- name: check that script ran and received parameters via splatting from a variable
  assert:
    that:
      - "test_script_with_splatting2_result.rc == 0"
      - "test_script_with_splatting2_result.stdout"
      - "test_script_with_splatting2_result.stdout_lines[0] == 'THIS'"
      - "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"

- name: run test script that has errors
  script: test_script_with_errors.ps1
  register: test_script_with_errors_result
  ignore_errors: true

- name: check that script ran but failed with errors
  assert:
    that:
      - "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"

- name: cleanup test file if it exists
  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 }}
  args:
    creates: "{{ test_win_script_filename }}"
  register: test_script_creates_file_result

- name: check that script ran and indicated a change
  assert:
    that:
      - "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"

- name: run test script that creates a file again
  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"

- name: run test script that removes a file
  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
  assert:
    that:
      - "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"

- name: run test script that removes a file again
  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"

# 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.
#- name: run simple batch file
#  script: test_script.bat
#  register: test_batch_result
#
#- name: check that batch file ran
#  assert:
#    that:
#      - "test_batch_result.rc == 0"
#      - "test_batch_result.stdout"
#      - "'batch' in test_batch_result.stdout"
#      - "not test_batch_result.stderr"
#      - "not test_batch_result | failed"
#      - "test_batch_result | changed"


#- name: run simple batch file with .cmd extension
#  script: test_script.cmd
#  register: test_cmd_result
#
#- name: check that batch file with .cmd extension ran
#  assert:
#    that:
#      - "test_cmd_result.rc == 0"
#      - "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"

- name: run test script that takes a boolean parameter
  script: test_script_bool.ps1 $true
  register: test_script_bool_result

- name: check that the script ran and the parameter was treated as a boolean
  assert:
    that:
      - "test_script_bool_result.stdout_lines[0] == 'System.Boolean'"
      - "test_script_bool_result.stdout_lines[1] == 'True'"

# FIXME: re-enable this test once script can run under the wrapper with powershell
#- name: run test script that uses envvars
#  script: test_script_with_env.ps1
#  environment:
#    taskenv: task
#  register: test_script_env_result
#
#- name: ensure that script ran and that environment var was passed
#  assert:
#    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