File size: 10,906 Bytes
8c763fb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 | # Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
Describe "DSC PowerShell Profile Resource Tests" -Tag "CI" {
BeforeAll {
$DSC_ROOT = $env:DSC_ROOT
$skipCleanup = $false
if (-not (Test-Path -Path $DSC_ROOT)) {
$skipCleanup = $true
throw "DSC_ROOT environment variable is not set or path does not exist."
}
Write-Verbose "DSC_ROOT is set to $DSC_ROOT" -Verbose
$originalPath = $env:PATH
$pathSeparator = [System.IO.Path]::PathSeparator
$env:PATH += "$pathSeparator$DSC_ROOT"
$env:PATH += "$pathSeparator$PSHome"
Write-Verbose "Updated PATH to include DSC_ROOT: $env:PATH" -Verbose
# Ensure DSC v3 is available
if (-not (Get-Command -name dsc -CommandType Application -ErrorAction SilentlyContinue)) {
Get-ChildItem $DSC_ROOT -Recurse 'dsc' | ForEach-Object {
Write-Verbose "Found DSC executable at $($_.FullName)" -Verbose
}
throw "DSC v3 is not installed"
}
$dscExe = Get-Command -name dsc -CommandType Application | Select-Object -First 1
$testProfileContent = "# Test profile content currentuser currenthost"
$testProfilePathCurrentUserCurrentHost = $PROFILE.CurrentUserCurrentHost
Copy-Item -Path $testProfilePathCurrentUserCurrentHost -Destination "$TestDrive/currentuser-currenthost-profile.bak" -Force -ErrorAction SilentlyContinue
New-Item -Path $testProfilePathCurrentUserCurrentHost -Value $testProfileContent -Force -ItemType File
$testProfileContent = "# Test profile content currentuser allhosts"
$testProfilePathCurrentUserAllHosts = $PROFILE.CurrentUserAllHosts
Copy-Item -Path $testProfilePathCurrentUserAllHosts -Destination "$TestDrive/currentuser-allhosts-profile.bak" -Force -ErrorAction SilentlyContinue
New-Item -Path $testProfilePathCurrentUserAllHosts -Value $testProfileContent -Force -ItemType File
}
AfterAll {
if ($skipCleanup) {
return
}
# Restore original profile
$testProfilePathCurrentUserCurrentHost = $PROFILE.CurrentUserCurrentHost
if (Test-Path "$TestDrive/currentuser-currenthost-profile.bak") {
Copy-Item -Path "$TestDrive/currentuser-currenthost-profile.bak" -Destination $testProfilePathCurrentUserCurrentHost -Force -ErrorAction SilentlyContinue
}
else {
Remove-Item $testProfilePathCurrentUserCurrentHost -Force -ErrorAction SilentlyContinue
}
$testProfilePathCurrentUserAllHosts = $PROFILE.CurrentUserAllHosts
if (Test-Path "$TestDrive/currentuser-allhosts-profile.bak") {
Copy-Item -Path "$TestDrive/currentuser-allhosts-profile.bak" -Destination $testProfilePathCurrentUserAllHosts -Force -ErrorAction SilentlyContinue
}
else {
Remove-Item $testProfilePathCurrentUserAllHosts -Force -ErrorAction SilentlyContinue
}
$env:PATH = $originalPath
Remove-Item -Path "$TestDrive/currentuser-currenthost-profile.bak" -Force -ErrorAction SilentlyContinue
Remove-Item -Path "$TestDrive/currentuser-allhosts-profile.bak" -Force -ErrorAction SilentlyContinue
}
It 'DSC resource is located at $PSHome' {
$resourceFile = Join-Path -Path $PSHome -ChildPath 'pwsh.profile.resource.ps1'
$resourceFile | Should -Exist
$resourceManifest = Join-Path -Path $PSHome -ChildPath 'pwsh.profile.dsc.resource.json'
$resourceManifest | Should -Exist
}
It 'DSC resource can be found' {
(& $dscExe resource list -o json | ConvertFrom-Json | Select-Object -Property type).type | Should -Contain 'Microsoft.PowerShell/Profile'
}
It 'DSC resource can set current user current host profile' {
$setOutput = (& $dscExe config set --file $PSScriptRoot/psprofile_currentuser_currenthost.dsc.yaml -o json) | ConvertFrom-Json
$expectedContent = "Write-Host 'Welcome to your PowerShell profile - CurrentUserCurrentHost!'"
$setOutput.results.result.afterState.content | Should -BeExactly $expectedContent
}
It 'DSC resource can get current user current host profile' {
$getOutput = (& $dscExe config get --file $PSScriptRoot/psprofile_currentuser_currenthost.dsc.yaml -o json) | ConvertFrom-Json
$expectedContent = "Write-Host 'Welcome to your PowerShell profile - CurrentUserCurrentHost!'"
$getOutput.results.result.actualState.content | Should -BeExactly $expectedContent
}
It 'DSC resource can set content as empty for current user current host profile' -Pending {
$setOutput = (& $dscExe config set --file $PSScriptRoot/psprofile_currentuser_currenthost_emptycontent.dsc.yaml -o json) | ConvertFrom-Json
$setOutput.results.result.afterState.content | Should -BeExactly ''
}
It 'DSC resource can set current user all hosts profile' {
$setOutput = (& $dscExe config set --file $PSScriptRoot/psprofile_currentuser_allhosts.dsc.yaml -o json) | ConvertFrom-Json
$expectedContent = "Write-Host 'Welcome to your PowerShell profile - CurrentUserAllHosts!'"
$setOutput.results.result.afterState.content | Should -BeExactly $expectedContent
}
It 'DSC resource can get current user all hosts profile' {
$getOutput = (& $dscExe config get --file $PSScriptRoot/psprofile_currentuser_allhosts.dsc.yaml -o json) | ConvertFrom-Json
$expectedContent = "Write-Host 'Welcome to your PowerShell profile - CurrentUserAllHosts!'"
$getOutput.results.result.actualState.content | Should -BeExactly $expectedContent
}
It 'DSC resource can export all profiles' {
$exportOutput = (& $dscExe config export --file $PSScriptRoot/psprofile_export.dsc.yaml -o json) | ConvertFrom-Json
$exportOutput.resources | Should -HaveCount 4
$exportOutput.resources | ForEach-Object {
$_.type | Should -Be 'Microsoft.PowerShell/Profile'
$_.name | Should -BeIn @('AllUsersCurrentHost', 'AllUsersAllHosts', 'CurrentUserCurrentHost', 'CurrentUserAllHosts')
}
}
}
Describe "DSC PowerShell Profile resource elevated tests" -Tag "CI", 'RequireAdminOnWindows', 'RequireSudoOnUnix' {
BeforeAll {
$DSC_ROOT = $env:DSC_ROOT
$skipCleanup = $false
if (-not (Test-Path -Path $DSC_ROOT)) {
$skipCleanup = $true
throw "DSC_ROOT environment variable is not set or path does not exist."
}
Write-Verbose "DSC_ROOT is set to $DSC_ROOT" -Verbose
$originalPath = $env:PATH
$pathSeparator = [System.IO.Path]::PathSeparator
$env:PATH += "$pathSeparator$DSC_ROOT"
$env:PATH += "$pathSeparator$PSHome"
Write-Verbose "Updated PATH to include DSC_ROOT: $env:PATH" -Verbose
# Ensure DSC v3 is available
if (-not (Get-Command -name dsc -CommandType Application -ErrorAction SilentlyContinue)) {
Get-ChildItem $DSC_ROOT -Recurse 'dsc' | ForEach-Object {
Write-Verbose "Found DSC executable at $($_.FullName)" -Verbose
}
throw "DSC v3 is not installed"
}
$dscExe = Get-Command -name dsc -CommandType Application | Select-Object -First 1
$testProfileContent = "# Test profile content allusers currenthost"
$testProfilePathAllUsersCurrentHost = $PROFILE.AllUsersCurrentHost
Copy-Item -Path $testProfilePathAllUsersCurrentHost -Destination "$TestDrive/allusers-currenthost-profile.bak" -Force -ErrorAction SilentlyContinue
New-Item -Path $testProfilePathAllUsersCurrentHost -Value $testProfileContent -Force -ItemType File
$testProfileContent = "# Test profile content allusers allhosts"
$testProfilePathAllUsersAllHosts = $PROFILE.AllUsersAllHosts
Copy-Item -Path $testProfilePathAllUsersAllHosts -Destination "$TestDrive/allusers-allhosts-profile.bak" -Force -ErrorAction SilentlyContinue
New-Item -Path $testProfilePathAllUsersAllHosts -Value $testProfileContent -Force -ItemType File
}
AfterAll {
if ($skipCleanup) {
return
}
$env:PATH = $originalPath
$testProfilePathAllUsersCurrentHost = $PROFILE.AllUsersCurrentHost
if (Test-Path "$TestDrive/allusers-currenthost-profile.bak") {
Copy-Item -Path "$TestDrive/allusers-currenthost-profile.bak" -Destination $testProfilePathAllUsersCurrentHost -Force -ErrorAction SilentlyContinue
}
else {
Remove-Item $testProfilePathAllUsersCurrentHost -Force -ErrorAction SilentlyContinue
}
$testProfilePathAllUsersAllHosts = $PROFILE.AllUsersAllHosts
if (Test-Path "$TestDrive/allusers-allhosts-profile.bak") {
Copy-Item -Path "$TestDrive/allusers-allhosts-profile.bak" -Destination $testProfilePathAllUsersAllHosts -Force -ErrorAction SilentlyContinue
}
else {
Remove-Item $testProfilePathAllUsersAllHosts -Force -ErrorAction SilentlyContinue
}
Remove-Item -Path "$TestDrive/currentuser-allhosts-profile.bak" -Force -ErrorAction SilentlyContinue
Remove-Item -Path "$TestDrive/allusers-allhosts-profile.bak" -Force -ErrorAction SilentlyContinue
}
It 'DSC resource can set all users all hosts profile' {
$setOutput = (& $dscExe config set --file $PSScriptRoot/psprofile_alluser_allhost.dsc.yaml -o json) | ConvertFrom-Json
$expectedContent = "Write-Host 'Welcome to your PowerShell profile - AllUsersAllHosts!'"
$setOutput.results.result.afterState.content | Should -BeExactly $expectedContent
}
It 'DSC resource can get all users all hosts profile' {
$getOutput = (& $dscExe config get --file $PSScriptRoot/psprofile_alluser_allhost.dsc.yaml -o json) | ConvertFrom-Json
$expectedContent = "Write-Host 'Welcome to your PowerShell profile - AllUsersAllHosts!'"
$getOutput.results.result.actualState.content | Should -BeExactly $expectedContent
}
It 'DSC resource can set all users current hosts profile' {
$setOutput = (& $dscExe config set --file $PSScriptRoot/psprofile_allusers_currenthost.dsc.yaml -o json) | ConvertFrom-Json
$expectedContent = "Write-Host 'Welcome to your PowerShell profile - AllUsersCurrentHost!'"
$setOutput.results.result.afterState.content | Should -BeExactly $expectedContent
}
It 'DSC resource can get all users current hosts profile' {
$getOutput = (& $dscExe config get --file $PSScriptRoot/psprofile_allusers_currenthost.dsc.yaml -o json) | ConvertFrom-Json
$expectedContent = "Write-Host 'Welcome to your PowerShell profile - AllUsersCurrentHost!'"
$getOutput.results.result.actualState.content | Should -BeExactly $expectedContent
}
}
|