From f4c76aba7bcab551ff6a03b1866e2a925a492953 Mon Sep 17 00:00:00 2001 From: Andrew Date: Fri, 27 Sep 2024 12:53:41 -0700 Subject: [PATCH] Fixed InDesiredState --- dsc_lib/src/dscresources/command_resource.rs | 32 +++++++++++++++---- .../Tests/powershellgroup.resource.tests.ps1 | 16 ++++++++++ .../psDscAdapter/psDscAdapter.psm1 | 4 +-- 3 files changed, 42 insertions(+), 10 deletions(-) diff --git a/dsc_lib/src/dscresources/command_resource.rs b/dsc_lib/src/dscresources/command_resource.rs index 67ebf10a..72346f24 100644 --- a/dsc_lib/src/dscresources/command_resource.rs +++ b/dsc_lib/src/dscresources/command_resource.rs @@ -282,13 +282,31 @@ pub fn invoke_test(resource: &ResourceManifest, cwd: &str, expected: &str) -> Re return Err(DscError::Operation(format!("Failed to parse json from test {}|{}|{} -> {err}", &test.executable, stdout, stderr))) } }; - let diff_properties = get_diff(&expected_value, &actual_value); - Ok(TestResult::Resource(ResourceTestResponse { - desired_state: expected_value, - actual_state: actual_value, - in_desired_state: diff_properties.is_empty(), - diff_properties, - })) + + // Special case for PSAdapters - Test operation in them returns a single 'InDesiredState' property + if (resource.resource_type == "Microsoft.DSC/PowerShell") + || (resource.resource_type == "Microsoft.Windows/WindowsPowerShell") { + let mut in_desired_state_result = false; + if let Some(res) = actual_value["result"][0]["properties"]["InDesiredState"].as_bool() { + in_desired_state_result = res; + } + + Ok(TestResult::Resource(ResourceTestResponse { + desired_state: expected_value, + actual_state: actual_value, + in_desired_state: in_desired_state_result, + diff_properties: Vec::new() + })) + } + else { + let diff_properties = get_diff(&expected_value, &actual_value); + Ok(TestResult::Resource(ResourceTestResponse { + desired_state: expected_value, + actual_state: actual_value, + in_desired_state: diff_properties.is_empty(), + diff_properties, + })) + } }, Some(ReturnKind::StateAndDiff) => { // command should be returning actual state as a JSON line and a list of properties that differ as separate JSON line diff --git a/powershell-adapter/Tests/powershellgroup.resource.tests.ps1 b/powershell-adapter/Tests/powershellgroup.resource.tests.ps1 index a15e6b4c..417e5de6 100644 --- a/powershell-adapter/Tests/powershellgroup.resource.tests.ps1 +++ b/powershell-adapter/Tests/powershellgroup.resource.tests.ps1 @@ -57,6 +57,7 @@ Describe 'PowerShell adapter resource tests' { $r = "{'Name':'TestClassResource1','Prop1':'ValueForProp1'}" | dsc resource test -r 'TestClassResource/TestClassResource' $LASTEXITCODE | Should -Be 0 $res = $r | ConvertFrom-Json + $res.InDesiredState | Should -Be $True $res.actualState.result.properties.InDesiredState | Should -Be $True # verify that only properties with DscProperty attribute are returned @@ -325,4 +326,19 @@ Describe 'PowerShell adapter resource tests' { "$TestDrive/tracing.txt" | Should -Not -FileContentMatchExactly 'Constructing Get-DscResource cache' } } + + It 'Verify InDesiredState in Test' { + + $r = "{'Name':'TestClassResource1','Prop1':'ValueForProp1'}" | dsc resource test -r 'TestClassResource/TestClassResource' + $LASTEXITCODE | Should -Be 0 + $res = $r | ConvertFrom-Json + $res.InDesiredState | Should -Be $True + $res.actualState.result.properties.InDesiredState | Should -Be $True + + $r = "{'Name':'TestClassResource1','Prop1':'abcd'}" | dsc resource test -r 'TestClassResource/TestClassResource' + $LASTEXITCODE | Should -Be 0 + $res = $r | ConvertFrom-Json + $res.InDesiredState | Should -Be $False + $res.actualState.result.properties.InDesiredState | Should -Be $False + } } diff --git a/powershell-adapter/psDscAdapter/psDscAdapter.psm1 b/powershell-adapter/psDscAdapter/psDscAdapter.psm1 index e3d6272c..5c470535 100644 --- a/powershell-adapter/psDscAdapter/psDscAdapter.psm1 +++ b/powershell-adapter/psDscAdapter/psDscAdapter.psm1 @@ -480,9 +480,7 @@ function Invoke-DscOperation { $dscResourceInstance.Set() } 'Test' { - $Result = @{} - $raw_obj = $dscResourceInstance.Test() - $ValidProperties | ForEach-Object { $Result[$_] = $raw_obj.$_ } + $Result = $dscResourceInstance.Test() $addToActualState.properties = [psobject]@{'InDesiredState'=$Result} } 'Export' {