|
| 1 | +# Copyright (c) Microsoft Corporation. |
| 2 | +# Licensed under the MIT License. |
| 3 | + |
| 4 | +BeforeAll { |
| 5 | + $fakeManifest = @{ |
| 6 | + '$schema' = "https://aka.ms/dsc/schemas/v3/bundled/resource/manifest.json" |
| 7 | + type = "Test/FakeResource" |
| 8 | + version = "0.1.0" |
| 9 | + get = @{ |
| 10 | + executable = "fakeResource" |
| 11 | + args = @( |
| 12 | + "get", |
| 13 | + @{ |
| 14 | + jsonInputArg = "--input" |
| 15 | + mandatory = $true |
| 16 | + } |
| 17 | + ) |
| 18 | + } |
| 19 | + } |
| 20 | + |
| 21 | + $manifestPath = Join-Path $TestDrive "fake.dsc.resource.json" |
| 22 | + $fakeManifest | ConvertTo-Json -Depth 10 | Set-Content -Path $manifestPath |
| 23 | + $script:OldPSModulePath = $env:PSModulePath |
| 24 | + $env:PSModulePath += [System.IO.Path]::PathSeparator + $TestDrive |
| 25 | + |
| 26 | + $script:discoverScript = Join-Path $PSScriptRoot "powershell.discover.ps1" |
| 27 | + |
| 28 | + $cacheFilePath = if ($IsWindows) { |
| 29 | + Join-Path $env:LocalAppData "dsc\PowerShellDiscoverCache.json" |
| 30 | + } else { |
| 31 | + Join-Path $env:HOME ".dsc" "PowerShellDiscoverCache.json" |
| 32 | + } |
| 33 | + $script:cacheFilePath = $cacheFilePath |
| 34 | + |
| 35 | + Remove-Item -Force -ErrorAction SilentlyContinue -Path $script:cacheFilePath |
| 36 | +} |
| 37 | + |
| 38 | +AfterAll { |
| 39 | + $env:PSModulePath = $script:OldPSModulePath |
| 40 | +} |
| 41 | + |
| 42 | +Describe 'Tests for PowerShell resource discovery' { |
| 43 | + It 'Should create cache file on first run' { |
| 44 | + $script:cacheFilePath | Should -Not -Exist |
| 45 | + $null = & $script:discoverScript 2>&1 |
| 46 | + $script:cacheFilePath | Should -Exist |
| 47 | + |
| 48 | + $cache = Get-Content -Raw $script:cacheFilePath | ConvertFrom-Json |
| 49 | + $cache.PSModulePaths | Should -Not -BeNullOrEmpty |
| 50 | + $cache.PathInfo | Should -Not -BeNullOrEmpty |
| 51 | + $cache.Manifests | Should -Not -BeNullOrEmpty |
| 52 | + } |
| 53 | + |
| 54 | + It 'Should find DSC PowerShell resources' { |
| 55 | + $out = dsc resource list | ConvertFrom-Json |
| 56 | + $LASTEXITCODE | Should -Be 0 |
| 57 | + $out.manifest.type | Should -Contain 'Test/FakeResource' |
| 58 | + } |
| 59 | + |
| 60 | + It 'Should use cache on subsequent runs' { |
| 61 | + $null = & $script:discoverScript 2>&1 |
| 62 | + $script:cacheFilePath | Should -Exist |
| 63 | + |
| 64 | + $cacheLastWriteTime = (Get-Item $script:cacheFilePath).LastWriteTimeUtc |
| 65 | + |
| 66 | + Start-Sleep -Milliseconds 100 |
| 67 | + |
| 68 | + $null = & $script:discoverScript 2>&1 |
| 69 | + |
| 70 | + $newLastWriteTime = (Get-Item $script:cacheFilePath).LastWriteTimeUtc |
| 71 | + $newLastWriteTime | Should -Be $cacheLastWriteTime |
| 72 | + } |
| 73 | + |
| 74 | + It 'Should invalidate cache when PSModulePath changes' { |
| 75 | + $null = & $script:discoverScript 2>&1 |
| 76 | + $script:cacheFilePath | Should -Exist |
| 77 | + |
| 78 | + $cache = Get-Content -Raw $script:cacheFilePath | ConvertFrom-Json |
| 79 | + $originalPaths = $cache.PSModulePaths |
| 80 | + $cache.PSModulePaths = @($originalPaths[0]) # Remove some paths |
| 81 | + $cache | ConvertTo-Json -Depth 10 | Set-Content -Path $script:cacheFilePath -Force |
| 82 | + |
| 83 | + $cacheLastWriteTime = (Get-Item $script:cacheFilePath).LastWriteTimeUtc |
| 84 | + Start-Sleep -Milliseconds 100 |
| 85 | + |
| 86 | + $null = & $script:discoverScript 2>&1 |
| 87 | + |
| 88 | + $newLastWriteTime = (Get-Item $script:cacheFilePath).LastWriteTimeUtc |
| 89 | + $newLastWriteTime | Should -Not -Be $cacheLastWriteTime |
| 90 | + } |
| 91 | + |
| 92 | + It 'Should invalidate cache when module directory is modified' { |
| 93 | + $null = & $script:discoverScript 2>&1 |
| 94 | + $script:cacheFilePath | Should -Exist |
| 95 | + |
| 96 | + $cache = Get-Content -Raw $script:cacheFilePath | ConvertFrom-Json |
| 97 | + |
| 98 | + $firstPath = $cache.PathInfo.PSObject.Properties | Select-Object -First 1 |
| 99 | + if ($firstPath) { |
| 100 | + $oldTimestamp = [DateTime]$firstPath.Value |
| 101 | + $newTimestamp = $oldTimestamp.AddDays(-1) |
| 102 | + $cache.PathInfo.($firstPath.Name) = $newTimestamp |
| 103 | + $cache | ConvertTo-Json -Depth 10 | Set-Content -Path $script:cacheFilePath -Force |
| 104 | + |
| 105 | + $cacheLastWriteTime = (Get-Item $script:cacheFilePath).LastWriteTimeUtc |
| 106 | + Start-Sleep -Milliseconds 100 |
| 107 | + |
| 108 | + $null = & $script:discoverScript 2>&1 |
| 109 | + |
| 110 | + $newLastWriteTime = (Get-Item $script:cacheFilePath).LastWriteTimeUtc |
| 111 | + $newLastWriteTime | Should -Not -Be $cacheLastWriteTime |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + It 'Should rebuild cache if cache file is corrupted' { |
| 116 | + "{ invalid json }" | Set-Content -Path $script:cacheFilePath -Force |
| 117 | + $script:cacheFilePath | Should -Exist |
| 118 | + |
| 119 | + $null = & $script:discoverScript 2>&1 |
| 120 | + |
| 121 | + $cache = Get-Content -Raw $script:cacheFilePath | ConvertFrom-Json |
| 122 | + $cache.PSModulePaths | Should -Not -BeNullOrEmpty |
| 123 | + $cache.PathInfo | Should -Not -BeNullOrEmpty |
| 124 | + } |
| 125 | + |
| 126 | + It 'Should include test manifest in discovery results' { |
| 127 | + $out = & $script:discoverScript | ConvertFrom-Json |
| 128 | + $out.manifestPath | Should -Contain $manifestPath |
| 129 | + } |
| 130 | +} |
0 commit comments