From 91447e62dec9fdbd5d24822eb8fefcc2b216d1b1 Mon Sep 17 00:00:00 2001 From: "James Truher [MSFT]" Date: Thu, 27 Jul 2023 15:04:30 -0700 Subject: [PATCH] Fixes for release tests (#20028) --- .../TabCompletion/TabCompletion.Tests.ps1 | 14 +++-- .../Test-Connection.Tests.ps1 | 14 +++-- .../Send-MailMessage.Tests.ps1 | 61 +++++++++++++++---- .../Remoting/RemoteSession.Basic.Tests.ps1 | 20 +++--- .../engine/Security/FileSignature.Tests.ps1 | 16 +++-- 5 files changed, 94 insertions(+), 31 deletions(-) diff --git a/test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1 b/test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1 index b2072345d9..378e9a8a74 100644 --- a/test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1 +++ b/test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1 @@ -1216,7 +1216,8 @@ class InheritedClassTest : System.Attribute It "Should keep '~' in completiontext when it's used to refer to home in input" { $res = TabExpansion2 -inputScript "~$separator" - $res.CompletionMatches[0].CompletionText | Should -BeLike "~$separator*" + $completedText = $res.CompletionMatches.CompletionText -join "," + $res.CompletionMatches[0].CompletionText | Should -BeLike "~$separator*" -Because "$completedText" } It "Should use '~' as relative filter text when not followed by separator" { @@ -1240,7 +1241,7 @@ class InheritedClassTest : System.Attribute $NewPath = Join-Path -Path $TestDrive -ChildPath $LiteralPath $null = New-Item -Path $NewPath -Force Push-Location $TestDrive - + $InputText = "Get-ChildItem -Path {0}.${separator}BacktickTest" $InputTextLiteral = "Get-ChildItem -LiteralPath {0}.${separator}BacktickTest" @@ -1263,7 +1264,9 @@ class InheritedClassTest : System.Attribute It 'Should keep custom drive names when completing file paths' { $TempDriveName = "asdf" $null = New-PSDrive -Name $TempDriveName -PSProvider FileSystem -Root $HOME - (TabExpansion2 -inputScript "${TempDriveName}:\").CompletionMatches[0].CompletionText | Should -BeLike "${TempDriveName}:*" + $completions = (TabExpansion2 -inputScript "${TempDriveName}:\").CompletionMatches + $completionText = $completions.CompletionText -join "," + $completions[0].CompletionText | Should -BeLike "${TempDriveName}:*" -Because "$completionText" Remove-PSDrive -Name $TempDriveName } @@ -2296,6 +2299,7 @@ dir -Recurse ` Context "Tab completion help test" { BeforeAll { + New-Item -ItemType File (Join-Path ${TESTDRIVE} "pwsh.xml") if ($IsWindows) { $userHelpRoot = Join-Path $HOME "Documents/PowerShell/Help/" } else { @@ -2469,10 +2473,10 @@ dir -Recurse ` } @{ Intent = 'Complete help keyword EXTERNALHELP argument' - Expected = Join-Path $PSHOME "pwsh.xml" + Expected = Join-Path $TESTDRIVE "pwsh.xml" TestString = @" <# -.EXTERNALHELP $PSHOME\pwsh.^ +.EXTERNALHELP $TESTDRIVE\pwsh.^ #> "@ } 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 cf6c7e2f78..2cd4e2a79b 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Test-Connection.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Test-Connection.Tests.ps1 @@ -105,10 +105,16 @@ Describe "Test-Connection" -tags "CI", "RequireSudoOnUnix" { It "Force IPv4 with implicit PingOptions" { $result = Test-Connection $testAddress -Count 1 -IPv4 - $result[0].Address | Should -BeExactly $testAddress - $result[0].Reply.Options.Ttl | Should -BeLessOrEqual 128 - if ($IsWindows) { - $result[0].Reply.Options.DontFragment | Should -BeFalse + $resultStatus = $result.Reply.Status + if ($resultStatus -eq "Success") { + $result[0].Address | Should -BeExactly $testAddress + $result[0].Reply.Options.Ttl | Should -BeLessOrEqual 128 + if ($IsWindows) { + $result[0].Reply.Options.DontFragment | Should -BeFalse + } + } + else { + Set-ItResult -Skipped -Because "Ping reply not Success, was: '$resultStatus'" } } diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Send-MailMessage.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Send-MailMessage.Tests.ps1 index 9bc5a4e302..93610ebda1 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Send-MailMessage.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Send-MailMessage.Tests.ps1 @@ -1,7 +1,11 @@ # Copyright (c) Microsoft Corporation. # Licensed under the MIT License. -if(-not ("netDumbster.smtp.SimpleSmtpServer" -as [type])) +# the send-mailmessage cmdlet is obsolete and will be removed in a future release. +# We will address no issues in this cmdlet, so these tests are marked as skipped. +$script:SkipTest = $true + +if(-not ("netDumbster.smtp.SimpleSmtpServer" -as [type]) -and ! $script:SkipTest) { Register-PackageSource -Name nuget.org -Location https://api.nuget.org/v3/index.json -ProviderName NuGet -ErrorAction SilentlyContinue @@ -22,6 +26,9 @@ $DefaultInputObject = @{ Describe "Send-MailMessage DRT Unit Tests" -Tags CI, RequireSudoOnUnix { BeforeAll { + if ($script:SkipTest) { + return + } $server = [netDumbster.smtp.SimpleSmtpServer]::Start(25) function Read-Mail @@ -37,6 +44,10 @@ Describe "Send-MailMessage DRT Unit Tests" -Tags CI, RequireSudoOnUnix { } BeforeEach { + if ($script:SkipTest) { + return + } + if($server) { $server.ClearReceivedEmail() @@ -44,6 +55,10 @@ Describe "Send-MailMessage DRT Unit Tests" -Tags CI, RequireSudoOnUnix { } AfterAll { + if ($script:SkipTest) { + return + } + if($server) { $server.Stop() @@ -109,7 +124,7 @@ Describe "Send-MailMessage DRT Unit Tests" -Tags CI, RequireSudoOnUnix { } ) - It "Shows obsolete message for cmdlet" { + It "Shows obsolete message for cmdlet" -skip:$script:SkipTest { $server | Should -Not -Be $null $powershell = [PowerShell]::Create() @@ -124,7 +139,7 @@ Describe "Send-MailMessage DRT Unit Tests" -Tags CI, RequireSudoOnUnix { $warnings[0].ToString() | Should -BeLike "The command 'Send-MailMessage' is obsolete. *" } - It "Can send mail message using named parameters " -TestCases $testCases { + It "Can send mail message using named parameters " -TestCases $testCases -skip:$script:SkipTest { param($InputObject) $server | Should -Not -Be $null @@ -156,7 +171,7 @@ Describe "Send-MailMessage DRT Unit Tests" -Tags CI, RequireSudoOnUnix { $mail.MessageParts[0].BodyData | Should -BeExactly $InputObject.Body } - It "Can send mail message using pipline named parameters " -TestCases $testCases -Pending { + It "Can send mail message using pipline named parameters " -TestCases $testCases -skip:$script:SkipTest { param($InputObject) Set-TestInconclusive "As of right now the Send-MailMessage cmdlet does not support piping named parameters (see issue 7591)" @@ -193,6 +208,10 @@ Describe "Send-MailMessage DRT Unit Tests" -Tags CI, RequireSudoOnUnix { Describe "Send-MailMessage Feature Tests" -Tags Feature, RequireSudoOnUnix { BeforeAll { + if ($script:SkipTest) { + return + } + function Read-Mail { param() @@ -206,6 +225,10 @@ Describe "Send-MailMessage Feature Tests" -Tags Feature, RequireSudoOnUnix { } BeforeEach { + if ($script:SkipTest) { + return + } + if($server) { $server.ClearReceivedEmail() @@ -214,17 +237,25 @@ Describe "Send-MailMessage Feature Tests" -Tags Feature, RequireSudoOnUnix { Context "Default Port 25" { BeforeAll { + if ($script:SkipTest) { + return + } + $server = [netDumbster.smtp.SimpleSmtpServer]::Start(25) } AfterAll { + if ($script:SkipTest) { + return + } + if($server) { $server.Stop() } } - It "Can throw on wrong mail addresses" { + It "Can throw on wrong mail addresses" -skip:$script:SkipTest { $server | Should -Not -Be $null $obj = $DefaultInputObject.Clone() @@ -233,7 +264,7 @@ Describe "Send-MailMessage Feature Tests" -Tags Feature, RequireSudoOnUnix { { Send-MailMessage @obj -ErrorAction Stop } | Should -Throw -ErrorId "FormatException,Microsoft.PowerShell.Commands.SendMailMessage" } - It "Can send mail with free-form email address" { + It "Can send mail with free-form email address" -skip:$script:SkipTest { $server | Should -Not -Be $null $obj = $DefaultInputObject.Clone() @@ -248,7 +279,7 @@ Describe "Send-MailMessage Feature Tests" -Tags Feature, RequireSudoOnUnix { $mail.ToAddresses | Should -BeExactly "user02@example.com" } - It "Can send mail with high priority" { + It "Can send mail with high priority" -skip:$script:SkipTest { $server | Should -Not -Be $null Send-MailMessage @DefaultInputObject -Priority High -ErrorAction SilentlyContinue @@ -257,7 +288,7 @@ Describe "Send-MailMessage Feature Tests" -Tags Feature, RequireSudoOnUnix { $mail.Priority | Should -BeExactly "urgent" } - It "Can send mail with HTML content as body" { + It "Can send mail with HTML content as body" -skip:$script:SkipTest { $server | Should -Not -Be $null $obj = $DefaultInputObject.Clone() @@ -271,7 +302,7 @@ Describe "Send-MailMessage Feature Tests" -Tags Feature, RequireSudoOnUnix { $mail.MessageParts[0].BodyData | Should -Be $obj.Body } - It "Can send mail with UTF8 encoding" { + It "Can send mail with UTF8 encoding" -skip:$script:SkipTest { $server | Should -Not -Be $null $obj = $DefaultInputObject.Clone() @@ -286,7 +317,7 @@ Describe "Send-MailMessage Feature Tests" -Tags Feature, RequireSudoOnUnix { $utf8Text | Should -Be $obj.Body } - It "Can send mail with attachments" { + It "Can send mail with attachments" -skip:$script:SkipTest { $attachment1 = "TestDrive:\attachment1.txt" $attachment2 = "TestDrive:\attachment2.txt" @@ -311,17 +342,25 @@ Describe "Send-MailMessage Feature Tests" -Tags Feature, RequireSudoOnUnix { Context "Custom Port 2525" { BeforeAll { + if ($script:SkipTest) { + return + } + $server = [netDumbster.smtp.SimpleSmtpServer]::Start(2525) } AfterAll { + if ($script:SkipTest) { + return + } + if($server) { $server.Stop() } } - It "Can send mail message using custom port 2525" { + It "Can send mail message using custom port 2525" -skip:$script:SkipTest { $server | Should -Not -Be $null $server.ReceivedEmailCount | Should -BeExactly 0 diff --git a/test/powershell/engine/Remoting/RemoteSession.Basic.Tests.ps1 b/test/powershell/engine/Remoting/RemoteSession.Basic.Tests.ps1 index e4432cd484..7c0a0f8172 100644 --- a/test/powershell/engine/Remoting/RemoteSession.Basic.Tests.ps1 +++ b/test/powershell/engine/Remoting/RemoteSession.Basic.Tests.ps1 @@ -59,11 +59,11 @@ Describe "Basic Auth over HTTP not allowed on Unix" -Tag @("CI") { Describe "JEA session Transcript script test" -Tag @("Feature", 'RequireAdminOnWindows') { BeforeAll { - $originalDefaultParameterValues = $PSDefaultParameterValues.Clone() + $originalDefaultParameterValues = $global:PSDefaultParameterValues.Clone() if ( ! $IsWindows -or !(Test-CanWriteToPsHome)) { - $PSDefaultParameterValues["it:skip"] = $true + $global:PSDefaultParameterValues["it:skip"] = $true } else { @@ -101,11 +101,11 @@ Describe "JEA session Transcript script test" -Tag @("Feature", 'RequireAdminOnW Describe "JEA session Get-Help test" -Tag @("CI", 'RequireAdminOnWindows') { BeforeAll { - $originalDefaultParameterValues = $PSDefaultParameterValues.Clone() + $originalDefaultParameterValues = $global:PSDefaultParameterValues.Clone() if ( ! $IsWindows -or !(Test-CanWriteToPsHome)) { - $PSDefaultParameterValues["it:skip"] = $true + $global:PSDefaultParameterValues["it:skip"] = $true } else { @@ -141,16 +141,22 @@ Describe "JEA session Get-Help test" -Tag @("CI", 'RequireAdminOnWindows') { Describe "Remoting loopback tests" -Tags @('CI', 'RequireAdminOnWindows') { BeforeAll { - $originalDefaultParameterValues = $PSDefaultParameterValues.Clone() + $originalDefaultParameterValues = $global:PSDefaultParameterValues.Clone() if ( ! $IsWindows ) { - $PSDefaultParameterValues["it:skip"] = $true + $global:PSDefaultParameterValues["it:skip"] = $true } else { Enable-PSRemoting -SkipNetworkProfileCheck - $endPoint = (Get-PSSessionConfiguration -Name "PowerShell.$(${PSVersionTable}.GitCommitId)").Name + $configName = "PowerShell." + $PSVersionTable.GitCommitId + $configuration = Get-PSSessionConfiguration -Name $configName -ErrorAction Ignore + if ($null -eq $configuration) { + $global:PSDefaultParameterValues["it:skip"] = $true + return + } + $endPoint = $configuration.Name $disconnectedSession = New-RemoteSession -ConfigurationName $endPoint -ComputerName localhost | Disconnect-PSSession $closedSession = New-RemoteSession -ConfigurationName $endPoint -ComputerName localhost $closedSession.Runspace.Close() diff --git a/test/powershell/engine/Security/FileSignature.Tests.ps1 b/test/powershell/engine/Security/FileSignature.Tests.ps1 index 7a67d4ac51..0a9b283273 100644 --- a/test/powershell/engine/Security/FileSignature.Tests.ps1 +++ b/test/powershell/engine/Security/FileSignature.Tests.ps1 @@ -89,14 +89,22 @@ Describe "Windows file content signatures" -Tags @('Feature', 'RequireAdminOnWin } AfterAll { - if ($shouldSkip) { return } - Remove-Item -Path Cert:\LocalMachine\Root\$caRootThumbprint -Force - Remove-Item -Path Cert:\LocalMachine\TrustedPublisher\$signingThumbprint -Force - Remove-Item -Path Cert:\CurrentUser\My\$signingThumbprint -Force + $paths = @( + "Cert:\LocalMachine\Root\$caRootThumbprint" + "Cert:\LocalMachine\TrustedPublisher\$signingThumbprint" + "Cert:\CurrentUser\My\$signingThumbprint" + ) + + foreach($path in $paths) { + if (Test-Path $path) { + # failing to remove is not fatal + Remove-Item -Force -Path $path -ErrorAction Continue + } + } } It "Validates signature using path on even char count with Encoding " -TestCases @(