Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ VariablesToExport = @()
AliasesToExport = @()

# DSC resources to export from this module
DscResourcesToExport = @('TestClassResource', 'NoExport', 'FilteredExport')
DscResourcesToExport = @('TestClassResource', 'NoExport', 'FilteredExport', 'StreamResource')

# Private data to pass to the module specified in RootModule/ModuleToProcess. This may also contain a PSData hashtable with additional module metadata used by PowerShell.
PrivateData = @{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,36 @@ class FilteredExport : BaseTestClass
}
}

[DscResource()]
class StreamResource : BaseTestClass
{
[DscProperty(Key)]
[string] $Name

[DscProperty()]
[string] $Prop1

[void] Set()
{
Write-Verbose -Verbose "This is a Verbose message"
Write-Debug -Debug "This is a Debug message"
Write-Error "This is an Error message"
}

[bool] Test()
{
Write-Host "This is a Host message"
Write-Information "This is an Information message"
return $true
}

[StreamResource] Get()
{
Write-Warning "This is a Warning message"
return $this
}
}

function Test-World()
{
"Hello world from PSTestModule!"
Expand Down
14 changes: 7 additions & 7 deletions adapters/powershell/Tests/powershellgroup.config.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ Describe 'PowerShell adapter resource tests' {

It 'Get works on config with class-based resources' {

$r = Get-Content -Raw $pwshConfigPath | dsc config get -f -
$LASTEXITCODE | Should -Be 0
$r = Get-Content -Raw $pwshConfigPath | dsc -l trace config get -f - 2> $TestDrive/tracing.txt
$LASTEXITCODE | Should -Be 0 -Because (Get-Content -Raw -Path $TestDrive/tracing.txt)
$res = $r | ConvertFrom-Json
$res.results[0].result.actualState.result[0].properties.Prop1 | Should -BeExactly 'ValueForProp1'
$res.results[0].result.actualState.result[0].properties.EnumProp | Should -BeExactly 'Expected'
Expand Down Expand Up @@ -99,10 +99,10 @@ Describe 'PowerShell adapter resource tests' {
- name: Class-resource Info
type: TestClassResource/NoExport
'@
$out = $yaml | dsc config export -f - 2>&1 | Out-String
$LASTEXITCODE | Should -Be 2
$out | Should -Not -BeNullOrEmpty
$out | Should -BeLike "*ERROR*Export method not implemented by resource 'TestClassResource/NoExport'*"
$null = $yaml | dsc -l trace config export -f - 2>$TestDrive/error.log
$logContent = Get-Content -Raw -Path $TestDrive/error.log
$LASTEXITCODE | Should -Be 2 -Because $logContent
$logContent | Should -BeLike "*ERROR*Export method not implemented by resource 'TestClassResource/NoExport'*"
}

It 'Export works with filtered export property' {
Expand All @@ -126,7 +126,7 @@ Describe 'PowerShell adapter resource tests' {
$res.resources[0].properties.result.count | Should -Be 1
$res.resources[0].properties.result[0].Name | Should -Be "FilteredExport"
$res.resources[0].properties.result[0].Prop1 | Should -Be "Filtered Property for FilteredExport"
"$TestDrive/export_trace.txt" | Should -FileContentMatch "Properties provided for filtered export"
"$TestDrive/export_trace.txt" | Should -FileContentMatch "Properties provided for filtered export" -Because (Get-Content -Raw -Path $TestDrive/export_trace.txt)
}

It 'Export fails when filtered export is requested but not implemented' {
Expand Down
46 changes: 45 additions & 1 deletion adapters/powershell/Tests/powershellgroup.resource.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ Describe 'PowerShell adapter resource tests' {
$null = dsc resource list '*' -a Microsoft.DSC/PowerShell
# call the ClearCache operation
$scriptPath = Join-Path $PSScriptRoot '..' 'psDscAdapter' 'powershell.resource.ps1'
$null = & $scriptPath -Operation ClearCache
$null = & $scriptPath -Operation ClearCache 2>$null
# verify that PSAdapter does not find the cache
dsc -l debug resource list '*' -a Microsoft.DSC/PowerShell 2> $TestDrive/tracing.txt
$LASTEXITCODE | Should -Be 0
Expand Down Expand Up @@ -377,4 +377,48 @@ Describe 'PowerShell adapter resource tests' {
$res = $r | ConvertFrom-Json
$res.actualState.SecureStringProp | Should -Not -BeNullOrEmpty
}

Context 'Tracing works' {
It 'Error messages come from Write-Error' {
$null = dsc -l error resource set -r TestClassResource/StreamResource -i '{"Name":"TestClassResource1"}' 2> $TestDrive/error.log
$logContent = Get-Content -Path $TestDrive/error.log -Raw
$LASTEXITCODE | Should -Be 2 -Because $logContent
$logContent | Should -Match 'ERROR .*? This is an Error message' -Because $logContent
}

It 'Warning messages come from Write-Warning' {
$null = "{'Name':'TestClassResource1','Prop1':'ValueForProp1'}" | dsc -l warn resource get -r 'TestClassResource/StreamResource' -f - 2> $TestDrive/warning.log
$logContent = Get-Content -Path $TestDrive/warning.log -Raw
$LASTEXITCODE | Should -Be 0 -Because $logContent
$logContent | Should -Match 'WARN .*? This is a Warning message' -Because $logContent
}

It 'Info messages come from Write-Host' {
$null = "{'Name':'TestClassResource1'}" | dsc -l info resource test -r 'TestClassResource/StreamResource' -f - 2> $TestDrive/verbose.log
$logContent = Get-Content -Path $TestDrive/verbose.log -Raw
$LASTEXITCODE | Should -Be 0 -Because $logContent
$logContent | Should -Match 'INFO .*? This is a Host message' -Because $logContent
}

It 'Debug messages come from Write-Verbose' {
$null = "{'Name':'TestClassResource1'}" | dsc -l debug resource set -r 'TestClassResource/StreamResource' -f - 2> $TestDrive/debug.log
$logContent = Get-Content -Path $TestDrive/debug.log -Raw
$LASTEXITCODE | Should -Be 2 -Because $logContent
$logContent | Should -Match 'DEBUG .*? This is a Verbose message' -Because $logContent
}

It 'Trace messages come from Write-Debug' {
$null = dsc -l trace resource set -r TestClassResource/StreamResource -i '{"Name":"TestClassResource1"}' 2> $TestDrive/trace.log
$logContent = Get-Content -Path $TestDrive/trace.log -Raw
$LASTEXITCODE | Should -Be 2 -Because $logContent
$logContent | Should -Match 'TRACE .*? This is a Debug message' -Because $logContent
}

It 'Trace messages come from Write-Information' {
$null = dsc -l trace resource test -r TestClassResource/StreamResource -i '{"Name":"TestClassResource1"}' 2> $TestDrive/trace_info.log
$logContent = Get-Content -Path $TestDrive/trace_info.log -Raw
$LASTEXITCODE | Should -Be 0 -Because $logContent
$logContent | Should -Match 'INFO .*? This is an Information message' -Because $logContent
}
}
}
6 changes: 4 additions & 2 deletions adapters/powershell/Tests/win_powershell_cache.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,14 @@ Describe 'WindowsPowerShell adapter resource tests - requires elevated permissio
It 'Verify that there are no cache rebuilds for several sequential executions' {
# first execution should build the cache
$null = dsc -l trace resource list -a Microsoft.Windows/WindowsPowerShell 2> $TestDrive/tracing.txt
"$TestDrive/tracing.txt" | Should -FileContentMatchExactly 'Constructing Get-DscResource cache'
$tracingContent = Get-Content -Path $TestDrive/tracing.txt | Out-String
$tracingContent | Should -BeLike '*Constructing Get-DscResource cache*' -Because $tracingContent

# next executions following shortly after should Not rebuild the cache
1..3 | ForEach-Object {
$null = dsc -l trace resource list -a Microsoft.Windows/WindowsPowerShell 2> $TestDrive/tracing.txt
"$TestDrive/tracing.txt" | Should -Not -FileContentMatchExactly 'Constructing Get-DscResource cache'
$tracingContent = Get-Content -Path $TestDrive/tracing.txt | Out-String
$tracingContent | Should -Not -BeLike '*Constructing Get-DscResource cache*' -Because $tracingContent
}
}

Expand Down
Loading