|
| 1 | +# Copyright (c) Microsoft Corporation. |
| 2 | +# Licensed under the MIT License. |
| 3 | + |
| 4 | +BeforeDiscovery { |
| 5 | + if ($IsWindows) { |
| 6 | + $identity = [System.Security.Principal.WindowsIdentity]::GetCurrent() |
| 7 | + $principal = [System.Security.Principal.WindowsPrincipal]::new($identity) |
| 8 | + $isElevated = $principal.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator) |
| 9 | + } |
| 10 | +} |
| 11 | + |
| 12 | +Describe 'WindowsPowerShell adapter resource tests - requires elevated permissions' -Skip:(!$IsWindows -or !$isElevated) { |
| 13 | + |
| 14 | + BeforeAll { |
| 15 | + $OldPSModulePath = $env:PSModulePath |
| 16 | + $dscHome = Split-Path (Get-Command dsc -ErrorAction Stop).Source -Parent |
| 17 | + $psexeHome = Split-Path (Get-Command powershell -ErrorAction Stop).Source -Parent |
| 18 | + $ps7exeHome = Split-Path (Get-Command pwsh -ErrorAction Stop).Source -Parent |
| 19 | + $env:DSC_RESOURCE_PATH = $dscHome + [System.IO.Path]::PathSeparator + $psexeHome + [System.IO.Path]::PathSeparator + $ps7exeHome |
| 20 | + $null = winrm quickconfig -quiet -force 2>&1 |
| 21 | + $env:PSModulePath = $PSScriptRoot + [System.IO.Path]::PathSeparator + $env:PSModulePath |
| 22 | + |
| 23 | + $winpsConfigPath = Join-path $PSScriptRoot "winps_resource.dsc.yaml" |
| 24 | + $cacheFilePath_v5 = Join-Path $env:LocalAppData "dsc" "WindowsPSAdapterCache.json" |
| 25 | + |
| 26 | + $script:winPSModule = Resolve-Path -Path (Join-Path $PSScriptRoot '..' 'psDscAdapter' 'win_psDscAdapter.psm1') | Select-Object -ExpandProperty Path |
| 27 | + Import-Module $winPSModule -Force -ErrorAction Stop |
| 28 | + } |
| 29 | + |
| 30 | + AfterAll { |
| 31 | + $env:PSModulePath = $OldPSModulePath |
| 32 | + $env:DSC_RESOURCE_PATH = $null |
| 33 | + |
| 34 | + # Remove after all the tests are done |
| 35 | + Remove-Module $script:winPSModule -Force -ErrorAction Ignore |
| 36 | + } |
| 37 | + |
| 38 | + BeforeEach { |
| 39 | + Remove-Item -Force -ea SilentlyContinue -Path $cacheFilePath_v5 |
| 40 | + } |
| 41 | + |
| 42 | + It 'Windows PowerShell adapter supports File resource' { |
| 43 | + |
| 44 | + $r = dsc resource list --adapter Microsoft.Windows/WindowsPowerShell |
| 45 | + $LASTEXITCODE | Should -Be 0 |
| 46 | + $resources = $r | ConvertFrom-Json |
| 47 | + ($resources | Where-Object { $_.Type -eq 'PSDesiredStateConfiguration/File' }).Count | Should -Be 1 |
| 48 | + } |
| 49 | + |
| 50 | + It 'Get works on Binary "File" resource' { |
| 51 | + |
| 52 | + $testFile = "$testdrive\test.txt" |
| 53 | + 'test' | Set-Content -Path $testFile -Force |
| 54 | + $r = '{"DestinationPath":"' + $testFile.replace('\', '\\') + '"}' | dsc resource get -r 'PSDesiredStateConfiguration/File' -f - |
| 55 | + $LASTEXITCODE | Should -Be 0 |
| 56 | + $res = $r | ConvertFrom-Json |
| 57 | + $res.actualState.DestinationPath | Should -Be "$testFile" |
| 58 | + } |
| 59 | + |
| 60 | + It 'Set works on Binary "File" resource' { |
| 61 | + |
| 62 | + $testFile = "$testdrive\test.txt" |
| 63 | + $null = '{"DestinationPath":"' + $testFile.replace('\', '\\') + '", type: File, contents: HelloWorld, Ensure: present}' | dsc resource set -r 'PSDesiredStateConfiguration/File' -f - |
| 64 | + $LASTEXITCODE | Should -Be 0 |
| 65 | + Get-Content -Raw -Path $testFile | Should -Be "HelloWorld" |
| 66 | + } |
| 67 | + |
| 68 | + It 'Get works on traditional "Script" resource' { |
| 69 | + |
| 70 | + $testFile = "$testdrive\test.txt" |
| 71 | + 'test' | Set-Content -Path $testFile -Force |
| 72 | + $r = '{"GetScript": "@{result = $(Get-Content ' + $testFile.replace('\', '\\') + ')}", "SetScript": "throw", "TestScript": "throw"}' | dsc resource get -r 'PSDesiredStateConfiguration/Script' -f - |
| 73 | + $LASTEXITCODE | Should -Be 0 |
| 74 | + $res = $r | ConvertFrom-Json |
| 75 | + $res.actualState.result | Should -Be 'test' |
| 76 | + } |
| 77 | + |
| 78 | + It 'Get works on config with File resource for WinPS' { |
| 79 | + |
| 80 | + $testFile = "$testdrive\test.txt" |
| 81 | + 'test' | Set-Content -Path $testFile -Force |
| 82 | + $r = (Get-Content -Raw $winpsConfigPath).Replace('c:\test.txt', "$testFile") | dsc config get -f - |
| 83 | + $LASTEXITCODE | Should -Be 0 |
| 84 | + $res = $r | ConvertFrom-Json |
| 85 | + $res.results[0].result.actualState.result[0].properties.DestinationPath | Should -Be "$testFile" |
| 86 | + } |
| 87 | + |
| 88 | + It 'Verify that there are no cache rebuilds for several sequential executions' { |
| 89 | + # first execution should build the cache |
| 90 | + $null = dsc -l trace resource list -a Microsoft.Windows/WindowsPowerShell 2> $TestDrive/tracing.txt |
| 91 | + "$TestDrive/tracing.txt" | Should -FileContentMatchExactly 'Constructing Get-DscResource cache' |
| 92 | + |
| 93 | + # next executions following shortly after should Not rebuild the cache |
| 94 | + 1..3 | ForEach-Object { |
| 95 | + $null = dsc -l trace resource list -a Microsoft.Windows/WindowsPowerShell 2> $TestDrive/tracing.txt |
| 96 | + "$TestDrive/tracing.txt" | Should -Not -FileContentMatchExactly 'Constructing Get-DscResource cache' |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + It 'Verify if assertion is used that no module is cleared in the cache' { |
| 101 | + # create a test file in the test drive |
| 102 | + $testFile = "$testdrive\test.txt" |
| 103 | + New-Item -Path $testFile -ItemType File -Force | Out-Null |
| 104 | + |
| 105 | + # build the cache |
| 106 | + dsc resource list --adapter Microsoft.Windows/WindowsPowerShell | Out-Null |
| 107 | + |
| 108 | + # Create a test module in the test drive |
| 109 | + $testModuleDir = "$testdrive\TestModule\1.0.0" |
| 110 | + New-Item -Path $testModuleDir -ItemType Directory -Force | Out-Null |
| 111 | + |
| 112 | + $manifestContent = @" |
| 113 | + @{ |
| 114 | + RootModule = 'TestModule.psm1' |
| 115 | + ModuleVersion = '1.0.0' |
| 116 | + GUID = '$([guid]::NewGuid().Guid)' |
| 117 | + Author = 'Microsoft Corporation' |
| 118 | + CompanyName = 'Microsoft Corporation' |
| 119 | + Copyright = '(c) Microsoft Corporation. All rights reserved.' |
| 120 | + Description = 'Test module for DSC tests' |
| 121 | + PowerShellVersion = '5.1' |
| 122 | + DscResourcesToExport = @() |
| 123 | + FunctionsToExport = @() |
| 124 | + CmdletsToExport = @() |
| 125 | + VariablesToExport = @() |
| 126 | + AliasesToExport = @() |
| 127 | + } |
| 128 | +"@ |
| 129 | + Set-Content -Path "$testModuleDir\TestModule.psd1" -Value $manifestContent |
| 130 | + |
| 131 | + $scriptContent = @" |
| 132 | +Write-Host 'The DSC world!' |
| 133 | +"@ |
| 134 | + Set-Content -Path "$testModuleDir\TestModule.psm1" -Value $scriptContent |
| 135 | + |
| 136 | + # Add the test module directory to PSModulePath |
| 137 | + $env:PSModulePath = $testdrive + [System.IO.Path]::PathSeparator + $env:PSModulePath |
| 138 | + |
| 139 | + $yaml = @" |
| 140 | +`$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json |
| 141 | +resources: |
| 142 | + - name: File |
| 143 | + type: Microsoft.Windows/WindowsPowerShell |
| 144 | + properties: |
| 145 | + resources: |
| 146 | + - name: File |
| 147 | + type: PSDesiredStateConfiguration/File |
| 148 | + properties: |
| 149 | + DestinationPath: $testfile |
| 150 | + - name: File present |
| 151 | + type: Microsoft.DSC/Assertion |
| 152 | + properties: |
| 153 | + `$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json |
| 154 | + resources: |
| 155 | + - name: Use powershell adapter |
| 156 | + type: Microsoft.Windows/WindowsPowerShell |
| 157 | + properties: |
| 158 | + resources: |
| 159 | + - name: File present |
| 160 | + type: PSDesiredStateConfiguration/File |
| 161 | + properties: |
| 162 | + DestinationPath: $testFile |
| 163 | + dependsOn: |
| 164 | + - "[resourceId('Microsoft.Windows/WindowsPowerShell', 'File')]" |
| 165 | + - name: TestPSRepository |
| 166 | + type: PSTestModule/TestPSRepository |
| 167 | + properties: |
| 168 | + Name: NuGet |
| 169 | + dependsOn: |
| 170 | + - "[resourceId('Microsoft.Windows/WindowsPowerShell', 'File')]" |
| 171 | + - "[resourceId('Microsoft.DSC/Assertion', 'File present')]" |
| 172 | +"@ |
| 173 | + # output to file for Windows PowerShell 5.1 |
| 174 | + $filePath = "$testdrive\test.assertion.dsc.resource.yaml" |
| 175 | + $yaml | Set-Content -Path $filePath -Force |
| 176 | + dsc config test -f $filePath 2> "$TestDrive/error.txt" |
| 177 | + $LASTEXITCODE | Should -Be 2 |
| 178 | + |
| 179 | + $cache = Get-Content -Path $cacheFilePath_v5 -Raw | ConvertFrom-Json |
| 180 | + $cache.ResourceCache.Type | Should -Contain 'PSTestModule/TestPSRepository' |
| 181 | + $cache.ResourceCache.Type | Should -Contain 'PSDesiredStateConfiguration/File' |
| 182 | + } |
| 183 | +} |
0 commit comments