Skip to content

Commit 03b8c73

Browse files
dduportallemeurherve
authored andcommitted
chore(windows/make.ps1) ensure Pester is only installed as user (instead of system) (jenkinsci#2236)
* chore(windows/make.ps1) ensure Pester is only installed as user (instead of system) Caused by jenkins-infra/helpdesk#4939 (comment) Ref. https://learn.microsoft.com/en-us/powershell/module/powershellget/install-module?view=powershellget-2.x#example-5-install-a-module-only-for-the-current-user an * tests(windows/plugins-cli) ensure workdir is cleaned up with docker to avoid permissions issues, only when needed * fixup Signed-off-by: Damien Duportal <damien.duportal@gmail.com> --------- Signed-off-by: Damien Duportal <damien.duportal@gmail.com>
1 parent 2ce2686 commit 03b8c73

File tree

3 files changed

+62
-101
lines changed

3 files changed

+62
-101
lines changed

make.ps1

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -205,14 +205,7 @@ if ($target -eq 'test') {
205205
$mod = Get-InstalledModule -Name Pester -MinimumVersion 5.3.0 -MaximumVersion 5.3.3 -ErrorAction SilentlyContinue
206206
if ($null -eq $mod) {
207207
Write-Host '= TEST: Pester 5.3.x not found: installing...'
208-
$module = 'C:\Program Files\WindowsPowerShell\Modules\Pester'
209-
if (Test-Path $module) {
210-
takeown /F $module /A /R
211-
icacls $module /reset
212-
icacls $module /grant Administrators:'F' /inheritance:d /T
213-
Remove-Item -Path $module -Recurse -Force -Confirm:$false
214-
}
215-
Install-Module -Force -Name Pester -MaximumVersion 5.3.3
208+
Install-Module -Force -Name Pester -MaximumVersion 5.3.3 -Scope CurrentUser
216209
}
217210

218211
Import-Module Pester

tests/plugins-cli.Tests.ps1

Lines changed: 40 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ $global:SUT_IMAGE=Get-SutImage
55
$global:SUT_CONTAINER=Get-SutImage
66
$global:TEST_TAG=$global:SUT_IMAGE.Replace('pester-jenkins-', '')
77

8-
$global:WORK = Join-Path $PSScriptRoot "upgrade-plugins/work-${global:SUT_IMAGE}"
9-
108
Describe "[plugins-cli > $global:TEST_TAG] build image" {
119
BeforeEach {
1210
Push-Location -StackName 'jenkins' -Path "$PSScriptRoot/.."
@@ -150,43 +148,35 @@ Describe "[plugins-cli > $global:TEST_TAG] plugins are installed with jenkins-pl
150148
$exitCode, $stdout, $stderr = Build-DockerChild $global:SUT_IMAGE-plugins-cli-update $PSScriptRoot/plugins-cli/update --no-cache
151149
$exitCode | Should -Be 0
152150
}
153-
151+
154152
It 'has the correct version of junit' {
155153
$exitCode, $stdout, $stderr = Run-Program 'docker.exe' "run --rm $global:SUT_IMAGE-plugins-cli-update Import-Module -Force -DisableNameChecking C:/ProgramData/Jenkins/jenkins-support.psm1 ; Expand-Zip `$env:JENKINS_HOME/plugins/junit.jpi 'META-INF/MANIFEST.MF'"
156154
$exitCode | Should -Be 0
157155
$stdout | Should -Match 'Plugin-Version: 1.28'
158156
}
159157
}
160158

161-
Describe "[plugins-cli > $global:TEST_TAG] clean work directory" {
162-
It 'cleanup' {
163-
if(Test-Path $PSScriptRoot/upgrade-plugins/work-$global:SUT_IMAGE) {
164-
Remove-Item -Recurse -Force $PSScriptRoot/upgrade-plugins/work-$global:SUT_IMAGE | Out-Null
165-
}
166-
}
167-
}
168-
169159
# Only test on Java 21, one JDK is enough to test all versions
170160
Describe "[plugins-cli > $global:TEST_TAG] plugins are getting upgraded but not downgraded" -Skip:(-not $global:TEST_TAG.Contains('jdk21-')) {
161+
BeforeAll {
162+
$dockerVolume = (New-Guid).Guid
163+
docker volume rm -f $dockerVolume
164+
}
171165
It 'builds child image' {
172166
# Initial execution
173167
$exitCode, $stdout, $stderr = Build-DockerChild $global:SUT_IMAGE-plugins-cli $PSScriptRoot/plugins-cli
174168
$exitCode | Should -Be 0
175169
}
176170

177171
It 'has correct version of junit and ant plugins' {
178-
if(-not (Test-Path $global:WORK)) {
179-
New-Item -ItemType Directory -Path $global:WORK
180-
}
181-
182172
# Image contains junit 1.6 and ant-plugin 1.3
183-
$exitCode, $stdout, $stderr = Run-Program 'docker.exe' "run -v `"${work}:C:\ProgramData\Jenkins\JenkinsHome`" --rm $global:SUT_IMAGE-plugins-cli exit 0"
173+
$exitCode, $stdout, $stderr = Run-Program 'docker.exe' "run -v ${dockerVolume}:C:\ProgramData\Jenkins\JenkinsHome --rm $global:SUT_IMAGE-plugins-cli exit 0"
184174
$exitCode | Should -Be 0
185175

186-
$exitCode, $stdout, $stderr = Unzip-Manifest $global:SUT_IMAGE "junit.jpi" $global:WORK
176+
$exitCode, $stdout, $stderr = Unzip-Manifest $global:SUT_IMAGE "junit.jpi" $dockerVolume
187177
$exitCode | Should -Be 0
188178
$stdout | Should -Match 'Plugin-Version: 1.6'
189-
$exitCode, $stdout, $stderr = Unzip-Manifest $global:SUT_IMAGE "ant.jpi" $global:WORK
179+
$exitCode, $stdout, $stderr = Unzip-Manifest $global:SUT_IMAGE "ant.jpi" $dockerVolume
190180
$exitCode | Should -Be 0
191181
$stdout | Should -Match 'Plugin-Version: 1.3'
192182
}
@@ -196,121 +186,107 @@ Describe "[plugins-cli > $global:TEST_TAG] plugins are getting upgraded but not
196186
$exitCode, $stdout, $stderr = Build-DockerChild $global:SUT_IMAGE-upgrade-plugins $PSScriptRoot/upgrade-plugins
197187
$exitCode | Should -Be 0
198188
# Images contains junit 1.28 and ant-plugin 1.2
199-
$exitCode, $stdout, $stderr = Run-Program 'docker.exe' "run -v `"${work}:C:\ProgramData\Jenkins\JenkinsHome`" --rm $global:SUT_IMAGE-upgrade-plugins exit 0"
189+
$exitCode, $stdout, $stderr = Run-Program 'docker.exe' "run -v ${dockerVolume}:C:\ProgramData\Jenkins\JenkinsHome --rm $global:SUT_IMAGE-upgrade-plugins exit 0"
200190
$exitCode | Should -Be 0
201-
$exitCode, $stdout, $stderr = Unzip-Manifest $global:SUT_IMAGE 'junit.jpi' $global:WORK
191+
$exitCode, $stdout, $stderr = Unzip-Manifest $global:SUT_IMAGE 'junit.jpi' $dockerVolume
202192
$exitCode | Should -Be 0
203193
# Should be updated
204194
$stdout | Should -Match 'Plugin-Version: 1.28'
205-
$exitCode, $stdout, $stderr = Unzip-Manifest $global:SUT_IMAGE 'ant.jpi' $global:WORK
195+
$exitCode, $stdout, $stderr = Unzip-Manifest $global:SUT_IMAGE 'ant.jpi' $dockerVolume
206196
$exitCode | Should -Be 0
207197
# 1.2 is older than the existing 1.3, so keep 1.3
208198
$stdout | Should -Match 'Plugin-Version: 1.3'
209199
}
210-
}
211-
212-
Describe "[plugins-cli > $global:TEST_TAG] clean work directory" {
213-
It 'cleanup' {
214-
if(Test-Path $global:WORK) {
215-
Remove-Item -Recurse -Force $global:WORK | Out-Null
216-
}
200+
AfterAll {
201+
docker volume rm -f $dockerVolume
217202
}
218203
}
219204

220205
# Only test on Java 21, one JDK is enough to test all versions
221206
Describe "[plugins-cli > $global:TEST_TAG] do not upgrade if plugin has been manually updated" -Skip:(-not $global:TEST_TAG.Contains('jdk21-')) {
222-
207+
BeforeAll {
208+
$dockerVolume = (New-Guid).Guid
209+
docker volume rm -f $dockerVolume
210+
}
211+
223212
It 'builds child image' {
224213
$exitCode, $stdout, $stderr = Build-DockerChild $global:SUT_IMAGE-plugins-cli $PSScriptRoot/plugins-cli
225214
$exitCode | Should -Be 0
226215
}
227216

228217
It 'updates plugin manually and then via plugin-cli' {
229-
if(-not (Test-Path $global:WORK)) {
230-
New-Item -ItemType Directory -Path $global:WORK
231-
}
232-
233218
# Image contains junit 1.8 and ant-plugin 1.3
234-
$exitCode, $stdout, $stderr = Run-Program 'docker.exe' "run -v `"${work}:C:\ProgramData\Jenkins\JenkinsHome`" --rm $global:SUT_IMAGE-plugins-cli curl.exe --connect-timeout 20 --retry 5 --retry-delay 0 --retry-max-time 60 -s -f -L https://updates.jenkins.io/download/plugins/junit/1.8/junit.hpi -o C:/ProgramData/Jenkins/JenkinsHome/plugins/junit.jpi"
219+
$exitCode, $stdout, $stderr = Run-Program 'docker.exe' "run -v ${dockerVolume}:C:\ProgramData\Jenkins\JenkinsHome --rm $global:SUT_IMAGE-plugins-cli curl.exe --connect-timeout 20 --retry 5 --retry-delay 0 --retry-max-time 60 -s -f -L https://updates.jenkins.io/download/plugins/junit/1.8/junit.hpi -o C:/ProgramData/Jenkins/JenkinsHome/plugins/junit.jpi"
235220
$exitCode | Should -Be 0
236-
$exitCode, $stdout, $stderr = Unzip-Manifest $global:SUT_IMAGE 'junit.jpi' $global:WORK
221+
$exitCode, $stdout, $stderr = Unzip-Manifest $global:SUT_IMAGE 'junit.jpi' $dockerVolume
237222
$exitCode | Should -Be 0
238223
$stdout | Should -Match 'Plugin-Version: 1.8'
239-
$exitCode, $stdout, $stderr = Unzip-Manifest $global:SUT_IMAGE 'ant.jpi' $global:WORK
224+
$exitCode, $stdout, $stderr = Unzip-Manifest $global:SUT_IMAGE 'ant.jpi' $dockerVolume
240225
$exitCode | Should -Be 0
241226
$stdout | Should -Match 'Plugin-Version: 1.3'
242227

243228
$exitCode, $stdout, $stderr = Build-DockerChild $global:SUT_IMAGE-upgrade-plugins $PSScriptRoot/upgrade-plugins
244229
$exitCode | Should -Be 0
245230

246231
# Images contains junit 1.28 and ant-plugin 1.2
247-
$exitCode, $stdout, $stderr = Run-Program 'docker.exe' "run -v `"${work}:C:\ProgramData\Jenkins\JenkinsHome`" --rm $global:SUT_IMAGE-upgrade-plugins exit 0"
232+
$exitCode, $stdout, $stderr = Run-Program 'docker.exe' "run -v ${dockerVolume}:C:\ProgramData\Jenkins\JenkinsHome --rm $global:SUT_IMAGE-upgrade-plugins exit 0"
248233
$exitCode | Should -Be 0
249234
# junit shouldn't be upgraded
250-
$exitCode, $stdout, $stderr = Unzip-Manifest $global:SUT_IMAGE 'junit.jpi' $global:WORK
235+
$exitCode, $stdout, $stderr = Unzip-Manifest $global:SUT_IMAGE 'junit.jpi' $dockerVolume
251236
$exitCode | Should -Be 0
252237
$stdout | Should -Match 'Plugin-Version: 1.8'
253238
$stdout | Should -Not -Match 'Plugin-Version: 1.28'
254239
# ant shouldn't be downgraded
255-
$exitCode, $stdout, $stderr = Unzip-Manifest $global:SUT_IMAGE 'ant.jpi' $global:WORK
240+
$exitCode, $stdout, $stderr = Unzip-Manifest $global:SUT_IMAGE 'ant.jpi' $dockerVolume
256241
$exitCode | Should -Be 0
257242
$stdout | Should -Match 'Plugin-Version: 1.3'
258243
$stdout | Should -Not -Match 'Plugin-Version: 1.2'
259244
}
260-
}
261-
262-
Describe "[plugins-cli > $global:TEST_TAG] clean work directory" {
263-
It 'cleanup' {
264-
if(Test-Path $global:WORK) {
265-
Remove-Item -Recurse -Force $global:WORK | Out-Null
266-
}
245+
AfterAll {
246+
docker volume rm -f $dockerVolume
267247
}
268248
}
269249

270250
# Only test on Java 21, one JDK is enough to test all versions
271251
Describe "[plugins-cli > $global:TEST_TAG] upgrade plugin even if it has been manually updated when PLUGINS_FORCE_UPGRADE=true" -Skip:(-not $global:TEST_TAG.Contains('jdk21-')) {
252+
BeforeAll {
253+
$dockerVolume = (New-Guid).Guid
254+
docker volume rm -f $dockerVolume
255+
}
256+
272257
It 'builds child image' {
273258
$exitCode, $stdout, $stderr = Build-DockerChild $global:SUT_IMAGE-plugins-cli $PSScriptRoot/plugins-cli
274259
$exitCode | Should -Be 0
275260
}
276261

277262
It 'upgrades plugins' {
278-
if(-not (Test-Path $global:WORK)) {
279-
New-Item -ItemType Directory -Path $global:WORK
280-
}
281-
282263
# Image contains junit 1.6 and ant-plugin 1.3
283-
$exitCode, $stdout, $stderr = Run-Program 'docker.exe' "run -v `"${work}:C:\ProgramData\Jenkins\JenkinsHome`" --rm $global:SUT_IMAGE-plugins-cli curl.exe --connect-timeout 20 --retry 5 --retry-delay 0 --retry-max-time 60 -s -f -L https://updates.jenkins.io/download/plugins/junit/1.8/junit.hpi -o C:/ProgramData/Jenkins/JenkinsHome/plugins/junit.jpi"
264+
$exitCode, $stdout, $stderr = Run-Program 'docker.exe' "run -v ${dockerVolume}:C:\ProgramData\Jenkins\JenkinsHome --rm $global:SUT_IMAGE-plugins-cli curl.exe --connect-timeout 20 --retry 5 --retry-delay 0 --retry-max-time 60 -s -f -L https://updates.jenkins.io/download/plugins/junit/1.8/junit.hpi -o C:/ProgramData/Jenkins/JenkinsHome/plugins/junit.jpi"
284265
$exitCode | Should -Be 0
285266

286-
$exitCode, $stdout, $stderr = Unzip-Manifest $global:SUT_IMAGE 'junit.jpi' $global:WORK
267+
$exitCode, $stdout, $stderr = Unzip-Manifest $global:SUT_IMAGE 'junit.jpi' $dockerVolume
287268
$exitCode | Should -Be 0
288269
$stdout | Should -Match 'Plugin-Version: 1.8'
289-
270+
290271
$exitCode, $stdout, $stderr = Build-DockerChild $global:SUT_IMAGE-upgrade-plugins $PSScriptRoot/upgrade-plugins
291272
$exitCode | Should -Be 0
292-
273+
293274
# Images contains junit 1.28 and ant-plugin 1.2
294-
$exitCode, $stdout, $stderr = Run-Program 'docker.exe' "run -e PLUGINS_FORCE_UPGRADE=true -v ${work}:C:/ProgramData/Jenkins/JenkinsHome --rm $global:SUT_IMAGE-upgrade-plugins exit 0"
275+
$exitCode, $stdout, $stderr = Run-Program 'docker.exe' "run -e PLUGINS_FORCE_UPGRADE=true -v ${dockerVolume}:C:\ProgramData\Jenkins\JenkinsHome --rm $global:SUT_IMAGE-upgrade-plugins exit 0" true
295276
$exitCode | Should -Be 0
296277
# junit should be upgraded
297-
$exitCode, $stdout, $stderr = Unzip-Manifest $global:SUT_IMAGE 'junit.jpi' $global:WORK
278+
$exitCode, $stdout, $stderr = Unzip-Manifest $global:SUT_IMAGE 'junit.jpi' $dockerVolume
298279
$exitCode | Should -Be 0
299280
$stdout | Should -Not -Match 'Plugin-Version: 1.8'
300281
$stdout | Should -Match 'Plugin-Version: 1.28'
301282
# ant shouldn't be downgraded
302-
$exitCode, $stdout, $stderr = Unzip-Manifest $global:SUT_IMAGE 'ant.jpi' $global:WORK
283+
$exitCode, $stdout, $stderr = Unzip-Manifest $global:SUT_IMAGE 'ant.jpi' $dockerVolume
303284
$exitCode | Should -Be 0
304285
$stdout | Should -Match 'Plugin-Version: 1.3'
305286
$stdout | Should -Not -Match 'Plugin-Version: 1.2'
306287
}
307-
}
308-
309-
Describe "[plugins-cli > $global:TEST_TAG] clean work directory" {
310-
It 'cleanup' {
311-
if(Test-Path $global:WORK) {
312-
Remove-Item -Recurse -Force $global:WORK | Out-Null
313-
}
288+
AfterAll {
289+
docker volume rm -f $dockerVolume
314290
}
315291
}
316292

@@ -329,11 +305,3 @@ Describe "[plugins-cli > $global:TEST_TAG] Use a custom jenkins.war" -Skip:(-not
329305
$exitCode | Should -Be 0
330306
}
331307
}
332-
333-
Describe "[plugins-cli > $global:TEST_TAG] clean work directory" {
334-
It 'cleanup' {
335-
if(Test-Path $global:WORK) {
336-
Remove-Item -Recurse -Force $global:WORK | Out-Null
337-
}
338-
}
339-
}

tests/test_helpers.psm1

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ function Test-CommandExists($command) {
55
$ErrorActionPreference = 'stop'
66
$res = $false
77
try {
8-
if(Get-Command $command) {
9-
$res = $true
8+
if(Get-Command $command) {
9+
$res = $true
1010
}
1111
} catch {
12-
$res = $false
12+
$res = $false
1313
} finally {
1414
$ErrorActionPreference=$oldPreference
1515
}
@@ -24,24 +24,24 @@ if(-Not (Test-CommandExists docker)) {
2424
function Retry-Command {
2525
[CmdletBinding()]
2626
param (
27-
[parameter(Mandatory, ValueFromPipeline)]
27+
[parameter(Mandatory, ValueFromPipeline)]
2828
[ValidateNotNullOrEmpty()]
2929
[scriptblock] $ScriptBlock,
3030
[int] $RetryCount = 3,
3131
[int] $Delay = 30,
3232
[string] $SuccessMessage = "Command executed successfully!",
3333
[string] $FailureMessage = "Failed to execute the command"
3434
)
35-
35+
3636
process {
3737
$Attempt = 1
3838
$Flag = $true
39-
39+
4040
do {
4141
try {
4242
$PreviousPreference = $ErrorActionPreference
4343
$ErrorActionPreference = 'Stop'
44-
Invoke-Command -NoNewScope -ScriptBlock $ScriptBlock -OutVariable Result 4>&1
44+
Invoke-Command -NoNewScope -ScriptBlock $ScriptBlock -OutVariable Result 4>&1
4545
$ErrorActionPreference = $PreviousPreference
4646

4747
# flow control will execute the next line only if the command in the scriptblock executed without any errors
@@ -84,20 +84,20 @@ function Run-Program($cmd, $params, $verbose=$false) {
8484
if($verbose) {
8585
Write-Host "$cmd $params"
8686
}
87-
$psi = New-Object System.Diagnostics.ProcessStartInfo
88-
$psi.CreateNoWindow = $true
89-
$psi.UseShellExecute = $false
90-
$psi.RedirectStandardOutput = $true
87+
$psi = New-Object System.Diagnostics.ProcessStartInfo
88+
$psi.CreateNoWindow = $true
89+
$psi.UseShellExecute = $false
90+
$psi.RedirectStandardOutput = $true
9191
$psi.RedirectStandardError = $true
9292
$psi.WorkingDirectory = (Get-Location)
93-
$psi.FileName = $cmd
93+
$psi.FileName = $cmd
9494
$psi.Arguments = $params
95-
$proc = New-Object System.Diagnostics.Process
96-
$proc.StartInfo = $psi
95+
$proc = New-Object System.Diagnostics.Process
96+
$proc.StartInfo = $psi
9797
[void]$proc.Start()
98-
$stdout = $proc.StandardOutput.ReadToEnd()
98+
$stdout = $proc.StandardOutput.ReadToEnd()
9999
$stderr = $proc.StandardError.ReadToEnd()
100-
$proc.WaitForExit()
100+
$proc.WaitForExit()
101101
if($proc.ExitCode -ne 0) {
102102
Write-Host "`n`nstdout:`n$stdout`n`nstderr:`n$stderr`n`n"
103103
}
@@ -156,7 +156,7 @@ function Run-In-Script-Console($Container, $Script) {
156156
return $res.Content.replace('Result: ', '')
157157
}
158158
}
159-
return $null
159+
return $null
160160
}
161161

162162
function Test-Url($Container, $Url) {
@@ -171,17 +171,17 @@ function Test-Url($Container, $Url) {
171171
$res = Invoke-WebRequest -Uri $('{0}{1}' -f $jenkinsUrl, $Url) -Headers $Headers -TimeoutSec 60 -Method Head -UseBasicParsing
172172
if($res.StatusCode -eq 200) {
173173
return $true
174-
}
174+
}
175175
}
176176
Write-Error "URL $(Get-JenkinsUrl $Container)$Url failed"
177-
return $false
177+
return $false
178178
}
179179

180180
function Cleanup($image) {
181181
docker kill "$image" 2>&1 | Out-Null
182182
docker rm -fv "$image" 2>&1 | Out-Null
183183
}
184184

185-
function Unzip-Manifest($Container, $Plugin, $Work) {
186-
return (Run-Program "docker.exe" "run --rm -v `"${Work}:C:\ProgramData\Jenkins\JenkinsHome`" $Container mkdir C:/ProgramData/Jenkins/temp | Out-Null ; Copy-Item C:/ProgramData/Jenkins/JenkinsHome/plugins/$Plugin C:/ProgramData/Jenkins/temp/$Plugin.zip ; Expand-Archive C:/ProgramData/Jenkins/temp/$Plugin.zip -Destinationpath C:/ProgramData/Jenkins/temp ; `$content = Get-Content C:/ProgramData/Jenkins/temp/META-INF/MANIFEST.MF ; Remove-Item -Force -Recurse C:/ProgramData/Jenkins/temp ; Write-Host `$content ; exit 0")
185+
function Unzip-Manifest($Container, $Plugin, $DockerVolume) {
186+
return (Run-Program "docker.exe" "run --rm -v `"${DockerVolume}:C:\ProgramData\Jenkins\JenkinsHome`" $Container mkdir C:/ProgramData/Jenkins/temp | Out-Null ; Copy-Item C:/ProgramData/Jenkins/JenkinsHome/plugins/$Plugin C:/ProgramData/Jenkins/temp/$Plugin.zip ; Expand-Archive C:/ProgramData/Jenkins/temp/$Plugin.zip -Destinationpath C:/ProgramData/Jenkins/temp ; `$content = Get-Content C:/ProgramData/Jenkins/temp/META-INF/MANIFEST.MF ; Remove-Item -Force -Recurse C:/ProgramData/Jenkins/temp ; Write-Host `$content ; exit 0")
187187
}

0 commit comments

Comments
 (0)