Skip to content

Commit f7ef3cb

Browse files
authored
Merge pull request #435 from anmenaga/winps
Separated WinPS adapter and tests
2 parents 06e79cd + f8f4fda commit f7ef3cb

9 files changed

+645
-254
lines changed

powershell-adapter/Tests/class_ps_resources.dsc.yaml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@ resources:
77
type: Microsoft.DSC/PowerShell
88
properties:
99
resources:
10-
- name: Repository Info
11-
type: PSTestModule/TestPSRepository
12-
properties:
13-
Name: TestPSRepository1
1410
- name: Class-resource Info
1511
type: TestClassResource/TestClassResource
1612
properties:

powershell-adapter/Tests/powershellgroup.config.tests.ps1

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,16 @@
44
Describe 'PowerShell adapter resource tests' {
55

66
BeforeAll {
7-
if ($isWindows) {
8-
winrm quickconfig -quiet -force
9-
}
107
$OldPSModulePath = $env:PSModulePath
118
$env:PSModulePath += [System.IO.Path]::PathSeparator + $PSScriptRoot
129
$pwshConfigPath = Join-path $PSScriptRoot "class_ps_resources.dsc.yaml"
13-
$winpsConfigPath = Join-path $PSScriptRoot "winps_resource.dsc.yaml"
1410

1511
if ($IsLinux -or $IsMacOS) {
1612
$cacheFilePath = Join-Path $env:HOME ".dsc" "PSAdapterCache.json"
1713
}
1814
else
1915
{
2016
$cacheFilePath = Join-Path $env:LocalAppData "dsc" "PSAdapterCache.json"
21-
$cacheFilePath_v5 = Join-Path $env:LocalAppData "dsc" "WindowsPSAdapterCache.json"
2217
}
2318
}
2419
AfterAll {
@@ -27,47 +22,32 @@ Describe 'PowerShell adapter resource tests' {
2722

2823
BeforeEach {
2924
Remove-Item -Force -ea SilentlyContinue -Path $cacheFilePath
30-
Remove-Item -Force -ea SilentlyContinue -Path $cacheFilePath_v5
3125
}
3226

33-
It 'Get works on config with class-based and script-based resources' -Skip:(!$IsWindows){
27+
It 'Get works on config with class-based resources' -Skip:(!$IsWindows){
3428

3529
$r = Get-Content -Raw $pwshConfigPath | dsc config get
3630
$LASTEXITCODE | Should -Be 0
3731
$res = $r | ConvertFrom-Json
38-
$res.results[0].result.actualState.result[0].properties.PublishLocation | Should -BeExactly 'https://www.powershellgallery.com/api/v2/package/'
39-
$res.results[0].result.actualState.result[1].properties.Prop1 | Should -BeExactly 'ValueForProp1'
40-
$res.results[0].result.actualState.result[1].properties.EnumProp | Should -BeExactly 'Expected'
41-
}
42-
43-
It 'Get works on config with File resource for WinPS' -Skip:(!$IsWindows){
44-
45-
$testFile = "$testdrive\test.txt"
46-
'test' | Set-Content -Path $testFile -Force
47-
$r = (Get-Content -Raw $winpsConfigPath).Replace('c:\test.txt',"$testFile") | dsc config get
48-
$LASTEXITCODE | Should -Be 0
49-
$res = $r | ConvertFrom-Json
50-
$res.results[0].result.actualState.result[0].properties.DestinationPath | Should -Be "$testFile"
32+
$res.results[0].result.actualState.result[0].properties.Prop1 | Should -BeExactly 'ValueForProp1'
33+
$res.results[0].result.actualState.result[0].properties.EnumProp | Should -BeExactly 'Expected'
5134
}
5235

53-
It 'Test works on config with class-based and script-based resources' -Skip:(!$IsWindows){
36+
It 'Test works on config with class-based resources' -Skip:(!$IsWindows){
5437

5538
$r = Get-Content -Raw $pwshConfigPath | dsc config test
5639
$LASTEXITCODE | Should -Be 0
5740
$res = $r | ConvertFrom-Json
5841
$res.results[0].result.actualState.result[0] | Should -Not -BeNull
59-
$res.results[0].result.actualState.result[1] | Should -Not -BeNull
6042
}
6143

62-
It 'Set works on config with class-based and script-based resources' -Skip:(!$IsWindows){
44+
It 'Set works on config with class-based resources' -Skip:(!$IsWindows){
6345

6446
$r = Get-Content -Raw $pwshConfigPath | dsc config set
6547
$LASTEXITCODE | Should -Be 0
6648
$res = $r | ConvertFrom-Json
67-
$res.results.result.afterState.result[0].type | Should -Be "PSTestModule/TestPSRepository"
68-
$res.results.result.afterState.result[1].type | Should -Be "TestClassResource/TestClassResource"
49+
$res.results.result.afterState.result[0].type | Should -Be "TestClassResource/TestClassResource"
6950
}
70-
7151

7252
It 'Export works on config with class-based resources' -Skip:(!$IsWindows){
7353

powershell-adapter/Tests/powershellgroup.resource.tests.ps1

Lines changed: 0 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44
Describe 'PowerShell adapter resource tests' {
55

66
BeforeAll {
7-
if ($isWindows) {
8-
winrm quickconfig -quiet -force
9-
}
107
$OldPSModulePath = $env:PSModulePath
118
$env:PSModulePath += [System.IO.Path]::PathSeparator + $PSScriptRoot
129

@@ -16,7 +13,6 @@ Describe 'PowerShell adapter resource tests' {
1613
else
1714
{
1815
$cacheFilePath = Join-Path $env:LocalAppData "dsc" "PSAdapterCache.json"
19-
$cacheFilePath_v5 = Join-Path $env:LocalAppData "dsc" "WindowsPSAdapterCache.json"
2016
}
2117
}
2218
AfterAll {
@@ -25,7 +21,6 @@ Describe 'PowerShell adapter resource tests' {
2521

2622
BeforeEach {
2723
Remove-Item -Force -ea SilentlyContinue -Path $cacheFilePath
28-
Remove-Item -Force -ea SilentlyContinue -Path $cacheFilePath_v5
2924
}
3025

3126
It 'Discovery includes class-based and script-based resources ' -Skip:(!$IsWindows){
@@ -37,34 +32,6 @@ Describe 'PowerShell adapter resource tests' {
3732
($resources | ? {$_.Type -eq 'PSTestModule/TestPSRepository'}).Count | Should -Be 1
3833
}
3934

40-
It 'Windows PowerShell adapter supports File resource' -Skip:(!$IsWindows){
41-
42-
$r = dsc resource list --adapter Microsoft.Windows/WindowsPowerShell
43-
$LASTEXITCODE | Should -Be 0
44-
$resources = $r | ConvertFrom-Json
45-
($resources | ? {$_.Type -eq 'PSDesiredStateConfiguration/File'}).Count | Should -Be 1
46-
}
47-
48-
It 'Get works on Binary "File" resource' -Skip:(!$IsWindows){
49-
50-
$testFile = "$testdrive\test.txt"
51-
'test' | Set-Content -Path $testFile -Force
52-
$r = '{"DestinationPath":"' + $testFile.replace('\','\\') + '"}' | dsc resource get -r 'PSDesiredStateConfiguration/File'
53-
$LASTEXITCODE | Should -Be 0
54-
$res = $r | ConvertFrom-Json
55-
$res.actualState.result.properties.DestinationPath | Should -Be "$testFile"
56-
}
57-
58-
It 'Get works on traditional "Script" resource' -Skip:(!$IsWindows){
59-
60-
$testFile = "$testdrive\test.txt"
61-
'test' | Set-Content -Path $testFile -Force
62-
$r = '{"GetScript": "@{result = $(Get-Content ' + $testFile.replace('\','\\') + ')}", "SetScript": "throw", "TestScript": "throw"}' | dsc resource get -r 'PSDesiredStateConfiguration/Script'
63-
$LASTEXITCODE | Should -Be 0
64-
$res = $r | ConvertFrom-Json
65-
$res.actualState.result.properties.result | Should -Be 'test'
66-
}
67-
6835
It 'Get works on class-based resource' -Skip:(!$IsWindows){
6936

7037
$r = "{'Name':'TestClassResource1'}" | dsc resource get -r 'TestClassResource/TestClassResource'
@@ -73,14 +40,6 @@ Describe 'PowerShell adapter resource tests' {
7340
$res.actualState.result.properties.Prop1 | Should -BeExactly 'ValueForProp1'
7441
}
7542

76-
It 'Get works on script-based resource' -Skip:(!$IsWindows){
77-
78-
$r = "{'Name':'TestPSRepository1'}" | dsc resource get -r 'PSTestModule/TestPSRepository'
79-
$LASTEXITCODE | Should -Be 0
80-
$res = $r | ConvertFrom-Json
81-
$res.actualState.result.properties.PublishLocation | Should -BeExactly 'https://www.powershellgallery.com/api/v2/package/'
82-
}
83-
8443
It 'Get uses enum names on class-based resource' -Skip:(!$IsWindows){
8544

8645
$r = "{'Name':'TestClassResource1'}" | dsc resource get -r 'TestClassResource/TestClassResource'
@@ -89,14 +48,6 @@ Describe 'PowerShell adapter resource tests' {
8948
$res.actualState.result.properties.EnumProp | Should -BeExactly 'Expected'
9049
}
9150

92-
It 'Get uses enum names on script-based resource' -Skip:(!$IsWindows){
93-
94-
$r = "{'Name':'TestPSRepository1'}" | dsc resource get -r 'PSTestModule/TestPSRepository'
95-
$LASTEXITCODE | Should -Be 0
96-
$res = $r | ConvertFrom-Json
97-
$res.actualState.result.properties.Ensure | Should -BeExactly 'Present'
98-
}
99-
10051
It 'Test works on class-based resource' -Skip:(!$IsWindows){
10152

10253
$r = "{'Name':'TestClassResource1','Prop1':'ValueForProp1'}" | dsc resource test -r 'TestClassResource/TestClassResource'
@@ -105,14 +56,6 @@ Describe 'PowerShell adapter resource tests' {
10556
$res.actualState.result.properties.InDesiredState | Should -Be $True
10657
}
10758

108-
It 'Test works on script-based resource' -Skip:(!$IsWindows){
109-
110-
$r = "{'Name':'TestPSRepository1','PackageManagementProvider':'NuGet'}" | dsc resource test -r 'PSTestModule/TestPSRepository'
111-
$LASTEXITCODE | Should -Be 0
112-
$res = $r | ConvertFrom-Json
113-
$res.actualState.result.properties.InDesiredState | Should -Be $True
114-
}
115-
11659
It 'Set works on class-based resource' -Skip:(!$IsWindows){
11760

11861
$r = "{'Name':'TestClassResource1','Prop1':'ValueForProp1'}" | dsc resource set -r 'TestClassResource/TestClassResource'
@@ -121,14 +64,6 @@ Describe 'PowerShell adapter resource tests' {
12164
$res.afterState.result | Should -Not -BeNull
12265
}
12366

124-
It 'Set works on script-based resource' -Skip:(!$IsWindows){
125-
126-
$r = "{'Name':'TestPSRepository1'}" | dsc resource set -r 'PSTestModule/TestPSRepository'
127-
$LASTEXITCODE | Should -Be 0
128-
$res = $r | ConvertFrom-Json
129-
$res.afterState.result.properties.RebootRequired | Should -Not -BeNull
130-
}
131-
13267
It 'Export works on PS class-based resource' -Skip:(!$IsWindows){
13368

13469
$r = dsc resource export -r TestClassResource/TestClassResource
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Copyright (c) Microsoft Corporation.
2+
# Licensed under the MIT License.
3+
4+
Describe 'WindowsPowerShell adapter resource tests' {
5+
6+
BeforeAll {
7+
if ($isWindows) {
8+
winrm quickconfig -quiet -force
9+
}
10+
$OldPSModulePath = $env:PSModulePath
11+
$env:PSModulePath += [System.IO.Path]::PathSeparator + $PSScriptRoot
12+
13+
$winpsConfigPath = Join-path $PSScriptRoot "winps_resource.dsc.yaml"
14+
if ($isWindows) {
15+
$cacheFilePath_v5 = Join-Path $env:LocalAppData "dsc" "WindowsPSAdapterCache.json"
16+
}
17+
}
18+
AfterAll {
19+
$env:PSModulePath = $OldPSModulePath
20+
}
21+
22+
BeforeEach {
23+
if ($isWindows) {
24+
Remove-Item -Force -ea SilentlyContinue -Path $cacheFilePath_v5
25+
}
26+
}
27+
28+
It 'Windows PowerShell adapter supports File resource' -Skip:(!$IsWindows){
29+
30+
$r = dsc resource list --adapter Microsoft.Windows/WindowsPowerShell
31+
$LASTEXITCODE | Should -Be 0
32+
$resources = $r | ConvertFrom-Json
33+
($resources | ? {$_.Type -eq 'PSDesiredStateConfiguration/File'}).Count | Should -Be 1
34+
}
35+
36+
It 'Get works on Binary "File" resource' -Skip:(!$IsWindows){
37+
38+
$testFile = "$testdrive\test.txt"
39+
'test' | Set-Content -Path $testFile -Force
40+
$r = '{"DestinationPath":"' + $testFile.replace('\','\\') + '"}' | dsc resource get -r 'PSDesiredStateConfiguration/File'
41+
$LASTEXITCODE | Should -Be 0
42+
$res = $r | ConvertFrom-Json
43+
$res.actualState.result.properties.DestinationPath | Should -Be "$testFile"
44+
}
45+
46+
It 'Get works on traditional "Script" resource' -Skip:(!$IsWindows){
47+
48+
$testFile = "$testdrive\test.txt"
49+
'test' | Set-Content -Path $testFile -Force
50+
$r = '{"GetScript": "@{result = $(Get-Content ' + $testFile.replace('\','\\') + ')}", "SetScript": "throw", "TestScript": "throw"}' | dsc resource get -r 'PSDesiredStateConfiguration/Script'
51+
$LASTEXITCODE | Should -Be 0
52+
$res = $r | ConvertFrom-Json
53+
$res.actualState.result.properties.result | Should -Be 'test'
54+
}
55+
56+
It 'Get works on config with File resource for WinPS' -Skip:(!$IsWindows){
57+
58+
$testFile = "$testdrive\test.txt"
59+
'test' | Set-Content -Path $testFile -Force
60+
$r = (Get-Content -Raw $winpsConfigPath).Replace('c:\test.txt',"$testFile") | dsc config get
61+
$LASTEXITCODE | Should -Be 0
62+
$res = $r | ConvertFrom-Json
63+
$res.results[0].result.actualState.result[0].properties.DestinationPath | Should -Be "$testFile"
64+
}
65+
}

powershell-adapter/copy_files.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
./psDscAdapter/powershell.resource.ps1
22
./psDscAdapter/psDscAdapter.psd1
3-
./psDscAdapter/psDscAdapter.psm1
3+
./psDscAdapter/psDscAdapter.psm1
4+
./psDscAdapter/win_psDscAdapter.psd1
5+
./psDscAdapter/win_psDscAdapter.psm1

powershell-adapter/psDscAdapter/powershell.resource.ps1

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,13 @@ if ('Validate' -ne $Operation) {
1515
$host.ui.WriteErrorLine($trace)
1616

1717
# load private functions of psDscAdapter stub module
18-
$psDscAdapter = Import-Module "$PSScriptRoot/psDscAdapter.psd1" -Force -PassThru
18+
if ($PSVersionTable.PSVersion.Major -le 5) {
19+
$psDscAdapter = Import-Module "$PSScriptRoot/win_psDscAdapter.psd1" -Force -PassThru
20+
}
21+
else {
22+
$psDscAdapter = Import-Module "$PSScriptRoot/psDscAdapter.psd1" -Force -PassThru
23+
}
24+
1925

2026
# initialize OUTPUT as array
2127
$result = [System.Collections.Generic.List[Object]]::new()

0 commit comments

Comments
 (0)