Skip to content

Commit 05179d9

Browse files
authored
Merge pull request #497 from anmenaga/issue_495
Fixed PSAdapter cache code when module files are missing
2 parents 2097285 + 0adb228 commit 05179d9

File tree

3 files changed

+68
-24
lines changed

3 files changed

+68
-24
lines changed

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

+28
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,34 @@ Describe 'PowerShell adapter resource tests' {
120120
"$TestDrive/tracing.txt" | Should -FileContentMatchExactly 'Incompatible version of cache in file'
121121
}
122122

123+
It 'Verify that removing a module results in cache rebuid' {
124+
125+
Copy-Item -Recurse -Force -Path "$PSScriptRoot/TestClassResource" -Destination $TestDrive
126+
Copy-Item -Recurse -Force -Path "$PSScriptRoot/TestClassResource" -Destination "$PSScriptRoot/Backup/TestClassResource"
127+
Remove-Item -Recurse -Force -Path "$PSScriptRoot/TestClassResource"
128+
129+
$oldPath = $env:PSModulePath
130+
try {
131+
$env:PSModulePath += [System.IO.Path]::PathSeparator + $TestDrive
132+
133+
# generate the cache
134+
$null = dsc resource list '*' -a Microsoft.DSC/PowerShell
135+
# remove the module files
136+
Remove-Item -Recurse -Force -Path "$TestDrive/TestClassResource"
137+
# verify that cache rebuid happened
138+
dsc -l trace resource list '*' -a Microsoft.DSC/PowerShell 2> $TestDrive/tracing.txt
139+
140+
$LASTEXITCODE | Should -Be 0
141+
"$TestDrive/tracing.txt" | Should -FileContentMatchExactly 'Detected non-existent cache entry'
142+
"$TestDrive/tracing.txt" | Should -FileContentMatchExactly 'Constructing Get-DscResource cache'
143+
}
144+
finally {
145+
$env:PSModulePath = $oldPath
146+
Copy-Item -Recurse -Force -Path "$PSScriptRoot/Backup/TestClassResource" -Destination "$PSScriptRoot"
147+
Remove-Item -Recurse -Force -Path "$PSScriptRoot/Backup"
148+
}
149+
}
150+
123151
It 'Verify inheritance works in class-based resources' {
124152

125153
$r = dsc resource list '*' -a Microsoft.DSC/PowerShell

powershell-adapter/psDscAdapter/psDscAdapter.psm1

+20-12
Original file line numberDiff line numberDiff line change
@@ -268,9 +268,15 @@ function Invoke-DscCacheRefresh {
268268

269269
$cacheEntry.LastWriteTimes.PSObject.Properties | ForEach-Object {
270270

271-
if (-not ((Get-Item $_.Name).LastWriteTime.Equals([DateTime]$_.Value)))
272-
{
273-
"Detected stale cache entry '$($_.Name)'" | Write-DscTrace
271+
if (Test-Path $_.Name) {
272+
if (-not ((Get-Item $_.Name).LastWriteTime.Equals([DateTime]$_.Value)))
273+
{
274+
"Detected stale cache entry '$($_.Name)'" | Write-DscTrace
275+
$refreshCache = $true
276+
break
277+
}
278+
} else {
279+
"Detected non-existent cache entry '$($_.Name)'" | Write-DscTrace
274280
$refreshCache = $true
275281
break
276282
}
@@ -279,19 +285,21 @@ function Invoke-DscCacheRefresh {
279285
if ($refreshCache) {break}
280286
}
281287

282-
"Checking cache for stale PSModulePath" | Write-DscTrace
288+
if (-not $refreshCache) {
289+
"Checking cache for stale PSModulePath" | Write-DscTrace
283290

284-
$m = $env:PSModulePath -split [IO.Path]::PathSeparator | %{Get-ChildItem -Directory -Path $_ -Depth 1 -ea SilentlyContinue}
291+
$m = $env:PSModulePath -split [IO.Path]::PathSeparator | %{Get-ChildItem -Directory -Path $_ -Depth 1 -ea SilentlyContinue}
285292

286-
$hs_cache = [System.Collections.Generic.HashSet[string]]($cache.PSModulePaths)
287-
$hs_live = [System.Collections.Generic.HashSet[string]]($m.FullName)
288-
$hs_cache.SymmetricExceptWith($hs_live)
289-
$diff = $hs_cache
293+
$hs_cache = [System.Collections.Generic.HashSet[string]]($cache.PSModulePaths)
294+
$hs_live = [System.Collections.Generic.HashSet[string]]($m.FullName)
295+
$hs_cache.SymmetricExceptWith($hs_live)
296+
$diff = $hs_cache
290297

291-
"PSModulePath diff '$diff'" | Write-DscTrace
298+
"PSModulePath diff '$diff'" | Write-DscTrace
292299

293-
if ($diff.Count -gt 0) {
294-
$refreshCache = $true
300+
if ($diff.Count -gt 0) {
301+
$refreshCache = $true
302+
}
295303
}
296304
}
297305
}

powershell-adapter/psDscAdapter/win_psDscAdapter.psm1

+20-12
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,15 @@ function Invoke-DscCacheRefresh {
9090

9191
$cacheEntry.LastWriteTimes.PSObject.Properties | ForEach-Object {
9292

93-
if (-not ((Get-Item $_.Name).LastWriteTime.Equals([DateTime]$_.Value)))
94-
{
95-
"Detected stale cache entry '$($_.Name)'" | Write-DscTrace
93+
if (Test-Path $_.Name) {
94+
if (-not ((Get-Item $_.Name).LastWriteTime.Equals([DateTime]$_.Value)))
95+
{
96+
"Detected stale cache entry '$($_.Name)'" | Write-DscTrace
97+
$refreshCache = $true
98+
break
99+
}
100+
} else {
101+
"Detected non-existent cache entry '$($_.Name)'" | Write-DscTrace
96102
$refreshCache = $true
97103
break
98104
}
@@ -101,19 +107,21 @@ function Invoke-DscCacheRefresh {
101107
if ($refreshCache) {break}
102108
}
103109

104-
"Checking cache for stale PSModulePath" | Write-DscTrace
110+
if (-not $refreshCache) {
111+
"Checking cache for stale PSModulePath" | Write-DscTrace
105112

106-
$m = $env:PSModulePath -split [IO.Path]::PathSeparator | %{Get-ChildItem -Directory -Path $_ -Depth 1 -ea SilentlyContinue}
113+
$m = $env:PSModulePath -split [IO.Path]::PathSeparator | %{Get-ChildItem -Directory -Path $_ -Depth 1 -ea SilentlyContinue}
107114

108-
$hs_cache = [System.Collections.Generic.HashSet[string]]($cache.PSModulePaths)
109-
$hs_live = [System.Collections.Generic.HashSet[string]]($m.FullName)
110-
$hs_cache.SymmetricExceptWith($hs_live)
111-
$diff = $hs_cache
115+
$hs_cache = [System.Collections.Generic.HashSet[string]]($cache.PSModulePaths)
116+
$hs_live = [System.Collections.Generic.HashSet[string]]($m.FullName)
117+
$hs_cache.SymmetricExceptWith($hs_live)
118+
$diff = $hs_cache
112119

113-
"PSModulePath diff '$diff'" | Write-DscTrace
120+
"PSModulePath diff '$diff'" | Write-DscTrace
114121

115-
if ($diff.Count -gt 0) {
116-
$refreshCache = $true
122+
if ($diff.Count -gt 0) {
123+
$refreshCache = $true
124+
}
117125
}
118126
}
119127
}

0 commit comments

Comments
 (0)