Skip to content

Commit

Permalink
Merge pull request #411 from anmenaga/psadapter_updates1
Browse files Browse the repository at this point in the history
Set/Test/Export and other updates for PSAdapter
  • Loading branch information
SteveL-MSFT authored Apr 18, 2024
2 parents 0a934c5 + 8b75f29 commit d8a12d3
Show file tree
Hide file tree
Showing 13 changed files with 107 additions and 655 deletions.
2 changes: 0 additions & 2 deletions build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,6 @@ $skip_test_projects_on_windows = @("tree-sitter-dscexpression")
}
}

Save-PSResource -Path $target -Name 'PSDesiredStateConfiguration' -Version '2.0.7' -Repository PSGallery -TrustRepository

if ($failed) {
Write-Host -ForegroundColor Red "Build failed"
exit 1
Expand Down
49 changes: 22 additions & 27 deletions powershell-adapter/Tests/powershellgroup.config.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,8 @@ Describe 'PowerShell adapter resource tests' {
$LASTEXITCODE | Should -Be 0
$res = $r | ConvertFrom-Json
$res.results[0].result.actualState.result[0].properties.DestinationPath | Should -Be "$testFile"
}

<#
}

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

$r = Get-Content -Raw $pwshConfigPath | dsc config test
Expand All @@ -51,8 +50,8 @@ Describe 'PowerShell adapter resource tests' {
$r = Get-Content -Raw $pwshConfigPath | dsc config set
$LASTEXITCODE | Should -Be 0
$res = $r | ConvertFrom-Json
$res.results.result.afterState.result[0].RebootRequired | Should -Not -BeNull
$res.results.result.afterState.result[1].RebootRequired | Should -Not -BeNull
$res.results.result.afterState.result[0].type | Should -Be "PSTestModule/TestPSRepository"
$res.results.result.afterState.result[1].type | Should -Be "TestClassResource/TestClassResource"
}


Expand All @@ -66,51 +65,47 @@ Describe 'PowerShell adapter resource tests' {
properties:
resources:
- name: Class-resource Info
type: PSTestModule/TestClassResource
type: TestClassResource/TestClassResource
'@
$out = $yaml | dsc config export
$LASTEXITCODE | Should -Be 0
$res = $out | ConvertFrom-Json
$res.'$schema' | Should -BeExactly 'https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2024/04/config/document.json'
$res.'resources' | Should -Not -BeNullOrEmpty
$res.resources.count | Should -Be 5
$res.resources[0].properties.Name | Should -Be "Object1"
$res.resources[0].properties.Prop1 | Should -Be "Property of object1"
$res.resources[0].properties.result.count | Should -Be 5
$res.resources[0].properties.result[0].Name | Should -Be "Object1"
$res.resources[0].properties.result[0].Prop1 | Should -Be "Property of object1"
}

#>

It 'Custom psmodulepath in config works' -Skip:(!$IsWindows){

$OldPSModulePath = $env:PSModulePath
Copy-Item -Recurse -Force -Path "$PSScriptRoot/PSTestModule" -Destination $TestDrive
Rename-Item -Path "$PSScriptRoot/PSTestModule" -NewName "_PSTestModule"
Copy-Item -Recurse -Force -Path "$PSScriptRoot/TestClassResource" -Destination $TestDrive
Rename-Item -Path "$PSScriptRoot/TestClassResource" -NewName "_TestClassResource"

try {
$yaml = @"
`$schema: https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2024/04/config/document.json
`$schema: https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2024/04/config/document.json
resources:
- name: Working with class-based resources
type: Microsoft.DSC/PowerShell
properties:
psmodulepath: `$env:PSModulePath;$TestDrive
resources:
- name: Working with class-based resources
type: Microsoft.DSC/PowerShell
properties:
psmodulepath: `$env:PSModulePath;$TestDrive
resources:
- name: Class-resource Info
type: PSTestModule/TestClassResource
- name: Class-resource Info
type: TestClassResource/TestClassResource
"@
<#
$out = $yaml | dsc config export
$LASTEXITCODE | Should -Be 0
$res = $out | ConvertFrom-Json
$res.'$schema' | Should -BeExactly 'https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2024/04/config/document.json'
$res.'resources' | Should -Not -BeNullOrEmpty
$res.resources.count | Should -Be 5
$res.resources[0].properties.Name | Should -Be "Object1"
$res.resources[0].properties.Prop1 | Should -Be "Property of object1"
#>
$res.resources[0].properties.result.count | Should -Be 5
$res.resources[0].properties.result[0].Name | Should -Be "Object1"
$res.resources[0].properties.result[0].Prop1 | Should -Be "Property of object1"
}
finally {
Rename-Item -Path "$PSScriptRoot/_PSTestModule" -NewName "PSTestModule"
Rename-Item -Path "$PSScriptRoot/_TestClassResource" -NewName "TestClassResource"
$env:PSModulePath = $OldPSModulePath
}
}
Expand Down
35 changes: 16 additions & 19 deletions powershell-adapter/Tests/powershellgroup.resource.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Describe 'PowerShell adapter resource tests' {

It 'Discovery includes class-based and script-based resources ' -Skip:(!$IsWindows){

$r = dsc resource list * -a *PowerShell*
$r = dsc resource list * -a Microsoft.DSC/PowerShell
$LASTEXITCODE | Should -Be 0
$resources = $r | ConvertFrom-Json
($resources | ? {$_.Type -eq 'TestClassResource/TestClassResource'}).Count | Should -Be 1
Expand Down Expand Up @@ -53,87 +53,84 @@ Describe 'PowerShell adapter resource tests' {

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

$r = "{'Name':'TestClassResource1', 'Type':'TestClassResource/TestClassResource'}" | dsc resource get -r 'Microsoft.Dsc/PowerShell'
$r = "{'Name':'TestClassResource1'}" | dsc resource get -r 'TestClassResource/TestClassResource'
$LASTEXITCODE | Should -Be 0
$res = $r | ConvertFrom-Json
$res.actualState.result.properties.Prop1 | Should -BeExactly 'ValueForProp1'
}

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

$r = "{'Name':'TestPSRepository1','Type':'PSTestModule/TestPSRepository'}" | dsc resource get -r 'Microsoft.Dsc/PowerShell'
$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','Type':'TestClassResource/TestClassResource'}" | dsc resource get -r 'Microsoft.Dsc/PowerShell'
$r = "{'Name':'TestClassResource1'}" | dsc resource get -r 'TestClassResource/TestClassResource'
$LASTEXITCODE | Should -Be 0
$res = $r | ConvertFrom-Json
$res.actualState.result.properties.EnumProp | Should -BeExactly 'Expected'
}

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

$r = "{'Name':'TestPSRepository1','Type':'PSTestModule/TestPSRepository'}" | dsc resource get -r 'Microsoft.Dsc/PowerShell'
$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','Type':'TestClassResource/TestClassResource'}" | dsc resource test -r 'Microsoft.Dsc/PowerShell'
$r = "{'Name':'TestClassResource1','Prop1':'ValueForProp1'}" | dsc resource test -r 'TestClassResource/TestClassResource'
$LASTEXITCODE | Should -Be 0
$res = $r | ConvertFrom-Json
$res.actualState.result.properties.InDesiredState | Should -Be $True
}

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

$r = "{'Name':'TestPSRepository1','PackageManagementProvider':'NuGet','Type':'PSTestModule/TestPSRepository'}" | dsc resource test -r 'Microsoft.Dsc/PowerShell'
$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','Type':'TestClassResource/TestClassResource'}" | dsc resource set -r 'Microsoft.Dsc/PowerShell'
$r = "{'Name':'TestClassResource1','Prop1':'ValueForProp1'}" | dsc resource set -r 'TestClassResource/TestClassResource'
$LASTEXITCODE | Should -Be 0
$res = $r | ConvertFrom-Json
$res.afterState.RebootRequired | Should -Not -BeNull
$res.afterState.result | Should -Not -BeNull
}

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

$r = "{'Name':'TestPSRepository1','Type':'PSTestModule/TestPSRepository'}" | dsc resource set -r 'Microsoft.Dsc/PowerShell'
$r = "{'Name':'TestPSRepository1'}" | dsc resource set -r 'PSTestModule/TestPSRepository'
$LASTEXITCODE | Should -Be 0
$res = $r | ConvertFrom-Json
$res.afterState.RebootRequired | Should -Not -BeNull
$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
$LASTEXITCODE | Should -Be 0
$res = $r | ConvertFrom-Json
$res.resources.count | Should -Be 5
$res.resources[0].type | Should -Be "TestClassResource/TestClassResource"
$res.resources[0].properties.Name | Should -Be "Object1"
$res.resources[0].properties.Prop1 | Should -Be "Property of object1"
$res.resources[0].properties.result.count | Should -Be 5
$res.resources[0].properties.result[0].Name | Should -Be "Object1"
$res.resources[0].properties.result[0].Prop1 | Should -Be "Property of object1"
}

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

$r = dsc resource get --all -r TestClassResource/TestClassResource
$LASTEXITCODE | Should -Be 0
$res = $r | ConvertFrom-Json
$res.count | Should -Be 5
$res | % {$_.actualState | Should -Not -BeNullOrEmpty}
$res.actualState.result.count | Should -Be 5
$res.actualState.result| % {$_.Name | Should -Not -BeNullOrEmpty}
}
#>
}
9 changes: 2 additions & 7 deletions powershell-adapter/copy_files.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
powershell.resource.ps1
./psDscAdapter/powershell.resource.ps1
./psDscAdapter/psDscAdapter.psd1
./psDscAdapter/psDscAdapter.psm1
./psDscAdapter/Configuration/BaseRegistration/BaseResource.Schema.mof
./psDscAdapter/Configuration/BaseRegistration/MSFT_MetaConfigurationExtensionClasses.Schema.mof
./psDscAdapter/Configuration/BaseRegistration/en-us/BaseResource.Schema.mfl
./psDscAdapter/Configuration/BaseRegistration/en-us/MSFT_MetaConfigurationExtensionClasses.Schema.mfl
./psDscAdapter/helpers/DscResourceInfo.psm1
./psDscAdapter/psDscAdapter.psm1
12 changes: 6 additions & 6 deletions powershell-adapter/powershell.dsc.resource.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"-NonInteractive",
"-NoProfile",
"-Command",
"./powershell.resource.ps1 List"
"./psDscAdapter/powershell.resource.ps1 List"
]
},
"config": "full"
Expand All @@ -27,7 +27,7 @@
"-NonInteractive",
"-NoProfile",
"-Command",
"$Input | ./powershell.resource.ps1 Get"
"$Input | ./psDscAdapter/powershell.resource.ps1 Get"
],
"input": "stdin"
},
Expand All @@ -38,7 +38,7 @@
"-NonInteractive",
"-NoProfile",
"-Command",
"$Input | ./powershell.resource.ps1 Set"
"$Input | ./psDscAdapter/powershell.resource.ps1 Set"
],
"input": "stdin",
"implementsPretest": true,
Expand All @@ -51,7 +51,7 @@
"-NonInteractive",
"-NoProfile",
"-Command",
"$Input | ./powershell.resource.ps1 Test"
"$Input | ./psDscAdapter/powershell.resource.ps1 Test"
],
"input": "stdin",
"return": "state"
Expand All @@ -63,7 +63,7 @@
"-NonInteractive",
"-NoProfile",
"-Command",
"$Input | ./powershell.resource.ps1 Export"
"$Input | ./psDscAdapter/powershell.resource.ps1 Export"
],
"input": "stdin",
"return": "state"
Expand All @@ -75,7 +75,7 @@
"-NonInteractive",
"-NoProfile",
"-Command",
"$Input | ./powershell.resource.ps1 Validate"
"$Input | ./psDscAdapter/powershell.resource.ps1 Validate"
],
"input": "stdin"
},
Expand Down

This file was deleted.

Loading

0 comments on commit d8a12d3

Please sign in to comment.