summaryrefslogtreecommitdiff
path: root/test/integration/targets/win_dsc/tasks/tests.yml
blob: b855e2fff2f2c9fa77ebc42ff7f3eaa1e115dee8 (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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# slightly non-destructive tests that will run if WMF 5.0 is installed
---
- name: start with feature absent
  win_feature:
    name: '{{test_win_dsc_feature_name}}'
    state: absent

- name: fail with incorrect DSC resource name
  win_dsc:
    resource_name: FakeResource
    Name: fake
  register: fail_invalid_resource
  failed_when: fail_invalid_resource.msg != "Resource FakeResource not found"

- name: fail by not setting mandatory option
  win_dsc:
    resource_name: WindowsFeature
    Ensure: Present
  register: fail_missing_mandatory
  failed_when: fail_missing_mandatory.msg != "Could not find mandatory property Name. Add this property and try again."

- name: fail to add a non-existant feature
  win_dsc:
    resource_name: WindowsFeature
    Name: InvalidFeature
    Ensure: Present
  register: fail_invalid_feature
  failed_when: '"The requested feature InvalidFeature is not found on the target machine" not in fail_invalid_feature.msg'

- name: add Windows feature (check mode)
  win_dsc:
    resource_name: WindowsFeature
    Name: '{{test_win_dsc_feature_name}}'
    Ensure: Present
  register: win_dsc_add_feature_check
  check_mode: yes

- name: get result of add Windows feature (check mode)
  win_shell: (Get-WindowsFeature -Name {{test_win_dsc_feature_name}}).Installed
  register: win_dsc_add_feature_result_check

- name: assert result of add Windows feature (check mode)
  assert:
    that:
    - win_dsc_add_feature_check is changed
    - win_dsc_add_feature_result_check.stdout == "False\r\n"

- name: add Windows feature
  win_dsc:
    resource_name: WindowsFeature
    Name: '{{test_win_dsc_feature_name}}'
    Ensure: Present
  register: win_dsc_add_feature

- name: get result of add Windows feature
  win_shell: (Get-WindowsFeature -Name {{test_win_dsc_feature_name}}).Installed
  register: win_dsc_add_feature_result

- name: assert result of add Windows feature
  assert:
    that:
    - win_dsc_add_feature is changed
    - win_dsc_add_feature_result.stdout == "True\r\n"

- name: add Windows feature (idempotent)
  win_dsc:
    resource_name: WindowsFeature
    Name: '{{test_win_dsc_feature_name}}'
    Ensure: Present
  register: win_dsc_add_feature_again

- name: assert result of add Windows feature (idempotent)
  assert:
    that:
    - win_dsc_add_feature_again is not changed

- name: remove Windows feature (check mode)
  win_dsc:
    resource_name: WindowsFeature
    Name: '{{test_win_dsc_feature_name}}'
    Ensure: Absent
  register: win_dsc_remove_feature_check
  check_mode: yes

- name: get result of remove Windows feature (check mode)
  win_shell: (Get-WindowsFeature -Name {{test_win_dsc_feature_name}}).Installed
  register: win_dsc_remove_feature_result_check

- name: assert result of remove Windows feature (check mode)
  assert:
    that:
    - win_dsc_remove_feature_check is changed
    - win_dsc_remove_feature_result_check.stdout == "True\r\n"

- name: remove Windows feature
  win_dsc:
    resource_name: WindowsFeature
    Name: '{{test_win_dsc_feature_name}}'
    Ensure: Absent
  register: win_dsc_remove_feature

- name: get result of remove Windows feature
  win_shell: (Get-WindowsFeature -Name {{test_win_dsc_feature_name}}).Installed
  register: win_dsc_remove_feature_result

- name: assert result of remove Windows feature
  assert:
    that:
    - win_dsc_remove_feature is changed
    - win_dsc_remove_feature_result.stdout == "False\r\n"

- name: remove Windows feature (idempotent)
  win_dsc:
    resource_name: WindowsFeature
    Name: '{{test_win_dsc_feature_name}}'
    Ensure: Absent
  register: win_dsc_remove_feature_again

- name: assert result of remove Windows feature (idempotent)
  assert:
    that:
    - win_dsc_remove_feature_again is not changed

- name: ensure test folder doesn't exist before test
  win_file:
    path: '{{test_win_dsc_folder}}'
    state: absent

- name: create test file (check mode)
  win_dsc:
    resource_name: File
    DestinationPath: '{{test_win_dsc_folder}}\dsc-file'
    Contents: file contents
    Attributes:
    - Hidden
    - ReadOnly
    Ensure: Present
    Type: File
  register: win_dsc_create_file_check
  check_mode: yes

- name: get result of create test file (check mode)
  win_stat:
    path: '{{test_win_dsc_folder}}\dsc-file'
  register: win_dsc_create_file_result_check

- name: assert results of create test file (check mode)
  assert:
    that:
    - win_dsc_create_file_check is changed
    - win_dsc_create_file_result_check.stat.exists == False

- name: create test file
  win_dsc:
    resource_name: File
    DestinationPath: '{{test_win_dsc_folder}}\dsc-file'
    Contents: file contents
    Attributes:
    - Hidden
    - ReadOnly
    Ensure: Present
    Type: File
  register: win_dsc_create_file

- name: get result of create test file
  win_stat:
    path: '{{test_win_dsc_folder}}\dsc-file'
  register: win_dsc_create_file_result

- name: assert results of create test file
  assert:
    that:
    - win_dsc_create_file is changed
    - win_dsc_create_file_result.stat.exists == True
    - win_dsc_create_file_result.stat.isdir == False
    - win_dsc_create_file_result.stat.ishidden == True
    - win_dsc_create_file_result.stat.isreadonly == True

- name: create test file (idempotent)
  win_dsc:
    resource_name: File
    DestinationPath: '{{test_win_dsc_folder}}\dsc-file'
    Contents: file contents
    Attributes: Hidden, ReadOnly
    Ensure: Present
    Type: File
  register: win_dsc_create_file_again

- name: assert results of create test file (idempotent)
  assert:
    that:
    - win_dsc_create_file_again is not changed

- name: get SID of current ansible user
  win_shell: (New-Object System.Security.Principal.NTAccount("{{ansible_user}}")).Translate([System.Security.Principal.SecurityIdentifier]).ToString()
  register: win_dsc_actual_sid

- name: run DSC process as another user
  win_dsc:
    resource_name: Script
    GetScript: '@{ Result = "" }'
    SetScript: |
      $output = &whoami.exe
      $sid = (New-Object System.Security.Principal.NTAccount($output)).Translate([System.Security.Principal.SecurityIdentifier]).ToString()
      Set-Content -Path "{{test_win_dsc_folder}}\file" -Value $sid
    TestScript: Test-Path -Path "{{test_win_dsc_folder}}\file"
    PsDscRunAsCredential_username: '{{ansible_user}}'
    PsDscRunAsCredential_password: '{{ansible_password}}'
  register: win_dsc_runas

- name: get content of script output file
  slurp:
    path: '{{test_win_dsc_folder}}\file'
  register: win_dsc_runas_result

- name: assert results of run DSC process as another user
  assert:
    that:
    - win_dsc_runas is changed
    - win_dsc_runas_result.content|b64decode == win_dsc_actual_sid.stdout

- name: create custom DSC resource folder
  win_file:
    path: C:\Program Files\WindowsPowerShell\Modules\xTestDsc\{{item}}\DSCResources\ANSIBLE_xTestResource
    state: directory
  with_items:
  - "1.0.0"
  - "2.0.0"

- name: template custom DSC resource files
  win_template:
    src: '{{item.src}}'
    dest: C:\Program Files\WindowsPowerShell\Modules\xTestDsc\{{item.version}}\{{item.dest}}
  with_items:
  - src: ANSIBLE_xTestResource.schema.mof
    dest: DSCResources\ANSIBLE_xTestResource\ANSIBLE_xTestResource.schema.mof
    version: "1.0.0"
  - src: ANSIBLE_xTestResource.psm1
    dest: DSCResources\ANSIBLE_xTestResource\ANSIBLE_xTestResource.psm1
    version: "1.0.0"
  - src: xTestDsc.psd1
    dest: xTestDsc.psd1
    version: "1.0.0"
  - src: ANSIBLE_xTestResource.schema.mof
    dest: DSCResources\ANSIBLE_xTestResource\ANSIBLE_xTestResource.schema.mof
    version: "2.0.0"
  - src: ANSIBLE_xTestResource.psm1
    dest: DSCResources\ANSIBLE_xTestResource\ANSIBLE_xTestResource.psm1
    version: "2.0.0"
  - src: xTestDsc.psd1
    dest: xTestDsc.psd1
    version: "2.0.0"

- name: run custom DSC resource
  win_dsc: &dsc_params
    resource_name: xTestResource
    Path: '{{test_win_dsc_folder}}\custom-output.txt'
    Ensure: Present
    StringParam: string param
    UInt32Param: 1000
    UInt64Param: 1000000
    StringArrayParam:
    - string 1
    - string 2
    UInt32ArrayParam:
    - 1000
    - 2000
    UInt64ArrayParam:
    - 1000000
    - 2000000
    BooleanParam: yes
    PSCredentialParam_username: username
    PSCredentialParam_password: password
    CimInstanceParam:
      StringKey: a
      BooleanKey: yes
      UInt32Key: 1
      StringArrayKey:
      - string 1
      - string 2
    CimInstanceArrayParam:
    - StringKey: b
      BooleanKey: no
      UInt32Key: 2
      StringArrayKey:
      - string 3
      - string 4
    - StringKey: c
      BooleanKey: no
      UInt32Key: 3
      StringArrayKey:
      - string 5
      - string 6
  register: test_dsc_custom

- name: get output of custom DSC resource
  slurp:
    path: '{{test_win_dsc_folder}}\custom-output.txt'
  register: test_dsc_custom_output

- name: get expected output of custom DSC resource
  set_fact:
    test_dsc_custom_expected: '{{lookup("file", "custom-result-normal.txt")}}'

- name: assert result of custom DSC resource
  assert:
    that:
    - test_dsc_custom is changed
    - test_dsc_custom_output.content|b64decode|strip_newline == test_dsc_custom_expected|strip_newline
    - test_dsc_custom.warnings | length == 2
    - "'[[xTestResource]DirectResourceAccess] test warning' in test_dsc_custom.warnings[0]"
    - "'[[xTestResource]DirectResourceAccess] set warning' in test_dsc_custom.warnings[1]"

- name: run custom DSC resource with version
  win_dsc:
    <<: *dsc_params
    module_version: '1.0.0'
  register: test_dsc_custom_version

- name: get output of custom DSC resource with version
  slurp:
    path: '{{test_win_dsc_folder}}\custom-output.txt'
  register: test_dsc_custom_output_version

- name: get expected output of custom DSC resource with version
  set_fact:
    test_dsc_custom_expected_version: '{{lookup("file", "custom-result-versioned.txt")}}'

- name: assert result of custom DSC resource with version
  assert:
    that:
    - test_dsc_custom is changed
    - test_dsc_custom_output_version.content|b64decode|strip_newline == test_dsc_custom_expected_version|strip_newline