Skip to content

Commit

Permalink
[PATCH] Merge pull request jenkinsci/docker-inbound-agent#363 from le…
Browse files Browse the repository at this point in the history
…meurherve/refactor-windows-builds-like-docker-agent

refactor: build `docker-inbound-agent` like `docker-agent`
  • Loading branch information
lemeurherve committed Jul 28, 2023
1 parent 46fa2ed commit 0b39025
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 19 deletions.
30 changes: 16 additions & 14 deletions tests/inboundAgent.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,37 +1,39 @@
Import-Module -DisableNameChecking -Force $PSScriptRoot/test_helpers.psm1

$global:AGENT_IMAGE = Get-EnvOrDefault 'AGENT_IMAGE' ''
$global:FOLDER = Get-EnvOrDefault 'FOLDER' ''
$global:JAVA_MAJOR_VERSION = Get-EnvOrDefault 'JAVA_MAJOR_VERSION' ''
$global:VERSION = Get-EnvOrDefault 'VERSION' ''
$global:BUILD_CONTEXT = Get-EnvOrDefault 'BUILD_CONTEXT' ''
$global:version = Get-EnvOrDefault 'VERSION' ''
$global:WINDOWS_VERSION_TAG = Get-EnvOrDefault 'WINDOWS_VERSION_TAG' ''

# TODO: make this name unique for concurency
$global:CONTAINERNAME = 'pester-jenkins-inbound-agent-{0}' -f $global:AGENT_IMAGE

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

$items = $global:AGENT_IMAGE.Split("-")

# Remove the 'jdk' prefix (3 first characters)
$global:JDKMAJORVERSION = $items[0].Remove(0,3)
$global:JAVA_MAJOR_VERSION = $items[0].Remove(0,3)
$global:WINDOWSFLAVOR = $items[1]
$global:WINDOWSVERSION = $items[2]

$global:CONTAINERSHELL="powershell.exe"
if($global:WINDOWSFLAVOR -eq 'nanoserver') {
$global:CONTAINERSHELL = "pwsh.exe"
# Special case for nanoserver-1809
if($global:WINDOWSVERSION -eq '1809') {
$global:WINDOWS_VERSION_TAG = '1809'
}
}


Cleanup($global:CONTAINERNAME)
Cleanup("nmap")
CleanupNetwork("jnlp-network")

BuildNcatImage
BuildNcatImage($global:WINDOWS_VERSION_TAG)

Describe "[$global:AGENT_IMAGE] build image" {
It 'builds image' {
$exitCode, $stdout, $stderr = Run-Program 'docker' "build --build-arg version=${global:VERSION} --build-arg JAVA_MAJOR_VERSION=${global:JAVA_MAJOR_VERSION} --tag=${global:AGENT_IMAGE} --file $global:FOLDER/Dockerfile ./"
$exitCode, $stdout, $stderr = Run-Program 'docker' "build --build-arg version=${global:version} --build-arg `"WINDOWS_VERSION_TAG=${global:WINDOWS_VERSION_TAG}`" --build-arg JAVA_MAJOR_VERSION=${global:JAVA_MAJOR_VERSION} --tag=${global:AGENT_IMAGE} --file ./windows/${global:WINDOWSFLAVOR}/Dockerfile ${global:BUILD_CONTEXT}"
$exitCode | Should -Be 0
}
}
Expand Down Expand Up @@ -114,15 +116,15 @@ Describe "[$global:AGENT_IMAGE] custom build args" {
BeforeAll {
Push-Location -StackName 'agent' -Path "$PSScriptRoot/.."
# Old version used to test overriding the build arguments.
# This old version must have the same tag suffixes as the current 4 windows images (`-jdk11-nanoserver` etc.)
$TEST_VERSION = "3046.v38db_38a_b_7a_86"
$DOCKER_AGENT_VERSION_SUFFIX = "1"
$ARG_TEST_VERSION = "${TEST_VERSION}-${DOCKER_AGENT_VERSION_SUFFIX}"
# This old version must have the same tag suffixes as the current windows images (`-jdk11-nanoserver` etc.), and the same Windows version (2019, 2022, etc.)
$TEST_VERSION = "3131.vf2b_b_798b_ce99"
$PARENT_IMAGE_VERSION_SUFFIX = "4"
$ARG_TEST_VERSION = "${TEST_VERSION}-${PARENT_IMAGE_VERSION_SUFFIX}"
$customImageName = "custom-${global:AGENT_IMAGE}"
}

It 'builds image with arguments' {
$exitCode, $stdout, $stderr = Run-Program 'docker' "build --build-arg version=${ARG_TEST_VERSION} --build-arg JAVA_MAJOR_VERSION=${global:JAVA_MAJOR_VERSION} --tag=${customImageName} --file=${global:FOLDER}/Dockerfile ./"
$exitCode, $stdout, $stderr = Run-Program 'docker' "build --build-arg version=${ARG_TEST_VERSION} --build-arg `"WINDOWS_VERSION_TAG=${global:WINDOWS_VERSION_TAG}`" --build-arg JAVA_MAJOR_VERSION=${global:JAVA_MAJOR_VERSION} --tag=${customImageName} --file=./windows/${global:WINDOWSFLAVOR}/Dockerfile ${global:BUILD_CONTEXT}"
$exitCode | Should -Be 0

$exitCode, $stdout, $stderr = Run-Program 'docker' "run --detach --tty --name $global:CONTAINERNAME $customImageName -Cmd $global:CONTAINERSHELL"
Expand Down Expand Up @@ -165,7 +167,7 @@ Describe "[$global:AGENT_IMAGE] passing JVM options (slow test)" {
Is-ContainerRunning $global:CONTAINERNAME | Should -BeTrue
$exitCode, $stdout, $stderr = Run-Program 'docker' "logs $global:CONTAINERNAME"
$exitCode | Should -Be 0
$stdout | Should -Match "OpenJDK Runtime Environment Temurin-${global:JDKMAJORVERSION}"
$stdout | Should -Match "OpenJDK Runtime Environment Temurin-${global:JAVA_MAJOR_VERSION}"
}

AfterAll {
Expand Down
3 changes: 2 additions & 1 deletion tests/netcat-helper/Dockerfile-windows
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

FROM mcr.microsoft.com/windows/servercore:1809
ARG WINDOWS_VERSION_TAG=1809
FROM mcr.microsoft.com/windows/servercore:"${WINDOWS_VERSION_TAG}"

SHELL ["powershell.exe", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]

Expand Down
8 changes: 4 additions & 4 deletions tests/tests.bats
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ SUT_IMAGE="$(get_sut_image)"
@test "[${SUT_IMAGE}] use build args correctly" {
cd "${BATS_TEST_DIRNAME}"/.. || false

local TEST_VERSION DOCKER_AGENT_VERSION_SUFFIX ARG_TEST_VERSION TEST_USER sut_image sut_cid
local TEST_VERSION PARENT_IMAGE_VERSION_SUFFIX ARG_TEST_VERSION TEST_USER sut_image sut_cid

# Old version used to test overriding the build arguments.
# This old version must have the same tag suffixes as the ones defined in the docker-bake file (`-jdk17`, `jdk11`, etc.)
TEST_VERSION="3046.v38db_38a_b_7a_86"
DOCKER_AGENT_VERSION_SUFFIX="1"
TEST_VERSION="3131.vf2b_b_798b_ce99"
PARENT_IMAGE_VERSION_SUFFIX="4"

ARG_TEST_VERSION="${TEST_VERSION}-${DOCKER_AGENT_VERSION_SUFFIX}"
ARG_TEST_VERSION="${TEST_VERSION}-${PARENT_IMAGE_VERSION_SUFFIX}"
TEST_USER="root"

sut_image="${SUT_IMAGE}-tests-${BATS_TEST_NUMBER}"
Expand Down

0 comments on commit 0b39025

Please sign in to comment.