|
| 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