Skip to content

Commit

Permalink
Fix tests from rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
G.Reijn authored and G.Reijn committed Dec 7, 2024
1 parent 8edeb32 commit b7e94a8
Showing 1 changed file with 59 additions and 34 deletions.
93 changes: 59 additions & 34 deletions powershell-adapter/Tests/powershellgroup.resource.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
Describe 'PowerShell adapter resource tests' {

BeforeAll {
$OldPSModulePath = $env:PSModulePath
$OldPSModulePath = $env:PSModulePath
$env:PSModulePath += [System.IO.Path]::PathSeparator + $PSScriptRoot

if ($IsLinux -or $IsMacOS) {
if ($IsLinux -or $IsMacOS)
{
$cacheFilePath = Join-Path $env:HOME ".dsc" "PSAdapterCache.json"
}
else
Expand All @@ -28,7 +29,7 @@ Describe 'PowerShell adapter resource tests' {
$r = dsc resource list '*' -a Microsoft.DSC/PowerShell
$LASTEXITCODE | Should -Be 0
$resources = $r | ConvertFrom-Json
($resources | ? {$_.Type -in @('TestClassResource/TestClassResource', 'TestClassNoVersion/TestClassNoVersion')}).Count | Should -Be 2
($resources | ? { $_.Type -in @('TestClassResource/TestClassResource', 'TestClassNoVersion/TestClassNoVersion') }).Count | Should -Be 2
}

It 'Get works on class-based resource' {
Expand All @@ -46,7 +47,7 @@ Describe 'PowerShell adapter resource tests' {

It 'Get works on class-based resource without sub-directory version' {

$r = "{'Name':'TestClassNoVersion'}" | dsc resource get -r 'TestClassNoVersion/TestClassNoVersion'
$r = "{'Name':'TestClassNoVersion'}" | dsc resource get -r 'TestClassNoVersion/TestClassNoVersion' -f -
$LASTEXITCODE | Should -Be 0
$res = $r | ConvertFrom-Json
$res.actualState.result.properties.Name | Should -BeExactly 'TestClassNoVersion'
Expand Down Expand Up @@ -76,7 +77,7 @@ Describe 'PowerShell adapter resource tests' {

It 'Test works on class-based resource without sub-directory version' {

$r = "{'Name':'TestClassNoVersion'}" | dsc resource test -r 'TestClassNoVersion/TestClassNoVersion'
$r = "{'Name':'TestClassNoVersion'}" | dsc resource test -r 'TestClassNoVersion/TestClassNoVersion' -f -
$LASTEXITCODE | Should -Be 0
$res = $r | ConvertFrom-Json
$res.actualState.result.properties.InDesiredState | Should -Be $True
Expand All @@ -93,7 +94,7 @@ Describe 'PowerShell adapter resource tests' {

It 'Set works on class-based resource without sub-directory version' {

$r = "{'Name':'TestClassNoVersion'}" | dsc resource set -r 'TestClassNoVersion/TestClassNoVersion'
$r = "{'Name':'TestClassNoVersion'}" | dsc resource set -r 'TestClassNoVersion/TestClassNoVersion' -f -
$LASTEXITCODE | Should -Be 0
$res = $r | ConvertFrom-Json
$res.afterState.result | Should -Not -BeNull
Expand All @@ -109,7 +110,7 @@ Describe 'PowerShell adapter resource tests' {
$res.resources[0].properties.result[0].Prop1 | Should -Be "Property of object1"

# verify that only properties with DscProperty attribute are returned
$res.resources[0].properties.result | %{
$res.resources[0].properties.result | % {
$propertiesNames = $_ | Get-Member -MemberType NoteProperty | % Name
$propertiesNames | Should -Not -Contain 'NonDscProperty'
$propertiesNames | Should -Not -Contain 'HiddenNonDscProperty'
Expand All @@ -122,7 +123,7 @@ Describe 'PowerShell adapter resource tests' {
$LASTEXITCODE | Should -Be 0
$res = $r | ConvertFrom-Json
$res.actualState.result.count | Should -Be 5
$res.actualState.result| % {$_.Name | Should -Not -BeNullOrEmpty}
$res.actualState.result | % { $_.Name | Should -Not -BeNullOrEmpty }
}

It 'Verify that ClearCache works in PSAdapter' {
Expand All @@ -141,14 +142,20 @@ Describe 'PowerShell adapter resource tests' {
# generate the cache
$null = dsc resource list '*' -a Microsoft.DSC/PowerShell
# update the version in the cache file
$cacheFilePath = if ($IsWindows) {
$cacheFilePath = if ($IsWindows)
{
# PS 6+ on Windows
Join-Path $env:LocalAppData "dsc\PSAdapterCache.json"
} else {
}
else
{
# either WinPS or PS 6+ on Linux/Mac
if ($PSVersionTable.PSVersion.Major -le 5) {
if ($PSVersionTable.PSVersion.Major -le 5)
{
Join-Path $env:LocalAppData "dsc\WindowsPSAdapterCache.json"
} else {
}
else
{
Join-Path $env:HOME ".dsc" "PSAdapterCache.json"
}
}
Expand All @@ -170,7 +177,8 @@ Describe 'PowerShell adapter resource tests' {
Remove-Item -Recurse -Force -Path "$PSScriptRoot/TestClassResource"

$oldPath = $env:PSModulePath
try {
try
{
$env:PSModulePath += [System.IO.Path]::PathSeparator + $TestDrive

# generate the cache
Expand All @@ -184,7 +192,8 @@ Describe 'PowerShell adapter resource tests' {
"$TestDrive/tracing.txt" | Should -FileContentMatchExactly 'Detected non-existent cache entry'
"$TestDrive/tracing.txt" | Should -FileContentMatchExactly 'Constructing Get-DscResource cache'
}
finally {
finally
{
$env:PSModulePath = $oldPath
Copy-Item -Recurse -Force -Path "$PSScriptRoot/Backup/TestClassResource" -Destination "$PSScriptRoot"
Remove-Item -Recurse -Force -Path "$PSScriptRoot/Backup"
Expand All @@ -196,7 +205,7 @@ Describe 'PowerShell adapter resource tests' {
$r = dsc resource list '*' -a Microsoft.DSC/PowerShell
$LASTEXITCODE | Should -Be 0
$resources = $r | ConvertFrom-Json
$t = $resources | ? {$_.Type -eq 'TestClassResource/TestClassResource'}
$t = $resources | ? { $_.Type -eq 'TestClassResource/TestClassResource' }
$t.properties | Should -Contain "BaseProperty"
}

Expand Down Expand Up @@ -232,74 +241,83 @@ Describe 'PowerShell adapter resource tests' {


$oldPath = $env:PSModulePath
try {
try
{
$env:PSModulePath += [System.IO.Path]::PathSeparator + $pathRoot1
$env:PSModulePath += [System.IO.Path]::PathSeparator + $pathRoot2

$r = dsc resource list '*' -a Microsoft.DSC/PowerShell
$LASTEXITCODE | Should -Be 0
$resources = $r | ConvertFrom-Json
$r = @($resources | ? {$_.Type -eq 'TestClassResource/TestClassResource'})
$r = @($resources | ? { $_.Type -eq 'TestClassResource/TestClassResource' })
$r.Count | Should -Be 1
$r[0].Version | Should -Be '2.0.1'
}
finally {
finally
{
$env:PSModulePath = $oldPath
}
}

It 'Verify adapted_dsc_type field in Get' {
$oldPath = $env:PATH
try {
try
{
$adapterPath = Join-Path $PSScriptRoot 'TestAdapter'
$env:PATH += [System.IO.Path]::PathSeparator + $adapterPath

$r = '{TestCaseId: 1}'| dsc resource get -r 'Test/TestCase' -f -
$r = '{TestCaseId: 1}' | dsc resource get -r 'Test/TestCase' -f -
$LASTEXITCODE | Should -Be 0
$resources = $r | ConvertFrom-Json
$resources.actualState.result | Should -Be $True
}
finally {
finally
{
$env:PATH = $oldPath
}
}

It 'Verify adapted_dsc_type field in Set' {
$oldPath = $env:PATH
try {
try
{
$adapterPath = Join-Path $PSScriptRoot 'TestAdapter'
$env:PATH += [System.IO.Path]::PathSeparator + $adapterPath

$r = '{TestCaseId: 1}'| dsc resource set -r 'Test/TestCase' -f -
$r = '{TestCaseId: 1}' | dsc resource set -r 'Test/TestCase' -f -
$LASTEXITCODE | Should -Be 0
$resources = $r | ConvertFrom-Json
$resources.beforeState.result | Should -Be $True
$resources.afterState.result | Should -Be $True
}
finally {
finally
{
$env:PATH = $oldPath
}
}

It 'Verify adapted_dsc_type field in Test' {
$oldPath = $env:PATH
try {
try
{
$adapterPath = Join-Path $PSScriptRoot 'TestAdapter'
$env:PATH += [System.IO.Path]::PathSeparator + $adapterPath

$r = '{TestCaseId: 1}'| dsc resource test -r 'Test/TestCase' -f -
$r = '{TestCaseId: 1}' | dsc resource test -r 'Test/TestCase' -f -
$LASTEXITCODE | Should -Be 0
$resources = $r | ConvertFrom-Json
$resources.actualState.result | Should -Be $True
}
finally {
finally
{
$env:PATH = $oldPath
}
}

It 'Verify adapted_dsc_type field in Export' {
$oldPath = $env:PATH
try {
try
{
$adapterPath = Join-Path $PSScriptRoot 'TestAdapter'
$env:PATH += [System.IO.Path]::PathSeparator + $adapterPath

Expand All @@ -308,7 +326,8 @@ Describe 'PowerShell adapter resource tests' {
$resources = $r | ConvertFrom-Json
$resources.resources[0].properties.result | Should -Be $True
}
finally {
finally
{
$env:PATH = $oldPath
}
}
Expand All @@ -328,14 +347,20 @@ Describe 'PowerShell adapter resource tests' {
It 'Verify that there are no cache rebuilds for several sequential executions' {

# remove cache file
$cacheFilePath = if ($IsWindows) {
$cacheFilePath = if ($IsWindows)
{
# PS 6+ on Windows
Join-Path $env:LocalAppData "dsc\PSAdapterCache.json"
} else {
}
else
{
# either WinPS or PS 6+ on Linux/Mac
if ($PSVersionTable.PSVersion.Major -le 5) {
if ($PSVersionTable.PSVersion.Major -le 5)
{
Join-Path $env:LocalAppData "dsc\WindowsPSAdapterCache.json"
} else {
}
else
{
Join-Path $env:HOME ".dsc" "PSAdapterCache.json"
}
}
Expand All @@ -346,7 +371,7 @@ Describe 'PowerShell adapter resource tests' {
"$TestDrive/tracing.txt" | Should -FileContentMatchExactly 'Constructing Get-DscResource cache'

# next executions following shortly after should Not rebuild the cache
1..3 | %{
1..3 | % {
dsc -l trace resource list -a Microsoft.DSC/PowerShell 2> $TestDrive/tracing.txt
"$TestDrive/tracing.txt" | Should -Not -FileContentMatchExactly 'Constructing Get-DscResource cache'
}
Expand Down

0 comments on commit b7e94a8

Please sign in to comment.