Skip to content

Commit f04a256

Browse files
author
Steve Lee (POWERSHELL HE/HIM) (from Dev Box)
committed
fix tets
1 parent fb583ab commit f04a256

File tree

11 files changed

+336
-304
lines changed

11 files changed

+336
-304
lines changed

build.ps1

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ param(
2626
$env:RUSTC_LOG=$null
2727
$env:RUSTFLAGS='-Dwarnings'
2828

29+
trap {
30+
Write-Error "An error occurred: $($_ | Out-String)"
31+
exit 1
32+
}
33+
2934
if ($Verbose) {
3035
$env:RUSTC_LOG='rustc_codegen_ssa::back::link=info'
3136
}
@@ -569,11 +574,7 @@ if ($Test) {
569574
(Get-Module -Name Pester -ListAvailable).Path
570575
}
571576

572-
try {
573-
Invoke-Pester -ErrorAction Stop
574-
} catch {
575-
throw "Pester had unexpected error: $($_.Exception.Message)"
576-
}
577+
Invoke-Pester -Output Detailed -ErrorAction Stop
577578
}
578579

579580
function Find-MakeAppx() {

dsc_lib/locales/en-us.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ processChildExit = "Process '%{executable}' id %{id} exited with code %{code}"
135135
processChildTerminated = "Process '%{executable}' id %{id} terminated by signal"
136136
processTerminated = "Process terminated by signal"
137137
commandInvoke = "Invoking command '%{executable}' with args %{args}"
138+
commandCwd = "Current working directory: %{cwd}"
138139
noArgs = "No args to process"
139140
parseAsEnvVars = "Parsing input as environment variables"
140141
parseAsStdin = "Parsing input as stdin"

dsc_lib/src/discovery/command_discovery.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,8 @@ impl ResourceDiscovery for CommandDiscovery {
200200
fn discover(&mut self, kind: &DiscoveryKind, filter: &str) -> Result<(), DscError> {
201201
info!("{}", t!("discovery.commandDiscovery.discoverResources", kind = kind : {:?}, filter = filter));
202202

203-
// if kind is DscResource, we need to discover extensions first
204-
if *kind == DiscoveryKind::Resource {
203+
// if kind is DscResource and DSC_RESOURCE_PATH is not defined, we need to discover extensions first
204+
if *kind == DiscoveryKind::Resource && env::var("DSC_RESOURCE_PATH").is_err() {
205205
self.discover(&DiscoveryKind::Extension, "*")?;
206206
}
207207

dsc_lib/src/dscresources/command_resource.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,9 @@ async fn run_process_async(executable: &str, args: Option<Vec<String>>, input: O
697697
#[allow(clippy::implicit_hasher)]
698698
pub fn invoke_command(executable: &str, args: Option<Vec<String>>, input: Option<&str>, cwd: Option<&str>, env: Option<HashMap<String, String>>, exit_codes: Option<&HashMap<i32, String>>) -> Result<(i32, String, String), DscError> {
699699
debug!("{}", t!("dscresources.commandResource.commandInvoke", executable = executable, args = args : {:?}));
700+
if let Some(cwd) = cwd {
701+
debug!("{}", t!("dscresources.commandResource.commandCwd", cwd = cwd));
702+
}
700703

701704
tokio::runtime::Builder::new_multi_thread()
702705
.enable_all()

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@ Describe 'PowerShell adapter resource tests' {
1515
$cacheFilePath = Join-Path $env:LocalAppData "dsc" "PSAdapterCache.json"
1616
}
1717
}
18+
1819
AfterAll {
1920
$env:PSModulePath = $OldPSModulePath
2021
}
2122

2223
BeforeEach {
23-
Remove-Item -Force -ea SilentlyContinue -Path $cacheFilePath
24+
Remove-Item -Force -ErrorAction Ignore -Path $cacheFilePath
2425
}
2526

2627
It 'Get works on config with class-based resources' {

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@ Describe 'PowerShell adapter resource tests' {
1414
$cacheFilePath = Join-Path $env:LocalAppData "dsc" "PSAdapterCache.json"
1515
}
1616
}
17+
1718
AfterAll {
1819
$env:PSModulePath = $OldPSModulePath
1920
}
2021

2122
BeforeEach {
22-
Remove-Item -Force -ea SilentlyContinue -Path $cacheFilePath
23+
Remove-Item -Force -ErrorAction Ignore -Path $cacheFilePath
2324
}
2425

2526
It 'Discovery includes class-based resources' {
@@ -173,7 +174,7 @@ Describe 'PowerShell adapter resource tests' {
173174
# remove the module files
174175
Remove-Item -Recurse -Force -Path "$TestDrive/TestClassResource"
175176
# verify that cache rebuid happened
176-
dsc -l trace resource list '*' -a Microsoft.DSC/PowerShell 2> $TestDrive/tracing.txt
177+
$null = dsc -l trace resource list '*' -a Microsoft.DSC/PowerShell 2> $TestDrive/tracing.txt
177178

178179
$LASTEXITCODE | Should -Be 0
179180
"$TestDrive/tracing.txt" | Should -FileContentMatchExactly 'Detected non-existent cache entry'
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
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

Comments
 (0)