This repository has been archived by the owner on Jan 21, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 527
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #142 from krufab/add_test_and_uniform_code
Added tests to the project
- Loading branch information
Showing
6 changed files
with
206 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.idea | ||
bats-core/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,37 @@ | ||
ROOT:=$(shell dirname $(realpath $(lastword $(MAKEFILE_LIST)))) | ||
IMAGE_NAME:=jenkins4eval/jnlp-slave:test | ||
|
||
build: | ||
docker build -t ${IMAGE_NAME} . | ||
docker build -t ${IMAGE_NAME}-alpine -f Dockerfile-alpine . | ||
docker build -t ${IMAGE_NAME}-jdk11 -f Dockerfile-jdk11 . | ||
IMAGE_NAME:=jenkins4eval/jnlp-slave | ||
IMAGE_ALPINE:=${IMAGE_NAME}:alpine | ||
IMAGE_DEBIAN:=${IMAGE_NAME}:test | ||
IMAGE_JDK11:=${IMAGE_NAME}:jdk11 | ||
|
||
build: build-alpine build-debian build-jdk11 | ||
|
||
build-alpine: | ||
docker build -t ${IMAGE_ALPINE} --file Dockerfile-alpine . | ||
|
||
build-debian: | ||
docker build -t ${IMAGE_DEBIAN} --file Dockerfile . | ||
|
||
build-jdk11: | ||
docker build -t ${IMAGE_JDK11} --file Dockerfile-jdk11 . | ||
|
||
bats: | ||
# The lastest version is v1.1.0 | ||
@if [ ! -d bats-core ]; then git clone https://github.com/bats-core/bats-core.git; fi | ||
@git -C bats-core reset --hard c706d1470dd1376687776bbe985ac22d09780327 | ||
|
||
.PHONY: test | ||
test: test-alpine test-debian test-jdk11 | ||
|
||
.PHONY: test-alpine | ||
test-alpine: bats | ||
@FLAVOR=alpine bats-core/bin/bats tests/tests.bats | ||
|
||
.PHONY: test-debian | ||
test-debian: bats | ||
@bats-core/bin/bats tests/tests.bats | ||
|
||
.PHONY: test-jdk11 | ||
test-jdk11: bats | ||
@FLAVOR=jdk11 bats-core/bin/bats tests/tests.bats |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
FROM alpine:3.11 | ||
|
||
RUN apk update --no-cache \ | ||
&& apk add --no-cache \ | ||
coreutils \ | ||
netcat-openbsd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#!/usr/bin/env bash | ||
|
||
set -eu | ||
|
||
# check dependencies | ||
( | ||
type docker &>/dev/null || ( echo "docker is not available"; exit 1 ) | ||
)>&2 | ||
|
||
function printMessage { | ||
echo "# ${@}" >&3 | ||
} | ||
|
||
# Assert that $1 is the output of a command $2 | ||
function assert { | ||
local expected_output | ||
local actual_output | ||
expected_output="${1}" | ||
shift | ||
actual_output=$("${@}") | ||
if ! [[ "${actual_output}" = "${expected_output}" ]]; then | ||
printMessage "Expected: '${expected_output}', actual: '${actual_output}'" | ||
false | ||
fi | ||
} | ||
|
||
# Retry a command $1 times until it succeeds. Wait $2 seconds between retries. | ||
function retry { | ||
local attempts | ||
local delay | ||
local i | ||
attempts="${1}" | ||
shift | ||
delay="${1}" | ||
shift | ||
|
||
for ((i=0; i < attempts; i++)); do | ||
run "${@}" | ||
if [[ "${status}" -eq 0 ]]; then | ||
return 0 | ||
fi | ||
sleep "${delay}" | ||
done | ||
|
||
printMessage "Command '${*}' failed $attempts times. Status: ${status}. Output: ${output}" | ||
|
||
false | ||
} | ||
|
||
function clean_test_container { | ||
docker kill "${SLAVE_CONTAINER}" "${NETCAT_HELPER_CONTAINER}" &>/dev/null || : | ||
docker rm -fv "${SLAVE_CONTAINER}" "${NETCAT_HELPER_CONTAINER}" &>/dev/null || : | ||
} | ||
|
||
function is_slave_container_running { | ||
sleep 1 | ||
retry 3 1 assert "true" docker inspect -f '{{.State.Running}}' "${SLAVE_CONTAINER}" | ||
} | ||
|
||
function buildNetcatImage() { | ||
if ! docker inspect --type=image netcat-helper:latest &>/dev/null; then | ||
docker build -t netcat-helper:latest tests/netcat-helper/ &>/dev/null | ||
fi | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
#!/usr/bin/env bats | ||
|
||
DOCKERFILE=Dockerfile | ||
JDK=8 | ||
SLAVE_IMAGE=jenkins-jnlp-slave | ||
SLAVE_CONTAINER=bats-jenkins-jnlp-slave | ||
NETCAT_HELPER_CONTAINER=netcat-helper | ||
|
||
if [[ -z "${FLAVOR}" ]] | ||
then | ||
FLAVOR="debian" | ||
elif [[ "${FLAVOR}" = "jdk11" ]] | ||
then | ||
DOCKERFILE+="-jdk11" | ||
JDK=11 | ||
SLAVE_IMAGE+=":jdk11" | ||
SLAVE_CONTAINER+="-jdk11" | ||
else | ||
DOCKERFILE+="-alpine" | ||
SLAVE_IMAGE+=":alpine" | ||
SLAVE_CONTAINER+="-alpine" | ||
fi | ||
|
||
load test_helpers | ||
|
||
clean_test_container | ||
|
||
buildNetcatImage | ||
|
||
function teardown () { | ||
clean_test_container | ||
} | ||
|
||
@test "[${FLAVOR}] build image" { | ||
cd "${BATS_TEST_DIRNAME}"/.. || false | ||
docker build -t "${SLAVE_IMAGE}" -f "${DOCKERFILE}" . | ||
} | ||
|
||
@test "[${FLAVOR}] image has installed jenkins-agent in PATH" { | ||
docker run -d -it --name "${SLAVE_CONTAINER}" -P "${SLAVE_IMAGE}" /bin/bash | ||
|
||
is_slave_container_running | ||
|
||
run docker exec "${SLAVE_CONTAINER}" which jenkins-slave | ||
[ "/usr/local/bin/jenkins-slave" = "${lines[0]}" ] | ||
|
||
run docker exec "${SLAVE_CONTAINER}" which jenkins-agent | ||
[ "/usr/local/bin/jenkins-agent" = "${lines[0]}" ] | ||
} | ||
|
||
@test "[${FLAVOR}] image starts jenkins-agent correctly" { | ||
docker run -d -it --name netcat-helper netcat-helper:latest /bin/sh | ||
|
||
docker run -d --link netcat-helper --name "${SLAVE_CONTAINER}" "${SLAVE_IMAGE}" -url http://netcat-helper:5000 aaa bbb | ||
|
||
run docker exec netcat-helper /bin/sh -c "timeout 10s nc -l 5000" | ||
|
||
# The GET request ends with a '\r' | ||
[ $'GET /tcpSlaveAgentListener/ HTTP/1.1\r' = "${lines[0]}" ] | ||
} | ||
|
||
@test "[${FLAVOR}] use build args correctly" { | ||
cd "${BATS_TEST_DIRNAME}"/.. || false | ||
|
||
local ARG_TEST_VERSION | ||
local TEST_VERSION="3.36" | ||
local TEST_USER="root" | ||
|
||
if [[ "${FLAVOR}" = "debian" ]] | ||
then | ||
ARG_TEST_VERSION="${TEST_VERSION}-1" | ||
elif [[ "${FLAVOR}" = "jdk11" ]] | ||
then | ||
ARG_TEST_VERSION="${TEST_VERSION}-1-jdk11" | ||
else | ||
ARG_TEST_VERSION="${TEST_VERSION}-1-alpine" | ||
fi | ||
|
||
docker build \ | ||
--build-arg "version=${ARG_TEST_VERSION}" \ | ||
--build-arg "user=${TEST_USER}" \ | ||
-t "${SLAVE_IMAGE}" \ | ||
-f "${DOCKERFILE}" . | ||
|
||
docker run -d -it --name "${SLAVE_CONTAINER}" -P "${SLAVE_IMAGE}" /bin/sh | ||
|
||
is_slave_container_running | ||
|
||
run docker exec "${SLAVE_CONTAINER}" sh -c "java -cp /usr/share/jenkins/agent.jar hudson.remoting.jnlp.Main -version" | ||
[ "${TEST_VERSION}" = "${lines[0]}" ] | ||
|
||
run docker exec "${SLAVE_CONTAINER}" sh -c "id -u -n ${TEST_USER}" | ||
[ "${TEST_USER}" = "${lines[0]}" ] | ||
} |