mirror of
https://github.com/bitnami/containers.git
synced 2026-03-07 18:17:20 +08:00
tests: rewrite tests for better maintenance and readability
- Reuse docker_helper.bash script in tests - Speed up of tests - Better readability
This commit is contained in:
@@ -1,74 +1,89 @@
|
||||
#!/usr/bin/env bats
|
||||
CONTAINER_NAME=bitnami-php-fpm-test
|
||||
NGINX_CONTAINER_NAME=bitnami-nginx-test
|
||||
IMAGE_NAME=bitnami/php-fpm
|
||||
|
||||
NGINX_IMAGE_NAME=bitnami/nginx
|
||||
NGINX_CONTAINER_NAME=bitnami-nginx-test
|
||||
|
||||
# source the helper script
|
||||
APP_NAME=php-fpm
|
||||
SLEEP_TIME=3
|
||||
VOL_PREFIX=/bitnami/php-fpm
|
||||
HOST_VOL_PREFIX=/tmp/bitnami/php-fpm
|
||||
VOL_PREFIX=/bitnami/$APP_NAME
|
||||
VOLUMES=/app:$VOL_PREFIX/logs:$VOL_PREFIX/conf
|
||||
load tests/docker_helper
|
||||
|
||||
cleanup_running_containers() {
|
||||
if [ "$(docker ps -a | grep $CONTAINER_NAME)" ]; then
|
||||
docker rm -fv $CONTAINER_NAME
|
||||
fi
|
||||
|
||||
if [ "$(docker ps -a | grep $NGINX_CONTAINER_NAME)" ]; then
|
||||
# Cleans up all running/stopped containers and host mounted volumes
|
||||
cleanup_environment() {
|
||||
if docker ps -a | grep $NGINX_CONTAINER_NAME; then
|
||||
docker rm -fv $NGINX_CONTAINER_NAME
|
||||
fi
|
||||
container_remove_full default
|
||||
}
|
||||
|
||||
create_container(){
|
||||
docker run -d --name $CONTAINER_NAME "$@" $IMAGE_NAME
|
||||
# Teardown called at the end of each test
|
||||
teardown() {
|
||||
cleanup_environment
|
||||
}
|
||||
|
||||
add_vhost() {
|
||||
docker exec $NGINX_CONTAINER_NAME sh -c "echo 'server { listen 0.0.0.0:81; root /app; location ~ \.php$ { fastcgi_pass php:9000; include fastcgi.conf; } }' > /bitnami/nginx/conf/vhosts/test.conf"
|
||||
# cleanup the environment of any leftover containers and volumes before starting the tests
|
||||
cleanup_environment
|
||||
|
||||
@test "php and php-fpm installed" {
|
||||
container_create default -d
|
||||
|
||||
run container_exec default php -v
|
||||
[ "$status" = 0 ]
|
||||
run container_exec default php-fpm -v
|
||||
[ "$status" = 0 ]
|
||||
}
|
||||
|
||||
create_nginx_container(){
|
||||
docker run -d --name $NGINX_CONTAINER_NAME\
|
||||
--link $CONTAINER_NAME:php $NGINX_IMAGE_NAME
|
||||
create_nginx_container() {
|
||||
docker run --name $NGINX_CONTAINER_NAME -d \
|
||||
$(container_link default $APP_NAME) $NGINX_IMAGE_NAME
|
||||
sleep $SLEEP_TIME
|
||||
add_vhost
|
||||
|
||||
docker exec $NGINX_CONTAINER_NAME sh -c "cat > /bitnami/nginx/conf/vhosts/test.conf <<EOF
|
||||
server {
|
||||
listen 0.0.0.0:81;
|
||||
root /app;
|
||||
location ~ \.php\$ {
|
||||
fastcgi_pass $APP_NAME:9000;
|
||||
include fastcgi.conf;
|
||||
}
|
||||
}
|
||||
EOF"
|
||||
|
||||
docker restart $NGINX_CONTAINER_NAME
|
||||
sleep $SLEEP_TIME
|
||||
}
|
||||
|
||||
setup () {
|
||||
cleanup_running_containers
|
||||
}
|
||||
|
||||
teardown() {
|
||||
cleanup_running_containers
|
||||
}
|
||||
|
||||
@test "php and php-fpm installed" {
|
||||
create_container
|
||||
run docker exec $CONTAINER_NAME php -v
|
||||
[ "$status" = 0 ]
|
||||
run docker exec $CONTAINER_NAME php-fpm -v
|
||||
[ "$status" = 0 ]
|
||||
}
|
||||
|
||||
@test "winter is coming via nginx" {
|
||||
create_container
|
||||
docker exec $CONTAINER_NAME sh -c "echo '<?php echo \"Winter is coming\"; ?>' > index.php"
|
||||
container_create default -d
|
||||
|
||||
# create test app
|
||||
container_exec default sh -c "cat > /app/index.php <<EOF
|
||||
<?php echo \"Winter is coming\"; ?>
|
||||
EOF"
|
||||
|
||||
create_nginx_container
|
||||
|
||||
run docker exec $NGINX_CONTAINER_NAME curl --noproxy 127.0.0.1 127.0.0.1:81/index.php
|
||||
[[ "$output" =~ "Winter is coming" ]]
|
||||
}
|
||||
|
||||
@test "required volumes exposed" {
|
||||
create_container
|
||||
run docker inspect $CONTAINER_NAME
|
||||
[[ "$output" =~ "/app" ]]
|
||||
container_create default -d
|
||||
run container_inspect default --format {{.Mounts}}
|
||||
[[ "$output" =~ "$VOL_PREFIX/logs" ]]
|
||||
[[ "$output" =~ "$VOL_PREFIX/conf" ]]
|
||||
}
|
||||
|
||||
@test "Data gets generated in conf if bind mounted in the host" {
|
||||
create_container -v $HOST_VOL_PREFIX/conf:$VOL_PREFIX/conf -v $HOST_VOL_PREFIX/logs:$VOL_PREFIX/logs
|
||||
run docker run --rm -v $HOST_VOL_PREFIX:$HOST_VOL_PREFIX $IMAGE_NAME ls -l $HOST_VOL_PREFIX/conf/php-fpm.conf $HOST_VOL_PREFIX/logs/php-fpm.log
|
||||
[ $status = 0 ]
|
||||
docker run --rm -v $HOST_VOL_PREFIX:$HOST_VOL_PREFIX $IMAGE_NAME sh -c "sudo rm -rf $HOST_VOL_PREFIX/*"
|
||||
container_create_with_host_volumes default -d
|
||||
|
||||
# files expected in conf volume
|
||||
run container_exec default ls -la $VOL_PREFIX/conf/
|
||||
[[ "$output" =~ "php-fpm.conf" ]]
|
||||
|
||||
# files expected in logs volume
|
||||
run container_exec default ls -la $VOL_PREFIX/conf/ $VOL_PREFIX/logs/
|
||||
[[ "$output" =~ "php-fpm.log" ]]
|
||||
}
|
||||
|
||||
177
bitnami/php-fpm/tests/docker_helper.bash
Normal file
177
bitnami/php-fpm/tests/docker_helper.bash
Normal file
@@ -0,0 +1,177 @@
|
||||
#!/bin/bash
|
||||
|
||||
##
|
||||
# Reusable helper script to do docker things in your tests
|
||||
##
|
||||
# The following variables should be defined in you BATS script for this helper
|
||||
# script to work correctly.
|
||||
#
|
||||
# APP_NAME - app name, also used as the link alias in container_link_and_run_command
|
||||
# CONTAINER_NAME - prefix for the name of containers that will be created (default: bitnami-$APP_NAME-test)
|
||||
# IMAGE_NAME - the docker image name (default: bitnami/$APP_NAME)
|
||||
# SLEEP_TIME - time in seconds to wait for containers to start (default: 5)
|
||||
# VOL_PREFIX - prefix of volumes inside the container (default: /bitnami/$APP_NAME)
|
||||
# VOLUMES - colon separated list of container volumes (default: $VOL_PREFIX/data:$VOL_PREFIX/conf:$VOL_PREFIX/logs)
|
||||
# HOST_VOL_PREFIX - prefix of volumes mounted from the host (default: /tmp/bitnami/$CONTAINER_NAME)
|
||||
# container_link_and_run_command_DOCKER_ARGS - optional arguments passed to docker run in container_link_and_run_command (default: none)
|
||||
##
|
||||
|
||||
CONTAINER_NAME=bitnami-$APP_NAME-test
|
||||
IMAGE_NAME=${IMAGE_NAME:-bitnami/$APP_NAME}
|
||||
SLEEP_TIME=${SLEEP_TIME:-5}
|
||||
VOL_PREFIX=${VOL_PREFIX:-/bitnami/$APP_NAME}
|
||||
VOLUMES=${VOLUMES:-$VOL_PREFIX/data:$VOL_PREFIX/conf:$VOL_PREFIX/logs}
|
||||
HOST_VOL_PREFIX=${HOST_VOL_PREFIX:-/tmp/bitnami/$CONTAINER_NAME}
|
||||
|
||||
# Creates a container whose name has the prefix $CONTAINER_NAME
|
||||
# $1: name for the new container
|
||||
# ${@:2}: additional arguments for docker run while starting the container
|
||||
container_create() {
|
||||
docker run --name $CONTAINER_NAME-$1 "${@:2}" $IMAGE_NAME
|
||||
sleep $SLEEP_TIME
|
||||
}
|
||||
|
||||
# Creates a container with host mounted volumes for volumes listed in VOLUMES
|
||||
# $1: name for the new container
|
||||
# ${@:2}: additional arguments for docker run while starting the container
|
||||
container_create_with_host_volumes() {
|
||||
# populate volume mount arguments from VOLUMES variable
|
||||
VOLUME_ARGS=
|
||||
OLD_IFS=${IFS}
|
||||
IFS=":"
|
||||
for VOLUME in $VOLUMES
|
||||
do
|
||||
VOL_NAME=$(basename $VOLUME)
|
||||
VOLUME_ARGS+="-v $HOST_VOL_PREFIX/$1/$VOL_NAME:$VOLUME "
|
||||
done
|
||||
IFS=${OLD_IFS}
|
||||
|
||||
container_create $1 "${@:2}" $VOLUME_ARGS
|
||||
}
|
||||
|
||||
# Start a stopped container
|
||||
# $1: name of the container
|
||||
container_start() {
|
||||
if docker ps -a | grep -q $CONTAINER_NAME-$1; then
|
||||
docker start $CONTAINER_NAME-$1
|
||||
sleep $SLEEP_TIME
|
||||
else
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
# Stop a running container
|
||||
# $1: name of the container
|
||||
container_stop() {
|
||||
if docker ps | grep -q $CONTAINER_NAME-$1; then
|
||||
docker stop $CONTAINER_NAME-$1
|
||||
else
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
# Restart a running container (stops the container and then starts it)
|
||||
# $1: name of the container
|
||||
container_restart() {
|
||||
if docker ps | grep -q $CONTAINER_NAME-$1; then
|
||||
docker stop $CONTAINER_NAME-$1
|
||||
docker start $CONTAINER_NAME-$1
|
||||
sleep $SLEEP_TIME
|
||||
fi
|
||||
}
|
||||
|
||||
# Remove a running/stopped container
|
||||
# $1: name of the container
|
||||
container_remove() {
|
||||
if docker ps -a | grep -q $CONTAINER_NAME-$1; then
|
||||
docker stop $CONTAINER_NAME-$1
|
||||
docker rm -v $CONTAINER_NAME-$1
|
||||
fi
|
||||
}
|
||||
|
||||
# Remove a running/stopped container and clear host volumes
|
||||
# $1: name of the container
|
||||
container_remove_full() {
|
||||
container_remove $1
|
||||
|
||||
# populate volume mount and rm arguments from VOLUMES variable
|
||||
VOLUME_ARGS=
|
||||
RM_ARGS=
|
||||
OLD_IFS=${IFS}
|
||||
IFS=":"
|
||||
for VOLUME in $VOLUMES
|
||||
do
|
||||
VOL_NAME=$(basename $VOLUME)
|
||||
VOLUME_ARGS+="-v $HOST_VOL_PREFIX/$1/$VOL_NAME:$VOLUME "
|
||||
RM_ARGS+="$VOLUME/* $VOLUME/.[^.]* "
|
||||
done
|
||||
IFS=${OLD_IFS}
|
||||
|
||||
docker run --rm --entrypoint bash $VOLUME_ARGS \
|
||||
$IMAGE_NAME -c "rm -rf $RM_ARGS"
|
||||
}
|
||||
|
||||
# Get the logs of a container
|
||||
# $1: name of the container
|
||||
container_logs() {
|
||||
if docker ps -a | grep -q $CONTAINER_NAME-$1; then
|
||||
docker logs $CONTAINER_NAME-$1
|
||||
else
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
# Docker inspect a container
|
||||
# $1: name of the container
|
||||
container_inspect() {
|
||||
if docker ps -a | grep -q $CONTAINER_NAME-$1; then
|
||||
# docker inspect "${@:2}" $CONTAINER_NAME-$1 # requires docker >= 1.9.0
|
||||
docker inspect $CONTAINER_NAME-$1
|
||||
else
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
# Execute a command in a running container using docker exec
|
||||
# $1: name of the container
|
||||
container_exec() {
|
||||
if docker ps | grep -q $CONTAINER_NAME-$1; then
|
||||
docker exec $CONTAINER_NAME-$1 "${@:2}"
|
||||
else
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
# Execute a command in a running container using docker exec (detached)
|
||||
# $1: name of the container
|
||||
container_exec_detached() {
|
||||
if docker ps | grep -q $CONTAINER_NAME-$1; then
|
||||
docker exec -d $CONTAINER_NAME-$1 "${@:2}"
|
||||
else
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
# Generates docker link parameter for linking to a container
|
||||
# $1: name of the container to link
|
||||
# $2: alias for the link
|
||||
container_link() {
|
||||
if docker ps -a | grep -q $CONTAINER_NAME-$1; then
|
||||
echo "--link $CONTAINER_NAME-$1:$2"
|
||||
fi
|
||||
}
|
||||
|
||||
# Link to container and execute command
|
||||
# $1: name of the container to link to
|
||||
# ${@:2}: command to execute
|
||||
container_link_and_run_command() {
|
||||
# launch command as the entrypoint to skip the s6 init sequence (speeds up the tests)
|
||||
docker run --rm $(container_link $1 $APP_NAME) $container_link_and_run_command_DOCKER_ARGS --entrypoint ${2} $IMAGE_NAME "${@:3}"
|
||||
}
|
||||
|
||||
# Link to container and execute curl
|
||||
# $1: name of the container to link to
|
||||
# ${@:2}: arguments to curl
|
||||
curl_client() {
|
||||
container_link_and_run_command $1 curl --noproxy $APP_NAME --retry 5 -L "${@:2}"
|
||||
}
|
||||
Reference in New Issue
Block a user