Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Separated WinPS adapter and tests #435

Merged
merged 3 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions powershell-adapter/Tests/class_ps_resources.dsc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ resources:
type: Microsoft.DSC/PowerShell
properties:
resources:
- name: Repository Info
type: PSTestModule/TestPSRepository
properties:
Name: TestPSRepository1
- name: Class-resource Info
type: TestClassResource/TestClassResource
properties:
Expand Down
32 changes: 6 additions & 26 deletions powershell-adapter/Tests/powershellgroup.config.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,16 @@
Describe 'PowerShell adapter resource tests' {

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

if ($IsLinux -or $IsMacOS) {
$cacheFilePath = Join-Path $env:HOME ".dsc" "PSAdapterCache.json"
}
else
{
$cacheFilePath = Join-Path $env:LocalAppData "dsc" "PSAdapterCache.json"
$cacheFilePath_v5 = Join-Path $env:LocalAppData "dsc" "WindowsPSAdapterCache.json"
}
}
AfterAll {
Expand All @@ -27,47 +22,32 @@ Describe 'PowerShell adapter resource tests' {

BeforeEach {
Remove-Item -Force -ea SilentlyContinue -Path $cacheFilePath
Remove-Item -Force -ea SilentlyContinue -Path $cacheFilePath_v5
}

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

$r = Get-Content -Raw $pwshConfigPath | dsc config get
$LASTEXITCODE | Should -Be 0
$res = $r | ConvertFrom-Json
$res.results[0].result.actualState.result[0].properties.PublishLocation | Should -BeExactly 'https://www.powershellgallery.com/api/v2/package/'
$res.results[0].result.actualState.result[1].properties.Prop1 | Should -BeExactly 'ValueForProp1'
$res.results[0].result.actualState.result[1].properties.EnumProp | Should -BeExactly 'Expected'
}

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

$testFile = "$testdrive\test.txt"
'test' | Set-Content -Path $testFile -Force
$r = (Get-Content -Raw $winpsConfigPath).Replace('c:\test.txt',"$testFile") | dsc config get
$LASTEXITCODE | Should -Be 0
$res = $r | ConvertFrom-Json
$res.results[0].result.actualState.result[0].properties.DestinationPath | Should -Be "$testFile"
$res.results[0].result.actualState.result[0].properties.Prop1 | Should -BeExactly 'ValueForProp1'
$res.results[0].result.actualState.result[0].properties.EnumProp | Should -BeExactly 'Expected'
}

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

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

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

$r = Get-Content -Raw $pwshConfigPath | dsc config set
$LASTEXITCODE | Should -Be 0
$res = $r | ConvertFrom-Json
$res.results.result.afterState.result[0].type | Should -Be "PSTestModule/TestPSRepository"
$res.results.result.afterState.result[1].type | Should -Be "TestClassResource/TestClassResource"
$res.results.result.afterState.result[0].type | Should -Be "TestClassResource/TestClassResource"
}


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

Expand Down
65 changes: 0 additions & 65 deletions powershell-adapter/Tests/powershellgroup.resource.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
Describe 'PowerShell adapter resource tests' {

BeforeAll {
if ($isWindows) {
winrm quickconfig -quiet -force
}
$OldPSModulePath = $env:PSModulePath
$env:PSModulePath += [System.IO.Path]::PathSeparator + $PSScriptRoot

Expand All @@ -16,7 +13,6 @@ Describe 'PowerShell adapter resource tests' {
else
{
$cacheFilePath = Join-Path $env:LocalAppData "dsc" "PSAdapterCache.json"
$cacheFilePath_v5 = Join-Path $env:LocalAppData "dsc" "WindowsPSAdapterCache.json"
}
}
AfterAll {
Expand All @@ -25,7 +21,6 @@ Describe 'PowerShell adapter resource tests' {

BeforeEach {
Remove-Item -Force -ea SilentlyContinue -Path $cacheFilePath
Remove-Item -Force -ea SilentlyContinue -Path $cacheFilePath_v5
}

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

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

$r = dsc resource list --adapter Microsoft.Windows/WindowsPowerShell
$LASTEXITCODE | Should -Be 0
$resources = $r | ConvertFrom-Json
($resources | ? {$_.Type -eq 'PSDesiredStateConfiguration/File'}).Count | Should -Be 1
}

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

$testFile = "$testdrive\test.txt"
'test' | Set-Content -Path $testFile -Force
$r = '{"DestinationPath":"' + $testFile.replace('\','\\') + '"}' | dsc resource get -r 'PSDesiredStateConfiguration/File'
$LASTEXITCODE | Should -Be 0
$res = $r | ConvertFrom-Json
$res.actualState.result.properties.DestinationPath | Should -Be "$testFile"
}

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

$testFile = "$testdrive\test.txt"
'test' | Set-Content -Path $testFile -Force
$r = '{"GetScript": "@{result = $(Get-Content ' + $testFile.replace('\','\\') + ')}", "SetScript": "throw", "TestScript": "throw"}' | dsc resource get -r 'PSDesiredStateConfiguration/Script'
$LASTEXITCODE | Should -Be 0
$res = $r | ConvertFrom-Json
$res.actualState.result.properties.result | Should -Be 'test'
}

It 'Get works on class-based resource' -Skip:(!$IsWindows){

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

It 'Get works on script-based resource' -Skip:(!$IsWindows){

$r = "{'Name':'TestPSRepository1'}" | dsc resource get -r 'PSTestModule/TestPSRepository'
$LASTEXITCODE | Should -Be 0
$res = $r | ConvertFrom-Json
$res.actualState.result.properties.PublishLocation | Should -BeExactly 'https://www.powershellgallery.com/api/v2/package/'
}

It 'Get uses enum names on class-based resource' -Skip:(!$IsWindows){

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

It 'Get uses enum names on script-based resource' -Skip:(!$IsWindows){

$r = "{'Name':'TestPSRepository1'}" | dsc resource get -r 'PSTestModule/TestPSRepository'
$LASTEXITCODE | Should -Be 0
$res = $r | ConvertFrom-Json
$res.actualState.result.properties.Ensure | Should -BeExactly 'Present'
}

It 'Test works on class-based resource' -Skip:(!$IsWindows){

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

It 'Test works on script-based resource' -Skip:(!$IsWindows){

$r = "{'Name':'TestPSRepository1','PackageManagementProvider':'NuGet'}" | dsc resource test -r 'PSTestModule/TestPSRepository'
$LASTEXITCODE | Should -Be 0
$res = $r | ConvertFrom-Json
$res.actualState.result.properties.InDesiredState | Should -Be $True
}

It 'Set works on class-based resource' -Skip:(!$IsWindows){

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

It 'Set works on script-based resource' -Skip:(!$IsWindows){

$r = "{'Name':'TestPSRepository1'}" | dsc resource set -r 'PSTestModule/TestPSRepository'
$LASTEXITCODE | Should -Be 0
$res = $r | ConvertFrom-Json
$res.afterState.result.properties.RebootRequired | Should -Not -BeNull
}

It 'Export works on PS class-based resource' -Skip:(!$IsWindows){

$r = dsc resource export -r TestClassResource/TestClassResource
Expand Down
65 changes: 65 additions & 0 deletions powershell-adapter/Tests/win_powershellgroup.tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

Describe 'WindowsPowerShell adapter resource tests' {

BeforeAll {
if ($isWindows) {
winrm quickconfig -quiet -force
}
$OldPSModulePath = $env:PSModulePath
$env:PSModulePath += [System.IO.Path]::PathSeparator + $PSScriptRoot

$winpsConfigPath = Join-path $PSScriptRoot "winps_resource.dsc.yaml"
if ($isWindows) {
$cacheFilePath_v5 = Join-Path $env:LocalAppData "dsc" "WindowsPSAdapterCache.json"
}
}
AfterAll {
$env:PSModulePath = $OldPSModulePath
}

BeforeEach {
if ($isWindows) {
Remove-Item -Force -ea SilentlyContinue -Path $cacheFilePath_v5
}
}

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

$r = dsc resource list --adapter Microsoft.Windows/WindowsPowerShell
$LASTEXITCODE | Should -Be 0
$resources = $r | ConvertFrom-Json
($resources | ? {$_.Type -eq 'PSDesiredStateConfiguration/File'}).Count | Should -Be 1
}

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

$testFile = "$testdrive\test.txt"
'test' | Set-Content -Path $testFile -Force
$r = '{"DestinationPath":"' + $testFile.replace('\','\\') + '"}' | dsc resource get -r 'PSDesiredStateConfiguration/File'
$LASTEXITCODE | Should -Be 0
$res = $r | ConvertFrom-Json
$res.actualState.result.properties.DestinationPath | Should -Be "$testFile"
}

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

$testFile = "$testdrive\test.txt"
'test' | Set-Content -Path $testFile -Force
$r = '{"GetScript": "@{result = $(Get-Content ' + $testFile.replace('\','\\') + ')}", "SetScript": "throw", "TestScript": "throw"}' | dsc resource get -r 'PSDesiredStateConfiguration/Script'
$LASTEXITCODE | Should -Be 0
$res = $r | ConvertFrom-Json
$res.actualState.result.properties.result | Should -Be 'test'
}

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

$testFile = "$testdrive\test.txt"
'test' | Set-Content -Path $testFile -Force
$r = (Get-Content -Raw $winpsConfigPath).Replace('c:\test.txt',"$testFile") | dsc config get
$LASTEXITCODE | Should -Be 0
$res = $r | ConvertFrom-Json
$res.results[0].result.actualState.result[0].properties.DestinationPath | Should -Be "$testFile"
}
}
4 changes: 3 additions & 1 deletion powershell-adapter/copy_files.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
./psDscAdapter/powershell.resource.ps1
./psDscAdapter/psDscAdapter.psd1
./psDscAdapter/psDscAdapter.psm1
./psDscAdapter/psDscAdapter.psm1
./psDscAdapter/win_psDscAdapter.psd1
./psDscAdapter/win_psDscAdapter.psm1
8 changes: 7 additions & 1 deletion powershell-adapter/psDscAdapter/powershell.resource.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ if ('Validate' -ne $Operation) {
$host.ui.WriteErrorLine($trace)

# load private functions of psDscAdapter stub module
$psDscAdapter = Import-Module "$PSScriptRoot/psDscAdapter.psd1" -Force -PassThru
if ($PSVersionTable.PSVersion.Major -le 5) {
$psDscAdapter = Import-Module "$PSScriptRoot/win_psDscAdapter.psd1" -Force -PassThru
}
else {
$psDscAdapter = Import-Module "$PSScriptRoot/psDscAdapter.psd1" -Force -PassThru
}


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