Skip to content

Commit 5cb3f0f

Browse files
author
Steve Lee (POWERSHELL HE/HIM) (from Dev Box)
committed
Skip winps tests if not Windows or not elevated, redirect native output to $null
1 parent b64465a commit 5cb3f0f

File tree

1 file changed

+36
-34
lines changed

1 file changed

+36
-34
lines changed

powershell-adapter/Tests/win_powershellgroup.tests.ps1

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,31 @@
11
# Copyright (c) Microsoft Corporation.
22
# Licensed under the MIT License.
33

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) {
513

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

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"
1622
}
1723
}
1824
AfterAll {
19-
if ($isWindows) {
20-
$env:PSModulePath = $OldPSModulePath
25+
$env:PSModulePath = $OldPSModulePath
2126

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
2529
}
2630

2731
BeforeEach {
@@ -30,15 +34,15 @@ Describe 'WindowsPowerShell adapter resource tests - requires elevated permissio
3034
}
3135
}
3236

33-
It 'Windows PowerShell adapter supports File resource' -Skip:(!$IsWindows) {
37+
It 'Windows PowerShell adapter supports File resource' {
3438

3539
$r = dsc resource list --adapter Microsoft.Windows/WindowsPowerShell
3640
$LASTEXITCODE | Should -Be 0
3741
$resources = $r | ConvertFrom-Json
3842
($resources | Where-Object { $_.Type -eq 'PSDesiredStateConfiguration/File' }).Count | Should -Be 1
3943
}
4044

41-
It 'Get works on Binary "File" resource' -Skip:(!$IsWindows) {
45+
It 'Get works on Binary "File" resource' {
4246

4347
$testFile = "$testdrive\test.txt"
4448
'test' | Set-Content -Path $testFile -Force
@@ -48,15 +52,15 @@ Describe 'WindowsPowerShell adapter resource tests - requires elevated permissio
4852
$res.actualState.DestinationPath | Should -Be "$testFile"
4953
}
5054

51-
It 'Set works on Binary "File" resource' -Skip:(!$IsWindows) {
55+
It 'Set works on Binary "File" resource' {
5256

5357
$testFile = "$testdrive\test.txt"
5458
$null = '{"DestinationPath":"' + $testFile.replace('\', '\\') + '", type: File, contents: HelloWorld, Ensure: present}' | dsc resource set -r 'PSDesiredStateConfiguration/File' -f -
5559
$LASTEXITCODE | Should -Be 0
5660
Get-Content -Raw -Path $testFile | Should -Be "HelloWorld"
5761
}
5862

59-
It 'Get works on traditional "Script" resource' -Skip:(!$IsWindows) {
63+
It 'Get works on traditional "Script" resource' {
6064

6165
$testFile = "$testdrive\test.txt"
6266
'test' | Set-Content -Path $testFile -Force
@@ -66,7 +70,7 @@ Describe 'WindowsPowerShell adapter resource tests - requires elevated permissio
6670
$res.actualState.result | Should -Be 'test'
6771
}
6872

69-
It 'Get works on config with File resource for WinPS' -Skip:(!$IsWindows) {
73+
It 'Get works on config with File resource for WinPS' {
7074

7175
$testFile = "$testdrive\test.txt"
7276
'test' | Set-Content -Path $testFile -Force
@@ -76,7 +80,7 @@ Describe 'WindowsPowerShell adapter resource tests - requires elevated permissio
7680
$res.results[0].result.actualState.result[0].properties.DestinationPath | Should -Be "$testFile"
7781
}
7882

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' {
8084
# remove cache file
8185
$cacheFilePath = Join-Path $env:LocalAppData "dsc\WindowsPSAdapterCache.json"
8286
Remove-Item -Force -Path $cacheFilePath -ErrorAction Ignore
@@ -92,7 +96,7 @@ Describe 'WindowsPowerShell adapter resource tests - requires elevated permissio
9296
}
9397
}
9498

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' {
96100
# create a test file in the test drive
97101
$testFile = "$testdrive\test.txt"
98102
New-Item -Path $testFile -ItemType File -Force | Out-Null
@@ -180,7 +184,7 @@ resources:
180184
$cache.ResourceCache.Type | Should -Contain 'PSDesiredStateConfiguration/File'
181185
}
182186

183-
It '_inDesiredState is returned correction: <Context>' -Skip:(!$IsWindows) -TestCases @(
187+
It '_inDesiredState is returned correction: <Context>' -TestCases @(
184188
@{ Context = 'Both running'; FirstState = 'Running'; SecondState = 'Running' }
185189
@{ Context = 'Both stopped'; FirstState = 'Stopped'; SecondState = 'Stopped' }
186190
@{ Context = 'First Stopped'; FirstState = 'Stopped'; SecondState = 'Running' }
@@ -218,14 +222,12 @@ resources:
218222
$out.results[0].result.inDesiredState | Should -Be $inDesiredState
219223
}
220224

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
225228

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() }
229231

230232
$jsonInput = @{
231233
resources = @{
@@ -252,7 +254,7 @@ resources:
252254
Should -Invoke -CommandName ConvertTo-SecureString -Exactly -Times 1 -Scope It
253255
}
254256

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' {
256258
$yaml = @"
257259
`$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
258260
resources:
@@ -271,7 +273,7 @@ resources:
271273
$out | Should -BeLike "*ERROR*Credential object 'Credential' requires both 'username' and 'password' properties*"
272274
}
273275

274-
It 'List works with class-based PS DSC resources' -Skip:(!$IsWindows) {
276+
It 'List works with class-based PS DSC resources' {
275277
BeforeDiscovery {
276278
$windowsPowerShellPath = Join-Path $testDrive 'WindowsPowerShell' 'Modules'
277279
$env:PSModulePath += [System.IO.Path]::PathSeparator + $windowsPowerShellPath
@@ -376,7 +378,7 @@ class PSClassResource {
376378
($out | Where-Object -Property type -EQ 'PSClassResource/PSClassResource').capabilities | Should -BeIn @('get', 'test', 'set', 'export')
377379
}
378380

379-
It 'Get works with class-based PS DSC resources' -Skip:(!$IsWindows) {
381+
It 'Get works with class-based PS DSC resources' {
380382

381383
$out = dsc resource get -r PSClassResource/PSClassResource --input (@{Name = 'TestName' } | ConvertTo-Json) | ConvertFrom-Json
382384
$LASTEXITCODE | Should -Be 0
@@ -385,14 +387,14 @@ class PSClassResource {
385387
$propCount.Count | Should -Be 1 # Only the DscProperty should be returned
386388
}
387389

388-
It 'Set works with class-based PS DSC resources' -Skip:(!$IsWindows) {
390+
It 'Set works with class-based PS DSC resources' {
389391

390392
$out = dsc resource set -r PSClassResource/PSClassResource --input (@{Name = 'TestName' } | ConvertTo-Json) | ConvertFrom-Json
391393
$LASTEXITCODE | Should -Be 0
392394
$out.afterstate.InDesiredState | Should -Be $true
393395
}
394396

395-
It 'Export works with class-based PS DSC resources' -Skip:(!$IsWindows) {
397+
It 'Export works with class-based PS DSC resources' {
396398

397399
$out = dsc resource export -r PSClassResource/PSClassResource | ConvertFrom-Json
398400
$LASTEXITCODE | Should -Be 0

0 commit comments

Comments
 (0)