Skip to content

Commit d817869

Browse files
author
Steve Lee (POWERSHELL HE/HIM) (from Dev Box)
committed
add synthetic test for configuration execution
1 parent a2792ec commit d817869

File tree

5 files changed

+74
-8
lines changed

5 files changed

+74
-8
lines changed

dsc/tests/dsc_config_set.tests.ps1

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ Describe 'dsc config get tests' {
1818
$out = $config_yaml | dsc config set | ConvertFrom-Json
1919
$LASTEXITCODE | Should -Be 0
2020
$out.results[0].type | Should -BeExactly 'Test/Exist'
21-
$out.results[0].result.beforeState.state | Should -BeExactly 'Absent'
2221
$out.results[0].result.beforeState._exist | Should -BeFalse
2322
$out.results[0].result.afterState.state | Should -BeExactly 'Absent'
2423
$out.results[0].result.afterState._exist | Should -BeFalse

dsc_lib/src/dscresources/command_resource.rs

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ pub fn invoke_set(resource: &ResourceManifest, cwd: &str, desired: &str, skip_te
120120
verify_json(resource, cwd, desired)?;
121121

122122
// if resource doesn't implement a pre-test, we execute test first to see if a set is needed
123-
if !skip_test && !set.pre_test.unwrap_or_default() {
123+
if !skip_test && set.pre_test != Some(true) {
124124
info!("No pretest, invoking test {}", &resource.resource_type);
125125
let (in_desired_state, actual_state) = match invoke_test(resource, cwd, desired)? {
126126
TestResult::Group(group_response) => {
@@ -282,7 +282,8 @@ pub fn invoke_set(resource: &ResourceManifest, cwd: &str, desired: &str, skip_te
282282
/// Error is returned if the underlying command returns a non-zero exit code.
283283
pub fn invoke_test(resource: &ResourceManifest, cwd: &str, expected: &str) -> Result<TestResult, DscError> {
284284
let Some(test) = &resource.test else {
285-
return Err(DscError::NotImplemented("test".to_string()));
285+
info!("Resource '{}' does not implement test, performing synthetic test", &resource.resource_type);
286+
return invoke_synthetic_test(resource, cwd, expected);
286287
};
287288

288289
verify_json(resource, cwd, expected)?;
@@ -375,6 +376,30 @@ pub fn invoke_test(resource: &ResourceManifest, cwd: &str, expected: &str) -> Re
375376
}
376377
}
377378

379+
fn invoke_synthetic_test(resource: &ResourceManifest, cwd: &str, expected: &str) -> Result<TestResult, DscError> {
380+
let get_result = invoke_get(resource, cwd, expected)?;
381+
let actual_state = match get_result {
382+
GetResult::Group(results) => {
383+
let mut result_array: Vec<Value> = Vec::new();
384+
for result in results {
385+
result_array.push(serde_json::to_value(&result)?);
386+
}
387+
Value::from(result_array)
388+
},
389+
GetResult::Resource(response) => {
390+
response.actual_state
391+
}
392+
};
393+
let expected_value: Value = serde_json::from_str(expected)?;
394+
let diff_properties = get_diff(&expected_value, &actual_state);
395+
Ok(TestResult::Resource(ResourceTestResponse {
396+
desired_state: expected_value,
397+
actual_state,
398+
in_desired_state: diff_properties.is_empty(),
399+
diff_properties,
400+
}))
401+
}
402+
378403
/// Invoke the delete operation against a command resource.
379404
///
380405
/// # Arguments
@@ -393,7 +418,7 @@ pub fn invoke_delete(resource: &ResourceManifest, cwd: &str, filter: &str) -> Re
393418

394419
let mut env: Option<HashMap<String, String>> = None;
395420
let mut input_filter: Option<&str> = None;
396-
let mut get_args = resource.get.args.clone();
421+
let mut delete_args = delete.args.clone();
397422
verify_json(resource, cwd, filter)?;
398423
match &delete.input {
399424
InputKind::Env => {
@@ -403,12 +428,12 @@ pub fn invoke_delete(resource: &ResourceManifest, cwd: &str, filter: &str) -> Re
403428
input_filter = Some(filter);
404429
},
405430
InputKind::Arg(arg_name) => {
406-
replace_token(&mut get_args, arg_name, filter)?;
431+
replace_token(&mut delete_args, arg_name, filter)?;
407432
},
408433
}
409434

410435
info!("Invoking delete '{}' using '{}'", &resource.resource_type, &delete.executable);
411-
let (exit_code, _stdout, stderr) = invoke_command(&delete.executable, get_args, input_filter, Some(cwd), env)?;
436+
let (exit_code, _stdout, stderr) = invoke_command(&delete.executable, delete_args, input_filter, Some(cwd), env)?;
412437
log_resource_traces(&stderr);
413438
if exit_code != 0 {
414439
return Err(DscError::Command(resource.resource_type.clone(), exit_code, stderr));

registry/registry.dsc.resource.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"executable": "registry",
3535
"args": [
3636
"config",
37-
"remove",
37+
"delete",
3838
"--input",
3939
"{json}"
4040
],

registry/tests/registry.config.set.tests.ps1

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
# Licensed under the MIT License.
33

44
Describe 'registry config set tests' {
5+
AfterEach {
6+
if ($IsWindows) {
7+
Remove-Item -Path 'HKCU:\1' -Recurse -ErrorAction Ignore
8+
}
9+
}
10+
511
It 'Can set a deeply nested key and value' -Skip:(!$IsWindows) {
612
$json = @'
713
{
@@ -29,4 +35,41 @@ Describe 'registry config set tests' {
2935
$result.valueData.String | Should -Be 'World'
3036
($result.psobject.properties | Measure-Object).Count | Should -Be 3
3137
}
38+
39+
It 'delete called when _exist is false' -Skip:(!$IsWindows) {
40+
$config = @{
41+
'$schema' = 'https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2023/08/config/document.json'
42+
resources = @(
43+
@{
44+
name = 'reg'
45+
type = 'Microsoft.Windows/Registry'
46+
properties = @{
47+
keyPath = 'HKCU\1\2'
48+
valueName = 'Test'
49+
valueData = @{
50+
String = 'Test'
51+
}
52+
_exist = $true
53+
}
54+
}
55+
)
56+
}
57+
58+
$out = dsc config set -d ($config | ConvertTo-Json -Depth 10)
59+
$LASTEXITCODE | Should -Be 0
60+
61+
$config.resources[0].properties._exist = $false
62+
$out = dsc config set -d ($config | ConvertTo-Json -Depth 10) | ConvertFrom-Json
63+
$LASTEXITCODE | Should -Be 0
64+
$out.results[0].result.afterState._exist | Should -Be $false
65+
66+
Get-ItemProperty -Path 'HKCU:\1\2' -Name 'Test' -ErrorAction Ignore | Should -BeNullOrEmpty
67+
68+
$config.resources[0].properties.valueName = $null
69+
$out = dsc config set -d ($config | ConvertTo-Json -Depth 10) | ConvertFrom-Json
70+
$LASTEXITCODE | Should -Be 0
71+
$out.results[0].result.afterState._exist | Should -Be $false
72+
73+
Get-Item -Path 'HKCU:\1\2' -ErrorAction Ignore | Should -BeNullOrEmpty
74+
}
3275
}

tools/dsctest/dscexist.dsc.resource.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
"arg": "{json}"
2525
},
2626
"handlesExist": true,
27-
"implementsPretest": true,
2827
"return": "state"
2928
},
3029
"schema": {

0 commit comments

Comments
 (0)