Added support for jenkins official container
This commit is contained in:
31
jenkins/tests/functions.bats
Normal file
31
jenkins/tests/functions.bats
Normal file
@ -0,0 +1,31 @@
|
||||
#!/usr/bin/env bats
|
||||
|
||||
SUT_IMAGE=bats-jenkins
|
||||
|
||||
load 'test_helper/bats-support/load'
|
||||
load 'test_helper/bats-assert/load'
|
||||
load test_helpers
|
||||
|
||||
. $BATS_TEST_DIRNAME/../jenkins-support
|
||||
|
||||
@test "build image" {
|
||||
cd $BATS_TEST_DIRNAME/..
|
||||
docker_build -t $SUT_IMAGE .
|
||||
}
|
||||
|
||||
@test "versionLT" {
|
||||
run docker run --rm $SUT_IMAGE bash -c "source /usr/local/bin/jenkins-support && versionLT 1.0 1.0"
|
||||
assert_failure
|
||||
run docker run --rm $SUT_IMAGE bash -c "source /usr/local/bin/jenkins-support && versionLT 1.0 1.1"
|
||||
assert_success
|
||||
run docker run --rm $SUT_IMAGE bash -c "source /usr/local/bin/jenkins-support && versionLT 1.1 1.0"
|
||||
assert_failure
|
||||
run docker run --rm $SUT_IMAGE bash -c "source /usr/local/bin/jenkins-support && versionLT 1.0-beta-1 1.0"
|
||||
assert_success
|
||||
run docker run --rm $SUT_IMAGE bash -c "source /usr/local/bin/jenkins-support && versionLT 1.0 1.0-beta-1"
|
||||
assert_failure
|
||||
run docker run --rm $SUT_IMAGE bash -c "source /usr/local/bin/jenkins-support && versionLT 1.0-alpha-1 1.0-beta-1"
|
||||
assert_success
|
||||
run docker run --rm $SUT_IMAGE bash -c "source /usr/local/bin/jenkins-support && versionLT 1.0-beta-1 1.0-alpha-1"
|
||||
assert_failure
|
||||
}
|
118
jenkins/tests/install-plugins.bats
Normal file
118
jenkins/tests/install-plugins.bats
Normal file
@ -0,0 +1,118 @@
|
||||
#!/usr/bin/env bats
|
||||
|
||||
SUT_IMAGE=bats-jenkins
|
||||
|
||||
load 'test_helper/bats-support/load'
|
||||
load 'test_helper/bats-assert/load'
|
||||
load test_helpers
|
||||
|
||||
@test "build image" {
|
||||
cd $BATS_TEST_DIRNAME/..
|
||||
docker_build -t $SUT_IMAGE .
|
||||
}
|
||||
|
||||
@test "plugins are installed with plugins.sh" {
|
||||
run docker build -t $SUT_IMAGE-plugins $BATS_TEST_DIRNAME/plugins
|
||||
assert_success
|
||||
# replace DOS line endings \r\n
|
||||
run bash -c "docker run --rm $SUT_IMAGE-plugins ls --color=never -1 /var/jenkins_home/plugins | tr -d '\r'"
|
||||
assert_success
|
||||
assert_line 'maven-plugin.jpi'
|
||||
assert_line 'maven-plugin.jpi.pinned'
|
||||
assert_line 'ant.jpi'
|
||||
assert_line 'ant.jpi.pinned'
|
||||
}
|
||||
|
||||
@test "plugins are installed with install-plugins.sh" {
|
||||
run docker build -t $SUT_IMAGE-install-plugins $BATS_TEST_DIRNAME/install-plugins
|
||||
assert_success
|
||||
refute_line --partial 'Skipping already bundled dependency'
|
||||
# replace DOS line endings \r\n
|
||||
run bash -c "docker run --rm $SUT_IMAGE-install-plugins ls --color=never -1 /var/jenkins_home/plugins | tr -d '\r'"
|
||||
assert_success
|
||||
assert_line 'maven-plugin.jpi'
|
||||
assert_line 'maven-plugin.jpi.pinned'
|
||||
assert_line 'ant.jpi'
|
||||
assert_line 'ant.jpi.pinned'
|
||||
assert_line 'credentials.jpi'
|
||||
assert_line 'credentials.jpi.pinned'
|
||||
assert_line 'mesos.jpi'
|
||||
assert_line 'mesos.jpi.pinned'
|
||||
# optional dependencies
|
||||
refute_line 'metrics.jpi'
|
||||
refute_line 'metrics.jpi.pinned'
|
||||
# plugins bundled but under detached-plugins, so need to be installed
|
||||
assert_line 'javadoc.jpi'
|
||||
assert_line 'javadoc.jpi.pinned'
|
||||
assert_line 'mailer.jpi'
|
||||
assert_line 'mailer.jpi.pinned'
|
||||
}
|
||||
|
||||
@test "plugins are installed with install-plugins.sh even when already exist" {
|
||||
run docker build -t $SUT_IMAGE-install-plugins-update --no-cache $BATS_TEST_DIRNAME/install-plugins/update
|
||||
assert_success
|
||||
assert_line "Using provided plugin: ant"
|
||||
refute_line --partial 'Skipping already bundled dependency'
|
||||
# replace DOS line endings \r\n
|
||||
run bash -c "docker run --rm $SUT_IMAGE-install-plugins-update unzip -p /var/jenkins_home/plugins/maven-plugin.jpi META-INF/MANIFEST.MF | tr -d '\r'"
|
||||
assert_success
|
||||
assert_line 'Plugin-Version: 2.13'
|
||||
}
|
||||
|
||||
@test "plugins are getting upgraded but not downgraded" {
|
||||
# Initial execution
|
||||
run docker build -t $SUT_IMAGE-install-plugins $BATS_TEST_DIRNAME/install-plugins
|
||||
assert_success
|
||||
local work; work="$BATS_TEST_DIRNAME/upgrade-plugins/work"
|
||||
mkdir -p $work
|
||||
# Image contains maven-plugin 2.7.1 and ant-plugin 1.3
|
||||
run bash -c "docker run -u $UID -v $work:/var/jenkins_home --rm $SUT_IMAGE-install-plugins true"
|
||||
assert_success
|
||||
run unzip_manifest maven-plugin.jpi $work
|
||||
assert_line 'Plugin-Version: 2.7.1'
|
||||
run unzip_manifest ant.jpi $work
|
||||
assert_line 'Plugin-Version: 1.3'
|
||||
|
||||
# Upgrade to new image with different plugins
|
||||
run docker build -t $SUT_IMAGE-upgrade-plugins $BATS_TEST_DIRNAME/upgrade-plugins
|
||||
assert_success
|
||||
# Images contains maven-plugin 2.13 and ant-plugin 1.2
|
||||
run bash -c "docker run -u $UID -v $work:/var/jenkins_home --rm $SUT_IMAGE-upgrade-plugins true"
|
||||
assert_success
|
||||
run unzip_manifest maven-plugin.jpi $work
|
||||
assert_success
|
||||
# Should be updated
|
||||
assert_line 'Plugin-Version: 2.13'
|
||||
run unzip_manifest ant.jpi $work
|
||||
# 1.2 is older than the existing 1.3, so keep 1.3
|
||||
assert_line 'Plugin-Version: 1.3'
|
||||
}
|
||||
|
||||
@test "clean work directory" {
|
||||
run bash -c "rm -rf $BATS_TEST_DIRNAME/upgrade-plugins/work"
|
||||
}
|
||||
|
||||
@test "do not upgrade if plugin has been manually updated" {
|
||||
run docker build -t $SUT_IMAGE-install-plugins $BATS_TEST_DIRNAME/install-plugins
|
||||
assert_success
|
||||
local work; work="$BATS_TEST_DIRNAME/upgrade-plugins/work"
|
||||
mkdir -p $work
|
||||
# Image contains maven-plugin 2.7.1 and ant-plugin 1.3
|
||||
run bash -c "docker run -u $UID -v $work:/var/jenkins_home --rm $SUT_IMAGE-install-plugins curl --connect-timeout 20 --retry 5 --retry-delay 0 --retry-max-time 60 -s -f -L https://updates.jenkins.io/download/plugins/maven-plugin/2.12.1/maven-plugin.hpi -o /var/jenkins_home/plugins/maven-plugin.jpi"
|
||||
assert_success
|
||||
run unzip_manifest maven-plugin.jpi $work
|
||||
assert_line 'Plugin-Version: 2.12.1'
|
||||
run docker build -t $SUT_IMAGE-upgrade-plugins $BATS_TEST_DIRNAME/upgrade-plugins
|
||||
assert_success
|
||||
# Images contains maven-plugin 2.13 and ant-plugin 1.2
|
||||
run bash -c "docker run -u $UID -v $work:/var/jenkins_home --rm $SUT_IMAGE-upgrade-plugins true"
|
||||
assert_success
|
||||
run unzip_manifest maven-plugin.jpi $work
|
||||
assert_success
|
||||
# Shouldn't be updated
|
||||
refute_line 'Plugin-Version: 2.13'
|
||||
}
|
||||
|
||||
@test "clean work directory" {
|
||||
run bash -c "rm -rf $BATS_TEST_DIRNAME/upgrade-plugins/work"
|
||||
}
|
3
jenkins/tests/install-plugins/Dockerfile
Normal file
3
jenkins/tests/install-plugins/Dockerfile
Normal file
@ -0,0 +1,3 @@
|
||||
FROM bats-jenkins
|
||||
|
||||
RUN /usr/local/bin/install-plugins.sh maven-plugin:2.7.1 ant:1.3 mesos:0.13.0
|
3
jenkins/tests/install-plugins/update/Dockerfile
Normal file
3
jenkins/tests/install-plugins/update/Dockerfile
Normal file
@ -0,0 +1,3 @@
|
||||
FROM bats-jenkins-install-plugins
|
||||
|
||||
RUN /usr/local/bin/install-plugins.sh maven-plugin:2.13 ant:1.3
|
4
jenkins/tests/plugins/Dockerfile
Normal file
4
jenkins/tests/plugins/Dockerfile
Normal file
@ -0,0 +1,4 @@
|
||||
FROM bats-jenkins
|
||||
|
||||
COPY plugins.txt /usr/share/jenkins/ref/
|
||||
RUN /usr/local/bin/plugins.sh /usr/share/jenkins/ref/plugins.txt
|
2
jenkins/tests/plugins/plugins.txt
Normal file
2
jenkins/tests/plugins/plugins.txt
Normal file
@ -0,0 +1,2 @@
|
||||
maven-plugin:2.7.1
|
||||
ant:1.3
|
56
jenkins/tests/runtime.bats
Normal file
56
jenkins/tests/runtime.bats
Normal file
@ -0,0 +1,56 @@
|
||||
#!/usr/bin/env bats
|
||||
|
||||
SUT_IMAGE=bats-jenkins
|
||||
SUT_CONTAINER=bats-jenkins
|
||||
|
||||
load 'test_helper/bats-support/load'
|
||||
load 'test_helper/bats-assert/load'
|
||||
load test_helpers
|
||||
|
||||
@test "build image" {
|
||||
cd $BATS_TEST_DIRNAME/..
|
||||
docker_build -t $SUT_IMAGE .
|
||||
}
|
||||
|
||||
@test "clean test containers" {
|
||||
cleanup $SUT_CONTAINER
|
||||
}
|
||||
|
||||
@test "test multiple JENKINS_OPTS" {
|
||||
# running --help --version should return the version, not the help
|
||||
local version=$(grep 'ENV JENKINS_VERSION' Dockerfile | sed -e 's/.*:-\(.*\)}/\1/')
|
||||
# need the last line of output
|
||||
assert "${version}" docker run --rm -e JENKINS_OPTS="--help --version" --name $SUT_CONTAINER -P $SUT_IMAGE | tail -n 1
|
||||
}
|
||||
|
||||
@test "test jenkins arguments" {
|
||||
# running --help --version should return the version, not the help
|
||||
local version=$(grep 'ENV JENKINS_VERSION' Dockerfile | sed -e 's/.*:-\(.*\)}/\1/')
|
||||
# need the last line of output
|
||||
assert "${version}" docker run --rm --name $SUT_CONTAINER -P $SUT_IMAGE --help --version | tail -n 1
|
||||
}
|
||||
|
||||
@test "create test container" {
|
||||
docker run -d -e JAVA_OPTS="-Duser.timezone=Europe/Madrid -Dhudson.model.DirectoryBrowserSupport.CSP=\"default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval'; style-src 'self' 'unsafe-inline';\"" --name $SUT_CONTAINER -P $SUT_IMAGE
|
||||
}
|
||||
|
||||
@test "test container is running" {
|
||||
sleep 1 # give time to eventually fail to initialize
|
||||
retry 3 1 assert "true" docker inspect -f {{.State.Running}} $SUT_CONTAINER
|
||||
}
|
||||
|
||||
@test "Jenkins is initialized" {
|
||||
retry 30 5 test_url /api/json
|
||||
}
|
||||
|
||||
@test "JAVA_OPTS are set" {
|
||||
local sed_expr='s/<wbr>//g;s/<td class="pane">.*<\/td><td class.*normal">//g;s/<t.>//g;s/<\/t.>//g'
|
||||
assert 'default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval'; style-src 'self' 'unsafe-inline';' \
|
||||
bash -c "curl -fsSL --user \"admin:$(get_jenkins_password)\" $(get_jenkins_url)/systemInfo | sed 's/<\/tr>/<\/tr>\'$'\n/g' | grep '<td class=\"pane\">hudson.model.DirectoryBrowserSupport.CSP</td>' | sed -e '${sed_expr}'"
|
||||
assert 'Europe/Madrid' \
|
||||
bash -c "curl -fsSL --user \"admin:$(get_jenkins_password)\" $(get_jenkins_url)/systemInfo | sed 's/<\/tr>/<\/tr>\'$'\n/g' | grep '<td class=\"pane\">user.timezone</td>' | sed -e '${sed_expr}'"
|
||||
}
|
||||
|
||||
@test "clean test containers" {
|
||||
cleanup $SUT_CONTAINER
|
||||
}
|
84
jenkins/tests/test_helpers.bash
Normal file
84
jenkins/tests/test_helpers.bash
Normal file
@ -0,0 +1,84 @@
|
||||
#!/bin/bash
|
||||
|
||||
# check dependencies
|
||||
(
|
||||
type docker &>/dev/null || ( echo "docker is not available"; exit 1 )
|
||||
type curl &>/dev/null || ( echo "curl is not available"; exit 1 )
|
||||
)>&2
|
||||
|
||||
# Assert that $1 is the outputof a command $2
|
||||
function assert {
|
||||
local expected_output=$1
|
||||
shift
|
||||
local actual_output
|
||||
actual_output=$("$@")
|
||||
actual_output="${actual_output//[$'\t\r\n']}" # remove newlines
|
||||
if ! [ "$actual_output" = "$expected_output" ]; then
|
||||
echo "expected: \"$expected_output\""
|
||||
echo "actual: \"$actual_output\""
|
||||
false
|
||||
fi
|
||||
}
|
||||
|
||||
# Retry a command $1 times until it succeeds. Wait $2 seconds between retries.
|
||||
function retry {
|
||||
local attempts=$1
|
||||
shift
|
||||
local delay=$1
|
||||
shift
|
||||
local i
|
||||
|
||||
for ((i=0; i < attempts; i++)); do
|
||||
run "$@"
|
||||
if [ "$status" -eq 0 ]; then
|
||||
return 0
|
||||
fi
|
||||
sleep $delay
|
||||
done
|
||||
|
||||
echo "Command \"$*\" failed $attempts times. Status: $status. Output: $output" >&2
|
||||
false
|
||||
}
|
||||
|
||||
function docker_build {
|
||||
if [ -n "$JENKINS_VERSION" ]; then
|
||||
docker build --build-arg JENKINS_VERSION=$JENKINS_VERSION --build-arg JENKINS_SHA=$JENKINS_SHA "$@"
|
||||
else
|
||||
docker build "$@"
|
||||
fi
|
||||
}
|
||||
|
||||
function get_jenkins_url {
|
||||
if [ -z "${DOCKER_HOST}" ]; then
|
||||
DOCKER_IP=localhost
|
||||
else
|
||||
DOCKER_IP=$(echo "$DOCKER_HOST" | sed -e 's|tcp://\(.*\):[0-9]*|\1|')
|
||||
fi
|
||||
echo "http://$DOCKER_IP:$(docker port "$SUT_CONTAINER" 8080 | cut -d: -f2)"
|
||||
}
|
||||
|
||||
function get_jenkins_password {
|
||||
docker logs "$SUT_CONTAINER" 2>&1 | grep -A 2 "Please use the following password to proceed to installation" | tail -n 1
|
||||
}
|
||||
|
||||
function test_url {
|
||||
run curl --user "admin:$(get_jenkins_password)" --output /dev/null --silent --head --fail --connect-timeout 30 --max-time 60 "$(get_jenkins_url)$1"
|
||||
if [ "$status" -eq 0 ]; then
|
||||
true
|
||||
else
|
||||
echo "URL $(get_jenkins_url)$1 failed" >&2
|
||||
echo "output: $output" >&2
|
||||
false
|
||||
fi
|
||||
}
|
||||
|
||||
function cleanup {
|
||||
docker kill "$1" &>/dev/null ||:
|
||||
docker rm -fv "$1" &>/dev/null ||:
|
||||
}
|
||||
|
||||
function unzip_manifest {
|
||||
local plugin=$1
|
||||
local work=$2
|
||||
bash -c "docker run --rm -v $work:/var/jenkins_home --entrypoint unzip $SUT_IMAGE -p /var/jenkins_home/plugins/$plugin META-INF/MANIFEST.MF | tr -d '\r'"
|
||||
}
|
3
jenkins/tests/upgrade-plugins/Dockerfile
Normal file
3
jenkins/tests/upgrade-plugins/Dockerfile
Normal file
@ -0,0 +1,3 @@
|
||||
FROM bats-jenkins
|
||||
|
||||
RUN /usr/local/bin/install-plugins.sh maven-plugin:2.13 ant:1.2
|
Reference in New Issue
Block a user