Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3a6fd10

Browse files
Joel BennettJoel Bennett
authored andcommitted
Add tests for parallel remoting and theadjobs
ParallelRunspace tests (ThreadJobs) are WIP: currently fail
1 parent 93b5033 commit 3a6fd10

File tree

3 files changed

+217
-8
lines changed

3 files changed

+217
-8
lines changed

Tests/ParallelRemote.Tests.ps1

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
#requires -Module PowerShellLogging, ThreadJob
2+
param($Count = 4)
3+
4+
Describe "Working when called in a remote runspace" -Tag "Remoting" {
5+
6+
$Path = "TestDrive:\log{0}.txt"
7+
$Path = (Join-Path (Convert-Path (Split-Path $Path)) (Split-Path $Path -Leaf))
8+
9+
BeforeAll {
10+
$script:PowerShell = $(
11+
foreach($index in 1..$Count) {
12+
$LocalHost = [System.Management.Automation.Runspaces.WSManConnectionInfo]@{ComputerName = "."; EnableNetworkAccess = $true }
13+
$Runspace = [runspacefactory]::CreateRunspace($LocalHost)
14+
$Runspace.Open()
15+
$PowerShell = [PowerShell]::Create()
16+
$PowerShell.Runspace = $Runspace
17+
$PowerShell
18+
}
19+
)
20+
}
21+
22+
AfterAll {
23+
foreach($PS in $script:PowerShell) {
24+
$PS.Runspace.Dispose()
25+
$PS.Dispose()
26+
}
27+
}
28+
29+
$TestScript = "
30+
Import-Module PowerShellLogging
31+
`$Logging = Enable-LogFile -Path '${Path}'
32+
Write-Host 'This is a host test from attempt {0}'
33+
'${Path}'
34+
Start-Sleep 2
35+
Write-Verbose 'This is a verbose test from attempt {0}' -Verbose
36+
Disable-LogFile `$Logging
37+
"
38+
39+
It "Should not crash when used" {
40+
$script:Results = & {
41+
$i = 0
42+
foreach($PS in $script:PowerShell) {
43+
$i += 1
44+
Start-ThreadJob { param($PS, $Script) $PS.AddScript($Script).Invoke() } -ArgumentList $PS, ($TestScript -f $i)
45+
}
46+
} | Wait-Job | Receive-Job
47+
}
48+
49+
It "Should not interfere with output" {
50+
$script:Results.Count | Should -Be $script:PowerShell.Count
51+
$i = 0
52+
foreach ($resultPath in $script:Results) {
53+
$i += 1
54+
$resultPath | Should -Be ($Path -f $i)
55+
}
56+
}
57+
58+
It "Should not cause Enable-LogFile to fail" {
59+
foreach($PS in $script:PowerShell) {
60+
$PS.Streams.Error.InvocationInfo.MyCommand.Name | Should -Not -Contain "Enable-LogFile"
61+
}
62+
}
63+
64+
It "Should not cause Write-Host to fail" {
65+
foreach ($PS in $script:PowerShell) {
66+
$PS.Streams.Error.InvocationInfo.MyCommand.Name | Should -Not -Contain "Write-Host"
67+
}
68+
}
69+
70+
It "Should not cause Write-Verbose to fail" {
71+
foreach ($PS in $script:PowerShell) {
72+
$PS.Streams.Error.InvocationInfo.MyCommand.Name | Should -Not -Contain "Write-Verbose"
73+
}
74+
}
75+
76+
It "Should not cause Disable-LogFile to fail" {
77+
foreach ($PS in $script:PowerShell) {
78+
$PS.Streams.Error.InvocationInfo.MyCommand.Name | Should -Not -Contain "Disable-LogFile"
79+
}
80+
}
81+
82+
It "Should not cause any errors" {
83+
foreach ($PS in $script:PowerShell) {
84+
$PS.Streams.Error.Count | Should -Be 0
85+
}
86+
}
87+
88+
Write-Warning "Expecting $($script:Results.Count) log files!"
89+
90+
It "Should create the log file" {
91+
# this is enough to prove the logging works
92+
$i = 0
93+
foreach($PS in $script:PowerShell) {
94+
$i += 1
95+
($Path -f $i) | Should -Exist
96+
}
97+
}
98+
99+
$i = 0
100+
foreach ($PS in $script:PowerShell) {
101+
$i += 1
102+
It "Should log host output to $($Path -f $i)" -Skip:(!(Test-Path ($Path -f $i))) {
103+
(Get-Content ($Path -f $i)) -match "This is a host test from attempt $i$" | Should -Not -BeNullOrEmpty
104+
}
105+
}
106+
107+
$i = 0
108+
foreach ($PS in $script:PowerShell) {
109+
$i += 1
110+
It "Should log host output to $($Path -f $i)" -Skip:(!(Test-Path ($Path -f $i))) {
111+
(Get-Content ($Path -f $i)) -match "This is a verbose test from attempt $i$" | Should -Not -BeNullOrEmpty
112+
}
113+
}
114+
}

Tests/ParallelRunspace.Tests.ps1

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#requires -Module PowerShellLogging, ThreadJob
2+
param($Count = 2)
3+
4+
Describe "Working when called in a remote runspace" -Tag "ThreadJob", "WIP" {
5+
6+
$Path = "TestDrive:\log{0}.txt"
7+
$Path = (Join-Path (Convert-Path (Split-Path $Path)) (Split-Path $Path -Leaf))
8+
9+
$TestScript = "
10+
Import-Module PowerShellLogging
11+
`$Logging = Enable-LogFile -Path '${Path}'
12+
Write-Host 'This is a host test from attempt {0}'
13+
'${Path}'
14+
Start-Sleep 2
15+
Write-Verbose 'This is a verbose test from attempt {0}' -Verbose
16+
Disable-LogFile `$Logging
17+
"
18+
19+
$TestScript = {
20+
param($Path, $Index)
21+
Import-Module PowerShellLogging
22+
$Logging = Enable-LogFile -Path $Path
23+
Microsoft.PowerShell.Utility\Write-Host "This is a host test from attempt $index"
24+
$Path
25+
Microsoft.PowerShell.Utility\Write-Verbose 'This is a verbose test' -Verbose
26+
# Does not output, because Verbose is suppressed
27+
Microsoft.PowerShell.Utility\Write-Verbose "This is a verbose test from attempt $index" -Verbose
28+
Disable-LogFile $Logging
29+
}
30+
31+
32+
It "Should not crash when used" {
33+
$script:Results = & {
34+
foreach ($i in 1..$Count) {
35+
Start-ThreadJob $TestScript -ArgumentList ($Path -f $i), $i
36+
}
37+
} | Wait-Job | Receive-Job -ErrorVariable Ev
38+
$Script:Ev = $Ev
39+
}
40+
41+
It "Should not interfere with output" {
42+
$script:Results.Count | Should -Be $Count
43+
$i = 0
44+
foreach ($resultPath in $script:Results) {
45+
$i += 1
46+
$resultPath | Should -Be ($Path -f $i)
47+
}
48+
}
49+
50+
It "Should not cause Enable-LogFile to fail" {
51+
$script:Ev.InvocationInfo.MyCommand.Name | Should -Not -Contain "Enable-LogFile"
52+
}
53+
54+
It "Should not cause Write-Host to fail" {
55+
$script:Ev.InvocationInfo.MyCommand.Name | Should -Not -Contain "Write-Host"
56+
}
57+
58+
It "Should not cause Write-Verbose to fail" {
59+
$script:Ev.InvocationInfo.MyCommand.Name | Should -Not -Contain "Write-Verbose"
60+
}
61+
62+
It "Should not cause Disable-LogFile to fail" {
63+
$script:Ev.InvocationInfo.MyCommand.Name | Should -Not -Contain "Disable-LogFile"
64+
}
65+
66+
It "Should not cause any errors" {
67+
$script:Ev.Count | Should -Be 0
68+
}
69+
70+
71+
Write-Warning "Expecting $($script:Results.Count) log files!"
72+
73+
It "Should create the log file" {
74+
# this is enough to prove the logging works
75+
foreach ($i in 1..$Count) {
76+
($Path -f $i) | Should -Exist
77+
}
78+
}
79+
80+
81+
foreach ($i in 1..$Count) {
82+
It "Should log host output to $($Path -f $i)" -Skip:(!(Test-Path ($Path -f $i))) {
83+
(Get-Content ($Path -f $i)) -match "This is a host test from attempt $i$" | Should -Not -BeNullOrEmpty
84+
}
85+
}
86+
87+
88+
foreach ($i in 1..$Count) {
89+
It "Should log host output to $($Path -f $i)" -Skip:(!(Test-Path ($Path -f $i))) {
90+
(Get-Content ($Path -f $i)) -match "This is a verbose test from attempt $i$" | Should -Not -BeNullOrEmpty
91+
}
92+
}
93+
}

Tests/Simple.Tests.ps1

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,42 +5,44 @@ Describe "Working in scripts run locally in the host" {
55
$Path = (Join-Path (Convert-Path (Split-Path $Path)) (Split-Path $Path -Leaf))
66

77
$TestScript = {
8+
[CmdletBinding()]param()
9+
810
Import-Module PowerShellLogging
911
$Logging = Enable-LogFile -Path $Path
1012
Write-Host 'This is a host test'
1113
'Returned OK'
12-
Write-Verbose 'This is a verbose test' -Verbose
13-
# Does not output, because Verbose is suppressed
14+
Write-Verbose 'This is a verbose test' -verbose
1415
Write-Verbose 'This is a another verbose test'
1516
Disable-LogFile $Logging
1617
}
1718

1819
It "Should not crash when used" {
19-
$script:Result = & $TestScript
20+
$script:Result = & $TestScript -ErrorVariable Ev
21+
$script:Ev = $Ev
2022
}
2123

2224
It "Should not interfere with output" {
2325
$script:Result | Should -Be "Returned OK"
2426
}
2527

2628
It "Should not cause Enable-LogFile to fail" {
27-
$script:PowerShell.Streams.Error.InvocationInfo.MyCommand.Name | Should -Not -Contain "Enable-LogFile"
29+
$script:Ev.InvocationInfo.MyCommand.Name | Should -Not -Contain "Enable-LogFile"
2830
}
2931

3032
It "Should not cause Write-Host to fail" {
31-
$script:PowerShell.Streams.Error.InvocationInfo.MyCommand.Name | Should -Not -Contain "Write-Host"
33+
$script:Ev.InvocationInfo.MyCommand.Name | Should -Not -Contain "Write-Host"
3234
}
3335

3436
It "Should not cause Write-Verbose to fail" {
35-
$script:PowerShell.Streams.Error.InvocationInfo.MyCommand.Name | Should -Not -Contain "Write-Verbose"
37+
$script:Ev.InvocationInfo.MyCommand.Name | Should -Not -Contain "Write-Verbose"
3638
}
3739

3840
It "Should not cause Disable-LogFile to fail" {
39-
$script:PowerShell.Streams.Error.InvocationInfo.MyCommand.Name | Should -Not -Contain "Disable-LogFile"
41+
$script:Ev.InvocationInfo.MyCommand.Name | Should -Not -Contain "Disable-LogFile"
4042
}
4143

4244
It "Should not cause any errors" {
43-
$script:PowerShell.Streams.Error.Count | Should -Be 0
45+
$script:Ev.Count | Should -Be 0
4446
}
4547

4648
It "Should create the log file" {

0 commit comments

Comments
 (0)