Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable more tests to be run in a container. #17294

Merged
merged 6 commits into from May 11, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -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());
}
Expand All @@ -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
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/System.Management.Automation/engine/Utils.cs
Expand Up @@ -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;
Expand Down
Expand Up @@ -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
}
}
Expand Down
Expand Up @@ -254,10 +254,7 @@ 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" -Pending:(($env:__INCONTAINER -eq 1) -or $IsMacOS) {
It "MTUSizeDetect works" {
$result = Test-Connection $testAddress -MtuSize

$result | Should -BeOfType Microsoft.PowerShell.Commands.TestConnectionCommand+PingMtuStatus
Expand All @@ -266,8 +263,7 @@ 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" -Pending:(($env:__INCONTAINER -eq 1) -or $IsMacOS) {
It "Quiet works" {
$result = Test-Connection $gatewayAddress -MtuSize -Quiet

$result | Should -BeOfType Int32
Expand Down
Expand Up @@ -5,18 +5,28 @@ 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 {
# 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
# do not test dayofweek because the number of the day is different between native and managed
$observed.Day | Should -Be $expected.Day
$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
}
}

Expand Down
43 changes: 1 addition & 42 deletions test/powershell/engine/Help/HelpSystem.OnlineHelp.Tests.ps1
Expand Up @@ -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
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion test/powershell/engine/Help/HelpSystem.Tests.ps1
Expand Up @@ -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 unexpectantly found for $helpTopic"
}
}

Expand Down