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

Fixed InDesiredState in PSAdapter Test operations #562

Closed
wants to merge 1 commit into from
Closed
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
32 changes: 25 additions & 7 deletions dsc_lib/src/dscresources/command_resource.rs
Original file line number Diff line number Diff line change
@@ -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")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like special casing resources. I would prefer we wait until resources can propagate metadata and passes up a inDesiredState boolean which would be used universally

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok; As discusses this will be implemented after #467 using metadata.

|| (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
16 changes: 16 additions & 0 deletions powershell-adapter/Tests/powershellgroup.resource.tests.ps1
Original file line number Diff line number Diff line change
@@ -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
}
}
4 changes: 1 addition & 3 deletions powershell-adapter/psDscAdapter/psDscAdapter.psm1
Original file line number Diff line number Diff line change
@@ -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' {