1
1
# Copyright (c) Microsoft Corporation.
2
2
# Licensed under the MIT License.
3
3
4
- Describe ' WindowsPowerShell adapter resource tests - requires elevated permissions' {
4
+ BeforeDiscovery {
5
+ if ($IsWindows ) {
6
+ $identity = [System.Security.Principal.WindowsIdentity ]::GetCurrent()
7
+ $principal = [System.Security.Principal.WindowsPrincipal ]::new($identity )
8
+ $isElevated = $principal.IsInRole ([System.Security.Principal.WindowsBuiltInRole ]::Administrator)
9
+ }
10
+ }
11
+
12
+ Describe ' WindowsPowerShell adapter resource tests - requires elevated permissions' - Skip:(! $IsWindows -or ! $isElevated ) {
5
13
6
14
BeforeAll {
7
- if ($isWindows ) {
8
- winrm quickconfig - quiet - force
9
- $OldPSModulePath = $env: PSModulePath
10
- $env: PSModulePath += [System.IO.Path ]::PathSeparator + $PSScriptRoot
15
+ $null = winrm quickconfig - quiet - force 2>&1
16
+ $OldPSModulePath = $env: PSModulePath
17
+ $env: PSModulePath += [System.IO.Path ]::PathSeparator + $PSScriptRoot
11
18
12
- $winpsConfigPath = Join-path $PSScriptRoot " winps_resource.dsc.yaml"
13
- if ($isWindows ) {
14
- $cacheFilePath_v5 = Join-Path $env: LocalAppData " dsc" " WindowsPSAdapterCache.json"
15
- }
19
+ $winpsConfigPath = Join-path $PSScriptRoot " winps_resource.dsc.yaml"
20
+ if ($isWindows ) {
21
+ $cacheFilePath_v5 = Join-Path $env: LocalAppData " dsc" " WindowsPSAdapterCache.json"
16
22
}
17
23
}
18
24
AfterAll {
19
- if ($isWindows ) {
20
- $env: PSModulePath = $OldPSModulePath
25
+ $env: PSModulePath = $OldPSModulePath
21
26
22
- # Remove after all the tests are done
23
- Remove-Module $script :winPSModule - Force - ErrorAction Ignore
24
- }
27
+ # Remove after all the tests are done
28
+ Remove-Module $script :winPSModule - Force - ErrorAction Ignore
25
29
}
26
30
27
31
BeforeEach {
@@ -30,15 +34,15 @@ Describe 'WindowsPowerShell adapter resource tests - requires elevated permissio
30
34
}
31
35
}
32
36
33
- It ' Windows PowerShell adapter supports File resource' - Skip:( ! $IsWindows ) {
37
+ It ' Windows PowerShell adapter supports File resource' {
34
38
35
39
$r = dsc resource list -- adapter Microsoft.Windows/ WindowsPowerShell
36
40
$LASTEXITCODE | Should - Be 0
37
41
$resources = $r | ConvertFrom-Json
38
42
($resources | Where-Object { $_.Type -eq ' PSDesiredStateConfiguration/File' }).Count | Should - Be 1
39
43
}
40
44
41
- It ' Get works on Binary "File" resource' - Skip:( ! $IsWindows ) {
45
+ It ' Get works on Binary "File" resource' {
42
46
43
47
$testFile = " $testdrive \test.txt"
44
48
' test' | Set-Content - Path $testFile - Force
@@ -48,15 +52,15 @@ Describe 'WindowsPowerShell adapter resource tests - requires elevated permissio
48
52
$res.actualState.DestinationPath | Should - Be " $testFile "
49
53
}
50
54
51
- It ' Set works on Binary "File" resource' - Skip:( ! $IsWindows ) {
55
+ It ' Set works on Binary "File" resource' {
52
56
53
57
$testFile = " $testdrive \test.txt"
54
58
$null = ' {"DestinationPath":"' + $testFile.replace (' \' , ' \\' ) + ' ", type: File, contents: HelloWorld, Ensure: present}' | dsc resource set - r ' PSDesiredStateConfiguration/File' -f -
55
59
$LASTEXITCODE | Should - Be 0
56
60
Get-Content - Raw - Path $testFile | Should - Be " HelloWorld"
57
61
}
58
62
59
- It ' Get works on traditional "Script" resource' - Skip:( ! $IsWindows ) {
63
+ It ' Get works on traditional "Script" resource' {
60
64
61
65
$testFile = " $testdrive \test.txt"
62
66
' test' | Set-Content - Path $testFile - Force
@@ -66,7 +70,7 @@ Describe 'WindowsPowerShell adapter resource tests - requires elevated permissio
66
70
$res.actualState.result | Should - Be ' test'
67
71
}
68
72
69
- It ' Get works on config with File resource for WinPS' - Skip:( ! $IsWindows ) {
73
+ It ' Get works on config with File resource for WinPS' {
70
74
71
75
$testFile = " $testdrive \test.txt"
72
76
' test' | Set-Content - Path $testFile - Force
@@ -76,7 +80,7 @@ Describe 'WindowsPowerShell adapter resource tests - requires elevated permissio
76
80
$res.results [0 ].result.actualState.result[0 ].properties.DestinationPath | Should - Be " $testFile "
77
81
}
78
82
79
- It ' Verify that there are no cache rebuilds for several sequential executions' - Skip:( ! $IsWindows ) {
83
+ It ' Verify that there are no cache rebuilds for several sequential executions' {
80
84
# remove cache file
81
85
$cacheFilePath = Join-Path $env: LocalAppData " dsc\WindowsPSAdapterCache.json"
82
86
Remove-Item - Force - Path $cacheFilePath - ErrorAction Ignore
@@ -92,7 +96,7 @@ Describe 'WindowsPowerShell adapter resource tests - requires elevated permissio
92
96
}
93
97
}
94
98
95
- It ' Verify if assertion is used that no module is cleared in the cache' - Skip:( ! $IsWindows ) {
99
+ It ' Verify if assertion is used that no module is cleared in the cache' {
96
100
# create a test file in the test drive
97
101
$testFile = " $testdrive \test.txt"
98
102
New-Item - Path $testFile - ItemType File - Force | Out-Null
@@ -180,7 +184,7 @@ resources:
180
184
$cache.ResourceCache.Type | Should - Contain ' PSDesiredStateConfiguration/File'
181
185
}
182
186
183
- It ' _inDesiredState is returned correction: <Context>' - Skip:( ! $IsWindows ) - TestCases @ (
187
+ It ' _inDesiredState is returned correction: <Context>' - TestCases @ (
184
188
@ { Context = ' Both running' ; FirstState = ' Running' ; SecondState = ' Running' }
185
189
@ { Context = ' Both stopped' ; FirstState = ' Stopped' ; SecondState = ' Stopped' }
186
190
@ { Context = ' First Stopped' ; FirstState = ' Stopped' ; SecondState = ' Running' }
@@ -218,14 +222,12 @@ resources:
218
222
$out.results [0 ].result.inDesiredState | Should - Be $inDesiredState
219
223
}
220
224
221
- It ' Config works with credential object' - Skip:(! $IsWindows ) {
222
- BeforeDiscovery {
223
- $script :winPSModule = Resolve-Path - Path (Join-Path $PSScriptRoot ' ..' ' psDscAdapter' ' win_psDscAdapter.psm1' ) | Select-Object - ExpandProperty Path
224
- Import-Module $winPSModule - Force - ErrorAction Stop
225
+ It ' Config works with credential object' {
226
+ $script :winPSModule = Resolve-Path - Path (Join-Path $PSScriptRoot ' ..' ' psDscAdapter' ' win_psDscAdapter.psm1' ) | Select-Object - ExpandProperty Path
227
+ Import-Module $winPSModule - Force - ErrorAction Stop
225
228
226
- # Mock the command to work on GitHub runners because Microsoft.PowerShell.Security is not available
227
- Mock - CommandName ConvertTo-SecureString - MockWith { [System.Security.SecureString ]::new() }
228
- }
229
+ # Mock the command to work on GitHub runners because Microsoft.PowerShell.Security is not available
230
+ Mock - CommandName ConvertTo-SecureString - MockWith { [System.Security.SecureString ]::new() }
229
231
230
232
$jsonInput = @ {
231
233
resources = @ {
@@ -252,7 +254,7 @@ resources:
252
254
Should - Invoke - CommandName ConvertTo-SecureString - Exactly - Times 1 - Scope It
253
255
}
254
256
255
- It ' Config does not work when credential properties are missing required fields' - Skip:( ! $IsWindows ) {
257
+ It ' Config does not work when credential properties are missing required fields' {
256
258
$yaml = @"
257
259
`$ schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
258
260
resources:
@@ -271,7 +273,7 @@ resources:
271
273
$out | Should - BeLike " *ERROR*Credential object 'Credential' requires both 'username' and 'password' properties*"
272
274
}
273
275
274
- It ' List works with class-based PS DSC resources' - Skip:( ! $IsWindows ) {
276
+ It ' List works with class-based PS DSC resources' {
275
277
BeforeDiscovery {
276
278
$windowsPowerShellPath = Join-Path $testDrive ' WindowsPowerShell' ' Modules'
277
279
$env: PSModulePath += [System.IO.Path ]::PathSeparator + $windowsPowerShellPath
@@ -376,7 +378,7 @@ class PSClassResource {
376
378
($out | Where-Object - Property type -EQ ' PSClassResource/PSClassResource' ).capabilities | Should - BeIn @ (' get' , ' test' , ' set' , ' export' )
377
379
}
378
380
379
- It ' Get works with class-based PS DSC resources' - Skip:( ! $IsWindows ) {
381
+ It ' Get works with class-based PS DSC resources' {
380
382
381
383
$out = dsc resource get - r PSClassResource/ PSClassResource -- input (@ {Name = ' TestName' } | ConvertTo-Json ) | ConvertFrom-Json
382
384
$LASTEXITCODE | Should - Be 0
@@ -385,14 +387,14 @@ class PSClassResource {
385
387
$propCount.Count | Should - Be 1 # Only the DscProperty should be returned
386
388
}
387
389
388
- It ' Set works with class-based PS DSC resources' - Skip:( ! $IsWindows ) {
390
+ It ' Set works with class-based PS DSC resources' {
389
391
390
392
$out = dsc resource set - r PSClassResource/ PSClassResource -- input (@ {Name = ' TestName' } | ConvertTo-Json ) | ConvertFrom-Json
391
393
$LASTEXITCODE | Should - Be 0
392
394
$out.afterstate.InDesiredState | Should - Be $true
393
395
}
394
396
395
- It ' Export works with class-based PS DSC resources' - Skip:( ! $IsWindows ) {
397
+ It ' Export works with class-based PS DSC resources' {
396
398
397
399
$out = dsc resource export - r PSClassResource/ PSClassResource | ConvertFrom-Json
398
400
$LASTEXITCODE | Should - Be 0
0 commit comments