-
Notifications
You must be signed in to change notification settings - Fork 198
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
Pre-init s2i extensibility #32
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#!/bin/sh | ||
|
||
# get_matched_files finds file for image extending | ||
function get_matched_files() { | ||
local custom_dir default_dir | ||
custom_dir="$1" | ||
default_dir="$2" | ||
files_matched="$3" | ||
find "$default_dir" -maxdepth 1 -type f -name "$files_matched" -printf "%f\n" | ||
[ -d "$custom_dir" ] && find "$custom_dir" -maxdepth 1 -type f -name "$files_matched" -printf "%f\n" | ||
} | ||
|
||
# process_extending_files process extending files in $1 and $2 directories | ||
# - source all *.sh files | ||
# (if there are files with same name source only file from $1) | ||
function process_extending_files() { | ||
local custom_dir default_dir | ||
custom_dir=$1 | ||
default_dir=$2 | ||
while read filename ; do | ||
if [ $filename ]; then | ||
echo "=> sourcing $filename ..." | ||
# Custom file is prefered | ||
if [ -f $custom_dir/$filename ]; then | ||
source $custom_dir/$filename | ||
elif [ -f $default_dir/$filename ]; then | ||
source $default_dir/$filename | ||
fi | ||
fi | ||
done <<<"$(get_matched_files "$custom_dir" "$default_dir" '*.sh' | sort -u)" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<html> | ||
<head> | ||
<title>Test NGINX passed</title> | ||
</head> | ||
<body> | ||
<h1>NGINX is working</h1> | ||
</body> | ||
</html> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<html> | ||
<head> | ||
<title>Test NGINX passed</title> | ||
</head> | ||
<body> | ||
<h1>NGINX2 is working</h1> | ||
</body> | ||
</html> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
server { | ||
listen 8080; | ||
server_name localhost2; | ||
root /opt/app-root/src; | ||
index ${INDEX_FILE}; | ||
resolver ${DNS_SERVER}; | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
setup_dns_env_var() { | ||
if [ -z "$DNS_SERVER" ]; then | ||
export DNS_SERVER=`cat /etc/resolv.conf | grep "nameserver " | awk '{print $2}' | tr '\n' ' '` | ||
echo "Using system dns server ${DNS_SERVER}" | ||
else | ||
echo "Using user defined dns server: ${DNS_SERVER}" | ||
fi | ||
} | ||
|
||
inject_env_vars() { | ||
## Replace only specified environment variables in specified file. | ||
envsubst '${DNS_SERVER},${INDEX_FILE}' < ${NGINX_CONFIGURATION_PATH}/$1 > output.conf | ||
cp output.conf ${NGINX_CONFIGURATION_PATH}/$1 | ||
echo "This is $1: " && cat ${NGINX_CONFIGURATION_PATH}/$1 | ||
} | ||
|
||
export INDEX_FILE=index2.html | ||
setup_dns_env_var | ||
inject_env_vars "default.conf" |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,7 +32,7 @@ container_ip() { | |
} | ||
|
||
run_s2i_build() { | ||
s2i build ${s2i_args} file://${test_dir}/test-app ${IMAGE_NAME} ${IMAGE_NAME}-testapp | ||
s2i build ${s2i_args} file://${test_dir}/${1} ${IMAGE_NAME} ${IMAGE_NAME}-${1} | ||
} | ||
|
||
prepare() { | ||
|
@@ -43,7 +43,7 @@ prepare() { | |
# TODO: S2I build require the application is a valid 'GIT' repository, we | ||
# should remove this restriction in the future when a file:// is used. | ||
info "Build the test application image" | ||
pushd ${test_dir}/test-app >/dev/null | ||
pushd ${test_dir}/${1} >/dev/null | ||
git init | ||
git config user.email "build@localhost" && git config user.name "builder" | ||
git add -A && git commit -m "Sample commit" | ||
|
@@ -52,7 +52,7 @@ prepare() { | |
|
||
run_test_application() { | ||
run_args=${CONTAINER_ARGS:-} | ||
docker run --user=100001 ${run_args} --cidfile=${cid_file} ${IMAGE_NAME}-testapp | ||
docker run --user=100001 ${run_args} --cidfile=${cid_file} ${IMAGE_NAME}-${1} | ||
} | ||
|
||
cleanup_test_app() { | ||
|
@@ -68,17 +68,17 @@ cleanup_test_app() { | |
|
||
cleanup() { | ||
info "Cleaning up the test application image" | ||
if image_exists ${IMAGE_NAME}-testapp; then | ||
docker rmi -f ${IMAGE_NAME}-testapp | ||
if image_exists ${IMAGE_NAME}-${1}; then | ||
docker rmi -f ${IMAGE_NAME}-${1} | ||
fi | ||
rm -rf ${test_dir}/test-app/.git | ||
rm -rf ${test_dir}/${1}/.git | ||
} | ||
|
||
check_result() { | ||
local result="$1" | ||
if [[ "$result" != "0" ]]; then | ||
info "TEST FAILED (${result})" | ||
cleanup | ||
cleanup $2 | ||
exit $result | ||
fi | ||
} | ||
|
@@ -116,6 +116,18 @@ test_scl_usage() { | |
echo "ERROR[/bin/bash -c "${run_cmd}"] Expected '${expected}', got '${out}'" | ||
return 1 | ||
fi | ||
test_command "$run_cmd" "$expected" | ||
return $? | ||
} | ||
|
||
test_command() { | ||
local run_cmd="$1" | ||
local expected="$2" | ||
local message="$3" | ||
|
||
if [ $message ]; then | ||
info ${3} | ||
fi | ||
out=$(docker exec $(cat ${cid_file}) /bin/bash -c "${run_cmd}" 2>&1) | ||
if ! echo "${out}" | grep -q "${expected}"; then | ||
echo "ERROR[exec /bin/bash -c "${run_cmd}"] Expected '${expected}', got '${out}'" | ||
|
@@ -125,7 +137,9 @@ test_scl_usage() { | |
if ! echo "${out}" | grep -q "${expected}"; then | ||
echo "ERROR[exec /bin/sh -ic "${run_cmd}"] Expected '${expected}', got '${out}'" | ||
return 1 | ||
fi | ||
fi | ||
|
||
return 0 | ||
} | ||
|
||
test_for_output() | ||
|
@@ -174,44 +188,83 @@ test_connection() { | |
return 1 | ||
} | ||
|
||
test_connection_s2i() { | ||
cat $cid_file | ||
info "Testing the HTTP connection (http://$(container_ip):${test_port})" | ||
|
||
if test_for_output "http://$(container_ip):${test_port}/" "NGINX is working" && | ||
test_for_output "http://$(container_ip):${test_port}/" "NGINX2 is working" localhost2 && | ||
test_for_output "http://$(container_ip):${test_port}/nginx-cfg/default.conf" "404"; then | ||
return 0 | ||
fi | ||
|
||
return 1 | ||
} | ||
|
||
test_application() { | ||
# Verify that the HTTP connection can be established to test application container | ||
run_test_application & | ||
run_test_application "test-app" & | ||
|
||
# Wait for the container to write it's CID file | ||
wait_for_cid | ||
|
||
test_scl_usage "nginx -v" "nginx version: nginx/1.10." | ||
check_result $? | ||
check_result $? "test-app" | ||
|
||
test_connection | ||
check_result $? | ||
check_result $? "test-app" | ||
cleanup_test_app | ||
} | ||
|
||
test_pre_init_script() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Definitely +1 for passing app name as argument to functions. So be able to use these functions in more cases.
I suggest:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1, will implement this. Just a note, it's the other way around, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I haven't changed this in #41, because I'd rather see using the |
||
# Verify that the HTTP connection can be established to test application container | ||
run_test_application "pre-init-test-app" & | ||
|
||
# Wait for the container to write it's CID file | ||
wait_for_cid | ||
|
||
test_command "cat /opt/app-root/etc/nginx.d/default.conf | grep 'resolver' | sed -e 's/resolver\(.*\);/\1/' | grep 'DNS_SERVER'" "" | ||
check_result $? "pre-init-test-app" | ||
|
||
test_connection_s2i | ||
check_result $? "pre-init-test-app" | ||
cleanup_test_app | ||
|
||
} | ||
|
||
cid_file=$(mktemp -u --suffix=.cid) | ||
|
||
# Since we built the candidate image locally, we don't want S2I attempt to pull | ||
# it from Docker hub | ||
s2i_args="--force-pull=false" | ||
|
||
prepare | ||
run_s2i_build | ||
check_result $? | ||
prepare "test-app" | ||
run_s2i_build "test-app" | ||
check_result $? "test-app" | ||
|
||
# Verify the 'usage' script is working properly when running the base image with 's2i usage ...' | ||
test_s2i_usage | ||
check_result $? | ||
check_result $? "test-app" | ||
|
||
# Verify the 'usage' script is working properly when running the base image with 'docker run ...' | ||
test_docker_run_usage | ||
check_result $? | ||
check_result $? "test-app" | ||
|
||
# Test application with default uid | ||
test_application | ||
test_application | ||
|
||
# Test application with random uid | ||
CONTAINER_ARGS="-u 12345" test_application | ||
cleanup | ||
cleanup "test-app" | ||
|
||
# Test pre-init script functionality | ||
cid_file=$(mktemp -u --suffix=.cid) | ||
|
||
prepare "pre-init-test-app" | ||
run_s2i_build "pre-init-test-app" | ||
check_result $? "pre-init-test-app" | ||
|
||
test_pre_init_script | ||
cleanup "pre-init-test-app" | ||
|
||
info "All tests finished successfully." |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any reason why to move
pre-init
scripts there?I'm not against it, maybe I'm missing something.
Otherwise
/opt/app-root/src
seems to me as better location that/opt/app-root/etc
- the etc is misleading...But
generate_container_user
andscl_enable
are already in/opt/app-root/etc
, so no big harm. But I vote for moving these scripts intoNGINX_CONTAINER_SCRIPTS_PATH
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, that makes more sense. I've used this location before, don't know why I chose something else here than
NGINX_CONTAINER_SCRIPTS_PATH
. Will change thatThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've changed this in #41, since I'm not allowed to push to this branch.