Skip to content

Commit 4209792

Browse files
committed
Fix reason validation on adapter
1 parent 81ab3c3 commit 4209792

3 files changed

Lines changed: 29 additions & 2 deletions

File tree

adapters/powershell/Tests/TestAdapter/testadapter.resource.ps1

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,15 @@ switch ($Operation) {
6464
)} | ConvertTo-Json -Depth 10 -Compress
6565
}
6666
'Validate' {
67-
@{ valid = $true } | ConvertTo-Json
67+
# Test validation with reason field
68+
if ($inputobj.resources[0].properties.TestCaseId -eq 99) {
69+
@{
70+
valid = $false
71+
reason = "TestCaseId 99 is not allowed for testing purposes"
72+
} | ConvertTo-Json
73+
}
74+
else {
75+
@{ valid = $true } | ConvertTo-Json
76+
}
6877
}
6978
}

adapters/powershell/Tests/powershellgroup.resource.tests.ps1

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,23 @@ Describe 'PowerShell adapter resource tests' {
309309
}
310310
}
311311

312+
It 'Verify validate operation shows reason on adapted resource' {
313+
$oldPath = $env:PATH
314+
try {
315+
$adapterPath = Join-Path $PSScriptRoot 'TestAdapter'
316+
$env:PATH += [System.IO.Path]::PathSeparator + $adapterPath
317+
318+
# Test with invalid TestCaseId that should trigger validation failure with reason
319+
$r = '{"TestCaseId": 99}' | dsc resource get -r 'Test/TestCase' -f - 2>&1
320+
$LASTEXITCODE | Should -Not -Be 0
321+
$errorOutput = $r | Out-String
322+
$errorOutput | Should -Match "TestCaseId 99 is not allowed for testing purposes"
323+
}
324+
finally {
325+
$env:PATH = $oldPath
326+
}
327+
}
328+
312329
It 'Dsc can process large resource output' -Pending {
313330
try {
314331
$env:TestClassResourceResultCount = 5000 # with sync resource invocations this was not possible

lib/dsc-lib/src/dscresources/command_resource.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1093,7 +1093,8 @@ fn verify_json_from_manifest(resource: &DscResource, json: &str, target_resource
10931093
return Ok(());
10941094
}
10951095

1096-
return Err(DscError::Validation(t!("dscresources.commandResource.resourceInvalidJson").to_string()));
1096+
let reason = result.reason.unwrap_or_else(|| t!("dscresources.commandResource.resourceInvalidJson").to_string());
1097+
return Err(DscError::Validation(reason));
10971098
}
10981099

10991100
// otherwise, use schema validation

0 commit comments

Comments
 (0)