From 5e52d50c5041faee767be582a2eea00994b89798 Mon Sep 17 00:00:00 2001 From: James Truher Date: Mon, 9 May 2022 13:09:40 -0700 Subject: [PATCH 1/6] Enable more tests to be run in a container. Improve debugging output for online help test. --- .../commands/utility/SetDateCommand.cs | 35 +++++++++++---- .../engine/Utils.cs | 2 + .../Test-Connection.Tests.ps1 | 4 +- .../Set-Date.Tests.ps1 | 11 +++-- .../Help/HelpSystem.OnlineHelp.Tests.ps1 | 43 +------------------ .../engine/Help/HelpSystem.Tests.ps1 | 2 +- 6 files changed, 39 insertions(+), 58 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/SetDateCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/SetDateCommand.cs index 8128c1a84ca6..3ace3ea0e3f3 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/SetDateCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/SetDateCommand.cs @@ -71,7 +71,13 @@ protected override void ProcessRecord() if (ShouldProcess(dateToUse.ToString())) { #if UNIX - if (!Platform.NonWindowsSetDate(dateToUse)) + // We are not validating the native call here. + // We just want to be sure that we're using the value the user provided us. + if (Dbg.Internal.InternalTestHooks.SetDate) + { + WriteObject(dateToUse); + } + else if (!Platform.NonWindowsSetDate(dateToUse)) { throw new Win32Exception(Marshal.GetLastWin32Error()); } @@ -86,16 +92,23 @@ protected override void ProcessRecord() systemTime.Second = (ushort)dateToUse.Second; systemTime.Milliseconds = (ushort)dateToUse.Millisecond; #pragma warning disable 56523 - if (!NativeMethods.SetLocalTime(ref systemTime)) + if (Dbg.Internal.InternalTestHooks.SetDate) { - throw new Win32Exception(Marshal.GetLastWin32Error()); + WriteObject(systemTime); } - - // MSDN says to call this twice to account for changes - // between DST - if (!NativeMethods.SetLocalTime(ref systemTime)) + else { - throw new Win32Exception(Marshal.GetLastWin32Error()); + if (!NativeMethods.SetLocalTime(ref systemTime)) + { + throw new Win32Exception(Marshal.GetLastWin32Error()); + } + + // MSDN says to call this twice to account for changes + // between DST + if (!NativeMethods.SetLocalTime(ref systemTime)) + { + throw new Win32Exception(Marshal.GetLastWin32Error()); + } } #pragma warning restore 56523 #endif @@ -106,7 +119,11 @@ protected override void ProcessRecord() PSNoteProperty note = new("DisplayHint", DisplayHint); outputObj.Properties.Add(note); - WriteObject(outputObj); + // If we've turned on the SetDate test hook, don't emit the output object here because we emitted it earlier. + if (!Dbg.Internal.InternalTestHooks.SetDate) + { + WriteObject(outputObj); + } } #endregion diff --git a/src/System.Management.Automation/engine/Utils.cs b/src/System.Management.Automation/engine/Utils.cs index 879f43944a52..16ac72bfb125 100644 --- a/src/System.Management.Automation/engine/Utils.cs +++ b/src/System.Management.Automation/engine/Utils.cs @@ -1592,6 +1592,8 @@ public static class InternalTestHooks internal static bool SetConsoleWidthToZero; internal static bool SetConsoleHeightToZero; + internal static bool SetDate; + // A location to test PSEdition compatibility functionality for Windows PowerShell modules with // since we can't manipulate the System32 directory in a test internal static string TestWindowsPowerShellPSHomeLocation; diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Test-Connection.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Test-Connection.Tests.ps1 index b8c9d14a1bed..9ccad8944e16 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Test-Connection.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Test-Connection.Tests.ps1 @@ -257,7 +257,7 @@ Describe "Test-Connection" -tags "CI", "RequireSudoOnUnix" { # We skip the MtuSize detection tests when in containers, as the environments throw raw exceptions # instead of returning a PacketTooBig response cleanly. # Test disabled due to .NET runtime issue: https://github.com/dotnet/runtime/issues/55961 - It "MTUSizeDetect works" -Pending:(($env:__INCONTAINER -eq 1) -or $IsMacOS) { + It "MTUSizeDetect works" { $result = Test-Connection $testAddress -MtuSize $result | Should -BeOfType Microsoft.PowerShell.Commands.TestConnectionCommand+PingMtuStatus @@ -267,7 +267,7 @@ Describe "Test-Connection" -tags "CI", "RequireSudoOnUnix" { } # Test disabled due to .NET runtime issue: https://github.com/dotnet/runtime/issues/55961 - It "Quiet works" -Pending:(($env:__INCONTAINER -eq 1) -or $IsMacOS) { + It "Quiet works" { $result = Test-Connection $gatewayAddress -MtuSize -Quiet $result | Should -BeOfType Int32 diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Set-Date.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Set-Date.Tests.ps1 index f8b974c457f5..15ce4501192c 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Set-Date.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Set-Date.Tests.ps1 @@ -5,15 +5,18 @@ Import-Module HelpersCommon Describe "Set-Date for admin" -Tag @('CI', 'RequireAdminOnWindows', 'RequireSudoOnUnix') { BeforeAll { - $skipTest = (Test-IsVstsLinux) -or ($env:__INCONTAINER -eq 1) + [System.Management.Automation.Internal.InternalTestHooks]::SetTestHook("SetDate", $true) } - # Fails in VSTS Linux with Operation not permitted - It "Set-Date should be able to set the date in an elevated context" -Skip:$skipTest { + AfterAll { + [System.Management.Automation.Internal.InternalTestHooks]::SetTestHook("SetDate", $false) + } + + It "Set-Date should be able to set the date in an elevated context" { { Get-Date | Set-Date } | Should -Not -Throw } # Fails in VSTS Linux with Operation not permitted - It "Set-Date should be able to set the date with -Date parameter" -Skip:$skipTest { + It "Set-Date should be able to set the date with -Date parameter" { $target = Get-Date $expected = $target Set-Date -Date $target | Should -Be $expected diff --git a/test/powershell/engine/Help/HelpSystem.OnlineHelp.Tests.ps1 b/test/powershell/engine/Help/HelpSystem.OnlineHelp.Tests.ps1 index 699221403473..b56effc86264 100644 --- a/test/powershell/engine/Help/HelpSystem.OnlineHelp.Tests.ps1 +++ b/test/powershell/engine/Help/HelpSystem.OnlineHelp.Tests.ps1 @@ -42,6 +42,7 @@ Describe 'Online help tests for PowerShell Cmdlets' -Tags "Feature" { # TopicTitle - is the cmdlet name in the csv file # HelpURI - is the expected help URI in the csv file + # If this is correct, then launching the cmdlet should open the expected help URI. It "Validate 'get-help $($cmdlet.TopicTitle) -Online'" -Skip:$skipTest { $actualURI = Get-Help $cmdlet.TopicTitle -Online @@ -52,48 +53,6 @@ Describe 'Online help tests for PowerShell Cmdlets' -Tags "Feature" { } } -Describe 'Get-Help -Online opens the default web browser and navigates to the cmdlet help content' -Tags "Feature" { - - $skipTest = [System.Management.Automation.Platform]::IsIoT -or - [System.Management.Automation.Platform]::IsNanoServer -or - $env:__INCONTAINER -eq 1 - - # this code is a workaround for issue: https://github.com/PowerShell/PowerShell/issues/3079 - if((-not ($skipTest)) -and $IsWindows) - { - $skipTest = $true - $regKey = "HKCU:\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice" - - try - { - $progId = (Get-ItemProperty $regKey).ProgId - if($progId) - { - if (-not (Test-Path 'HKCR:\')) - { - New-PSDrive -PSProvider registry -Root HKEY_CLASSES_ROOT -Name HKCR | Should -Not -BeNullOrEmpty - } - $browserExe = ((Get-ItemProperty "HKCR:\$progId\shell\open\command")."(default)" -replace '"', '') -split " " - if ($browserExe.count -ge 1) - { - if($browserExe[0] -match '.exe') - { - $skipTest = $false - } - } - } - } - catch - { - # We are not able to access Registry, skipping test. - } - } - - It "Get-Help get-process -online" -Skip:$skipTest { - { Get-Help get-process -Online } | Should -Not -Throw - } -} - Describe 'Get-Help -Online is not supported on Nano Server and IoT' -Tags "CI" { $skipTest = -not ([System.Management.Automation.Platform]::IsIoT -or [System.Management.Automation.Platform]::IsNanoServer) diff --git a/test/powershell/engine/Help/HelpSystem.Tests.ps1 b/test/powershell/engine/Help/HelpSystem.Tests.ps1 index 207053b058b4..d32a9bba66f8 100644 --- a/test/powershell/engine/Help/HelpSystem.Tests.ps1 +++ b/test/powershell/engine/Help/HelpSystem.Tests.ps1 @@ -578,7 +578,7 @@ Describe "Help failure cases" -Tags Feature { # under some conditions this does not throw, so include what we actually got $helpTopic = [guid]::NewGuid().ToString("N") - { & $command $helpTopic -ErrorAction Stop } | Should -Throw -ErrorId "HelpNotFound,Microsoft.PowerShell.Commands.GetHelpCommand" -Because "$(& $command $helpTopic -ea Ignore)" + { & $command $helpTopic -ErrorAction Stop } | Should -Throw -ErrorId "HelpNotFound,Microsoft.PowerShell.Commands.GetHelpCommand" -Because "A help topic was found for $helpTopic" } } From 4e2d1092fdbe80f9f688dfeb7909fe70e9371e03 Mon Sep 17 00:00:00 2001 From: James Truher Date: Mon, 9 May 2022 14:25:03 -0700 Subject: [PATCH 2/6] improve message when test fails. --- test/powershell/engine/Help/HelpSystem.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/powershell/engine/Help/HelpSystem.Tests.ps1 b/test/powershell/engine/Help/HelpSystem.Tests.ps1 index d32a9bba66f8..bd523b1033e3 100644 --- a/test/powershell/engine/Help/HelpSystem.Tests.ps1 +++ b/test/powershell/engine/Help/HelpSystem.Tests.ps1 @@ -578,7 +578,7 @@ Describe "Help failure cases" -Tags Feature { # under some conditions this does not throw, so include what we actually got $helpTopic = [guid]::NewGuid().ToString("N") - { & $command $helpTopic -ErrorAction Stop } | Should -Throw -ErrorId "HelpNotFound,Microsoft.PowerShell.Commands.GetHelpCommand" -Because "A help topic was found for $helpTopic" + { & $command $helpTopic -ErrorAction Stop } | Should -Throw -ErrorId "HelpNotFound,Microsoft.PowerShell.Commands.GetHelpCommand" -Because "A help topic was unexpectantly found for $helpTopic" } } From 0856b906547577d25eba2e70d73b8b6ed2f509d9 Mon Sep 17 00:00:00 2001 From: James Truher Date: Mon, 9 May 2022 15:51:06 -0700 Subject: [PATCH 3/6] change test to validate individual properties since the objects may be of different types (on windows). --- .../Microsoft.PowerShell.Utility/Set-Date.Tests.ps1 | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Set-Date.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Set-Date.Tests.ps1 index 15ce4501192c..03c880fc2396 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Set-Date.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Set-Date.Tests.ps1 @@ -15,11 +15,18 @@ Describe "Set-Date for admin" -Tag @('CI', 'RequireAdminOnWindows', 'RequireSudo { Get-Date | Set-Date } | Should -Not -Throw } - # Fails in VSTS Linux with Operation not permitted + # Check the individual properties as the types may be different It "Set-Date should be able to set the date with -Date parameter" { $target = Get-Date $expected = $target - Set-Date -Date $target | Should -Be $expected + $observed = Set-Date -Date $target + $observed.Day | Should -Be $expected.Day + $observed.DayOfWeek | Should -Be $expected.DayOfWeek + $observed.Hour | Should -Be $expected.Hour + $observed.Minutes | Should -Be $expected.Minutes + $observed.Month | Should -Be $expected.Month + $observed.Second | Should -Be $expected.Second + $observed.Year | Should -Be $expected.Year } } From cce8b485c408e726fae19b0382d59ceb964389cb Mon Sep 17 00:00:00 2001 From: James Truher Date: Mon, 9 May 2022 16:13:28 -0700 Subject: [PATCH 4/6] remove validation of day of week since the number _should_ be different between native and managed. --- .../Modules/Microsoft.PowerShell.Utility/Set-Date.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Set-Date.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Set-Date.Tests.ps1 index 03c880fc2396..f382c6fa2140 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Set-Date.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Set-Date.Tests.ps1 @@ -20,8 +20,8 @@ Describe "Set-Date for admin" -Tag @('CI', 'RequireAdminOnWindows', 'RequireSudo $target = Get-Date $expected = $target $observed = Set-Date -Date $target + # do not test dayofweek because the number of the day is different between native and managed $observed.Day | Should -Be $expected.Day - $observed.DayOfWeek | Should -Be $expected.DayOfWeek $observed.Hour | Should -Be $expected.Hour $observed.Minutes | Should -Be $expected.Minutes $observed.Month | Should -Be $expected.Month From 3f2c3a3fcad2280a6e9f2b3467550667932aebd4 Mon Sep 17 00:00:00 2001 From: James Truher Date: Mon, 9 May 2022 16:56:00 -0700 Subject: [PATCH 5/6] Remove comments about skipped tests since they're not skipped any longer. --- .../Microsoft.PowerShell.Management/Test-Connection.Tests.ps1 | 4 ---- 1 file changed, 4 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Test-Connection.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Test-Connection.Tests.ps1 index 9ccad8944e16..1d639fe5f2da 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Test-Connection.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Test-Connection.Tests.ps1 @@ -254,9 +254,6 @@ Describe "Test-Connection" -tags "CI", "RequireSudoOnUnix" { } Context "MTUSizeDetect" { - # We skip the MtuSize detection tests when in containers, as the environments throw raw exceptions - # instead of returning a PacketTooBig response cleanly. - # Test disabled due to .NET runtime issue: https://github.com/dotnet/runtime/issues/55961 It "MTUSizeDetect works" { $result = Test-Connection $testAddress -MtuSize @@ -266,7 +263,6 @@ Describe "Test-Connection" -tags "CI", "RequireSudoOnUnix" { $result.MtuSize | Should -BeGreaterThan 0 } - # Test disabled due to .NET runtime issue: https://github.com/dotnet/runtime/issues/55961 It "Quiet works" { $result = Test-Connection $gatewayAddress -MtuSize -Quiet From 972d565a310b8d50c09714db538255af57f57d7a Mon Sep 17 00:00:00 2001 From: James Truher Date: Tue, 10 May 2022 12:47:02 -0700 Subject: [PATCH 6/6] Add a .5 second wait before getting process list. --- .../Scripting/NativeExecution/NativeCommandProcessor.Tests.ps1 | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/powershell/Language/Scripting/NativeExecution/NativeCommandProcessor.Tests.ps1 b/test/powershell/Language/Scripting/NativeExecution/NativeCommandProcessor.Tests.ps1 index b76cf67bfecf..8175c4b07cea 100644 --- a/test/powershell/Language/Scripting/NativeExecution/NativeCommandProcessor.Tests.ps1 +++ b/test/powershell/Language/Scripting/NativeExecution/NativeCommandProcessor.Tests.ps1 @@ -47,6 +47,8 @@ Describe 'native commands with pipeline' -tags 'Feature' { It 'native command should be killed when pipeline is disposed' -Skip:($IsWindows) { $yes = (Get-Process 'yes' -ErrorAction Ignore).Count yes | Select-Object -First 2 + # wait a little to be sure that the process is ended + Start-Sleep -Milliseconds 500 (Get-Process 'yes' -ErrorAction Ignore).Count | Should -Be $yes } }