2018-02-13 18:23:53 +01:00
|
|
|
# Copyright (c) Microsoft Corporation. All rights reserved.
|
|
|
|
# Licensed under the MIT License.
|
2017-09-30 01:28:15 +02:00
|
|
|
|
|
|
|
Describe "Tests Debugger GetCallStack() on runspaces when attached to a WinRM host process" -Tags "CI" {
|
|
|
|
|
|
|
|
It -skip "Disabled test because it is fragile and does not consistently succeed on test VMs" { }
|
|
|
|
return
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
# Create PSSession
|
|
|
|
$wc = [System.Management.Automation.Runspaces.WSManConnectionInfo]::new()
|
2020-01-13 20:34:10 +01:00
|
|
|
$rs = [runspacefactory]::CreateRunspace($Host, $wc)
|
2017-09-30 01:28:15 +02:00
|
|
|
$rs.Open()
|
|
|
|
|
|
|
|
# Get WinRM host process id
|
|
|
|
[powershell] $ps = [powershell]::Create()
|
|
|
|
$ps.Runspace = $rs
|
|
|
|
$result = $ps.AddScript('$pid').Invoke()
|
|
|
|
It "Verifies the WinRM host process Id was found" {
|
2018-03-21 18:47:08 +01:00
|
|
|
$result | Should -Not -BeNullOrEmpty
|
|
|
|
($result.Count -eq 1) | Should -BeTrue
|
2017-09-30 01:28:15 +02:00
|
|
|
}
|
|
|
|
[int]$winRMHostProcId = $result[0]
|
|
|
|
|
|
|
|
# Run script to stop at breakpoint
|
|
|
|
$ps.Commands.Clear()
|
|
|
|
$ps.AddScript('"Hello"; Wait-Debugger; "Goodbye"').BeginInvoke()
|
|
|
|
|
|
|
|
# Attach to process.
|
|
|
|
Enter-PSHostProcess -Id $winRMHostProcId
|
|
|
|
|
|
|
|
# Get local remote runspace to attached process
|
|
|
|
$hostRS = Get-Runspace -Name PSAttachRunspace
|
|
|
|
It "Verifies that the attached-to host runspace was found" {
|
2018-03-21 18:47:08 +01:00
|
|
|
$hostRS | Should -Not -BeNullOrEmpty
|
|
|
|
($hostRS.RunspaceStateInfo.State -eq 'Opened') | Should -BeTrue
|
2017-09-30 01:28:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# Wait for host runspace to become available.
|
|
|
|
$count = 0
|
|
|
|
while (($hostRS.RunspaceAvailability -ne 'Available') -and ($count++ -lt 60))
|
|
|
|
{
|
2018-12-28 09:48:23 +01:00
|
|
|
Start-Sleep -Milliseconds 500
|
2017-09-30 01:28:15 +02:00
|
|
|
}
|
|
|
|
It "Verifies that the attached-to host runspace is available" {
|
2018-03-21 18:47:08 +01:00
|
|
|
($hostRS.RunspaceAvailability -eq 'Available') | Should -BeTrue
|
2017-09-30 01:28:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# Get call stack from default runspace.
|
|
|
|
$script = @'
|
|
|
|
$rs = Get-Runspace -Id 1
|
|
|
|
if ($null -eq $rs) { throw 'Runspace not found' }
|
|
|
|
return $rs.Debugger.GetCallStack()
|
|
|
|
'@
|
|
|
|
[powershell]$psHost = [powershell]::Create()
|
|
|
|
$psHost.Runspace = $hostRS
|
|
|
|
$psHost.AddScript($script)
|
|
|
|
$stack = $psHost.Invoke()
|
|
|
|
|
|
|
|
# Detach from process
|
|
|
|
Exit-PSHostProcess
|
|
|
|
|
|
|
|
It "Verifies a call stack was returned from the attached-to host." {
|
2018-03-21 18:47:08 +01:00
|
|
|
$stack | Should -Not -BeNullOrEmpty
|
|
|
|
($stack.Count -gt 0) | Should -BeTrue
|
2017-09-30 01:28:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
# Clean up
|
2020-01-13 20:34:10 +01:00
|
|
|
if ($Host.IsRunspacePushed) { $Host.PopRunspace() }
|
2017-09-30 01:28:15 +02:00
|
|
|
|
|
|
|
if ($null -ne $psHost) { $psHost.Dispose() }
|
|
|
|
if ($null -ne $hostRS) { $hostRS.Dispose() }
|
|
|
|
if ($null -ne $ps) { $ps.Dispose() }
|
|
|
|
if ($null -ne $rs) { $rs.Dispose() }
|
|
|
|
}
|
|
|
|
}
|