Skip to content

Commit d03480b

Browse files
committed
Add missing tests
1 parent dfad8a0 commit d03480b

File tree

4 files changed

+48
-11
lines changed

4 files changed

+48
-11
lines changed

powershell-adapter/Tests/powershellgroup.config.tests.ps1

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,4 +255,22 @@ Describe 'PowerShell adapter resource tests' {
255255
$out.results.result.actualstate.Credential.UserName | Should -Be 'User'
256256
$out.results.result.actualState.result.Credential.Password.Length | Should -Not -BeNullOrEmpty
257257
}
258+
259+
It 'Config does not work when credential properties are missing required fields' {
260+
$yaml = @"
261+
`$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
262+
resources:
263+
- name: Class-resource credential info
264+
type: TestClassResource/TestClassResource
265+
properties:
266+
Name: 'TestClassResource'
267+
Credential:
268+
UserName: 'User'
269+
OtherProperty: 'Password'
270+
"@
271+
$out = dsc config get -i $yaml 2>&1 | Out-String
272+
$LASTEXITCODE | Should -Be 2
273+
$out | Should -Not -BeNullOrEmpty
274+
$out | Should -BeLike "*ERROR*The PSCredential property 'Credential' is missing required fields*"
275+
}
258276
}

powershell-adapter/Tests/win_powershellgroup.tests.ps1

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,4 +211,22 @@ resources:
211211
$LASTEXITCODE | Should -Be 0
212212
$out.results[0].result.inDesiredState | Should -Be $inDesiredState
213213
}
214+
215+
It 'Config does not work when credential properties are missing required fields' -Skip:(!$IsWindows) {
216+
$yaml = @"
217+
`$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
218+
resources:
219+
- name: Service info
220+
type: PsDesiredStateConfiguration/Service
221+
properties:
222+
Name: Spooler
223+
Credential:
224+
UserName: 'User'
225+
OtherProperty: 'Password'
226+
"@
227+
$out = dsc config get -i $yaml 2>&1 | Out-String
228+
$LASTEXITCODE | Should -Be 2
229+
$out | Should -Not -BeNullOrEmpty
230+
$out | Should -BeLike "*ERROR*The PSCredential property 'Credential' is missing required fields*"
231+
}
214232
}

powershell-adapter/psDscAdapter/psDscAdapter.psm1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -425,8 +425,8 @@ function Invoke-DscOperation {
425425
if ($_.Value -is [System.Management.Automation.PSCustomObject]) {
426426
$validateProperty = $cachedDscResourceInfo.Properties | Where-Object -Property Name -EQ $_.Name
427427
if ($validateProperty.PropertyType -eq 'PSCredential') {
428-
if (-not $_.Value.Username -and -not $_.Value.Password) {
429-
"Credential property '$($_.Name)' requires both username and password input object" | Write-DscTrace -Operation Error
428+
if (-not $_.Value.Username -or -not $_.Value.Password) {
429+
"The PSCredential property '$($_.Name)' is missing required fields 'Username' and 'Password'" | Write-DscTrace -Operation Error
430430
exit 1
431431
}
432432
$dscResourceInstance.$($_.Name) = [System.Management.Automation.PSCredential]::new($_.Value.Username, (ConvertTo-SecureString -AsPlainText $_.Value.Password -Force))

powershell-adapter/psDscAdapter/win_psDscAdapter.psm1

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -365,9 +365,10 @@ function Invoke-DscOperation {
365365
$DesiredState.properties.psobject.properties | ForEach-Object -Begin { $property = @{} } -Process {
366366
if ($_.Value -is [System.Management.Automation.PSCustomObject]) {
367367
$validateProperty = $cachedDscResourceInfo.Properties | Where-Object -Property Name -EQ $_.Name
368-
if ($validateProperty.PropertyType -eq 'PSCredential') {
369-
if (-not $_.Value.Username -and -not $_.Value.Password) {
370-
"Credential property '$($_.Name)' requires both username and password input object" | Write-DscTrace -Operation Error
368+
$validateProperty | Write-DscTrace -Operation Debug
369+
if ($validateProperty.PropertyType -eq '[PSCredential]') {
370+
if (-not $_.Value.Username -or -not $_.Value.Password) {
371+
"The PSCredential property '$($_.Name)' is missing required fields 'Username' and 'Password'" | Write-DscTrace -Operation Error
371372
exit 1
372373
}
373374
$property.$($_.Name) = [System.Management.Automation.PSCredential]::new($_.Value.Username, (ConvertTo-SecureString -AsPlainText $_.Value.Password -Force))
@@ -415,9 +416,9 @@ function Invoke-DscOperation {
415416
# handle input objects by converting them to a hash table
416417
if ($_.Value -is [System.Management.Automation.PSCustomObject]) {
417418
$validateProperty = $cachedDscResourceInfo.Properties | Where-Object -Property Name -EQ $_.Name
418-
if ($validateProperty.PropertyType -eq 'PSCredential') {
419-
if (-not $_.Value.Username -and -not $_.Value.Password) {
420-
"Credential property '$($_.Name)' requires both username and password input object" | Write-DscTrace -Operation Error
419+
if ($validateProperty.PropertyType -eq '[PSCredential]') {
420+
if (-not $_.Value.Username -or -not $_.Value.Password) {
421+
"The PSCredential property '$($_.Name)' is missing required fields 'Username' and 'Password'" | Write-DscTrace -Operation Error
421422
exit 1
422423
}
423424
$dscResourceInstance.$($_.Name) = [System.Management.Automation.PSCredential]::new($_.Value.Username, (ConvertTo-SecureString -AsPlainText $_.Value.Password -Force))
@@ -473,9 +474,9 @@ function Invoke-DscOperation {
473474
$DesiredState.properties.psobject.properties | ForEach-Object -Begin { $property = @{} } -Process {
474475
if ($_.Value -is [System.Management.Automation.PSCustomObject]) {
475476
$validateProperty = $cachedDscResourceInfo.Properties | Where-Object -Property Name -EQ $_.Name
476-
if ($validateProperty.PropertyType -eq 'PSCredential') {
477-
if (-not $_.Value.Username -and -not $_.Value.Password) {
478-
"Credential property '$($_.Name)' requires both username and password input object" | Write-DscTrace -Operation Error
477+
if ($validateProperty.PropertyType -eq '[PSCredential]') {
478+
if (-not $_.Value.Username -or -not $_.Value.Password) {
479+
"The PSCredential property '$($_.Name)' is missing required fields 'Username' and 'Password'" | Write-DscTrace -Operation Error
479480
exit 1
480481
}
481482
$property.$($_.Name) = [System.Management.Automation.PSCredential]::new($_.Value.Username, (ConvertTo-SecureString -AsPlainText $_.Value.Password -Force))

0 commit comments

Comments
 (0)