Skip to content

Commit f2bbae1

Browse files
G.ReijnGijsreyn
authored andcommitted
Create test to valid scenario
1 parent 9075bd4 commit f2bbae1

File tree

1 file changed

+82
-3
lines changed

1 file changed

+82
-3
lines changed

powershell-adapter/Tests/win_powershellgroup.tests.ps1

Lines changed: 82 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Describe 'WindowsPowerShell adapter resource tests - requires elevated permissio
3030
$r = dsc resource list --adapter Microsoft.Windows/WindowsPowerShell
3131
$LASTEXITCODE | Should -Be 0
3232
$resources = $r | ConvertFrom-Json
33-
($resources | ? {$_.Type -eq 'PSDesiredStateConfiguration/File'}).Count | Should -Be 1
33+
($resources | Where-Object {$_.Type -eq 'PSDesiredStateConfiguration/File'}).Count | Should -Be 1
3434
}
3535

3636
It 'Get works on Binary "File" resource' -Skip:(!$IsWindows){
@@ -72,7 +72,6 @@ Describe 'WindowsPowerShell adapter resource tests - requires elevated permissio
7272
}
7373

7474
It 'Verify that there are no cache rebuilds for several sequential executions' -Skip:(!$IsWindows) {
75-
7675
# remove cache file
7776
$cacheFilePath = Join-Path $env:LocalAppData "dsc\WindowsPSAdapterCache.json"
7877
Remove-Item -Force -Path $cacheFilePath -ErrorAction Ignore
@@ -82,12 +81,92 @@ Describe 'WindowsPowerShell adapter resource tests - requires elevated permissio
8281
"$TestDrive/tracing.txt" | Should -FileContentMatchExactly 'Constructing Get-DscResource cache'
8382

8483
# next executions following shortly after should Not rebuild the cache
85-
1..3 | %{
84+
1..3 | ForEach-Object {
8685
dsc -l trace resource list -a Microsoft.Windows/WindowsPowerShell 2> $TestDrive/tracing.txt
8786
"$TestDrive/tracing.txt" | Should -Not -FileContentMatchExactly 'Constructing Get-DscResource cache'
8887
}
8988
}
9089

90+
It 'Verify if assertion is used that no module is cleared in the cache' -Skip:(!$IsWindows) {
91+
BeforeAll {
92+
New-Item -Path "$testdrive\test.txt" -ItemType File -Force | Out-Null
93+
}
94+
95+
# remove cache file
96+
$cacheFilePath = Join-Path $env:LocalAppData "dsc\WindowsPSAdapterCache.json"
97+
Remove-Item -Force -Path $cacheFilePath -ErrorAction Ignore
98+
99+
# build the cache
100+
dsc resource list --adapter Microsoft.Windows/WindowsPowerShell | Out-Null
101+
102+
# Create a test module in the test drive
103+
$testModuleDir = "$testdrive\TestModule\1.0.0"
104+
New-Item -Path $testModuleDir -ItemType Directory -Force | Out-Null
105+
106+
$manifestContent = @"
107+
@{
108+
RootModule = 'TestModule.psm1'
109+
ModuleVersion = '1.0.0'
110+
GUID = $([guid]::NewGuid().Guid)
111+
Author = 'Microsoft Corporation'
112+
CompanyName = 'Microsoft Corporation'
113+
Copyright = '(c) Microsoft Corporation. All rights reserved.'
114+
Description = 'Test module for DSC tests'
115+
PowerShellVersion = '5.1'
116+
DscResourcesToExport = @()
117+
FunctionsToExport = @()
118+
CmdletsToExport = @()
119+
VariablesToExport = @()
120+
AliasesToExport = @()
121+
}
122+
"@
123+
Set-Content -Path "$testModuleDir\TestModule.psd1" -Value $manifestContent
124+
125+
$scriptContent = @"
126+
Write-Host 'Hello from TestModule!'
127+
"@
128+
Set-Content -Path "$testModuleDir\TestModule.psm1" -Value $scriptContent
129+
130+
# Add the test module directory to PSModulePath
131+
$env:PSModulePath += [System.IO.Path]::PathSeparator + $testdrive
132+
133+
$yaml = @"
134+
`$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
135+
resources:
136+
- name: File resource
137+
type: PSDesiredStateConfiguration/File
138+
properties:
139+
DestinationPath: $testdrive\test.txt
140+
- name: File assertion
141+
type: Microsoft.DSC/Assertion
142+
properties:
143+
`$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
144+
resources:
145+
- name: File
146+
type: PSDesiredStateConfiguration/File
147+
properties:
148+
DestinationPath: $testdrive\test.txt
149+
dependsOn:
150+
- "[resourceId('PSDesiredStateConfiguration/File', 'File resource')]"
151+
- name: TestPSRepository
152+
type: DscResources/TestPSRepository
153+
properties:
154+
Name: NuGet
155+
dependsOn:
156+
- "[resourceId('PSDesiredStateConfiguration/File', 'File resource')]"
157+
- "[resourceId('Microsoft.DSC/Assertion', 'File assertion')]"
158+
"@
159+
$r = dsc config test -i $yaml | ConvertFrom-Json
160+
$LASTEXITCODE | Should -Be 0
161+
$r.results[0].result.inDesiredState | Should -Be $true
162+
$cache = Get-Content -Raw -Path $cacheFilePath
163+
$cache.ResourceCache | Should -Contain @('DscResources/TestPSRepository', 'PSDesiredStateConfiguration/File')
164+
165+
166+
# Clean up the test module directory
167+
Remove-Item -Path "$testModuleDir" -Recurse -Force -ErrorAction Ignore
168+
}
169+
91170
It '_inDesiredState is returned correction: <Context>' -Skip:(!$IsWindows) -TestCases @(
92171
@{ Context = 'Both running'; FirstState = 'Running'; SecondState = 'Running' }
93172
@{ Context = 'Both stopped'; FirstState = 'Stopped'; SecondState = 'Stopped' }

0 commit comments

Comments
 (0)