Skip to content

Commit

Permalink
Merge pull request #533 from anmenaga/date_fix
Browse files Browse the repository at this point in the history
Fix DateTime comparison in PSAdapter cache code
  • Loading branch information
SteveL-MSFT authored Sep 4, 2024
2 parents 2ef6308 + 2b72deb commit f047b42
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 4 deletions.
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

0 comments on commit f047b42

Please sign in to comment.