Skip to content

Commit

Permalink
chore: lint *.ps1
Browse files Browse the repository at this point in the history
  • Loading branch information
lemeurherve committed Jun 23, 2024
1 parent 22934df commit 19cce30
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 97 deletions.
78 changes: 39 additions & 39 deletions build.ps1
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[CmdletBinding()]
Param(
[Parameter(Position=1)]
[String] $Target = "build",
[String] $Target = 'build',
[String] $RemotingVersion = '3256.v88a_f6e922152',
[String] $AgentType = '',
[String] $BuildNumber = '1',
Expand Down Expand Up @@ -32,39 +32,39 @@ $Repository = @{
'inbound-agent' = 'inbound-agent'
}

if(![String]::IsNullOrWhiteSpace($env:TESTS_DEBUG)) {
if (![String]::IsNullOrWhiteSpace($env:TESTS_DEBUG)) {
$TestsDebug = $env:TESTS_DEBUG
}
$env:TESTS_DEBUG = $TestsDebug

if(!$DisableEnvProps) {
if (!$DisableEnvProps) {
Get-Content env.props | ForEach-Object {
$items = $_.Split("=")
if($items.Length -eq 2) {
$items = $_.Split('=')
if ($items.Length -eq 2) {
$name = $items[0].Trim()
$value = $items[1].Trim()
Set-Item -Path "env:$($name)" -Value $value
}
}
}

if(![String]::IsNullOrWhiteSpace($env:DOCKERHUB_ORGANISATION)) {
if (![String]::IsNullOrWhiteSpace($env:DOCKERHUB_ORGANISATION)) {
$Organisation = $env:DOCKERHUB_ORGANISATION
}

if(![String]::IsNullOrWhiteSpace($env:DOCKERHUB_REPO_AGENT)) {
if (![String]::IsNullOrWhiteSpace($env:DOCKERHUB_REPO_AGENT)) {
$Repository['agent'] = $env:DOCKERHUB_REPO_AGENT
}

if(![String]::IsNullOrWhiteSpace($env:DOCKERHUB_REPO_INBOUND_AGENT)) {
if (![String]::IsNullOrWhiteSpace($env:DOCKERHUB_REPO_INBOUND_AGENT)) {
$Repository['inbound-agent'] = $env:DOCKERHUB_REPO_INBOUND_AGENT
}

if(![String]::IsNullOrWhiteSpace($env:REMOTING_VERSION)) {
if (![String]::IsNullOrWhiteSpace($env:REMOTING_VERSION)) {
$RemotingVersion = $env:REMOTING_VERSION
}

if(![String]::IsNullOrWhiteSpace($env:IMAGE_TYPE)) {
if (![String]::IsNullOrWhiteSpace($env:IMAGE_TYPE)) {
$ImageType = $env:IMAGE_TYPE
}

Expand All @@ -78,15 +78,15 @@ Function Test-CommandExists {
$oldPreference = $ErrorActionPreference
$ErrorActionPreference = 'stop'
try {
if(Get-Command $command){
if (Get-Command $command){
Write-Debug "$command exists"
}
}
Catch {
"$command does not exist"
}
Finally {
$ErrorActionPreference=$oldPreference
$ErrorActionPreference = $oldPreference
}
}

Expand All @@ -95,7 +95,7 @@ $env:DOCKERHUB_ORGANISATION = "$Organisation"
$env:REMOTING_VERSION = "$RemotingVersion"
$env:BUILD_NUMBER = $BuildNumber

$items = $ImageType.Split("-")
$items = $ImageType.Split('-')
$env:WINDOWS_FLAVOR = $items[0]
$env:WINDOWS_VERSION_TAG = $items[1]
$env:TOOLS_WINDOWS_VERSION = $items[1]
Expand All @@ -108,20 +108,20 @@ if ($items[1] -eq 'ltsc2019') {

$ProgressPreference = 'SilentlyContinue' # Disable Progress bar for faster downloads

Test-CommandExists "docker"
Test-CommandExists "docker-compose"
Test-CommandExists "yq"
Test-CommandExists 'docker'
Test-CommandExists 'docker-compose'
Test-CommandExists 'yq'

function Test-Image {
param (
$AgentTypeAndImageName
)

$items = $AgentTypeAndImageName.Split("|")
$items = $AgentTypeAndImageName.Split('|')
$agentType = $items[0]
$imageName = $items[1]
$javaVersion = $items[2]
$imageNameItems = $imageName.Split(":")
$imageNameItems = $imageName.Split(':')
$imageTag = $imageNameItems[1]

Write-Host "= TEST: Testing ${imageName} image:"
Expand All @@ -131,7 +131,7 @@ function Test-Image {
$env:JAVA_VERSION = "$javaVersion"

$targetPath = '.\target\{0}\{1}' -f $agentType, $imageTag
if(Test-Path $targetPath) {
if (Test-Path $targetPath) {
Remove-Item -Recurse -Force $targetPath
}
New-Item -Path $targetPath -Type Directory | Out-Null
Expand Down Expand Up @@ -161,35 +161,35 @@ foreach($agentType in $AgentTypes) {
# Temporary docker compose file (git ignored)
Copy-Item -Path $originalDockerComposeFile -Destination $finalDockerComposeFile
# If it's an "agent" type, add the corresponding target
if($agentType -eq 'agent') {
if ($agentType -eq 'agent') {
yq '.services.[].build.target = \"agent\"' $originalDockerComposeFile | Out-File -FilePath $finalDockerComposeFile
}

Write-Host "= PREPARE: List of $Organisation/$env:DOCKERHUB_REPO images and tags to be processed:"
Invoke-Expression "$baseDockerCmd config"

Write-Host "= BUILD: Building all images..."
Write-Host '= BUILD: Building all images...'
switch ($DryRun) {
$true { Write-Host "(dry-run) $baseDockerBuildCmd" }
$false { Invoke-Expression $baseDockerBuildCmd }
}
Write-Host "= BUILD: Finished building all images."
Write-Host '= BUILD: Finished building all images.'

if($lastExitCode -ne 0) {
if ($lastExitCode -ne 0) {
exit $lastExitCode
}

if($target -eq "test") {
if ($target -eq 'test') {
if ($DryRun) {
Write-Host "= TEST: (dry-run) test harness"
Write-Host '= TEST: (dry-run) test harness'
} else {
Write-Host "= TEST: Starting test harness"
Write-Host '= TEST: Starting test harness'

$mod = Get-InstalledModule -Name Pester -MinimumVersion 5.3.0 -MaximumVersion 5.3.3 -ErrorAction SilentlyContinue
if($null -eq $mod) {
Write-Host "= TEST: Pester 5.3.x not found: installing..."
$module = "c:\Program Files\WindowsPowerShell\Modules\Pester"
if(Test-Path $module) {
if ($null -eq $mod) {
Write-Host '= TEST: Pester 5.3.x not found: installing...'
$module = 'C:\Program Files\WindowsPowerShell\Modules\Pester'
if (Test-Path $module) {
takeown /F $module /A /R
icacls $module /reset
icacls $module /grant Administrators:'F' /inheritance:d /T
Expand All @@ -199,7 +199,7 @@ foreach($agentType in $AgentTypes) {
}

Import-Module Pester
Write-Host "= TEST: Setting up Pester environment..."
Write-Host '= TEST: Setting up Pester environment...'
$configuration = [PesterConfiguration]::Default
$configuration.Run.PassThru = $true
$configuration.Run.Path = '.\tests'
Expand All @@ -218,7 +218,7 @@ foreach($agentType in $AgentTypes) {
}

# Fail if any test failures
if($testFailed -ne $false) {
if ($testFailed -ne $false) {
Write-Error "Test stage failed for ${agentType}!"
exit 1
} else {
Expand All @@ -227,24 +227,24 @@ foreach($agentType in $AgentTypes) {
}
}

if($target -eq "publish") {
Write-Host "= PUBLISH: push all images and tags"
if ($target -eq 'publish') {
Write-Host '= PUBLISH: push all images and tags'
switch($DryRun) {
$true { Write-Host "(dry-run) $baseDockerCmd push" }
$false { Invoke-Expression "$baseDockerCmd push" }
}

# Fail if any issues when publising the docker images
if($lastExitCode -ne 0) {
Write-Error "= PUBLISH: failed!"
if ($lastExitCode -ne 0) {
Write-Error '= PUBLISH: failed!'
exit 1
}
}
}

if($lastExitCode -ne 0) {
Write-Error "Build failed!"
if ($lastExitCode -ne 0) {
Write-Error 'Build failed!'
} else {
Write-Host "= Build finished successfully"
Write-Host '= Build finished successfully'
}
exit $lastExitCode
40 changes: 20 additions & 20 deletions tests/agent.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ $global:IMAGE_NAME = Get-EnvOrDefault 'IMAGE_NAME' ''
$global:VERSION = Get-EnvOrDefault 'VERSION' ''
$global:JAVA_VERSION = Get-EnvOrDefault 'JAVA_VERSION' ''

$imageItems = $global:IMAGE_NAME.Split(":")
$imageItems = $global:IMAGE_NAME.Split(':')
$GLOBAL:IMAGE_TAG = $imageItems[1]

$items = $global:IMAGE_TAG.Split("-")
$items = $global:IMAGE_TAG.Split('-')
# Remove the 'jdk' prefix
$global:JAVAMAJORVERSION = $items[0].Remove(0,3)
$global:WINDOWSFLAVOR = $items[1]
Expand All @@ -20,9 +20,9 @@ if ($items[2] -eq 'ltsc2019') {
# TODO: make this name unique for concurency
$global:CONTAINERNAME = 'pester-jenkins-agent-{0}' -f $global:IMAGE_TAG

$global:CONTAINERSHELL="powershell.exe"
if($global:WINDOWSFLAVOR -eq 'nanoserver') {
$global:CONTAINERSHELL = "pwsh.exe"
$global:CONTAINERSHELL = 'powershell.exe'
if ($global:WINDOWSFLAVOR -eq 'nanoserver') {
$global:CONTAINERSHELL = 'pwsh.exe'
}

$global:GITLFSVERSION = '3.5.1'
Expand Down Expand Up @@ -58,7 +58,7 @@ Describe "[$global:IMAGE_NAME] image has correct applications in the PATH" {
}

It 'has java installed and in the path' {
$exitCode, $stdout, $stderr = Run-Program 'docker' "exec $global:CONTAINERNAME $global:CONTAINERSHELL -C `"if(`$null -eq (Get-Command java.exe -ErrorAction SilentlyContinue)) { exit -1 } else { exit 0 }`""
$exitCode, $stdout, $stderr = Run-Program 'docker' "exec $global:CONTAINERNAME $global:CONTAINERSHELL -C `"if (`$null -eq (Get-Command java.exe -ErrorAction SilentlyContinue)) { exit -1 } else { exit 0 }`""
$exitCode | Should -Be 0
$exitCode, $stdout, $stderr = Run-Program 'docker' "exec $global:CONTAINERNAME $global:CONTAINERSHELL -C `"`$global:VERSION = java -version 2>&1 ; Write-Host `$global:VERSION`""
$r = [regex] "^openjdk version `"(?<major>\d+)"
Expand All @@ -70,13 +70,13 @@ Describe "[$global:IMAGE_NAME] image has correct applications in the PATH" {
It 'has AGENT_WORKDIR in the environment' {
$exitCode, $stdout, $stderr = Run-Program 'docker' "exec $global:CONTAINERNAME $global:CONTAINERSHELL -C `"Get-ChildItem env:`""
$exitCode | Should -Be 0
$stdout.Trim() | Should -Match "AGENT_WORKDIR.*C:/Users/jenkins/Work"
$stdout.Trim() | Should -Match 'AGENT_WORKDIR.*C:/Users/jenkins/Work'
}

It 'has user in the environment' {
$exitCode, $stdout, $stderr = Run-Program 'docker' "exec $global:CONTAINERNAME $global:CONTAINERSHELL -C `"Get-ChildItem env:`""
$exitCode | Should -Be 0
$stdout.Trim() | Should -Match "user.*jenkins"
$stdout.Trim() | Should -Match 'user.*jenkins'
}

It 'has git-lfs (and thus git) installed' {
Expand All @@ -86,7 +86,7 @@ Describe "[$global:IMAGE_NAME] image has correct applications in the PATH" {
}

It 'does not include jenkins-agent.ps1 (inbound-agent)' {
$exitCode, $stdout, $stderr = Run-Program 'docker' "exec $global:CONTAINERNAME $global:CONTAINERSHELL -C `"if(Test-Path C:/ProgramData/Jenkins/jenkins-agent.ps1) { exit -1 } else { exit 0 }`""
$exitCode, $stdout, $stderr = Run-Program 'docker' "exec $global:CONTAINERNAME $global:CONTAINERSHELL -C `"if (Test-Path C:/ProgramData/Jenkins/jenkins-agent.ps1) { exit -1 } else { exit 0 }`""
$exitCode | Should -Be 0
}

Expand All @@ -102,12 +102,12 @@ Describe "[$global:IMAGE_NAME] check user account" {
}

It 'Password never expires' {
$exitCode, $stdout, $stderr = Run-Program 'docker' "exec $global:CONTAINERNAME $global:CONTAINERSHELL -C `"if((net user jenkins | Select-String -Pattern 'Password expires') -match 'Never') { exit 0 } else { exit -1 }`""
$exitCode, $stdout, $stderr = Run-Program 'docker' "exec $global:CONTAINERNAME $global:CONTAINERSHELL -C `"if ((net user jenkins | Select-String -Pattern 'Password expires') -match 'Never') { exit 0 } else { exit -1 }`""
$exitCode | Should -Be 0
}

It 'Password not required' {
$exitCode, $stdout, $stderr = Run-Program 'docker' "exec $global:CONTAINERNAME $global:CONTAINERSHELL -C `"if((net user jenkins | Select-String -Pattern 'Password required') -match 'No') { exit 0 } else { exit -1 }`""
$exitCode, $stdout, $stderr = Run-Program 'docker' "exec $global:CONTAINERNAME $global:CONTAINERSHELL -C `"if ((net user jenkins | Select-String -Pattern 'Password required') -match 'No') { exit 0 } else { exit -1 }`""
$exitCode | Should -Be 0
}

Expand All @@ -123,17 +123,17 @@ Describe "[$global:IMAGE_NAME] check user access to directories" {
}

It 'can write to HOME' {
$exitCode, $stdout, $stderr = Run-Program 'docker' "exec $global:CONTAINERNAME $global:CONTAINERSHELL -C `"New-Item -ItemType File -Path C:/Users/jenkins/a.txt | Out-Null ; if(Test-Path C:/Users/jenkins/a.txt) { exit 0 } else { exit -1 }`""
$exitCode, $stdout, $stderr = Run-Program 'docker' "exec $global:CONTAINERNAME $global:CONTAINERSHELL -C `"New-Item -ItemType File -Path C:/Users/jenkins/a.txt | Out-Null ; if (Test-Path C:/Users/jenkins/a.txt) { exit 0 } else { exit -1 }`""
$exitCode | Should -Be 0
}

It 'can write to HOME/.jenkins' {
$exitCode, $stdout, $stderr = Run-Program 'docker' "exec $global:CONTAINERNAME $global:CONTAINERSHELL -C `"New-Item -ItemType File -Path C:/Users/jenkins/.jenkins/a.txt | Out-Null ; if(Test-Path C:/Users/jenkins/.jenkins/a.txt) { exit 0 } else { exit -1 }`""
$exitCode, $stdout, $stderr = Run-Program 'docker' "exec $global:CONTAINERNAME $global:CONTAINERSHELL -C `"New-Item -ItemType File -Path C:/Users/jenkins/.jenkins/a.txt | Out-Null ; if (Test-Path C:/Users/jenkins/.jenkins/a.txt) { exit 0 } else { exit -1 }`""
$exitCode | Should -Be 0
}

It 'can write to HOME/Work' {
$exitCode, $stdout, $stderr = Run-Program 'docker' "exec $global:CONTAINERNAME $global:CONTAINERSHELL -C `"New-Item -ItemType File -Path C:/Users/jenkins/Work/a.txt | Out-Null ; if(Test-Path C:/Users/jenkins/Work/a.txt) { exit 0 } else { exit -1 }`""
$exitCode, $stdout, $stderr = Run-Program 'docker' "exec $global:CONTAINERNAME $global:CONTAINERSHELL -C `"New-Item -ItemType File -Path C:/Users/jenkins/Work/a.txt | Out-Null ; if (Test-Path C:/Users/jenkins/Work/a.txt) { exit 0 } else { exit -1 }`""
$exitCode | Should -Be 0
}

Expand All @@ -142,9 +142,9 @@ Describe "[$global:IMAGE_NAME] check user access to directories" {
}
}

$global:TEST_VERSION="4.0"
$global:TEST_USER="test-user"
$global:TEST_AGENT_WORKDIR="C:/test-user/something"
$global:TEST_VERSION = '4.0'
$global:TEST_USER = 'test-user'
$global:TEST_AGENT_WORKDIR = 'C:/test-user/something'

Describe "[$global:IMAGE_NAME] can be built with custom build arguments" {
BeforeAll {
Expand Down Expand Up @@ -176,17 +176,17 @@ Describe "[$global:IMAGE_NAME] can be built with custom build arguments" {
}

It 'can write to HOME' {
$exitCode, $stdout, $stderr = Run-Program 'docker' "exec $global:CONTAINERNAME $global:CONTAINERSHELL -C `"New-Item -ItemType File -Path C:/Users/${TEST_USER}/a.txt | Out-Null ; if(Test-Path C:/Users/${TEST_USER}/a.txt) { exit 0 } else { exit -1 }`""
$exitCode, $stdout, $stderr = Run-Program 'docker' "exec $global:CONTAINERNAME $global:CONTAINERSHELL -C `"New-Item -ItemType File -Path C:/Users/${TEST_USER}/a.txt | Out-Null ; if (Test-Path C:/Users/${TEST_USER}/a.txt) { exit 0 } else { exit -1 }`""
$exitCode | Should -Be 0
}

It 'can write to HOME/.jenkins' {
$exitCode, $stdout, $stderr = Run-Program 'docker' "exec $global:CONTAINERNAME $global:CONTAINERSHELL -C `"New-Item -ItemType File -Path C:/Users/${TEST_USER}/.jenkins/a.txt | Out-Null ; if(Test-Path C:/Users/${TEST_USER}/.jenkins/a.txt) { exit 0 } else { exit -1 }`""
$exitCode, $stdout, $stderr = Run-Program 'docker' "exec $global:CONTAINERNAME $global:CONTAINERSHELL -C `"New-Item -ItemType File -Path C:/Users/${TEST_USER}/.jenkins/a.txt | Out-Null ; if (Test-Path C:/Users/${TEST_USER}/.jenkins/a.txt) { exit 0 } else { exit -1 }`""
$exitCode | Should -Be 0
}

It 'can write to HOME/Work' {
$exitCode, $stdout, $stderr = Run-Program 'docker' "exec $global:CONTAINERNAME $global:CONTAINERSHELL -C `"New-Item -ItemType File -Path ${TEST_AGENT_WORKDIR}/a.txt | Out-Null ; if(Test-Path ${TEST_AGENT_WORKDIR}/a.txt) { exit 0 } else { exit -1 }`""
$exitCode, $stdout, $stderr = Run-Program 'docker' "exec $global:CONTAINERNAME $global:CONTAINERSHELL -C `"New-Item -ItemType File -Path ${TEST_AGENT_WORKDIR}/a.txt | Out-Null ; if (Test-Path ${TEST_AGENT_WORKDIR}/a.txt) { exit 0 } else { exit -1 }`""
$exitCode | Should -Be 0
}

Expand Down
Loading

0 comments on commit 19cce30

Please sign in to comment.