Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add automatic tests to the project #101

Merged
merged 8 commits into from
Jan 29, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 2 additions & 1 deletion Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ pipeline {
steps {
script {
if(!infra.isTrusted()) {
sh './build.sh ; docker system prune --force --all'
sh 'make tests && make build'
sh 'docker system prune --force --all'
}
}
}
Expand Down
14 changes: 14 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
ROOT:=$(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
IMAGE_NAME:=jenkins4eval/slave
IMAGE_NAME_AGENT:=jenkins4eval/agent

build:
docker build -t ${IMAGE_NAME}:latest -t ${IMAGE_NAME_AGENT}:latest .
docker build -t ${IMAGE_NAME}:alpine -t ${IMAGE_NAME_AGENT}:alpine -f Dockerfile-alpine .
docker build -t ${IMAGE_NAME}:jdk11 -t ${IMAGE_NAME_AGENT}:jdk11 -f Dockerfile-jdk11 .

.PHONY: tests
tests:
@bats tests/tests.bats
@FLAVOR=alpine bats tests/tests.bats
@FLAVOR=jdk11 bats tests/tests.bats
20 changes: 0 additions & 20 deletions build.sh

This file was deleted.

58 changes: 58 additions & 0 deletions tests/test_helpers.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/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}" &>/dev/null || :
docker rm -fv "${SLAVE_CONTAINER}" &>/dev/null || :
}

function is_slave_container_running {
sleep 1
retry 3 1 assert "true" docker inspect -f '{{.State.Running}}' "${SLAVE_CONTAINER}"
}
122 changes: 122 additions & 0 deletions tests/tests.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
#!/usr/bin/env bats

DOCKERFILE=Dockerfile
JDK=8
SLAVE_IMAGE=jenkins-slave
krufab marked this conversation as resolved.
Show resolved Hide resolved
SLAVE_CONTAINER=bats-jenkins-slave

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

function teardown () {
clean_test_container
}

@test "[${FLAVOR}] build image" {
cd "${BATS_TEST_DIRNAME}"/.. || false
docker build -t "${SLAVE_IMAGE}" -f "${DOCKERFILE}" .
}

@test "[${FLAVOR}] checking image metadata" {
local VOLUMES_MAP
VOLUMES_MAP="$(docker inspect -f '{{.Config.Volumes}}' ${SLAVE_IMAGE})"

echo "${VOLUMES_MAP}" | grep '/home/jenkins/.jenkins'
echo "${VOLUMES_MAP}" | grep '/home/jenkins/agent'
}

@test "[${FLAVOR}] image has bash and java installed and in the PATH" {
docker run -d -it --name "${SLAVE_CONTAINER}" -P "${SLAVE_IMAGE}" /bin/sh

is_slave_container_running

run docker exec "${SLAVE_CONTAINER}" which bash
[ "${status}" -eq 0 ]
run docker exec "${SLAVE_CONTAINER}" bash --version
[ "${status}" -eq 0 ]
run docker exec "${SLAVE_CONTAINER}" which java
[ "${status}" -eq 0 ]

if [[ "${JDK}" -eq 8 ]]
then
run docker exec "${SLAVE_CONTAINER}" sh -c "
java -version 2>&1 \
| grep -o -E '^openjdk version \"[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+.*\"' \
| grep -o -E '\.[[:digit:]]+\.' \
| grep -o -E '[[:digit:]]+'
"
else
run docker exec "${SLAVE_CONTAINER}" sh -c "
java -version 2>&1 \
| grep -o -E '^openjdk version \"[[:digit:]]+\.' \
| grep -o -E '\"[[:digit:]]+\.' \
| grep -o -E '[[:digit:]]+'
"
fi
[ "${JDK}" = "${lines[0]}" ]

run docker exec "${SLAVE_CONTAINER}" sh -c "printenv | grep AGENT_WORKDIR"
[ "AGENT_WORKDIR=/home/jenkins/agent" = "${output}" ]
}

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

local TEST_VERSION="3.36"
krufab marked this conversation as resolved.
Show resolved Hide resolved
local TEST_USER="test-user"
local TEST_GROUP="test-group"
local TEST_UID=2000
local TEST_GID=3000
local TEST_AGENT_WORKDIR="/home/test-user/something"

docker build \
--build-arg "VERSION=${TEST_VERSION}" \
--build-arg "user=${TEST_USER}" \
--build-arg "group=${TEST_GROUP}" \
--build-arg "uid=${TEST_UID}" \
--build-arg "gid=${TEST_GID}" \
--build-arg "AGENT_WORKDIR=${TEST_AGENT_WORKDIR}" \
-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]}" ]

run docker exec "${SLAVE_CONTAINER}" sh -c "id -g -n ${TEST_USER}"
[ "${TEST_GROUP}" = "${lines[0]}" ]

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

run docker exec "${SLAVE_CONTAINER}" sh -c "id -g ${TEST_USER}"
[ "${TEST_GID}" = "${lines[0]}" ]

run docker exec "${SLAVE_CONTAINER}" sh -c "printenv | grep AGENT_WORKDIR"
[ "AGENT_WORKDIR=/home/${TEST_USER}/something" = "${lines[0]}" ]

run docker exec "${SLAVE_CONTAINER}" sh -c 'stat -c "%U:%G" "${AGENT_WORKDIR}"'
[ "${TEST_USER}:${TEST_GROUP}" = "${lines[0]}" ]
}