Skip to content

Conversation

@TylerLeonhardt
Copy link
Member

PR Summary

This PR totally refactors the Enter-PSHostProcess tests.

fixes #8971

PR Context

The biggest change was that rather then sharing an instance of PowerShell, the tests will each have their own instance of PowerShell to interact with.

PR Checklist

@TylerLeonhardt
Copy link
Member Author

Just to be sure, I'd like to run CI a few times to see if the failure shows up. I'm going to 😴but if someone in some other place (where it's not late) wants to kick them off again by adding commits or something, that'd be helpful!

@iSazonov
Copy link
Collaborator

iSazonov commented Feb 28, 2019

I'd can restart but it is already failed :-(

@TylerLeonhardt
Copy link
Member Author

Jeez... OK... Ran this like 20 times in a container and couldn't repro the issue.

Back to the drawing board...

@TylerLeonhardt
Copy link
Member Author

I can repro the same problem if I do this:

> "Enter-PSHostProcess -Id 40328 -ErrorAction Stop
                  `$pid
                  Exit-PSHostProcess" >  test.ps1

> ./test.ps1

@iSazonov
Copy link
Collaborator

Looks tricky!
Perhaps @JamesWTruher has ideas how make the tests reliable.

@TylerLeonhardt
Copy link
Member Author

TylerLeonhardt commented Feb 28, 2019

The odd thing is... I added -ErrorAction Stop to the Enter-PSHostProcess's so this this failure is even more peculiar because $pid should never get run. It should stop executing when Enter-PSHostProcess fails...

"Enter-PSHostProcess -Id $($pwsh.Id) -ErrorAction Stop
`$pid
Exit-PSHostProcess" | pwsh -c - | Should -Be $pwsh.Id
TEST FAILURES
Description: Can enter, exit, and re-enter another PSHost
Name:        Enter-PSHostProcess tests.By Process Id.Can enter, exit, and re-enter another PSHost
message:
Expected 872, but got '1432'.
stack-trace:
at <ScriptBlock>, D:\a\1\s\test\powershell\Modules\Microsoft.PowerShell.Core\Enter-PSHostProcess.Tests.ps1: line 47
47:                 Exit-PSHostProcess" | pwsh -c - | Should -Be $pwsh.Id

In my local testing... I noticed that:

Expected 872, but got '1432'.

1432 was typically the $pid of the pwsh that executed Enter-PSHostProcess but that could be a red herring

@iSazonov
Copy link
Collaborator

@TylerLeonhardt Maybe #1995?

@vexx32
Copy link
Collaborator

vexx32 commented Feb 28, 2019

@TylerLeonhardt does this repro the issue?

@'
try {{
    Enter-PSHostProcess -Id {0} -ErrorAction Stop
    $pid
}}
finally {{
    Exit-PSHostProcess
}}
'@ -f $pwsh.Id | pwsh -c - | Should -Be $pwsh.Id

In other words, is the -ErrorAction Stop behaving as we expect?


It "Should throw if CustomPipeName is too long on Linux or macOS" {
It "Should throw if CustomPipeName is too long on Linux or macOS" -Skip:($IsWindows) {
$longPipeName = "DoggoipsumwaggywagssmolborkingdoggowithalongsnootforpatsdoingmeafrightenporgoYapperporgolongwatershoobcloudsbigolpupperlengthboy"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this a thing?
(just wondering)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because on unix they're not called "Named Pipes" they're called "UNIX domain sockets" which have a limit on macOS and Linux.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant $longPipeName = "DoggoipsumwaggywagssmolborkingdoggowithalongsnootforpatsdoingmeafrightenporgoYapperporgolongwatershoobcloudsbigolpupperlengthboy"

Copy link
Collaborator

@iSazonov iSazonov Feb 28, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 to generate needed length and add a comment.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see.

@TylerLeonhardt
Copy link
Member Author

TylerLeonhardt commented Feb 28, 2019

@iSazonov Maybe... but I had 100% success rate from 20 runs on my mac and in an ubuntu container.

@vexx32 I'm open to give that a try... the test should still fail, but maybe we'll get more info as to why (also edited your comment to escape the {})

@iSazonov
Copy link
Collaborator

@TylerLeonhardt I suggest replace "-c" with "-file" to avoid escaping problem.

test.ps1 Outdated
@@ -0,0 +1,3 @@
Enter-PSHostProcess -Id 40328 -ErrorAction Stop
$pid
Exit-PSHostProcess
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will do. Still getting data. moved to WIP

@TylerLeonhardt TylerLeonhardt changed the title Fix Enter-PSHostProcess tests flakiness WIP: Fix Enter-PSHostProcess tests flakiness Feb 28, 2019
@TylerLeonhardt
Copy link
Member Author

@iSazonov

I suggest replace "-c" with "-file" to avoid escaping problem.

Sorry, where exactly?

}}
finally {{
Exit-PSHostProcess
}}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

double braces?

Copy link
Member Author

@TylerLeonhardt TylerLeonhardt Feb 28, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need this for template strings to escape { and } otherwise it throws a bad formatting exception

@TylerLeonhardt
Copy link
Member Author

jeez...

Description: Can enter, exit, and re-enter another PSHost
Name:        Enter-PSHostProcess tests.By Process Id.Can enter, exit, and re-enter another PSHost
message:
Expected 4564, but got $null.
stack-trace:
at <ScriptBlock>, D:\a\1\s\test\powershell\Modules\Microsoft.PowerShell.Core\Enter-PSHostProcess.Tests.ps1: line 56
56: '@ -f $pwsh.Id | pwsh -c - | Should -Be $pwsh.Id
Description: Can enter and exit another Windows PowerShell PSHost
Name:        Enter-PSHostProcess tests.By Process Id.Can enter and exit another Windows PowerShell PSHost
message:
Expected 3752, but got $null.
stack-trace:
at <ScriptBlock>, D:\a\1\s\test\powershell\Modules\Microsoft.PowerShell.Core\Enter-PSHostProcess.Tests.ps1: line 93
93: '@ -f $powershell.Id | pwsh -c - | Should -Be $powershell.Id
Description: Can enter, exit, and re-enter using CustomPipeName
Name:        Enter-PSHostProcess tests.By CustomPipeName.Can enter, exit, and re-enter using CustomPipeName
message:
Expected 360, but got $null.
stack-trace:
at <ScriptBlock>, D:\a\1\s\test\powershell\Modules\Microsoft.PowerShell.Core\Enter-PSHostProcess.Tests.ps1: line 139
139: '@ -f $pipeName | pwsh -c - | Should -Be $pwsh.Id
3 tests in test/powershell failed
At D:\a\1\s\build.psm1:1371 char:13
+             throw "$($x.'test-results'.failures) tests in $TestArea f ...
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (3 tests in test/powershell failed:String) [], RuntimeException
    + FullyQualifiedErrorId : 3 tests in test/powershell failed


}
It "Can enter, exit, and re-enter another PSHost" {
# $pwsh = Start-PSProcess
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why commented?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should probably just hold off on reviewing. This issue only repros in CI... so it'll be a lot of trial and error.

When I remove WIP, you can review for real.

Open to suggestions on how to fix the tests though 😅

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My suggestion was above - use "-file" parameter and put the temporary test script in file.

@TylerLeonhardt
Copy link
Member Author

If this succeeds CI, please restart a few times. I need 😴

If this works, I'll need to add a test to Startup.Tests.ps1 to test that pwsh -CustomPipeName foo actually works. And can move the last test into that as well.

@TylerLeonhardt
Copy link
Member Author

TylerLeonhardt commented Mar 1, 2019

it... passed once so far. Triggered again with some comments... now time for bed.

@iSazonov
Copy link
Collaborator

iSazonov commented Mar 1, 2019

I restarted CI-windows as you ask (previous was Ok).

# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

function Wait-JobPid {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like we should have a count limit in the loop, in case the script fails for some reason, and throw on failure.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added timeout

@TylerLeonhardt
Copy link
Member Author

I spoke to @PaulHigin about this and we agreed that since the tests have been reliable in this PR, we'll keep them in. If they become a problem again, we can remove the ones that use *-PSHostProcess

@iSazonov iSazonov added the CL-Test Indicates that a PR should be marked as a test change in the Change Log label Mar 7, 2019
$pipePath = Get-PipePath $pipeName

# The pipePath should be created by the time the -Command is executed.
pwsh -CustomPipeName $pipeName -Command "Test-Path '$pipePath'" | Should -BeTrue
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use $powershell instead. Using pwsh will call the installed pwsh on the system, not the one which we build.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If what you say is true, then none of my CustomPipeName tests would be passing, right? I'm willing to change it though. Is that a magic variable or something?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably set during the build or on test initialization with Start-PSPester, I'd imagine. 🙂

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I see what you're saying now :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've fixed this so it's consistent


"`$pid" | & $powershell -CustomPipeName $longPipeName -c -
# 64 is the ExitCode for BadCommandLineParameter
$LASTEXITCODE | Should -Be 64
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can check for FullyQualifiedErrorId that is not localized.

Should -BeTrue -Because "The script was able to re-enter another process and grab the pid of '$powershellId'."

} finally {
$powershellJob | Stop-Process -Force -ErrorAction SilentlyContinue
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be Stop-Job | Remove-Job instead?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you're totally right. Added.

$ps.AddScript('$pid').Invoke() | Should -Be $pwsh.Id
$ps.AddScript('$pid').Invoke() | Should -Be $pwshId
} finally {
$rs.Dispose()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also add $ps.Dispose()

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added

@adityapatwardhan
Copy link
Member

@TylerLeonhardt - To fix the static analysis CI - update the link to https://www.itprotoday.com/powershell/powershell-basics-remote-management

@adityapatwardhan
Copy link
Member

Restarted Windows CI

@adityapatwardhan
Copy link
Member

@TravisEz13 can you update your review?

@adityapatwardhan
Copy link
Member

@TylerLeonhardt Just realized the tests that were changed are tagged as Feature. Please push an empty commit with the commit message as [Feature] to execute those tests.

@TylerLeonhardt
Copy link
Member Author

triggered

@adityapatwardhan adityapatwardhan merged commit a26d639 into PowerShell:master Mar 11, 2019
@TylerLeonhardt TylerLeonhardt deleted the fix-enter-pshostprocess-tests branch March 11, 2019 18:15
@daxian-dbw daxian-dbw added this to the 6.2.0-GA-Consider milestone Mar 11, 2019
@TravisEz13 TravisEz13 modified the milestones: 6.2.0-GA-Consider, 6.2.0 Mar 21, 2019
@TravisEz13
Copy link
Member

This has become unreliable again. I have filed #11168

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CL-Test Indicates that a PR should be marked as a test change in the Change Log

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Enter-PSHostProcess tests.By Process Id.Can enter using NamedPipeConnectionInfo is Failing 6% of the time

8 participants