Skip to content
Merged
Show file tree
Hide file tree
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
26 changes: 11 additions & 15 deletions dsc/tests/dsc_discovery.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ Describe 'tests for resource discovery' {
$env:DSC_RESOURCE_PATH = $testdrive
Set-Content -Path "$testdrive/test.dsc.resource.json" -Value $manifest
$out = dsc resource list 2>&1
write-verbose -verbose ($out | Out-String)
$out | Should -Match 'WARN.*?Validation.*?invalid version' -Because ($out | Out-String)
}
finally {
Expand Down Expand Up @@ -166,21 +165,18 @@ Describe 'tests for resource discovery' {
$env:PSModulePath = $oldPSModulePath
}

It 'Verify non-zero exit code when resource not found' {
It 'Verify non-zero exit code when resource not found: <cmdline>' -TestCases @(
@{ cmdline = "dsc resource get -r abc/def" }
@{ cmdline = "dsc resource get --all -r abc/def" }
@{ cmdline = "dsc resource set -r abc/def -i 'abc'" }
@{ cmdline = "dsc resource test -r abc/def -i 'abc'" }
@{ cmdline = "dsc resource delete -r abc/def -i 'abc'" }
@{ cmdline = "dsc resource export -r abc/def" }
@{ cmdline = "dsc resource schema -r abc/def" }
) {
param($cmdline)

$out = dsc resource get -r abc/def
$LASTEXITCODE | Should -Be 7
$out = dsc resource get --all -r abc/def
$LASTEXITCODE | Should -Be 7
$out = 'abc' | dsc resource set -r abc/def -f -
$LASTEXITCODE | Should -Be 7
$out = 'abc' | dsc resource test -r abc/def -f -
$LASTEXITCODE | Should -Be 7
$out = 'abc' | dsc resource delete -r abc/def -f -
$LASTEXITCODE | Should -Be 7
$out = dsc resource export -r abc/def
$LASTEXITCODE | Should -Be 7
$out = dsc resource schema -r abc/def
Invoke-Expression $cmdline 2>$null
$LASTEXITCODE | Should -Be 7
}

Expand Down
1 change: 0 additions & 1 deletion dsc/tests/dsc_extension_discover.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ Describe 'Discover extension tests' {
$out.Count | Should -Be 1 -Because ($out | Out-String)
$out.type | Should -Be 'Test/DiscoverRelative'
$out = dsc resource list 2> $TestDrive/error.log
write-verbose -verbose (Get-Content -Path "$TestDrive/error.log" -Raw)
$LASTEXITCODE | Should -Be 0
$out | Should -BeNullOrEmpty
$errorMessage = Get-Content -Path "$TestDrive/error.log" -Raw
Expand Down
2 changes: 1 addition & 1 deletion dsc/tests/dsc_include.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Describe 'Include tests' {
"@
$configPath = Join-Path $TestDrive 'config.dsc.yaml'
$config | Set-Content -Path $configPath
dsc config get -f $configPath
dsc config get -f $configPath 2>$null
$LASTEXITCODE | Should -Be 2
}

Expand Down
4 changes: 2 additions & 2 deletions dsc/tests/dsc_securitycontext.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Describe 'Tests for configuration security context metadata' {
}

It 'Require admin' {
$out = dsc config get -f $PSScriptRoot/../examples/require_admin.yaml
$out = dsc config get -f $PSScriptRoot/../examples/require_admin.yaml 2>$null
if ($isAdmin) {
$LASTEXITCODE | Should -Be 0
$out | Should -Not -BeNullOrEmpty
Expand All @@ -24,7 +24,7 @@ Describe 'Tests for configuration security context metadata' {
}

It 'Require non-admin' {
$out = dsc config get -f $PSScriptRoot/../examples/require_nonadmin.yaml
$out = dsc config get -f $PSScriptRoot/../examples/require_nonadmin.yaml 2>$null
if ($isAdmin) {
$LASTEXITCODE | Should -Be 2
}
Expand Down
4 changes: 2 additions & 2 deletions dsc/tests/dsc_set.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ Describe 'resource set tests' {
"_exist": false
}
'@
$null = registry config set --input $json
$null = registry config set --input $json 2>$null
}
}

Expand All @@ -73,7 +73,7 @@ Describe 'resource set tests' {
"_exist": false
}
'@
$null = registry config set --input $json
$null = registry config set --input $json 2>$null
}
}

Expand Down
2 changes: 1 addition & 1 deletion dsc/tests/dsc_test.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Describe 'resource test tests' {
"valueName": "ProductName"
}
'@
$current = registry config get --input $json
$current = registry config get --input $json 2>$null
$out = $current | dsc resource test -r Microsoft.Windows/Registry -f -
$LASTEXITCODE | Should -Be 0
$out = $out | ConvertFrom-Json
Expand Down
2 changes: 0 additions & 2 deletions dsc/tests/dsc_variables.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ resources:
output: "[variables('myVariable')]"
'@
$out = dsc config get -i $configYaml | ConvertFrom-Json
Write-Verbose -Verbose $out
$LASTEXITCODE | Should -Be 0
$out.results[0].result.actualState.output | Should -Be 'bar'
}
Expand All @@ -39,7 +38,6 @@ resources:
output: "[variables('myVariable')]"
'@
$out = dsc config get -i $configYaml 2>&1 | Out-String
Write-Verbose -Verbose $out
$LASTEXITCODE | Should -Be 2
$out | Should -BeLike "*Variable 'myVariable' does not exist or has not been initialized yet*"
}
Expand Down
19 changes: 9 additions & 10 deletions powershell-adapter/psDscAdapter/powershell.resource.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@ trap {
Write-DscTrace -Operation Debug -Message ($_ | Format-List -Force | Out-String)
}

# Adding some debug info to STDERR
'PSVersion=' + $PSVersionTable.PSVersion.ToString() | Write-DscTrace
'PSPath=' + $PSHome | Write-DscTrace
'PSModulePath=' + $env:PSModulePath | Write-DscTrace

if ($Operation -eq 'ClearCache') {
$cacheFilePath = if ($IsWindows) {
# PS 6+ on Windows
Expand All @@ -44,11 +39,15 @@ if ($Operation -eq 'ClearCache') {
}
}

'Deleting cache file ' + $cacheFilePath | Write-DscTrace
Remove-Item -Force -ea SilentlyContinue -Path $cacheFilePath
exit 0
}

# Adding some debug info to STDERR
'PSVersion=' + $PSVersionTable.PSVersion.ToString() | Write-DscTrace
'PSPath=' + $PSHome | Write-DscTrace
'PSModulePath=' + $env:PSModulePath | Write-DscTrace

if ($PSVersionTable.PSVersion.Major -le 5) {
# For Windows PowerShell, we want to remove any PowerShell 7 paths from PSModulePath
$env:PSModulePath = ($env:PSModulePath -split ';' | Where-Object { $_ -notlike '*\powershell\*' }) -join ';'
Expand Down Expand Up @@ -87,7 +86,7 @@ switch ($Operation) {

# cache was refreshed on script load
foreach ($dscResource in $dscResourceCache) {

# https://learn.microsoft.com/dotnet/api/system.management.automation.dscresourceinfo
$DscResourceInfo = $dscResource.DscResourceInfo

Expand All @@ -99,7 +98,7 @@ switch ($Operation) {
if ($DscResourceInfo.Capabilities) {
$capabilities = $DscResourceInfo.Capabilities
} elseif ($module.PrivateData.PSData.DscCapabilities) {

$capabilities = $module.PrivateData.PSData.DscCapabilities
} else {
$capabilities = @('get', 'set', 'test')
Expand Down Expand Up @@ -183,7 +182,7 @@ switch ($Operation) {
}
$result += $actualState
}

# OUTPUT json to stderr for debug, and to stdout
if ($Operation -eq 'Test') {
$result = @{ result = $result; _inDesiredState = $inDesiredState } | ConvertTo-Json -Depth 10 -Compress
Expand All @@ -196,7 +195,7 @@ switch ($Operation) {
}
'Validate' {
# VALIDATE not implemented

# OUTPUT
@{ valid = $true } | ConvertTo-Json
}
Expand Down
4 changes: 2 additions & 2 deletions registry/tests/registry.config.get.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Describe 'Registry config get tests' {
"keyPath": "HKLM\\Software\\Microsoft\\Windows\\CurrentVersion"
}
'@
$out = registry config get --input $json
$out = registry config get --input $json 2>$null
$LASTEXITCODE | Should -Be 0
$result = $out | ConvertFrom-Json
$result.keyPath | Should -Be 'HKLM\Software\Microsoft\Windows\CurrentVersion'
Expand All @@ -22,7 +22,7 @@ Describe 'Registry config get tests' {
"valueName": "ProgramFilesPath"
}
'@
$out = registry config get --input $json
$out = registry config get --input $json 2>$null
$LASTEXITCODE | Should -Be 0
$result = $out | ConvertFrom-Json
$result.keyPath | Should -Be 'HKLM\Software\Microsoft\Windows\CurrentVersion'
Expand Down
8 changes: 4 additions & 4 deletions registry/tests/registry.config.set.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ Describe 'registry config set tests' {
}
}
'@
$out = registry config set --input $json
$out = registry config set --input $json 2>$null
$LASTEXITCODE | Should -Be 0
$out | Should -BeNullOrEmpty
$result = registry config get --input $json | ConvertFrom-Json
$result = registry config get --input $json 2>$null | ConvertFrom-Json
$result.keyPath | Should -Be 'HKCU\1\2\3'
$result.valueName | Should -Be 'Hello'
$result.valueData.String | Should -Be 'World'
($result.psobject.properties | Measure-Object).Count | Should -Be 3

$out = registry config get --input $json
$out = registry config get --input $json 2>$null
$LASTEXITCODE | Should -Be 0
$result = $out | ConvertFrom-Json
$result.keyPath | Should -Be 'HKCU\1\2\3'
Expand Down Expand Up @@ -82,7 +82,7 @@ Describe 'registry config set tests' {
properties:
keyPath: 'HKCU\1'
valueName: Test
_exist: true
_exist: true
'@

$out = dsc config set -i $configYaml | ConvertFrom-Json
Expand Down
24 changes: 12 additions & 12 deletions registry/tests/registry.config.whatif.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ Describe 'registry config whatif tests' {
"keyPath": "HKCU\\1\\2\\3"
}
'@
registry config set -w --input $json | Write-Host
$get_before = registry config get --input $json
$result = registry config set -w --input $json | ConvertFrom-Json
$null = registry config set -w --input $json 2>$null
$get_before = registry config get --input $json 2>$null
$result = registry config set -w --input $json 2>$null | ConvertFrom-Json
$LASTEXITCODE | Should -Be 0
$result.keyPath | Should -Be 'HKCU\1\2\3'
$result._metadata.whatIf[0] | Should -Match '.*1.*'
$result._metadata.whatIf[1] | Should -Match '.*2.*'
$result._metadata.whatIf[2] | Should -Match '.*3.*'
$get_after = registry config get --input $json
$get_after = registry config get --input $json 2>$null
$get_before | Should -EQ $get_after
}

Expand All @@ -38,7 +38,7 @@ Describe 'registry config whatif tests' {
}
}
'@
$result = registry config set -w --input $json | ConvertFrom-Json
$result = registry config set -w --input $json 2>$null | ConvertFrom-Json
$LASTEXITCODE | Should -Be 0
$result.keyPath | Should -Be 'HKCU\1\2\3'
$result.valueName | Should -Be 'Hello'
Expand All @@ -54,7 +54,7 @@ Describe 'registry config whatif tests' {
"keyPath": "HKCU\\1\\2"
}
'@
registry config set --input $set_json
registry config set --input $set_json 2>$null
$whatif_json = @'
{
"keyPath": "HKCU\\1\\2",
Expand All @@ -64,7 +64,7 @@ Describe 'registry config whatif tests' {
}
}
'@
$result = registry config set -w --input $whatif_json | ConvertFrom-Json
$result = registry config set -w --input $whatif_json 2>$null | ConvertFrom-Json
$LASTEXITCODE | Should -Be 0
$result.keyPath | Should -Be 'HKCU\1\2'
$result.valueName | Should -Be 'Hello'
Expand All @@ -82,7 +82,7 @@ Describe 'registry config whatif tests' {
}
}
'@
registry config set --input $set_json
registry config set --input $set_json 2>$null
$whatif_json = @'
{
"keyPath": "HKCU\\1\\2\\3",
Expand All @@ -92,7 +92,7 @@ Describe 'registry config whatif tests' {
}
}
'@
$result = registry config set -w --input $whatif_json | ConvertFrom-Json
$result = registry config set -w --input $whatif_json 2>$null | ConvertFrom-Json
$LASTEXITCODE | Should -Be 0
$result.keyPath | Should -Be 'HKCU\1\2\3'
$result.valueName | Should -Be 'Hello'
Expand All @@ -110,7 +110,7 @@ Describe 'registry config whatif tests' {
}
}
'@
registry config set --input $set_json
registry config set --input $set_json 2>$null
$set_json = @'
{
"keyPath": "HKCU\\1\\2\\3",
Expand All @@ -120,13 +120,13 @@ Describe 'registry config whatif tests' {
}
}
'@
registry config set --input $set_json
registry config set --input $set_json 2>$null
$whatif_json = @'
{
"keyPath": "HKCU\\1\\2"
}
'@
$result = registry config set -w --input $whatif_json | ConvertFrom-Json
$result = registry config set -w --input $whatif_json 2>$null | ConvertFrom-Json
$LASTEXITCODE | Should -Be 0
$result.keyPath | Should -Be 'HKCU\1\2'
($result.psobject.properties | Measure-Object).Count | Should -Be 1
Expand Down
8 changes: 4 additions & 4 deletions runcommandonset/tests/runcommandonset.get.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@ Describe 'tests for runcommandonset get' {
}
"@

$result = $json | dsc resource get -r Microsoft.DSC.Transitional/RunCommandOnSet -f - | ConvertFrom-Json
$result = $json | dsc resource get -r Microsoft.DSC.Transitional/RunCommandOnSet -f - 2>$null | ConvertFrom-Json
$LASTEXITCODE | Should -Be 0
$result.actualState.arguments | Should -BeExactly @('bar', 'baz')
$result.actualState.executable | Should -BeExactly 'foo'
$result.actualState.exitCode | Should -BeExactly 5
}

It 'Executable is a required input via CLI arguments' {
$null = runcommandonset get -a foo
$null = runcommandonset get -a foo 2>$null
$LASTEXITCODE | Should -Be 4
}

It 'Executable is a required input via STDIN' {
'{ "arguments": "foo" }' | dsc resource get -r Microsoft.DSC.Transitional/RunCommandOnSet -f -
'{ "arguments": "foo" }' | dsc resource get -r Microsoft.DSC.Transitional/RunCommandOnSet -f - 2>$null
$LASTEXITCODE | Should -Be 2
}

Expand All @@ -39,7 +39,7 @@ Describe 'tests for runcommandonset get' {
arguments:
- "bar"
"@
$out = $config_yaml | dsc config get -f - | ConvertFrom-Json
$out = $config_yaml | dsc config get -f - 2>$null | ConvertFrom-Json
$LASTEXITCODE | Should -Be 0
$out.hadErrors | Should -BeFalse
$out.results.Count | Should -Be 1
Expand Down
Loading
Loading