Skip to content

Commit

Permalink
Use exec instead of bash to start java
Browse files Browse the repository at this point in the history
Fix passing multiple arguments
Add test for jenkins arguments and JENKINS_OPTS
  • Loading branch information
carlossg committed Feb 28, 2016
1 parent a74dd10 commit 5fc9cd0
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
2 changes: 1 addition & 1 deletion jenkins.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ find /usr/share/jenkins/ref/ -type f -exec bash -c "copy_reference_file '{}'" \;

# if `docker run` first argument start with `--` the user is passing jenkins launcher arguments
if [[ $# -lt 1 ]] || [[ "$1" == "--"* ]]; then
exec /bin/bash -c "java $JAVA_OPTS -jar /usr/share/jenkins/jenkins.war $JENKINS_OPTS $@"
exec java $JAVA_OPTS -jar /usr/share/jenkins/jenkins.war $JENKINS_OPTS "$@"
fi

# As argument is not jenkins, assume user want to run his own process, for sample a `bash` shell to explore this image
Expand Down
7 changes: 6 additions & 1 deletion tests/test_helpers.bash
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
function assert {
local expected_output=$1
shift
actual_output=$("$@")
local actual_output=$("$@")
if ! [ "$actual_output" = "$expected_output" ]; then
echo "expected: \"$expected_output\", actual: \"$actual_output\""
false
Expand Down Expand Up @@ -55,3 +55,8 @@ function test_url {
false
fi
}

function cleanup {
docker kill $1 &>/dev/null ||:
docker rm -fv $1 &>/dev/null ||:
}
31 changes: 25 additions & 6 deletions tests/tests.bats
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,43 @@ SUT_CONTAINER=bats-jenkins
load test_helpers

@test "build image" {
cd $BATS_TEST_DIRNAME/..
docker build -t $SUT_IMAGE .
cd $BATS_TEST_DIRNAME/..
docker build -t $SUT_IMAGE .
}

@test "clean test containers" {
docker kill $SUT_CONTAINER &>/dev/null ||:
docker rm -fv $SUT_CONTAINER &>/dev/null ||:
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/ENV JENKINS_VERSION //')
# need the last line of output, removing the last char
local actual_version=$(docker run --rm -ti -e JENKINS_OPTS="--help --version" --name $SUT_CONTAINER -P $SUT_IMAGE | tail -n 1)
assert "${version}" echo ${actual_version::-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/ENV JENKINS_VERSION //')
# need the last line of output, removing the last char
local actual_version=$(docker run --rm -ti --name $SUT_CONTAINER -P $SUT_IMAGE --help --version | tail -n 1)
assert "${version}" echo ${actual_version::-1}
}

@test "create test container" {
docker run -d --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
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 "clean test containers" {
cleanup $SUT_CONTAINER
}

0 comments on commit 5fc9cd0

Please sign in to comment.