change base image to s2i-base

This commit is contained in:
Radoslav Pitonak 2017-05-31 10:34:16 +02:00
commit d53cf50fe1
3 changed files with 125 additions and 17 deletions

View file

@ -1,4 +1,4 @@
FROM registry.fedoraproject.org/fedora:25
FROM registry.fedoraproject.org/f25/s2i-base
# Description
# Environment:
@ -10,16 +10,12 @@ FROM registry.fedoraproject.org/fedora:25
# Additional packages
# * findutils are needed to help fix permissions.
MAINTAINER Rado Pitonak <rpitonak@redhat.com>
LABEL MAINTAINER Rado Pitonak <rpitonak@redhat.com>
RUN dnf install -y --setopt=tsflags=nodocs nodejs npm && \
dnf install -y --setopt=tsflags=nodocs findutils && \
dnf -y clean all
ENV NAME=nodejs \
VERSION=0 \
RELEASE=1 \
ARCH=x86_64
ENV NAME=nodejs VERSION=0 RELEASE=2 ARCH=x86_64
LABEL summary="Javascript runtime." \
name="$FGC/$NAME" \
@ -52,9 +48,7 @@ ENV HOME_APP_ROOT=/opt/app-root \
# create home directory and system user with UID 1001 GID 0 and default name
# set user home directory to $HOME_APP_ROOT
RUN mkdir -p ${HOME_APP_ROOT} && \
useradd -u 1001 -r -g 0 -d ${HOME_APP_ROOT} -s /sbin/nologin \
-c "Default user" default
RUN mkdir -p ${HOME_APP_ROOT}
# EXPOSE instruction exposes port from container to host.
# Specify it during `docker run` as parameter: "-p <host_port>:<container_port>"

16
Makefile Normal file
View file

@ -0,0 +1,16 @@
.PHONY: build build-test run test
IMAGE_NAME = nodejs
CANDIDATE_IMAGE_NAME=nodejs-candidate
build:
docker build --tag=$(IMAGE_NAME):6 .
build-test:
docker build --tag=$(CANDIDATE_IMAGE_NAME) .
run: build
docker run -d $(IMAGE_NAME):6
test: build-test
./test/run

112
test/run
View file

@ -9,7 +9,7 @@
# IMAGE_NAME specifies a name of the candidate image used for testing.
# The image has to be available before this script is executed.
#
IMAGE_NAME=${IMAGE_NAME-nodejs-fedora-candidate}
IMAGE_NAME=${IMAGE_NAME-nodejs-candidate}
# Determining system utility executables (darwin compatibility check)
READLINK_EXEC="readlink"
@ -26,11 +26,15 @@ cid_file=$($MKTEMP_EXEC -u --suffix=.cid)
# Since we built the candidate image locally, we don't want S2I to attempt to pull
# it from Docker hub
s2i_args="--pull-policy=never --loglevel=2"
s2i_args="--force-pull=false"
# Port the image exposes service to be tested
test_port=8080
info() {
echo -e "\n\e[1m[INFO] $@...\e[0m\n"
}
image_exists() {
docker inspect $1 &>/dev/null
}
@ -47,6 +51,10 @@ container_ip() {
fi
}
container_logs() {
docker logs $(cat $cid_file)
}
container_port() {
if [ ! -z "$DOCKER_HOST" ] && [[ "$OSTYPE" =~ 'darwin' ]]; then
docker inspect --format="{{(index .NetworkSettings.Ports \"$test_port/tcp\" 0).HostPort}}" "$(cat "${cid_file}")"
@ -70,11 +78,10 @@ prepare() {
git config user.email "build@localhost" && git config user.name "builder"
git add -A && git commit -m "Sample commit"
popd >/dev/null
run_s2i_build
}
run_test_application() {
docker run --rm --cidfile=${cid_file} -p ${test_port} ${IMAGE_NAME}-testapp
docker run --rm --cidfile=${cid_file} -p ${test_port} $1 ${IMAGE_NAME}-testapp
}
cleanup() {
@ -84,8 +91,9 @@ cleanup() {
fi
fi
if image_exists ${IMAGE_NAME}-testapp; then
docker rmi ${IMAGE_NAME}-testapp
docker rmi -f ${IMAGE_NAME}-testapp
fi
rm -rf ${test_dir}/test-app/.git
}
check_result() {
@ -110,11 +118,56 @@ wait_for_cid() {
done
}
test_usage() {
test_s2i_usage() {
echo "Testing 's2i usage'..."
s2i usage ${s2i_args} ${IMAGE_NAME} &>/dev/null
}
test_docker_run_usage() {
echo "Testing 'docker run' usage..."
docker run ${IMAGE_NAME} &>/dev/null
}
validate_default_value() {
local label=$1
IFS=':' read -a label_vals <<< $(docker inspect -f "{{index .Config.Labels \"$label\"}}" ${IMAGE_NAME})
label_var=${label_vals[0]}
default_label_val=${label_vals[1]}
actual_label_val=$(docker run --rm $IMAGE_NAME /bin/bash -c "echo $"$label_var)
if [ "$actual_label_val" != "$default_label_val" ]; then
echo "ERROR default value for $label with environment variable $label_var; Expected $default_label_val, got $actual_label_val"
return 1
fi
}
# Gets the NODE_ENV environment variable from the container.
get_node_env_from_container() {
local dev_mode="$1"
local node_env="$2"
IFS=':' read -a label_val <<< $(docker inspect -f '{{index .Config.Labels "com.redhat.dev-mode"}}' $IMAGE_NAME)
dev_mode_label_var="${label_val[0]}"
echo $(docker run --rm --env $dev_mode_label_var=$dev_mode --env NODE_ENV=$node_env $IMAGE_NAME /bin/bash -c 'echo "$NODE_ENV"')
}
# Ensures that a docker container run with '--env NODE_ENV=$current_val' produces a NODE_ENV value of $expected when
# DEV_MODE=dev_mode.
validate_node_env() {
local current_val="$1"
local dev_mode_val="$2"
local expected="$3"
actual=$(get_node_env_from_container "$dev_mode_val" "$current_val")
if [ "$actual" != "$expected" ]; then
echo "ERROR default value for NODE_ENV when development mode is $dev_mode_val; should be $expected but is $actual"
return 1
fi
}
test_connection() {
echo "Testing HTTP connection (http://$(container_ip):$(container_port))"
local max_attempts=10
@ -143,10 +196,20 @@ prepare
run_s2i_build
check_result $?
info "Save-artifacts test passed successfully"
# Verify the 'usage' script is working properly
test_usage
test_s2i_usage
check_result $?
info "Usage test passed successfully"
# Verify the 'usage' script is working properly when running the base image with 'docker run ...'
test_docker_run_usage
check_result $?
info "Run usage test passed successfully"
# Verify that the HTTP connection can be established to test application container
run_test_application &
@ -156,4 +219,39 @@ wait_for_cid
test_connection
check_result $?
info "Connection test passed successfully"
echo "Testing DEV_MODE=false"
logs=$(container_logs)
echo ${logs} | grep -q DEV_MODE=false
check_result $?
echo ${logs} | grep -q NODE_ENV=production
check_result $?
echo ${logs} | grep -q DEBUG_PORT=5858
check_result $?
info "Production test passed"
docker kill $(cat $cid_file)
rm $cid_file
echo "Testing DEV_MODE=true"
run_test_application "-e DEV_MODE=true" &
wait_for_cid
test_connection
check_result $?
logs=$(container_logs)
echo ${logs} | grep -q DEV_MODE=true
check_result $?
echo ${logs} | grep -q NODE_ENV=development
check_result $?
echo ${logs} | grep -q DEBUG_PORT=5858
check_result $?
info "Development test passed"
cleanup
info "All tests finished successfully."