Skip to content

Commit df98f0d

Browse files
committed
Update test files
1 parent a8e60d9 commit df98f0d

2 files changed

Lines changed: 116 additions & 61 deletions

File tree

resources/windows_service/tests/windows_service_set.tests.ps1

Lines changed: 0 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -268,65 +268,4 @@ Describe 'Windows Service set tests' -Skip:(!$IsWindows) {
268268
$result._exist | Should -BeTrue
269269
}
270270
}
271-
272-
Context 'What-if set' -Skip:(!$isAdmin) {
273-
It 'Projects a startType change without modifying the service' {
274-
$before = Get-ServiceState -Name $testServiceName
275-
$desiredStartType = if ($before.startType -eq 'Disabled') { 'Manual' } else { 'Disabled' }
276-
$json = @{ name = $testServiceName; startType = $desiredStartType } | ConvertTo-Json -Compress
277-
278-
$out = $json | dsc resource set --what-if -r $resourceType -f - 2>$testdrive/error.log
279-
$LASTEXITCODE | Should -Be 0 -Because (Get-Content -Raw $testdrive/error.log)
280-
$result = $out | ConvertFrom-Json
281-
$after = $result.afterState
282-
283-
$after.name | Should -BeExactly $testServiceName
284-
$after.startType | Should -BeExactly $desiredStartType
285-
$after._exist | Should -BeTrue
286-
$after._metadata.whatIf | Should -Not -BeNullOrEmpty
287-
$after._metadata.whatIf | Should -Contain "Would change startType from '$($before.startType)' to '$desiredStartType'"
288-
289-
$current = Get-ServiceState -Name $testServiceName
290-
$current.startType | Should -BeExactly $before.startType
291-
}
292-
293-
It 'Projects a displayName change without modifying the service' {
294-
$before = Get-ServiceState -Name $testServiceName
295-
$desiredDisplayName = "$($before.displayName) (whatif)"
296-
$json = @{ name = $testServiceName; displayName = $desiredDisplayName } | ConvertTo-Json -Compress
297-
298-
$out = $json | dsc resource set --what-if -r $resourceType -f - 2>$testdrive/error.log
299-
$LASTEXITCODE | Should -Be 0 -Because (Get-Content -Raw $testdrive/error.log)
300-
$result = $out | ConvertFrom-Json
301-
$after = $result.afterState
302-
303-
$after.name | Should -BeExactly $testServiceName
304-
$after.displayName | Should -BeExactly $desiredDisplayName
305-
$after._exist | Should -BeTrue
306-
$after._metadata.whatIf | Should -Not -BeNullOrEmpty
307-
$after._metadata.whatIf | Should -Contain "Would change displayName from '$($before.displayName)' to '$desiredDisplayName'"
308-
309-
$current = Get-ServiceState -Name $testServiceName
310-
$current.displayName | Should -BeExactly $before.displayName
311-
}
312-
313-
It 'Projects deletion when _exist is false without modifying the service' {
314-
$before = Get-ServiceState -Name $testServiceName
315-
$json = @{ name = $testServiceName; _exist = $false } | ConvertTo-Json -Compress
316-
317-
$out = $json | dsc resource set --what-if -r $resourceType -f - 2>$testdrive/error.log
318-
$LASTEXITCODE | Should -Be 0 -Because (Get-Content -Raw $testdrive/error.log)
319-
$result = $out | ConvertFrom-Json
320-
$after = $result.afterState
321-
322-
$after.name | Should -BeExactly $testServiceName
323-
$after._exist | Should -BeFalse
324-
$after._metadata.whatIf | Should -Not -BeNullOrEmpty
325-
$after._metadata.whatIf | Should -Contain "Would delete service '$testServiceName'"
326-
327-
$current = Get-ServiceState -Name $testServiceName
328-
$current._exist | Should -BeTrue
329-
$current.startType | Should -BeExactly $before.startType
330-
}
331-
}
332271
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# Copyright (c) Microsoft Corporation.
2+
# Licensed under the MIT License.
3+
4+
Describe 'windows service whatif tests' -Skip:(!$IsWindows) {
5+
BeforeAll {
6+
$testServiceName = 'Spooler'
7+
8+
function Get-ServiceState {
9+
param([string]$Name = $testServiceName)
10+
$json = @{ name = $Name } | ConvertTo-Json -Compress
11+
$out = $json | windows_service get --input $json 2>$null | ConvertFrom-Json
12+
return $out
13+
}
14+
}
15+
16+
It 'Can whatif a startType change' {
17+
$before = Get-ServiceState
18+
$desiredStartType = if ($before.startType -eq 'Disabled') { 'Manual' } else { 'Disabled' }
19+
$json = @{ name = $testServiceName; startType = $desiredStartType } | ConvertTo-Json -Compress
20+
21+
$result = windows_service set -w --input $json 2>$null | ConvertFrom-Json
22+
$LASTEXITCODE | Should -Be 0
23+
24+
$result.name | Should -BeExactly $testServiceName
25+
$result.startType | Should -BeExactly $desiredStartType
26+
$result._exist | Should -BeTrue
27+
$result._metadata.whatIf | Should -Not -BeNullOrEmpty
28+
$result._metadata.whatIf | Should -Contain "Would change startType from '$($before.startType)' to '$desiredStartType'"
29+
30+
# Assert no mutation happened
31+
$after = Get-ServiceState
32+
$after.startType | Should -BeExactly $before.startType
33+
}
34+
35+
It 'Can whatif a displayName change' {
36+
$before = Get-ServiceState
37+
$desiredDisplayName = "$($before.displayName) (whatif)"
38+
$json = @{ name = $testServiceName; displayName = $desiredDisplayName } | ConvertTo-Json -Compress
39+
40+
$result = windows_service set -w --input $json 2>$null | ConvertFrom-Json
41+
$LASTEXITCODE | Should -Be 0
42+
43+
$result.name | Should -BeExactly $testServiceName
44+
$result.displayName | Should -BeExactly $desiredDisplayName
45+
$result._exist | Should -BeTrue
46+
$result._metadata.whatIf | Should -Not -BeNullOrEmpty
47+
$result._metadata.whatIf | Should -Contain "Would change displayName from '$($before.displayName)' to '$desiredDisplayName'"
48+
49+
# Assert no mutation happened
50+
$after = Get-ServiceState
51+
$after.displayName | Should -BeExactly $before.displayName
52+
}
53+
54+
It 'Can whatif a description change' {
55+
$before = Get-ServiceState
56+
$desiredDescription = 'DSC whatif test description'
57+
$json = @{ name = $testServiceName; description = $desiredDescription } | ConvertTo-Json -Compress
58+
59+
$result = windows_service set -w --input $json 2>$null | ConvertFrom-Json
60+
$LASTEXITCODE | Should -Be 0
61+
62+
$result.name | Should -BeExactly $testServiceName
63+
$result.description | Should -BeExactly $desiredDescription
64+
$result._exist | Should -BeTrue
65+
$result._metadata.whatIf | Should -Not -BeNullOrEmpty
66+
$result._metadata.whatIf | Should -Match "Would change description"
67+
68+
# Assert no mutation happened
69+
$after = Get-ServiceState
70+
$after.description | Should -BeExactly $before.description
71+
}
72+
73+
It 'Returns no whatIf messages when no properties would change' {
74+
$before = Get-ServiceState
75+
# Pass back the exact current values — nothing should change
76+
$json = @{
77+
name = $testServiceName
78+
startType = $before.startType
79+
} | ConvertTo-Json -Compress
80+
81+
$result = windows_service set -w --input $json 2>$null | ConvertFrom-Json
82+
$LASTEXITCODE | Should -Be 0
83+
84+
$result.name | Should -BeExactly $testServiceName
85+
$result._exist | Should -BeTrue
86+
$result._metadata | Should -BeNullOrEmpty
87+
}
88+
89+
It 'Can whatif delete an existing service using _exist is false' {
90+
$before = Get-ServiceState
91+
$json = @{ name = $testServiceName; '_exist' = $false } | ConvertTo-Json -Compress
92+
93+
$result = windows_service set -w --input $json 2>$null | ConvertFrom-Json
94+
$LASTEXITCODE | Should -Be 0
95+
96+
$result.name | Should -BeExactly $testServiceName
97+
$result._exist | Should -BeFalse
98+
$result._metadata.whatIf | Should -Not -BeNullOrEmpty
99+
$result._metadata.whatIf | Should -Contain "Would delete service '$testServiceName'"
100+
101+
# Assert no mutation happened — service still exists
102+
$after = Get-ServiceState
103+
$after._exist | Should -BeTrue
104+
$after.startType | Should -BeExactly $before.startType
105+
}
106+
107+
It 'Reports service not found when service does not exist' {
108+
$json = @{ name = 'DSC-NonExistent-Service-12345' } | ConvertTo-Json -Compress
109+
110+
$result = windows_service set -w --input $json 2>$null | ConvertFrom-Json
111+
$LASTEXITCODE | Should -Be 0
112+
113+
$result._exist | Should -BeFalse
114+
$result._metadata.whatIf | Should -Not -BeNullOrEmpty
115+
}
116+
}

0 commit comments

Comments
 (0)