Skip to content

Commit

Permalink
Merge pull request #497 from anmenaga/issue_495
Browse files Browse the repository at this point in the history
Fixed PSAdapter cache code when module files are missing
  • Loading branch information
SteveL-MSFT authored Jul 24, 2024
2 parents 2097285 + 0adb228 commit 05179d9
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 24 deletions.
28 changes: 28 additions & 0 deletions powershell-adapter/Tests/powershellgroup.resource.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,34 @@ Describe 'PowerShell adapter resource tests' {
"$TestDrive/tracing.txt" | Should -FileContentMatchExactly 'Incompatible version of cache in file'
}

It 'Verify that removing a module results in cache rebuid' {

Copy-Item -Recurse -Force -Path "$PSScriptRoot/TestClassResource" -Destination $TestDrive
Copy-Item -Recurse -Force -Path "$PSScriptRoot/TestClassResource" -Destination "$PSScriptRoot/Backup/TestClassResource"
Remove-Item -Recurse -Force -Path "$PSScriptRoot/TestClassResource"

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

# generate the cache
$null = dsc resource list '*' -a Microsoft.DSC/PowerShell
# remove the module files
Remove-Item -Recurse -Force -Path "$TestDrive/TestClassResource"
# verify that cache rebuid happened
dsc -l trace resource list '*' -a Microsoft.DSC/PowerShell 2> $TestDrive/tracing.txt

$LASTEXITCODE | Should -Be 0
"$TestDrive/tracing.txt" | Should -FileContentMatchExactly 'Detected non-existent cache entry'
"$TestDrive/tracing.txt" | Should -FileContentMatchExactly 'Constructing Get-DscResource cache'
}
finally {
$env:PSModulePath = $oldPath
Copy-Item -Recurse -Force -Path "$PSScriptRoot/Backup/TestClassResource" -Destination "$PSScriptRoot"
Remove-Item -Recurse -Force -Path "$PSScriptRoot/Backup"
}
}

It 'Verify inheritance works in class-based resources' {

$r = dsc resource list '*' -a Microsoft.DSC/PowerShell
Expand Down
32 changes: 20 additions & 12 deletions powershell-adapter/psDscAdapter/psDscAdapter.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -268,9 +268,15 @@ function Invoke-DscCacheRefresh {

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

if (-not ((Get-Item $_.Name).LastWriteTime.Equals([DateTime]$_.Value)))
{
"Detected stale cache entry '$($_.Name)'" | Write-DscTrace
if (Test-Path $_.Name) {
if (-not ((Get-Item $_.Name).LastWriteTime.Equals([DateTime]$_.Value)))
{
"Detected stale cache entry '$($_.Name)'" | Write-DscTrace
$refreshCache = $true
break
}
} else {
"Detected non-existent cache entry '$($_.Name)'" | Write-DscTrace
$refreshCache = $true
break
}
Expand All @@ -279,19 +285,21 @@ function Invoke-DscCacheRefresh {
if ($refreshCache) {break}
}

"Checking cache for stale PSModulePath" | Write-DscTrace
if (-not $refreshCache) {
"Checking cache for stale PSModulePath" | Write-DscTrace

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

$hs_cache = [System.Collections.Generic.HashSet[string]]($cache.PSModulePaths)
$hs_live = [System.Collections.Generic.HashSet[string]]($m.FullName)
$hs_cache.SymmetricExceptWith($hs_live)
$diff = $hs_cache
$hs_cache = [System.Collections.Generic.HashSet[string]]($cache.PSModulePaths)
$hs_live = [System.Collections.Generic.HashSet[string]]($m.FullName)
$hs_cache.SymmetricExceptWith($hs_live)
$diff = $hs_cache

"PSModulePath diff '$diff'" | Write-DscTrace
"PSModulePath diff '$diff'" | Write-DscTrace

if ($diff.Count -gt 0) {
$refreshCache = $true
if ($diff.Count -gt 0) {
$refreshCache = $true
}
}
}
}
Expand Down
32 changes: 20 additions & 12 deletions powershell-adapter/psDscAdapter/win_psDscAdapter.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,15 @@ function Invoke-DscCacheRefresh {

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

if (-not ((Get-Item $_.Name).LastWriteTime.Equals([DateTime]$_.Value)))
{
"Detected stale cache entry '$($_.Name)'" | Write-DscTrace
if (Test-Path $_.Name) {
if (-not ((Get-Item $_.Name).LastWriteTime.Equals([DateTime]$_.Value)))
{
"Detected stale cache entry '$($_.Name)'" | Write-DscTrace
$refreshCache = $true
break
}
} else {
"Detected non-existent cache entry '$($_.Name)'" | Write-DscTrace
$refreshCache = $true
break
}
Expand All @@ -101,19 +107,21 @@ function Invoke-DscCacheRefresh {
if ($refreshCache) {break}
}

"Checking cache for stale PSModulePath" | Write-DscTrace
if (-not $refreshCache) {
"Checking cache for stale PSModulePath" | Write-DscTrace

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

$hs_cache = [System.Collections.Generic.HashSet[string]]($cache.PSModulePaths)
$hs_live = [System.Collections.Generic.HashSet[string]]($m.FullName)
$hs_cache.SymmetricExceptWith($hs_live)
$diff = $hs_cache
$hs_cache = [System.Collections.Generic.HashSet[string]]($cache.PSModulePaths)
$hs_live = [System.Collections.Generic.HashSet[string]]($m.FullName)
$hs_cache.SymmetricExceptWith($hs_live)
$diff = $hs_cache

"PSModulePath diff '$diff'" | Write-DscTrace
"PSModulePath diff '$diff'" | Write-DscTrace

if ($diff.Count -gt 0) {
$refreshCache = $true
if ($diff.Count -gt 0) {
$refreshCache = $true
}
}
}
}
Expand Down

0 comments on commit 05179d9

Please sign in to comment.