Skip to content

Commit ec2a716

Browse files
author
Andrew
committed
Code 1
1 parent 861bdf1 commit ec2a716

File tree

4 files changed

+137
-62
lines changed

4 files changed

+137
-62
lines changed

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,42 @@ Describe 'PowerShell adapter resource tests' {
8181
$res.actualState.result.count | Should -Be 5
8282
$res.actualState.result| % {$_.Name | Should -Not -BeNullOrEmpty}
8383
}
84+
85+
It 'Verify that ClearCache works in PSAdapter' {
86+
# generate the cache
87+
$null = dsc resource list * -a Microsoft.DSC/PowerShell
88+
# call the ClearCache operation
89+
$scriptPath = Join-Path $PSScriptRoot '..' 'psDscAdapter' 'powershell.resource.ps1'
90+
$null = & $scriptPath -Operation ClearCache 2
91+
# verify that PSAdapter does not find the cache
92+
dsc -l debug resource list * -a Microsoft.DSC/PowerShell 2> $TestDrive/tracing.txt
93+
$LASTEXITCODE | Should -Be 0
94+
"$TestDrive/tracing.txt" | Should -FileContentMatchExactly 'Cache file not found'
95+
}
96+
97+
It 'Verify that a new PS Cache version results in cache rebuid' {
98+
# generate the cache
99+
$null = dsc resource list * -a Microsoft.DSC/PowerShell
100+
# update the version in the cache file
101+
$cacheFilePath = if ($IsWindows) {
102+
# PS 6+ on Windows
103+
Join-Path $env:LocalAppData "dsc\PSAdapterCache.json"
104+
} else {
105+
# either WinPS or PS 6+ on Linux/Mac
106+
if ($PSVersionTable.PSVersion.Major -le 5) {
107+
Join-Path $env:LocalAppData "dsc\WindowsPSAdapterCache.json"
108+
} else {
109+
Join-Path $env:HOME ".dsc" "PSAdapterCache.json"
110+
}
111+
}
112+
$cache = Get-Content -Raw $cacheFilePath | ConvertFrom-Json
113+
$cache.CacheSchemaVersion = 0
114+
$jsonCache = $cache | ConvertTo-Json -Depth 90
115+
New-Item -Force -Path $cacheFilePath -Value $jsonCache -Type File | Out-Null
116+
117+
# verify that a new PS Cache version results in cache rebuid
118+
dsc -l debug resource list * -a Microsoft.DSC/PowerShell 2> $TestDrive/tracing.txt
119+
$LASTEXITCODE | Should -Be 0
120+
"$TestDrive/tracing.txt" | Should -FileContentMatchExactly 'Incompartible version of cache in file'
121+
}
84122
}

powershell-adapter/psDscAdapter/powershell.resource.ps1

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[CmdletBinding()]
44
param(
55
[Parameter(Mandatory = $true, Position = 0, HelpMessage = 'Operation to perform. Choose from List, Get, Set, Test, Export, Validate.')]
6-
[ValidateSet('List', 'Get', 'Set', 'Test', 'Export', 'Validate')]
6+
[ValidateSet('List', 'Get', 'Set', 'Test', 'Export', 'Validate', 'ClearCache')]
77
[string]$Operation,
88
[Parameter(Mandatory = $false, Position = 1, ValueFromPipeline = $true, HelpMessage = 'Configuration or resource input in JSON format.')]
99
[string]$jsonInput = '@{}'
@@ -27,6 +27,24 @@ function Write-DscTrace {
2727
'PSPath=' + $PSHome | Write-DscTrace
2828
'PSModulePath=' + $env:PSModulePath | Write-DscTrace
2929

30+
if ($Operation -eq 'ClearCache') {
31+
$cacheFilePath = if ($IsWindows) {
32+
# PS 6+ on Windows
33+
Join-Path $env:LocalAppData "dsc\PSAdapterCache.json"
34+
} else {
35+
# either WinPS or PS 6+ on Linux/Mac
36+
if ($PSVersionTable.PSVersion.Major -le 5) {
37+
Join-Path $env:LocalAppData "dsc\WindowsPSAdapterCache.json"
38+
} else {
39+
Join-Path $env:HOME ".dsc" "PSAdapterCache.json"
40+
}
41+
}
42+
43+
'Deleting cache file ' + $cacheFilePath | Write-DscTrace
44+
Remove-Item -Force -ea SilentlyContinue -Path $cacheFilePath
45+
exit 0
46+
}
47+
3048
if ('Validate' -ne $Operation) {
3149
# write $jsonInput to STDERR for debugging
3250
$trace = @{'Debug' = 'jsonInput=' + $jsonInput } | ConvertTo-Json -Compress

powershell-adapter/psDscAdapter/psDscAdapter.psm1

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Copyright (c) Microsoft Corporation.
22
# Licensed under the MIT License.
33

4+
$script:CurrentCacheSchemaVersion = 1
5+
46
function Write-DscTrace {
57
param(
68
[Parameter(Mandatory = $false)]
@@ -230,47 +232,53 @@ function Invoke-DscCacheRefresh {
230232
"Reading from Get-DscResource cache file $cacheFilePath" | Write-DscTrace
231233

232234
$cache = Get-Content -Raw $cacheFilePath | ConvertFrom-Json
233-
$dscResourceCacheEntries = $cache.ResourceCache
234235

235-
if ($dscResourceCacheEntries.Count -eq 0) {
236-
# if there is nothing in the cache file - refresh cache
236+
if ($cache.CacheSchemaVersion -ne $script:CurrentCacheSchemaVersion) {
237237
$refreshCache = $true
238+
"Incompartible version of cache in file '"+$cache.CacheSchemaVersion+"' (expected '"+$script:CurrentCacheSchemaVersion+"')" | Write-DscTrace
239+
} else {
240+
$dscResourceCacheEntries = $cache.ResourceCache
238241

239-
"Filtered DscResourceCache cache is empty" | Write-DscTrace
240-
}
241-
else
242-
{
243-
"Checking cache for stale entries" | Write-DscTrace
242+
if ($dscResourceCacheEntries.Count -eq 0) {
243+
# if there is nothing in the cache file - refresh cache
244+
$refreshCache = $true
244245

245-
foreach ($cacheEntry in $dscResourceCacheEntries) {
246-
"Checking cache entry '$($cacheEntry.Type) $($cacheEntry.LastWriteTimes)'" | Write-DscTrace -Operation Trace
246+
"Filtered DscResourceCache cache is empty" | Write-DscTrace
247+
}
248+
else
249+
{
250+
"Checking cache for stale entries" | Write-DscTrace
247251

248-
$cacheEntry.LastWriteTimes.PSObject.Properties | ForEach-Object {
249-
250-
if (-not ((Get-Item $_.Name).LastWriteTime.Equals([DateTime]$_.Value)))
251-
{
252-
"Detected stale cache entry '$($_.Name)'" | Write-DscTrace
253-
$refreshCache = $true
254-
break
252+
foreach ($cacheEntry in $dscResourceCacheEntries) {
253+
"Checking cache entry '$($cacheEntry.Type) $($cacheEntry.LastWriteTimes)'" | Write-DscTrace -Operation Trace
254+
255+
$cacheEntry.LastWriteTimes.PSObject.Properties | ForEach-Object {
256+
257+
if (-not ((Get-Item $_.Name).LastWriteTime.Equals([DateTime]$_.Value)))
258+
{
259+
"Detected stale cache entry '$($_.Name)'" | Write-DscTrace
260+
$refreshCache = $true
261+
break
262+
}
255263
}
256-
}
257264

258-
if ($refreshCache) {break}
259-
}
265+
if ($refreshCache) {break}
266+
}
260267

261-
"Checking cache for stale PSModulePath" | Write-DscTrace
268+
"Checking cache for stale PSModulePath" | Write-DscTrace
262269

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

265-
$hs_cache = [System.Collections.Generic.HashSet[string]]($cache.PSModulePaths)
266-
$hs_live = [System.Collections.Generic.HashSet[string]]($m.FullName)
267-
$hs_cache.SymmetricExceptWith($hs_live)
268-
$diff = $hs_cache
272+
$hs_cache = [System.Collections.Generic.HashSet[string]]($cache.PSModulePaths)
273+
$hs_live = [System.Collections.Generic.HashSet[string]]($m.FullName)
274+
$hs_cache.SymmetricExceptWith($hs_live)
275+
$diff = $hs_cache
269276

270-
"PSModulePath diff '$diff'" | Write-DscTrace
277+
"PSModulePath diff '$diff'" | Write-DscTrace
271278

272-
if ($diff.Count -gt 0) {
273-
$refreshCache = $true
279+
if ($diff.Count -gt 0) {
280+
$refreshCache = $true
281+
}
274282
}
275283
}
276284
}
@@ -318,6 +326,7 @@ function Invoke-DscCacheRefresh {
318326
$cache.ResourceCache = $dscResourceCacheEntries
319327
$m = $env:PSModulePath -split [IO.Path]::PathSeparator | %{Get-ChildItem -Directory -Path $_ -Depth 1 -ea SilentlyContinue}
320328
$cache.PSModulePaths = $m.FullName
329+
$cache.CacheSchemaVersion = $script:CurrentCacheSchemaVersion
321330

322331
# save cache for future use
323332
# TODO: replace this with a high-performance serializer
@@ -482,6 +491,7 @@ class dscResourceCacheEntry {
482491
}
483492

484493
class dscResourceCache {
494+
[int] $CacheSchemaVersion
485495
[string[]] $PSModulePaths
486496
[dscResourceCacheEntry[]] $ResourceCache
487497
}

powershell-adapter/psDscAdapter/win_psDscAdapter.psm1

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Copyright (c) Microsoft Corporation.
22
# Licensed under the MIT License.
33

4+
$script:CurrentCacheSchemaVersion = 1
5+
46
function Write-DscTrace {
57
param(
68
[Parameter(Mandatory = $false)]
@@ -68,47 +70,52 @@ function Invoke-DscCacheRefresh {
6870
"Reading from Get-DscResource cache file $cacheFilePath" | Write-DscTrace
6971

7072
$cache = Get-Content -Raw $cacheFilePath | ConvertFrom-Json
71-
$dscResourceCacheEntries = $cache.ResourceCache
72-
73-
if ($dscResourceCacheEntries.Count -eq 0) {
74-
# if there is nothing in the cache file - refresh cache
73+
if ($cache.CacheSchemaVersion -ne $script:CurrentCacheSchemaVersion) {
7574
$refreshCache = $true
75+
"Incompartible version of cache in file '"+$cache.CacheSchemaVersion+"' (expected '"+$script:CurrentCacheSchemaVersion+"')" | Write-DscTrace
76+
} else {
77+
$dscResourceCacheEntries = $cache.ResourceCache
7678

77-
"Filtered DscResourceCache cache is empty" | Write-DscTrace
78-
}
79-
else
80-
{
81-
"Checking cache for stale entries" | Write-DscTrace
82-
83-
foreach ($cacheEntry in $dscResourceCacheEntries) {
84-
"Checking cache entry '$($cacheEntry.Type) $($cacheEntry.LastWriteTimes)'" | Write-DscTrace -Operation Trace
85-
86-
$cacheEntry.LastWriteTimes.PSObject.Properties | ForEach-Object {
87-
88-
if (-not ((Get-Item $_.Name).LastWriteTime.Equals([DateTime]$_.Value)))
89-
{
90-
"Detected stale cache entry '$($_.Name)'" | Write-DscTrace
91-
$refreshCache = $true
92-
break
93-
}
94-
}
79+
if ($dscResourceCacheEntries.Count -eq 0) {
80+
# if there is nothing in the cache file - refresh cache
81+
$refreshCache = $true
9582

96-
if ($refreshCache) {break}
83+
"Filtered DscResourceCache cache is empty" | Write-DscTrace
9784
}
85+
else
86+
{
87+
"Checking cache for stale entries" | Write-DscTrace
88+
89+
foreach ($cacheEntry in $dscResourceCacheEntries) {
90+
"Checking cache entry '$($cacheEntry.Type) $($cacheEntry.LastWriteTimes)'" | Write-DscTrace -Operation Trace
9891

99-
"Checking cache for stale PSModulePath" | Write-DscTrace
92+
$cacheEntry.LastWriteTimes.PSObject.Properties | ForEach-Object {
93+
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+
}
101+
102+
if ($refreshCache) {break}
103+
}
100104

101-
$m = $env:PSModulePath -split [IO.Path]::PathSeparator | %{Get-ChildItem -Directory -Path $_ -Depth 1 -ea SilentlyContinue}
105+
"Checking cache for stale PSModulePath" | Write-DscTrace
102106

103-
$hs_cache = [System.Collections.Generic.HashSet[string]]($cache.PSModulePaths)
104-
$hs_live = [System.Collections.Generic.HashSet[string]]($m.FullName)
105-
$hs_cache.SymmetricExceptWith($hs_live)
106-
$diff = $hs_cache
107+
$m = $env:PSModulePath -split [IO.Path]::PathSeparator | %{Get-ChildItem -Directory -Path $_ -Depth 1 -ea SilentlyContinue}
107108

108-
"PSModulePath diff '$diff'" | Write-DscTrace
109+
$hs_cache = [System.Collections.Generic.HashSet[string]]($cache.PSModulePaths)
110+
$hs_live = [System.Collections.Generic.HashSet[string]]($m.FullName)
111+
$hs_cache.SymmetricExceptWith($hs_live)
112+
$diff = $hs_cache
109113

110-
if ($diff.Count -gt 0) {
111-
$refreshCache = $true
114+
"PSModulePath diff '$diff'" | Write-DscTrace
115+
116+
if ($diff.Count -gt 0) {
117+
$refreshCache = $true
118+
}
112119
}
113120
}
114121
}
@@ -223,6 +230,7 @@ function Invoke-DscCacheRefresh {
223230
$cache.ResourceCache = $dscResourceCacheEntries
224231
$m = $env:PSModulePath -split [IO.Path]::PathSeparator | %{Get-ChildItem -Directory -Path $_ -Depth 1 -ea SilentlyContinue}
225232
$cache.PSModulePaths = $m.FullName
233+
$cache.CacheSchemaVersion = $script:CurrentCacheSchemaVersion
226234

227235
# save cache for future use
228236
# TODO: replace this with a high-performance serializer
@@ -472,6 +480,7 @@ class dscResourceCacheEntry {
472480
}
473481

474482
class dscResourceCache {
483+
[int] $CacheSchemaVersion
475484
[string[]] $PSModulePaths
476485
[dscResourceCacheEntry[]] $ResourceCache
477486
}

0 commit comments

Comments
 (0)