Skip to content

Commit

Permalink
Fixed InDesiredState
Browse files Browse the repository at this point in the history
  • Loading branch information
anmenaga committed Sep 27, 2024
1 parent 78cb8a1 commit 5c916ee
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 10 deletions.
32 changes: 25 additions & 7 deletions dsc_lib/src/dscresources/command_resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions powershell-adapter/Tests/powershellgroup.resource.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
}
4 changes: 1 addition & 3 deletions powershell-adapter/psDscAdapter/psDscAdapter.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -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' {
Expand Down

0 comments on commit 5c916ee

Please sign in to comment.