Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ param(
[switch]$Test,
[string[]]$Project,
[switch]$ExcludeRustTests,
[string]$RustTestFilter,
[switch]$ExcludePesterTests,
[ValidateSet("dsc", "adapters", "extensions", "grammars", "resources")]
[string[]]$PesterTestGroup,
Expand Down Expand Up @@ -266,6 +267,9 @@ process {
Architecture = $Architecture
Release = $Release
}
if (-not [string]::IsNullOrEmpty($RustTestFilter)) {
$rustTestParams.TestFilter = $RustTestFilter
}
Write-BuildProgress @progressParams -Status "Testing Rust projects"
Test-RustProject @rustTestParams @VerboseParam
}
Expand Down
9 changes: 7 additions & 2 deletions helpers.build.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -1798,7 +1798,8 @@ function Test-RustProject {
[ValidateSet('current','aarch64-pc-windows-msvc','x86_64-pc-windows-msvc','aarch64-apple-darwin','x86_64-apple-darwin','aarch64-unknown-linux-gnu','aarch64-unknown-linux-musl','x86_64-unknown-linux-gnu','x86_64-unknown-linux-musl')]
$Architecture = 'current',
[switch]$Release,
[switch]$Docs
[switch]$Docs,
[string]$TestFilter
)

begin {
Expand Down Expand Up @@ -1828,7 +1829,11 @@ function Test-RustProject {
} else {
Write-Verbose -Verbose "Testing rust projects: [$members]"
}
cargo test @flags
if (-not [string]::IsNullOrEmpty($TestFilter)) {
cargo test @flags -- $TestFilter
} else {
cargo test @flags
}

if ($null -ne $LASTEXITCODE -and $LASTEXITCODE -ne 0) {
Write-Error "Last exit code is $LASTEXITCODE, rust tests failed"
Expand Down
2 changes: 2 additions & 0 deletions lib/dsc-lib/src/dscresources/command_resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,8 @@ async fn run_process_async(executable: &str, args: Option<Vec<String>>, input: O
let mut command = Command::new(executable);
if input.is_some() {
command.stdin(Stdio::piped());
} else {
command.stdin(Stdio::null());
}
Comment thread
SteveL-MSFT marked this conversation as resolved.
command.stdout(Stdio::piped());
command.stderr(Stdio::piped());
Expand Down
64 changes: 64 additions & 0 deletions lib/dsc-lib/tests/integration/command_resource.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

#[cfg(test)]
mod invoke_command {
Comment thread
SteveL-MSFT marked this conversation as resolved.
use dsc_lib::{dscresources::command_resource::invoke_command, types::ExitCodesMap};

/// Verifies that when `invoke_command` is called with `input = None`, the child process
/// receives an immediate EOF on stdin (i.e., stdin is set to null) rather than inheriting
/// the parent's stdin handle.
///
/// This is a regression test for the hang introduced in DSC 3.2.0 when the PowerShell
/// adapter changed from `"config": "full"` to `"config": "single"`. In single mode, the
/// adapter's export operation is called with no input, leaving stdin unset in the previous
/// code. Child processes that read from stdin would then block indefinitely when the parent
/// process itself had an open stdin handle — either a TTY in a terminal or a pipe in CI.
///
/// The test uses a timed async read rather than a blocking read so that the child process
/// always exits within a bounded time. If stdin is null (the fix), `ReadAsync` completes
/// immediately returning 0 bytes (EOF), which maps to -1. If stdin is inherited (the bug),
/// `ReadAsync` blocks until the timeout fires and the test receives -2, which fails the
/// assertion.
#[test]
fn no_input_does_not_block_on_stdin() {
let exit_codes = ExitCodesMap::default();

// Use PowerShell's own async timeout so the child process always exits within ~2s,
// regardless of fix status. We never leave a hanging thread:
// byte:-1 → ReadAsync got EOF immediately → stdin was null → PASS
// byte:-2 → ReadAsync timed out (2 s) → stdin was NOT null → FAIL
let ps_command = concat!(
"$reader = [Console]::OpenStandardInput();",
"$buf = [byte[]]::new(1);",
"$task = $reader.ReadAsync($buf, 0, 1);",
"$completed = $task.Wait(2000);",
"$b = if ($completed) { if ($task.Result -eq 0) { -1 } else { $buf[0] } } else { -2 };",
Comment thread
SteveL-MSFT marked this conversation as resolved.
"Write-Output \"byte:$b\""
);

let result = invoke_command(
"pwsh",
Some(vec![
"-NonInteractive".to_string(),
"-NoProfile".to_string(),
"-Command".to_string(),
ps_command.to_string(),
]),
Comment thread
SteveL-MSFT marked this conversation as resolved.
None, // no input — the scenario that caused the hang
None,
None,
&exit_codes,
).expect("invoke_command should succeed");

let (exit_code, stdout, _stderr) = result;
assert_eq!(exit_code, 0, "Command should exit 0");
// -1 means ReadAsync got EOF immediately, confirming stdin was set to null.
// -2 means stdin was open (inherited) and the read timed out after 2s.
assert!(
stdout.contains("byte:-1"),
"Expected EOF (byte:-1) from null stdin, got: {stdout:?}\n\
'byte:-2' means stdin was inherited from the parent rather than set to null."
);
}
}
1 change: 1 addition & 0 deletions lib/dsc-lib/tests/integration/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@
//! minimize compilation times. If we defined the tests one level higher in the `tests` folder,
//! Rust would generate numerous binaries to execute our tests.

#[cfg(test)] mod command_resource;
#[cfg(test)] mod schemas;
#[cfg(test)] mod types;
44 changes: 22 additions & 22 deletions resources/WindowsUpdate/tests/windowsupdate_export.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Describe 'Windows Update Export operation tests' {

Context 'Export operation' {
It 'should return UpdateList with array of updates' -Skip:(!$IsWindows) {
$out = '{"updates":[{}]}' | dsc resource export -r $resourceType -o json 2>&1
$out = '{"updates":[{}]}' | dsc resource export -r $resourceType -f - -o json 2>&1

$LASTEXITCODE | Should -Be 0
$config = $out | ConvertFrom-Json
Expand All @@ -18,7 +18,7 @@ Describe 'Windows Update Export operation tests' {
}

It 'should work without input filter' -Skip:(!$IsWindows) {
$out = '' | dsc resource export -r $resourceType -o json 2>&1
$out = dsc resource export -r $resourceType -o json 2>&1

$LASTEXITCODE | Should -Be 0
$config = $out | ConvertFrom-Json
Expand All @@ -28,7 +28,7 @@ Describe 'Windows Update Export operation tests' {

It 'should filter by isInstalled=true' -Skip:(!$IsWindows) {
$json = '{"updates":[{"isInstalled": true}]}'
$out = $json | dsc resource export -r $resourceType -o json 2>&1
$out = $json | dsc resource export -r $resourceType -f - -o json 2>&1

$LASTEXITCODE | Should -Be 0
$config = $out | ConvertFrom-Json
Expand All @@ -42,7 +42,7 @@ Describe 'Windows Update Export operation tests' {

It 'should filter by isInstalled=false' -Skip:(!$IsWindows) {
$json = '{"updates":[{"isInstalled": false}]}'
$out = $json | dsc resource export -r $resourceType -o json 2>&1
$out = $json | dsc resource export -r $resourceType -f - -o json 2>&1

$LASTEXITCODE | Should -Be 0
$config = $out | ConvertFrom-Json
Expand All @@ -56,7 +56,7 @@ Describe 'Windows Update Export operation tests' {

It 'should filter by title with wildcard in middle' -Skip:(!$IsWindows) {
$json = '{"updates":[{"title": "*Windows*"}]}'
$out = $json | dsc resource export -r $resourceType -o json 2>&1
$out = $json | dsc resource export -r $resourceType -f - -o json 2>&1

if ($LASTEXITCODE -eq 0) {
$config = $out | ConvertFrom-Json
Expand All @@ -70,7 +70,7 @@ Describe 'Windows Update Export operation tests' {
}

It 'should return proper structure for each update' -Skip:(!$IsWindows) {
$out = '{"updates":[{}]}' | dsc resource export -r $resourceType -o json 2>&1
$out = '{"updates":[{}]}' | dsc resource export -r $resourceType -f - -o json 2>&1

$LASTEXITCODE | Should -Be 0
$config = $out | ConvertFrom-Json
Expand All @@ -92,7 +92,7 @@ Describe 'Windows Update Export operation tests' {

It 'should fail when wildcard filter has no matches' -Skip:(!$IsWindows) {
$json = '{"updates":[{"title": "ThisUpdateShouldNeverExist99999*"}]}'
$stderr = $json | dsc resource export -r $resourceType -o json 2>&1
$stderr = $json | dsc resource export -r $resourceType -f - -o json 2>&1

# Should fail because the filter has criteria but no matches
$LASTEXITCODE | Should -Not -Be 0
Expand All @@ -111,7 +111,7 @@ Describe 'Windows Update Export operation tests' {
}
)
} | ConvertTo-Json -Depth 10 -Compress
$stderr = $json | dsc resource export -r $resourceType 2>&1
$stderr = $json | dsc resource export -r $resourceType -f - 2>&1

# Should fail because the filter has criteria but no matches
$LASTEXITCODE | Should -Not -Be 0
Expand All @@ -128,7 +128,7 @@ Describe 'Windows Update Export operation tests' {
@{}
)
} | ConvertTo-Json -Depth 10 -Compress
$out = $json | dsc resource export -r $resourceType -o json 2>&1
$out = $json | dsc resource export -r $resourceType -f - -o json 2>&1

$LASTEXITCODE | Should -Be 0
$config = $out | ConvertFrom-Json
Expand All @@ -138,7 +138,7 @@ Describe 'Windows Update Export operation tests' {

It 'should fail if any filter with criteria has no matches' -Skip:(!$IsWindows) {
# Get an actual update
$allOut = '{"updates":[{}]}' | dsc resource export -r $resourceType -o json 2>&1
$allOut = '{"updates":[{}]}' | dsc resource export -r $resourceType -f - -o json 2>&1

if ($LASTEXITCODE -eq 0) {
$allConfig = $allOut | ConvertFrom-Json
Expand All @@ -157,7 +157,7 @@ Describe 'Windows Update Export operation tests' {
}
)
} | ConvertTo-Json -Depth 10 -Compress
$stderr = $json | dsc resource export -r $resourceType 2>&1
$stderr = $json | dsc resource export -r $resourceType -f - 2>&1

# Should fail because second filter has no matches
$LASTEXITCODE | Should -Not -Be 0
Expand All @@ -171,7 +171,7 @@ Describe 'Windows Update Export operation tests' {

It 'should return results when all filters find matches' -Skip:(!$IsWindows) {
# Get actual updates
$allOut = '{"updates":[{}]}' | dsc resource export -r $resourceType -o json 2>&1
$allOut = '{"updates":[{}]}' | dsc resource export -r $resourceType -f - -o json 2>&1

if ($LASTEXITCODE -eq 0) {
$allConfig = $allOut | ConvertFrom-Json
Expand All @@ -190,7 +190,7 @@ Describe 'Windows Update Export operation tests' {
}
)
} | ConvertTo-Json -Depth 10 -Compress
$out = $json | dsc resource export -r $resourceType -o json 2>&1
$out = $json | dsc resource export -r $resourceType -f - -o json 2>&1

$LASTEXITCODE | Should -Be 0
$config = $out | ConvertFrom-Json
Expand All @@ -205,7 +205,7 @@ Describe 'Windows Update Export operation tests' {

It 'should filter by msrcSeverity' -Skip:(!$IsWindows) {
$json = '{"updates":[{"msrcSeverity": "Critical"}]}'
$out = $json | dsc resource export -r $resourceType -o json 2>&1
$out = $json | dsc resource export -r $resourceType -f - -o json 2>&1

if ($LASTEXITCODE -eq 0) {
$config = $out | ConvertFrom-Json
Expand All @@ -220,7 +220,7 @@ Describe 'Windows Update Export operation tests' {

It 'should filter by updateType Software' -Skip:(!$IsWindows) {
$json = '{"updates":[{"updateType": "Software"}]}'
$out = $json | dsc resource export -r $resourceType -o json 2>&1
$out = $json | dsc resource export -r $resourceType -f - -o json 2>&1

if ($LASTEXITCODE -eq 0) {
$config = $out | ConvertFrom-Json
Expand All @@ -235,7 +235,7 @@ Describe 'Windows Update Export operation tests' {

It 'should support OR logic with multiple filters in array' -Skip:(!$IsWindows) {
# Get some updates to use as filters
$allOut = '{"updates":[{}]}' | dsc resource export -r $resourceType -o json 2>&1
$allOut = '{"updates":[{}]}' | dsc resource export -r $resourceType -f - -o json 2>&1

if ($LASTEXITCODE -eq 0) {
$allConfig = $allOut | ConvertFrom-Json
Expand All @@ -245,7 +245,7 @@ Describe 'Windows Update Export operation tests' {
$id1 = $allResult.updates[0].id
$id2 = $allResult.updates[1].id
$json = "{`"updates`":[{`"id`": `"$id1`"}, {`"id`": `"$id2`"}]}"
$out = $json | dsc resource export -r $resourceType -o json 2>&1
$out = $json | dsc resource export -r $resourceType -f - -o json 2>&1

$LASTEXITCODE | Should -Be 0
$config = $out | ConvertFrom-Json
Expand All @@ -267,7 +267,7 @@ Describe 'Windows Update Export operation tests' {
It 'should support AND logic within single filter object' -Skip:(!$IsWindows) {
# Multiple properties in one filter = AND logic
$json = '{"updates":[{"isInstalled": true, "updateType": "Software"}]}'
$out = $json | dsc resource export -r $resourceType -o json 2>&1
$out = $json | dsc resource export -r $resourceType -f - -o json 2>&1

if ($LASTEXITCODE -eq 0) {
$config = $out | ConvertFrom-Json
Expand All @@ -284,7 +284,7 @@ Describe 'Windows Update Export operation tests' {

It 'should not return duplicates when multiple filters match same update' -Skip:(!$IsWindows) {
# Get an update with known properties
$allOut = '{"updates":[{}]}' | dsc resource export -r $resourceType -o json 2>&1
$allOut = '{"updates":[{}]}' | dsc resource export -r $resourceType -f - -o json 2>&1

if ($LASTEXITCODE -eq 0) {
$allConfig = $allOut | ConvertFrom-Json
Expand All @@ -294,7 +294,7 @@ Describe 'Windows Update Export operation tests' {
# Use the same ID in both filters - this should only return one update
# Even though technically both filters specify the same criteria
$json = "{`"updates`":[{`"id`": `"$($testUpdate.id)`"}, {`"id`": `"$($testUpdate.id)`"}]}"
$out = $json | dsc resource export -r $resourceType -o json 2>&1
$out = $json | dsc resource export -r $resourceType -f - -o json 2>&1

$LASTEXITCODE | Should -Be 0 -Because $out
$config = $out | ConvertFrom-Json
Expand All @@ -308,7 +308,7 @@ Describe 'Windows Update Export operation tests' {
}

It 'should return installationBehavior property when present' -Skip:(!$IsWindows) {
$out = '{"updates":[{}]}' | dsc resource export -r $resourceType -o json 2>&1
$out = '{"updates":[{}]}' | dsc resource export -r $resourceType -f - -o json 2>&1

$LASTEXITCODE | Should -Be 0
$config = $out | ConvertFrom-Json
Expand All @@ -326,7 +326,7 @@ Describe 'Windows Update Export operation tests' {
}

It 'should return valid installationBehavior enum values for all updates' -Skip:(!$IsWindows) {
$out = '{"updates":[{}]}' | dsc resource export -r $resourceType -o json 2>&1
$out = '{"updates":[{}]}' | dsc resource export -r $resourceType -f - -o json 2>&1

$LASTEXITCODE | Should -Be 0
$config = $out | ConvertFrom-Json
Expand Down
Loading