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

Add automatic tests to the project #142

Merged
merged 6 commits into from
Jan 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea
bats-core/
6 changes: 5 additions & 1 deletion Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ pipeline {
if(!infra.isTrusted()) {
deleteDir()
checkout scm
sh "make build ; docker system prune --force --all"
sh '''
make build
make test
docker system prune --force --all
'''
}
}
}
Expand Down
40 changes: 35 additions & 5 deletions Makefile
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
6 changes: 6 additions & 0 deletions tests/netcat-helper/Dockerfile
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
64 changes: 64 additions & 0 deletions tests/test_helpers.bash
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
}
94 changes: 94 additions & 0 deletions tests/tests.bats
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]}" ]
}