Skip to content

Fix DateTime comparison in PSAdapter cache code #533

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Sep 4, 2024
27 changes: 27 additions & 0 deletions powershell-adapter/Tests/powershellgroup.resource.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -280,4 +280,31 @@ Describe 'PowerShell adapter resource tests' {

$env:TestClassResourceResultCount = $null
}

It 'Verify that there are no cache rebuilds for several sequential executions' {

# remove cache file
$cacheFilePath = if ($IsWindows) {
# PS 6+ on Windows
Join-Path $env:LocalAppData "dsc\PSAdapterCache.json"
} else {
# either WinPS or PS 6+ on Linux/Mac
if ($PSVersionTable.PSVersion.Major -le 5) {
Join-Path $env:LocalAppData "dsc\WindowsPSAdapterCache.json"
} else {
Join-Path $env:HOME ".dsc" "PSAdapterCache.json"
}
}
Remove-Item -Force -Path $cacheFilePath -ErrorAction Ignore

# first execution should build the cache
dsc -l trace resource list -a Microsoft.DSC/PowerShell 2> $TestDrive/tracing.txt
"$TestDrive/tracing.txt" | Should -FileContentMatchExactly 'Constructing Get-DscResource cache'

# next executions following shortly after should Not rebuild the cache
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'
}
}
}
17 changes: 17 additions & 0 deletions powershell-adapter/Tests/win_powershellgroup.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,21 @@ Describe 'WindowsPowerShell adapter resource tests' {
$res = $r | ConvertFrom-Json
$res.results[0].result.actualState.result[0].properties.DestinationPath | Should -Be "$testFile"
}

It 'Verify that there are no cache rebuilds for several sequential executions' -Skip:(!$IsWindows) {

# remove cache file
$cacheFilePath = Join-Path $env:LocalAppData "dsc\WindowsPSAdapterCache.json"
Remove-Item -Force -Path $cacheFilePath -ErrorAction Ignore

# first execution should build the cache
dsc -l trace resource list -a Microsoft.Windows/WindowsPowerShell 2> $TestDrive/tracing.txt
"$TestDrive/tracing.txt" | Should -FileContentMatchExactly 'Constructing Get-DscResource cache'

# next executions following shortly after should Not rebuild the cache
1..3 | %{
dsc -l trace resource list -a Microsoft.Windows/WindowsPowerShell 2> $TestDrive/tracing.txt
"$TestDrive/tracing.txt" | Should -Not -FileContentMatchExactly 'Constructing Get-DscResource cache'
}
}
}
11 changes: 9 additions & 2 deletions powershell-adapter/psDscAdapter/psDscAdapter.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -264,12 +264,19 @@ function Invoke-DscCacheRefresh {
"Checking cache for stale entries" | Write-DscTrace

foreach ($cacheEntry in $dscResourceCacheEntries) {
#"Checking cache entry '$($cacheEntry.Type) $($cacheEntry.LastWriteTimes)'" | Write-DscTrace -Operation Trace

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

if (Test-Path $_.Name) {
if (-not ((Get-Item $_.Name).LastWriteTime.Equals([DateTime]$_.Value)))
$file_LastWriteTime = (Get-Item $_.Name).LastWriteTime
# Truncate DateTime to seconds
$file_LastWriteTime = $file_LastWriteTime.AddTicks( - ($file_LastWriteTime.Ticks % [TimeSpan]::TicksPerSecond));

$cache_LastWriteTime = [DateTime]$_.Value
# Truncate DateTime to seconds
$cache_LastWriteTime = $cache_LastWriteTime.AddTicks( - ($cache_LastWriteTime.Ticks % [TimeSpan]::TicksPerSecond));

if (-not ($file_LastWriteTime.Equals($cache_LastWriteTime)))
{
"Detected stale cache entry '$($_.Name)'" | Write-DscTrace
$refreshCache = $true
Expand Down
11 changes: 9 additions & 2 deletions powershell-adapter/psDscAdapter/win_psDscAdapter.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,19 @@ function Invoke-DscCacheRefresh {
"Checking cache for stale entries" | Write-DscTrace

foreach ($cacheEntry in $dscResourceCacheEntries) {
#"Checking cache entry '$($cacheEntry.Type) $($cacheEntry.LastWriteTimes)'" | Write-DscTrace -Operation Trace

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

if (Test-Path $_.Name) {
if (-not ((Get-Item $_.Name).LastWriteTime.Equals([DateTime]$_.Value)))
$file_LastWriteTime = (Get-Item $_.Name).LastWriteTimeUtc
# Truncate DateTime to seconds
$file_LastWriteTime = $file_LastWriteTime.AddTicks( - ($file_LastWriteTime.Ticks % [TimeSpan]::TicksPerSecond));

$cache_LastWriteTime = [DateTime]$_.Value
# Truncate DateTime to seconds
$cache_LastWriteTime = $cache_LastWriteTime.AddTicks( - ($cache_LastWriteTime.Ticks % [TimeSpan]::TicksPerSecond));

if (-not ($file_LastWriteTime.Equals($cache_LastWriteTime)))
{
"Detected stale cache entry '$($_.Name)'" | Write-DscTrace
$refreshCache = $true
Expand Down
Loading