Skip to content

Commit

Permalink
Merge branch 'master' into windows_javaopts
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkEWaite authored Nov 20, 2021
2 parents 6d66b7d + 6188c33 commit 88cabf6
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 79 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ To run a Docker container

Windows agent:

docker run jenkins/inbound-agent:windowsservercore-1809 -Url http://jenkins-server:port -Secret <secret> -Name <agent name>
docker run jenkins/inbound-agent:windowsservercore-ltsc2019 -Url http://jenkins-server:port -Secret <secret> -Name <agent name>

To run a Docker container with [Work Directory](https://github.com/jenkinsci/remoting/blob/master/docs/workDir.md)

Expand All @@ -34,7 +34,7 @@ To run a Docker container with [Work Directory](https://github.com/jenkinsci/rem

Windows agent:

docker run jenkins/inbound-agent:windowsservercore-1809 -Url http://jenkins-server:port -WorkDir=C:/Jenkins/agent -Secret <secret> -Name <agent name>
docker run jenkins/inbound-agent:windowsservercore-ltsc2019 -Url http://jenkins-server:port -WorkDir=C:/Jenkins/agent -Secret <secret> -Name <agent name>

Optional environment variables:

Expand Down
17 changes: 11 additions & 6 deletions jenkins-agent
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

# Usage jenkins-agent.sh [options] -url http://jenkins [SECRET] [AGENT_NAME]
# Optional environment variables :
# * JENKINS_JAVA_BIN : Java executable to use instead of the default in PATH or obtained from JAVA_HOME
# * JENKINS_TUNNEL : HOST:PORT for a tunnel to route TCP traffic to jenkins host, when jenkins can't be directly accessed over network
# * JENKINS_URL : alternate jenkins URL
# * JENKINS_SECRET : agent secret, if not set as an argument
Expand All @@ -36,9 +37,9 @@
# the agent skips connecting to an HTTP(S) port for connection info.
# * JENKINS_PROTOCOLS: Specify the remoting protocols to attempt when instanceIdentity is provided.

if [ $# -eq 1 ]; then
if [ $# -eq 1 ] && [ "${1#-}" = "$1" ] ; then

# if `docker run` only has one arguments, we assume user is running alternate command like `bash` to inspect the image
# if `docker run` only has one arguments and it is not an option as `-help`, we assume user is running alternate command like `bash` to inspect the image
exec "$@"

else
Expand Down Expand Up @@ -85,10 +86,14 @@ else
INSTANCE_IDENTITY="-instanceIdentity $JENKINS_INSTANCE_IDENTITY"
fi

# if java home is defined, use it
JAVA_BIN="java"
if [ "$JAVA_HOME" ]; then
JAVA_BIN="$JAVA_HOME/bin/java"
if [ "$JENKINS_JAVA_BIN" ]; then
JAVA_BIN="$JENKINS_JAVA_BIN"
else
# if java home is defined, use it
JAVA_BIN="java"
if [ "$JAVA_HOME" ]; then
JAVA_BIN="$JAVA_HOME/bin/java"
fi
fi

# if both required options are defined, do not pass the parameters
Expand Down
19 changes: 13 additions & 6 deletions jenkins-agent.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,22 @@
Param(
$Cmd = '', # this is only used when docker run has one arg positional arg
$Url = $( if([System.String]::IsNullOrWhiteSpace($Cmd) -and [System.String]::IsNullOrWhiteSpace($env:JENKINS_URL)) { throw ("Url is required") } else { '' } ),
$Secret = $( if([System.String]::IsNullOrWhiteSpace($Cmd) -and [System.String]::IsNullOrWhiteSpace($env:JENKINS_SECRET)) { throw ("Secret is required") } else { '' } ),
$Name = $( if([System.String]::IsNullOrWhiteSpace($Cmd) -and [System.String]::IsNullOrWhiteSpace($env:JENKINS_AGENT_NAME)) { throw ("Name is required") } else { '' } ),
[Parameter(Position=0)]$Secret = $( if([System.String]::IsNullOrWhiteSpace($Cmd) -and [System.String]::IsNullOrWhiteSpace($env:JENKINS_SECRET)) { throw ("Secret is required") } else { '' } ),
[Parameter(Position=1)]$Name = $( if([System.String]::IsNullOrWhiteSpace($Cmd) -and [System.String]::IsNullOrWhiteSpace($env:JENKINS_AGENT_NAME)) { throw ("Name is required") } else { '' } ),
$Tunnel = '',
$WorkDir = '',
[switch] $WebSocket = $false,
$DirectConnection = '',
$InstanceIdentity = '',
$Protocols = '',
$JenkinsJavaBin = '',
$JavaHome = $env:JAVA_HOME,
$JavaOpts = ''
)

# Usage jenkins-agent.ps1 [options] -Url http://jenkins -Secret [SECRET] -Name [AGENT_NAME]
# Optional environment variables :
# * JENKINS_JAVA_BIN : Java executable to use instead of the default in PATH or obtained from JAVA_HOME
# * JENKINS_TUNNEL : HOST:PORT for a tunnel to route TCP traffic to jenkins host, when jenkins can't be directly accessed over network
# * JENKINS_URL : alternate jenkins URL
# * JENKINS_SECRET : agent secret, if not set as an argument
Expand All @@ -57,6 +59,7 @@ if(![System.String]::IsNullOrWhiteSpace($Cmd)) {

# this maps the variable name from the CmdletBinding to environment variables
$ParamMap = @{
'JenkinsJavaBin' = 'JENKINS_JAVA_BIN';
'Tunnel' = 'JENKINS_TUNNEL';
'Url' = 'JENKINS_URL';
'Secret' = 'JENKINS_SECRET';
Expand Down Expand Up @@ -132,10 +135,14 @@ if(![System.String]::IsNullOrWhiteSpace($Cmd)) {
# parameters to agent.jar
$AgentArguments += @($Secret, $Name)

# if java home is defined, use it
$JAVA_BIN="java.exe"
if(![System.String]::IsNullOrWhiteSpace($JavaHome)) {
$JAVA_BIN="$JavaHome/bin/java.exe"
if(![System.String]::IsNullOrWhiteSpace($JenkinsJavaBin)) {
$JAVA_BIN = $JenkinsJavaBin
} else {
# if java home is defined, use it
$JAVA_BIN = "java.exe"
if (![System.String]::IsNullOrWhiteSpace($JavaHome)) {
$JAVA_BIN = "$JavaHome/bin/java.exe"
}
}

#TODO: Handle the case when the command-line and Environment variable contain different values.
Expand Down
2 changes: 1 addition & 1 deletion tests/inboundAgent.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ $AGENT_CONTAINER='pester-jenkins-inbound-agent'
$SHELL="powershell.exe"

$FOLDER = Get-EnvOrDefault 'FOLDER' ''
$VERSION = Get-EnvOrDefault 'VERSION' '4.7-1'
$VERSION = Get-EnvOrDefault 'VERSION' '4.9-1'

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

Expand Down
95 changes: 31 additions & 64 deletions tests/tests.bats
Original file line number Diff line number Diff line change
@@ -1,69 +1,34 @@
#!/usr/bin/env bats

AGENT_IMAGE=jenkins-jnlp-agent
AGENT_CONTAINER=bats-jenkins-jnlp-agent
NETCAT_HELPER_CONTAINER=netcat-helper

REGEX='^([0-9]+)/(.+)$'

REAL_FOLDER=$(realpath "${BATS_TEST_DIRNAME}/../${FOLDER}")

if [[ ${FOLDER} =~ ${REGEX} ]] && [[ -d "${REAL_FOLDER}" ]]
then
JDK="${BASH_REMATCH[1]}"
FLAVOR="${BASH_REMATCH[2]}"
else
echo "Wrong folder format or folder does not exist: ${FOLDER}"
exit 1
fi

if [[ "${JDK}" = "11" ]]
then
AGENT_IMAGE+=":jdk11"
AGENT_CONTAINER+="-jdk11"
else
if [[ "${FLAVOR}" = "alpine*" ]]
then
AGENT_IMAGE+=":alpine"
AGENT_CONTAINER+="-alpine"
else
AGENT_IMAGE+=":latest"
fi
fi

load test_helpers

clean_test_container

buildNetcatImage

function teardown () {
clean_test_container
}

@test "[${JDK} ${FLAVOR}] build image" {
cd "${BATS_TEST_DIRNAME}"/.. || false
docker build -t "${AGENT_IMAGE}" ${FOLDER}
}
SUT_IMAGE=$(get_sut_image)

@test "[${JDK} ${FLAVOR}] image has installed jenkins-agent in PATH" {
docker run -d -it --name "${AGENT_CONTAINER}" -P "${AGENT_IMAGE}" /bin/bash
@test "[${SUT_IMAGE}] image has installed jenkins-agent in PATH" {
cid=$(docker run -d -it -P "${SUT_IMAGE}" /bin/bash)

is_slave_container_running
is_agent_container_running $cid

run docker exec "${AGENT_CONTAINER}" which jenkins-agent
run docker exec "${cid}" which jenkins-agent
[ "/usr/local/bin/jenkins-agent" = "${lines[0]}" ]

run docker exec "${AGENT_CONTAINER}" which jenkins-agent
run docker exec "${cid}" which jenkins-agent
[ "/usr/local/bin/jenkins-agent" = "${lines[0]}" ]

cleanup $cid
}

@test "[${JDK} ${FLAVOR}] image starts jenkins-agent correctly (slow test)" {
@test "[${SUT_IMAGE}] image starts jenkins-agent correctly (slow test)" {
# Spin off a helper image which contains netcat
docker run -d -it --name netcat-helper netcat-helper:latest /bin/sh
netcat_cid=$(docker run -d -it --name netcat-helper netcat-helper:latest /bin/sh)

# Run jenkins agent which tries to connect to the netcat-helper container at port 5000
docker run -d --link netcat-helper --name "${AGENT_CONTAINER}" "${AGENT_IMAGE}" -url http://netcat-helper:5000 aaa bbb
cid=$(docker run -d --link netcat-helper "${SUT_IMAGE}" -url http://netcat-helper:5000 aaa bbb)

# Launch the netcat utility, listening at port 5000 for 30 sec
# bats will capture the output from netcat and compare the first line
Expand All @@ -72,39 +37,41 @@ function teardown () {

# The GET request ends with a '\r'
[ $'GET /tcpSlaveAgentListener/ HTTP/1.1\r' = "${lines[0]}" ]

cleanup $netcat_cid
cleanup $cid
}

@test "[${JDK} ${FLAVOR}] use build args correctly" {
@test "[${SUT_IMAGE}] use build args correctly" {
cd "${BATS_TEST_DIRNAME}"/.. || false

local ARG_TEST_VERSION
local TEST_VERSION="4.3"
local DOCKER_AGENT_VERSION_SUFFIX="4"
local TEST_USER="root"
local ARG_TEST_VERSION="${TEST_VERSION}-${DOCKER_AGENT_VERSION_SUFFIX}"

if [[ "${FLAVOR}" = "debian" ]]
then
ARG_TEST_VERSION="${TEST_VERSION}-${DOCKER_AGENT_VERSION_SUFFIX}"
elif [[ "${FLAVOR}" = "jdk11" ]]
then
ARG_TEST_VERSION="${TEST_VERSION}-${DOCKER_AGENT_VERSION_SUFFIX}-jdk11"
else
ARG_TEST_VERSION="${TEST_VERSION}-${DOCKER_AGENT_VERSION_SUFFIX}-alpine"
fi
local FOLDER=$(get_dockerfile_directory)

docker build \
--build-arg "version=${ARG_TEST_VERSION}" \
--build-arg "user=${TEST_USER}" \
-t "${AGENT_IMAGE}" \
${FOLDER}
local sut_image="${SUT_IMAGE}-tests-${BATS_TEST_NUMBER}"

docker run -d -it --name "${AGENT_CONTAINER}" -P "${AGENT_IMAGE}" /bin/sh
docker buildx bake \
--set "${IMAGE}".args.version="${ARG_TEST_VERSION}" \
--set "${IMAGE}".args.user="${TEST_USER}" \
--set "${IMAGE}".platform="linux/${ARCH}" \
--set "${IMAGE}".tags="${sut_image}" \
--load \
"${IMAGE}"

is_slave_container_running
cid=$(docker run -d -it --name "${AGENT_CONTAINER}" -P "${sut_image}" /bin/sh)

run docker exec "${AGENT_CONTAINER}" sh -c "java -cp /usr/share/jenkins/agent.jar hudson.remoting.jnlp.Main -version"
is_agent_container_running $cid

run docker exec "${cid}" sh -c "java -cp /usr/share/jenkins/agent.jar hudson.remoting.jnlp.Main -version"
[ "${TEST_VERSION}" = "${lines[0]}" ]

run docker exec "${AGENT_CONTAINER}" sh -c "id -u -n ${TEST_USER}"
[ "${TEST_USER}" = "${lines[0]}" ]

cleanup $cid
}

0 comments on commit 88cabf6

Please sign in to comment.