Skip to content
This repository has been archived by the owner on Jan 21, 2024. It is now read-only.

Commit

Permalink
Merge pull request #195 from slide/windows_refactor
Browse files Browse the repository at this point in the history
Windows refactor
  • Loading branch information
slide authored Oct 5, 2020
2 parents 03a1cbd + a32a822 commit 7cc199b
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 30 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@
bats-core/
8/*/jenkins-agent*
11/*/jenkins-agent*

/**/windows/**/jenkins-agent.ps1
/.vscode/
File renamed without changes.
File renamed without changes.
51 changes: 28 additions & 23 deletions make.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Param(
[String] $AdditionalArgs = '',
[String] $Build = '',
[String] $VersionTag = '4.3-8',
[String] $DockerAgentVersion = '4.3-7',
[switch] $PushVersions = $false
)

Expand All @@ -19,30 +20,34 @@ if(![String]::IsNullOrWhiteSpace($env:DOCKERHUB_ORGANISATION)) {
$Organization = $env:DOCKERHUB_ORGANISATION
}

$builds = @{
'jdk8' = @{
'Folder' = '8\windowsservercore-1809';
'Tags' = @( "windowsservercore-1809", "jdk8-windowsservercore-1809" );
};
'jdk11' = @{
'Folder' = '11\windowsservercore-1809';
'Tags' = @( "jdk11-windowsservercore-1809" );
};
'nanoserver' = @{
'Folder' = '8\nanoserver-1809';
'Tags' = @( "nanoserver-1809", "jdk8-nanoserver-1809" );
};
'nanoserver-jdk11' = @{
'Folder' = '11\nanoserver-1809';
'Tags' = @( "jdk11-nanoserver-1809" );
};
# this is the jdk version that will be used for the 'bare tag' images, e.g., jdk8-windowsservercore-1809 -> windowsserver-1809
$defaultBuild = '8'
$builds = @{}

Get-ChildItem -Recurse -Include windows -Directory | ForEach-Object {
Get-ChildItem -Directory -Path $_ | Where-Object { Test-Path (Join-Path $_.FullName "Dockerfile") } | ForEach-Object {
$dir = $_.FullName.Replace((Get-Location), "").TrimStart("\")
$items = $dir.Split("\")
$jdkVersion = $items[0]
$baseImage = $items[2]
$basicTag = "jdk${jdkVersion}-${baseImage}"
$tags = @( $basicTag )
if($jdkVersion -eq $defaultBuild) {
$tags += $baseImage
}

$builds[$basicTag] = @{
'Folder' = $dir;
'Tags' = $tags;
}
}
}

if(![System.String]::IsNullOrWhiteSpace($Build) -and $builds.ContainsKey($Build)) {
foreach($tag in $builds[$Build]['Tags']) {
Copy-Item -Path 'jenkins-agent.ps1' -Destination (Join-Path $builds[$Build]['Folder'] 'jenkins-agent.ps1') -Force
Write-Host "Building $Build => tag=$tag"
$cmd = "docker build -t {0}/{1}:{2} {3} {4}" -f $Organization, $Repository, $tag, $AdditionalArgs, $builds[$Build]['Folder']
$cmd = "docker build --build-arg 'VERSION={0}' -t {1}/{2}:{3} {4} {5}" -f $DockerAgentVersion, $Organization, $Repository, $tag, $AdditionalArgs, $builds[$Build]['Folder']
Invoke-Expression $cmd

if($PushVersions) {
Expand All @@ -51,7 +56,7 @@ if(![System.String]::IsNullOrWhiteSpace($Build) -and $builds.ContainsKey($Build)
$buildTag = "$VersionTag"
}
Write-Host "Building $Build => tag=$buildTag"
$cmd = "docker build -t {0}/{1}:{2} {3} {4}" -f $Organization, $Repository, $buildTag, $AdditionalArgs, $builds[$Build]['Folder']
$cmd = "docker build --build-arg 'VERSION={0}' -t {1}/{2}:{3} {4} {5}" -f $DockerAgentVersion, $Organization, $Repository, $buildTag, $AdditionalArgs, $builds[$Build]['Folder']
Invoke-Expression $cmd
}
}
Expand All @@ -60,7 +65,7 @@ if(![System.String]::IsNullOrWhiteSpace($Build) -and $builds.ContainsKey($Build)
Copy-Item -Path 'jenkins-agent.ps1' -Destination (Join-Path $builds[$b]['Folder'] 'jenkins-agent.ps1') -Force
foreach($tag in $builds[$b]['Tags']) {
Write-Host "Building $b => tag=$tag"
$cmd = "docker build -t {0}/{1}:{2} {3} {4}" -f $Organization, $Repository, $tag, $AdditionalArgs, $builds[$b]['Folder']
$cmd = "docker build --build-arg 'VERSION={0}' -t {1}/{2}:{3} {4} {5}" -f $DockerAgentVersion, $Organization, $Repository, $tag, $AdditionalArgs, $builds[$b]['Folder']
Invoke-Expression $cmd

if($PushVersions) {
Expand All @@ -69,7 +74,7 @@ if(![System.String]::IsNullOrWhiteSpace($Build) -and $builds.ContainsKey($Build)
$buildTag = "$VersionTag"
}
Write-Host "Building $Build => tag=$buildTag"
$cmd = "docker build -t {0}/{1}:{2} {3} {4}" -f $Organization, $Repository, $buildTag, $AdditionalArgs, $builds[$b]['Folder']
$cmd = "docker build --build-arg 'VERSION={0}' -t {1}/{2}:{3} {4} {5}" -f $DockerAgentVersion, $Organization, $Repository, $buildTag, $AdditionalArgs, $builds[$b]['Folder']
Invoke-Expression $cmd
}
}
Expand All @@ -95,13 +100,13 @@ if($Target -eq "test") {

if(![System.String]::IsNullOrWhiteSpace($Build) -and $builds.ContainsKey($Build)) {
$env:FOLDER = $builds[$Build]['Folder']
$env:VERSION = "$RemotingVersion-$BuildNumber"
$env:VERSION = $DockerAgentVersion
Invoke-Pester -Path tests -EnableExit
Remove-Item env:\FOLDER
} else {
foreach($b in $builds.Keys) {
$env:FOLDER = $builds[$b]['Folder']
$env:VERSION = "$RemotingVersion-$BuildNumber"
$env:VERSION = $DockerAgentVersion
Invoke-Pester -Path tests -EnableExit
Remove-Item env:\FOLDER
}
Expand Down
30 changes: 26 additions & 4 deletions tests/jnlpAgent.Tests.ps1 → tests/inboundAgent.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ $AGENT_CONTAINER='pester-jenkins-inbound-agent'
$SHELL="powershell.exe"

$FOLDER = Get-EnvOrDefault 'FOLDER' ''
$VERSION = Get-EnvOrDefault 'VERSION' '4.3-6'

$REAL_FOLDER=Resolve-Path -Path "$PSScriptRoot/../${FOLDER}"

Expand All @@ -16,9 +17,9 @@ if(($FOLDER -match '^(?<jdk>[0-9]+)[\\/](?<flavor>.+)$') -and (Test-Path $REAL_F
exit 1
}

if($FLAVOR -match "nanoserver") {
if($FLAVOR -match "nanoserver-(\d+)") {
$AGENT_IMAGE += "-nanoserver"
$AGENT_CONTAINER += "-nanoserver-1809"
$AGENT_CONTAINER += "-nanoserver-$($Matches[1])"
$SHELL = "pwsh.exe"
}

Expand All @@ -41,7 +42,7 @@ Describe "[$JDK $FLAVOR] build image" {
}

It 'builds image' {
$exitCode, $stdout, $stderr = Run-Program 'docker.exe' "build -t $AGENT_IMAGE $FOLDER"
$exitCode, $stdout, $stderr = Run-Program 'docker.exe' "build --build-arg 'VERSION=$VERSION' -t $AGENT_IMAGE $FOLDER"
$exitCode | Should -Be 0
}

Expand All @@ -50,14 +51,35 @@ Describe "[$JDK $FLAVOR] build image" {
}
}

Describe "[$JDK $FLAVOR] check user account" {
BeforeAll {
docker run -d -it --name "$AGENT_CONTAINER" -P "$AGENT_IMAGE" "$SHELL"
Is-ContainerRunning $AGENT_CONTAINER
}

It 'Password never expires' {
$exitCode, $stdout, $stderr = Run-Program 'docker.exe' "exec $AGENT_CONTAINER $SHELL -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.exe' "exec $AGENT_CONTAINER $SHELL -C `"if((net user jenkins | Select-String -Pattern 'Password required') -match 'No') { exit 0 } else { exit -1 }`""
$exitCode | Should -Be 0
}

AfterAll {
Cleanup($AGENT_CONTAINER)
}
}

Describe "[$JDK $FLAVOR] image has jenkins-agent.ps1 in the correct location" {
BeforeAll {
& docker run -dit --name "$AGENT_CONTAINER" -P "$AGENT_IMAGE" $SHELL
Is-ContainerRunning $AGENT_CONTAINER | Should -BeTrue
}

It 'has jenkins-agent.ps1 in C:/ProgramData/Jenkins' {
$exitCode, $stdout, $stderr = Run-Program 'docker.exe' "exec $AGENT_CONTAINER $SHELL -C `"if(Test-Path C:/ProgramData/Jenkins/jenkins-agent.ps1) { exit 0 } else { exit 1}`""
$exitCode, $stdout, $stderr = Run-Program 'docker.exe' "exec $AGENT_CONTAINER $SHELL -C `"if(Test-Path 'C:/ProgramData/Jenkins/jenkins-agent.ps1') { exit 0 } else { exit 1 }`""
$exitCode | Should -Be 0
}

Expand Down
9 changes: 6 additions & 3 deletions tests/test_helpers.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ function CleanupNetwork($name) {
}

function Is-ContainerRunning($container) {
Start-Sleep -Seconds 5
return Retry-Command -RetryCount 10 -Delay 2 -ScriptBlock {
Start-Sleep -Seconds 10
return Retry-Command -RetryCount 10 -Delay 3 -ScriptBlock {
$exitCode, $stdout, $stderr = Run-Program 'docker.exe' "inspect -f `"{{.State.Running}}`" $container"
if(($exitCode -ne 0) -or (-not $stdout.Contains('true')) ) {
throw('Exit code incorrect, or invalid value for running state')
Expand All @@ -92,7 +92,9 @@ function Is-ContainerRunning($container) {
}

function Run-Program($cmd, $params, $quiet=$true) {
#Write-Host "cmd = $cmd, params = $params"
if(-not $quiet) {
Write-Host "cmd = $cmd, params = $params"
}
$psi = New-Object System.Diagnostics.ProcessStartInfo
$psi.CreateNoWindow = $true
$psi.UseShellExecute = $false
Expand All @@ -115,6 +117,7 @@ function Run-Program($cmd, $params, $quiet=$true) {
}

function BuildNcatImage() {
Write-Host "Building nmap image for testing"
$exitCode, $stdout, $stderr = Run-Program 'docker.exe' "inspect --type=image nmap"
if($exitCode -ne 0) {
Push-Location -StackName 'agent' -Path "$PSScriptRoot/.."
Expand Down

0 comments on commit 7cc199b

Please sign in to comment.