Skip to content

Commit d9be42b

Browse files
committed
chore(windows): run each JDK tests in parallel
1 parent e9e802e commit d9be42b

File tree

2 files changed

+70
-44
lines changed

2 files changed

+70
-44
lines changed

build.ps1

Lines changed: 49 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -80,31 +80,31 @@ Function Test-CommandExists {
8080
}
8181
}
8282

83+
# Ex: Test-Image -AgentType inbound-agent -RemotingVersion 3345.v03dee9b_f88fc -ImageName docker.io/jenkins/agent:jdk21-windowsservercore-ltsc2019 -JavaVersion 21.0.7_6
8384
function Test-Image {
8485
param (
85-
[String] $AgentTypeAndImageName
86+
[String] $AgentType,
87+
[String] $RemotingVersion,
88+
[String] $ImageName,
89+
[String] $JavaVersion
8690
)
8791

88-
# Ex: agent|docker.io/jenkins/agent:jdk21-windowsservercore-ltsc2019|21.0.7_6
89-
$items = $AgentTypeAndImageName.Split('|')
90-
$agentType = $items[0]
91-
$imageName = $items[1] -replace 'docker.io/', ''
92-
$javaVersion = $items[2]
92+
$imageName = $ImageName -replace 'docker.io/', ''
9393
$imageNameItems = $imageName.Split(':')
9494
$imageTag = $imageNameItems[1]
9595

96-
Write-Host "= TEST: Testing ${imageName} image:"
96+
Write-Host "=== TEST: Testing ${imageName} image:"
9797

9898
$env:IMAGE_NAME = $imageName
9999
$env:VERSION = "$RemotingVersion"
100-
$env:JAVA_VERSION = "$javaVersion"
100+
$env:JAVA_VERSION = "$JavaVersion"
101101

102-
$targetPath = '.\target\{0}\{1}' -f $agentType, $imageTag
102+
$targetPath = '.\target\{0}\{1}' -f $AgentType, $imageTag
103103
if (Test-Path $targetPath) {
104104
Remove-Item -Recurse -Force $targetPath
105105
}
106106
New-Item -Path $targetPath -Type Directory | Out-Null
107-
$configuration.Run.Path = 'tests\{0}.Tests.ps1' -f $agentType
107+
$configuration.Run.Path = 'tests\{0}.Tests.ps1' -f $AgentType
108108
$configuration.TestResult.OutputPath = '{0}\junit-results.xml' -f $targetPath
109109
$TestResults = Invoke-Pester -Configuration $configuration
110110
$failed = $false
@@ -173,6 +173,8 @@ Test-CommandExists 'docker-compose'
173173
Test-CommandExists 'docker buildx'
174174
Test-CommandExists 'yq'
175175

176+
$testImageFunction = ${function:Test-Image}
177+
$workspacePath = (Get-Location).Path
176178
foreach($agentType in $AgentTypes) {
177179
$dockerComposeFile = 'build-windows_{0}_{1}.yaml' -f $AgentType, $ImageType
178180
$baseDockerCmd = 'docker-compose --file={0}' -f $dockerComposeFile
@@ -219,23 +221,45 @@ foreach($agentType in $AgentTypes) {
219221
Install-Module -Force -Name Pester -MaximumVersion 5.3.3
220222
}
221223

222-
Import-Module Pester
223-
Write-Host '= TEST: Setting up Pester environment...'
224-
$configuration = [PesterConfiguration]::Default
225-
$configuration.Run.PassThru = $true
226-
$configuration.Run.Path = '.\tests'
227-
$configuration.Run.Exit = $true
228-
$configuration.TestResult.Enabled = $true
229-
$configuration.TestResult.OutputFormat = 'JUnitXml'
230-
$configuration.Output.Verbosity = 'Diagnostic'
231-
$configuration.CodeCoverage.Enabled = $false
232-
233224
Write-Host "= TEST: Testing all ${agentType} images..."
234-
# Only fail the run afterwards in case of any test failures
225+
$jdks = Invoke-Expression "$baseDockerCmd config" | yq --unwrapScalar --output-format json '.services' | ConvertFrom-Json
226+
227+
# Run Test-Image in parallel for each JDK
228+
$jobs = @()
229+
foreach ($jdk in $jdks.PSObject.Properties) {
230+
$image = $jdk.Value.image
231+
$javaVersion = $jdk.Value.build.args.JAVA_VERSION
232+
Write-Host "= TEST: Starting ${image} tests in parallel..."
233+
$jobs += Start-Job -ScriptBlock {
234+
param($anAgentType, $aRemotingVersion, $anImage, $aJavaVersion, $aTestImageFunction, $aWorkspacePath)
235+
236+
Write-Host "== TEST: Setting up Pester environment for $anImage testing..."
237+
Import-Module Pester
238+
$configuration = [PesterConfiguration]::Default
239+
$configuration.Run.PassThru = $true
240+
$configuration.Run.Path = '{0}\tests' -f $aWorkspacePath
241+
$configuration.Run.Exit = $true
242+
$configuration.TestResult.Enabled = $true
243+
$configuration.TestResult.OutputFormat = 'JUnitXml'
244+
$configuration.Output.Verbosity = 'Diagnostic'
245+
$configuration.CodeCoverage.Enabled = $false
246+
Set-Item -Path Function:Test-Image -Value $aTestImageFunction
247+
Set-Location -Path $aWorkspacePath
248+
249+
Test-Image -AgentType $anAgentType -RemotingVersion $aRemotingVersion -ImageName $anImage -JavaVersion $aJavaVersion
250+
} -ArgumentList $agentType, $RemotingVersion, $image, $javaVersion, $testImageFunction, $workspacePath
251+
}
252+
253+
# Wait for all jobs to finish and collect results
235254
$testFailed = $false
236-
$imageDefinitions = Invoke-Expression "$baseDockerCmd config" | yq --unwrapScalar --output-format json '.services' | ConvertFrom-Json
237-
foreach ($imageDefinition in $imageDefinitions.PSObject.Properties) {
238-
$testFailed = $testFailed -or (Test-Image ('{0}|{1}|{2}' -f $agentType, $imageDefinition.Value.image, $imageDefinition.Value.build.args.JAVA_VERSION))
255+
foreach ($job in $jobs) {
256+
$result = Receive-Job -Job $job -Wait
257+
if ($result.Failed) {
258+
Write-Host "= TEST: Error(s), see the results below"
259+
$result.Tests | ConvertTo-Json | Write-Host
260+
$testFailed = $true
261+
}
262+
Remove-Job $job
239263
}
240264

241265
# Fail if any test failures

tests/inbound-agent.Tests.ps1

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ $global:WINDOWSVERSIONTAG = $items[2]
1717

1818
$random = Get-Random
1919
$global:CONTAINERNAME = 'pester-jenkins-inbound-agent_{0}_{1}' -f $global:IMAGE_TAG, $random
20+
$global:JNLPNETWORKNAME = 'jnlp-{0}' -f $random
21+
$global:NMAPCONTAINERNAME = 'nmap-{0}' -f $random
2022
Write-Host "= TESTS: container name $global:CONTAINERNAME"
2123

2224
$global:CONTAINERSHELL = 'powershell.exe'
@@ -31,8 +33,8 @@ if ($global:WINDOWSFLAVOR -eq 'nanoserver') {
3133
# Get-ChildItem Env: | ForEach-Object { Write-Host "$($_.Name) = $($_.Value)" }
3234

3335
Cleanup($global:CONTAINERNAME)
34-
Cleanup('nmap')
35-
CleanupNetwork('jnlp-network')
36+
Cleanup($global:NMAPCONTAINERNAME)
37+
CleanupNetwork($global:JNLPNETWORKNAME)
3638

3739
BuildNcatImage($global:WINDOWSVERSIONTAG)
3840

@@ -84,36 +86,36 @@ Describe "[$global:IMAGE_NAME] image has jenkins-agent.ps1 in the correct locati
8486

8587
Describe "[$global:IMAGE_NAME] image starts jenkins-agent.ps1 correctly (slow test)" {
8688
It 'connects to the nmap container' {
87-
$exitCode, $stdout, $stderr = Run-Program 'docker' 'network create --driver nat jnlp-network'
89+
$exitCode, $stdout, $stderr = Run-Program 'docker' "network create --driver nat $global:JNLPNETWORKNAME"
8890
# Launch the netcat utility, listening at port 5000 for 30 sec
8991
# bats will capture the output from netcat and compare the first line
9092
# of the header of the first HTTP request with the expected one
91-
$exitCode, $stdout, $stderr = Run-Program 'docker' 'run --detach --tty --name nmap --network=jnlp-network nmap:latest ncat.exe -w 30 -l 5000'
93+
$exitCode, $stdout, $stderr = Run-Program 'docker' "run --detach --tty --name $global:NMAPCONTAINERNAME --network=$global:JNLPNETWORKNAME nmap:latest ncat.exe -w 30 -l 5000"
9294
$exitCode | Should -Be 0
93-
Is-ContainerRunning "nmap" | Should -BeTrue
95+
Is-ContainerRunning $global:NMAPCONTAINERNAME | Should -BeTrue
9496

9597
# get the ip address of the nmap container
96-
$exitCode, $stdout, $stderr = Run-Program 'docker' "inspect --format `"{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}`" nmap"
98+
$exitCode, $stdout, $stderr = Run-Program 'docker' "inspect --format `"{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}`" $global:NMAPCONTAINERNAME"
9799
$exitCode | Should -Be 0
98100
$nmap_ip = $stdout.Trim()
99101

100102
# run Jenkins agent which tries to connect to the nmap container at port 5000
101103
$secret = "aaa"
102104
$name = "bbb"
103-
$exitCode, $stdout, $stderr = Run-Program 'docker' "run --detach --tty --network=jnlp-network --name $global:CONTAINERNAME $global:IMAGE_NAME -Url http://${nmap_ip}:5000 $secret $name"
105+
$exitCode, $stdout, $stderr = Run-Program 'docker' "run --detach --tty --network=$global:JNLPNETWORKNAME --name $global:CONTAINERNAME $global:IMAGE_NAME -Url http://${nmap_ip}:5000 $secret $name"
104106
$exitCode | Should -Be 0
105107
Is-ContainerRunning $global:CONTAINERNAME | Should -BeTrue
106108

107-
$exitCode, $stdout, $stderr = Run-Program 'docker' 'wait nmap'
108-
$exitCode, $stdout, $stderr = Run-Program 'docker' 'logs nmap'
109+
$exitCode, $stdout, $stderr = Run-Program 'docker' "wait $global:NMAPCONTAINERNAME"
110+
$exitCode, $stdout, $stderr = Run-Program 'docker' "logs $global:NMAPCONTAINERNAME"
109111
$exitCode | Should -Be 0
110112
$stdout | Should -Match "GET /tcpSlaveAgentListener/ HTTP/1.1`r"
111113
}
112114

113115
AfterAll {
114116
Cleanup($global:CONTAINERNAME)
115-
Cleanup('nmap')
116-
CleanupNetwork('jnlp-network')
117+
Cleanup($global:NMAPCONTAINERNAME)
118+
CleanupNetwork($global:JNLPNETWORKNAME)
117119
}
118120
}
119121

@@ -148,24 +150,24 @@ Describe "[$global:IMAGE_NAME] custom build args" {
148150
}
149151

150152
Describe "[$global:IMAGE_NAME] passing JVM options (slow test)" {
151-
It "shows the java version ${global:JAVAMAJORVERSION} with --show-version" {
152-
$exitCode, $stdout, $stderr = Run-Program 'docker' 'network create --driver nat jnlp-network'
153+
It "connects to the nmap container and shows the java version ${global:JAVAMAJORVERSION} with --show-version" {
154+
$exitCode, $stdout, $stderr = Run-Program 'docker' "network create --driver nat $global:JNLPNETWORKNAME"
153155
# Launch the netcat utility, listening at port 5000 for 30 sec
154156
# bats will capture the output from netcat and compare the first line
155157
# of the header of the first HTTP request with the expected one
156-
$exitCode, $stdout, $stderr = Run-Program 'docker' 'run --detach --tty --name nmap --network=jnlp-network nmap:latest ncat.exe -w 30 -l 5000'
158+
$exitCode, $stdout, $stderr = Run-Program 'docker' "run --detach --tty --name $global:NMAPCONTAINERNAME --network=$global:JNLPNETWORKNAME nmap:latest ncat.exe -w 30 -l 5000"
157159
$exitCode | Should -Be 0
158-
Is-ContainerRunning 'nmap' | Should -BeTrue
160+
Is-ContainerRunning $global:NMAPCONTAINERNAME | Should -BeTrue
159161

160162
# get the ip address of the nmap container
161-
$exitCode, $stdout, $stderr = Run-Program 'docker' "inspect --format `"{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}`" nmap"
163+
$exitCode, $stdout, $stderr = Run-Program 'docker' "inspect --format `"{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}`" $global:NMAPCONTAINERNAME"
162164
$exitCode | Should -Be 0
163165
$nmap_ip = $stdout.Trim()
164166

165167
# run Jenkins agent which tries to connect to the nmap container at port 5000
166168
$secret = 'aaa'
167169
$name = 'bbb'
168-
$exitCode, $stdout, $stderr = Run-Program 'docker' "run --detach --tty --network=jnlp-network --name $global:CONTAINERNAME $global:IMAGE_NAME -Url http://${nmap_ip}:5000 -JenkinsJavaOpts `"--show-version`" $secret $name"
170+
$exitCode, $stdout, $stderr = Run-Program 'docker' "run --detach --tty --network=$global:JNLPNETWORKNAME --name $global:CONTAINERNAME $global:IMAGE_NAME -Url http://${nmap_ip}:5000 -JenkinsJavaOpts `"--show-version`" $secret $name"
169171
$exitCode | Should -Be 0
170172
Is-ContainerRunning $global:CONTAINERNAME | Should -BeTrue
171173
Start-Sleep -Seconds 20
@@ -176,7 +178,7 @@ Describe "[$global:IMAGE_NAME] passing JVM options (slow test)" {
176178

177179
AfterAll {
178180
Cleanup($global:CONTAINERNAME)
179-
Cleanup('nmap')
180-
CleanupNetwork('jnlp-network')
181+
Cleanup($global:NMAPCONTAINERNAME)
182+
CleanupNetwork($global:JNLPNETWORKNAME)
181183
}
182184
}

0 commit comments

Comments
 (0)