From 28b8083fc1b76ad0c31ca39627b0b24e50151ca2 Mon Sep 17 00:00:00 2001 From: "Steve Lee (POWERSHELL HE/HIM) (from Dev Box)" Date: Mon, 29 Jul 2024 17:44:01 -0700 Subject: [PATCH 1/2] Add specific error if PS class resource doesn't implement export --- .../0.0.1/TestClassResource.psd1 | 2 +- .../0.0.1/TestClassResource.psm1 | 43 +++++++++++++++++++ .../Tests/powershellgroup.config.tests.ps1 | 17 ++++++++ .../psDscAdapter/psDscAdapter.psm1 | 4 ++ 4 files changed, 65 insertions(+), 1 deletion(-) diff --git a/powershell-adapter/Tests/TestClassResource/0.0.1/TestClassResource.psd1 b/powershell-adapter/Tests/TestClassResource/0.0.1/TestClassResource.psd1 index 62e3fdeb..8f52bda1 100644 --- a/powershell-adapter/Tests/TestClassResource/0.0.1/TestClassResource.psd1 +++ b/powershell-adapter/Tests/TestClassResource/0.0.1/TestClassResource.psd1 @@ -34,7 +34,7 @@ VariablesToExport = @() AliasesToExport = @() # DSC resources to export from this module -DscResourcesToExport = 'TestClassResource' +DscResourcesToExport = @('TestClassResource', 'NoExport') # Private data to pass to the module specified in RootModule/ModuleToProcess. This may also contain a PSData hashtable with additional module metadata used by PowerShell. PrivateData = @{ diff --git a/powershell-adapter/Tests/TestClassResource/0.0.1/TestClassResource.psm1 b/powershell-adapter/Tests/TestClassResource/0.0.1/TestClassResource.psm1 index 71eb6afc..1d894acb 100644 --- a/powershell-adapter/Tests/TestClassResource/0.0.1/TestClassResource.psm1 +++ b/powershell-adapter/Tests/TestClassResource/0.0.1/TestClassResource.psm1 @@ -74,6 +74,49 @@ class TestClassResource : BaseTestClass } } +[DscResource()] +class NoExport: BaseTestClass +{ + [DscProperty(Key)] + [string] $Name + + [DscProperty()] + [string] $Prop1 + + [DscProperty()] + [string] $EnumProp + + [void] Set() + { + } + + [bool] Test() + { + if (($this.Name -eq "TestClassResource1") -and ($this.Prop1 -eq "ValueForProp1")) + { + return $true + } + else + { + return $false + } + } + + [NoExport] Get() + { + if ($this.Name -eq "TestClassResource1") + { + $this.Prop1 = "ValueForProp1" + } + else + { + $this.Prop1 = $env:DSC_CONFIG_ROOT + } + $this.EnumProp = ([EnumPropEnumeration]::Expected).ToString() + return $this + } +} + function Test-World() { "Hello world from PSTestModule!" diff --git a/powershell-adapter/Tests/powershellgroup.config.tests.ps1 b/powershell-adapter/Tests/powershellgroup.config.tests.ps1 index cd152d69..70f31bd7 100644 --- a/powershell-adapter/Tests/powershellgroup.config.tests.ps1 +++ b/powershell-adapter/Tests/powershellgroup.config.tests.ps1 @@ -71,6 +71,23 @@ Describe 'PowerShell adapter resource tests' { $res.resources[0].properties.result[0].Prop1 | Should -Be "Property of object1" } + It 'Export fails when class-based resource does not implement' { + $yaml = @' + $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: + resources: + - name: Class-resource Info + type: TestClassResource/NoExport +'@ + $out = $yaml | dsc config export 2>&1 | Out-String + $LASTEXITCODE | Should -Be 2 + $out | Should -Not -BeNullOrEmpty + $out | Should -BeLike "*ERROR*Export method not implemented by resource 'TestClassResource/NoExport'*" + } + It 'Custom psmodulepath in config works' { $OldPSModulePath = $env:PSModulePath diff --git a/powershell-adapter/psDscAdapter/psDscAdapter.psm1 b/powershell-adapter/psDscAdapter/psDscAdapter.psm1 index 79174668..debb9768 100644 --- a/powershell-adapter/psDscAdapter/psDscAdapter.psm1 +++ b/powershell-adapter/psDscAdapter/psDscAdapter.psm1 @@ -475,6 +475,10 @@ function Invoke-DscOperation { 'Export' { $t = $dscResourceInstance.GetType() $method = $t.GetMethod('Export') + if ($null -eq $method) { + "Export method not implemented by resource '$($DesiredState.Type)'" | Write-DscTrace -Operation Error + exit 1 + } $resultArray = $method.Invoke($null,$null) $addToActualState = $resultArray } From 309cfbb53f9e801a677e9cae5a91b3e56d86af02 Mon Sep 17 00:00:00 2001 From: "Steve Lee (POWERSHELL HE/HIM) (from Dev Box)" Date: Mon, 5 Aug 2024 14:32:31 -0700 Subject: [PATCH 2/2] remove unused implement in NoExport test class --- .../0.0.1/TestClassResource.psm1 | 18 +----------------- 1 file changed, 1 insertion(+), 17 deletions(-) diff --git a/powershell-adapter/Tests/TestClassResource/0.0.1/TestClassResource.psm1 b/powershell-adapter/Tests/TestClassResource/0.0.1/TestClassResource.psm1 index 1d894acb..bfe3f8f9 100644 --- a/powershell-adapter/Tests/TestClassResource/0.0.1/TestClassResource.psm1 +++ b/powershell-adapter/Tests/TestClassResource/0.0.1/TestClassResource.psm1 @@ -92,27 +92,11 @@ class NoExport: BaseTestClass [bool] Test() { - if (($this.Name -eq "TestClassResource1") -and ($this.Prop1 -eq "ValueForProp1")) - { - return $true - } - else - { - return $false - } + return $true } [NoExport] Get() { - if ($this.Name -eq "TestClassResource1") - { - $this.Prop1 = "ValueForProp1" - } - else - { - $this.Prop1 = $env:DSC_CONFIG_ROOT - } - $this.EnumProp = ([EnumPropEnumeration]::Expected).ToString() return $this } }