Merge pull request #8 from sameersbn/rewrite-tests

Rewrite tests
This commit is contained in:
Sameer Naik
2015-11-09 22:21:31 +05:30
2 changed files with 299 additions and 114 deletions

236
bitnami/mongodb/test.sh Normal file → Executable file
View File

@@ -1,160 +1,168 @@
#!/usr/bin/env bats
CONTAINER_NAME=bitnami-mongodb-test
IMAGE_NAME=${IMAGE_NAME:-bitnami/mongodb}
SLEEP_TIME=5
MONGODB_ROOT_USER=root
MONGODB_DATABASE=test_database
MONGODB_USER=test_user
MONGODB_PASSWORD=test_password
VOL_PREFIX=/bitnami/mongodb
HOST_VOL_PREFIX=${HOST_VOL_PREFIX:-/tmp/bitnami/$CONTAINER_NAME}
cleanup_running_containers() {
if [ "$(docker ps -a | grep $CONTAINER_NAME)" ]; then
docker rm -fv $CONTAINER_NAME
fi
# source the helper script
APP_NAME=mongodb
SLEEP_TIME=5
load tests/docker_helper
# Link to container and execute mongo client
# $1 : name of the container to link to
# ${@:2} : arguments for the mongo command
mongo_client() {
container_link_and_run_command $1 mongo --host $APP_NAME "${@:2}"
}
setup() {
cleanup_running_containers
mkdir -p $HOST_VOL_PREFIX
# Cleans up all running/stopped containers and host mounted volumes
cleanup_environment() {
container_remove_full default
}
# Teardown called at the end of each test
teardown() {
cleanup_running_containers
cleanup_environment
}
cleanup_volumes_content() {
docker run --rm\
-v $HOST_VOL_PREFIX/data:$VOL_PREFIX/data\
-v $HOST_VOL_PREFIX/conf:$VOL_PREFIX/conf\
-v $HOST_VOL_PREFIX/logs:$VOL_PREFIX/logs\
$IMAGE_NAME rm -rf $VOL_PREFIX/data/ $VOL_PREFIX/logs/ $VOL_PREFIX/conf/
}
create_container(){
docker run --name $CONTAINER_NAME "$@" $IMAGE_NAME
sleep $SLEEP_TIME
}
# $1 is the command
mongo_client(){
docker run --rm --link $CONTAINER_NAME:$CONTAINER_NAME $IMAGE_NAME mongo --host $CONTAINER_NAME "$@"
}
create_full_container(){
docker run -d --name $CONTAINER_NAME\
-e MONGODB_USER=$MONGODB_USER\
-e MONGODB_DATABASE=$MONGODB_DATABASE\
-e MONGODB_PASSWORD=$MONGODB_PASSWORD $IMAGE_NAME
sleep $SLEEP_TIME
}
create_full_container_mounted(){
docker run -d --name $CONTAINER_NAME\
-e MONGODB_USER=$MONGODB_USER\
-e MONGODB_DATABASE=$MONGODB_DATABASE\
-e MONGODB_PASSWORD=$MONGODB_PASSWORD\
-v $HOST_VOL_PREFIX/data:$VOL_PREFIX/data\
-v $HOST_VOL_PREFIX/conf:$VOL_PREFIX/conf\
-v $HOST_VOL_PREFIX/logs:$VOL_PREFIX/logs\
$IMAGE_NAME
sleep $SLEEP_TIME
}
# cleanup the environment before starting the tests
cleanup_environment
@test "Port 27017 exposed and accepting external connections" {
create_container -d
run mongo_client admin --eval "printjson(db.adminCommand('listDatabases'))"
[[ "$output" =~ '"ok" : 1' ]]
}
container_create default -d
@test "Root user created with password" {
create_container -d -e MONGODB_PASSWORD=$MONGODB_PASSWORD
# Can not login as root
run mongo_client -u $MONGODB_ROOT_USER admin --eval "printjson(db.adminCommand('listDatabases'))"
[[ "$output" =~ "login failed" ]]
run mongo_client -u $MONGODB_ROOT_USER -p $MONGODB_PASSWORD admin --eval "printjson(db.adminCommand('listDatabases'))"
[[ "$output" =~ '"ok" : 1' ]]
}
@test "Root user has access to admin database" {
create_container -d -e MONGODB_PASSWORD=$MONGODB_PASSWORD
run mongo_client -u $MONGODB_ROOT_USER -p $MONGODB_PASSWORD admin --eval "printjson(db.adminCommand('listDatabases'))"
[[ "$output" =~ '"ok" : 1' ]]
# ping the mongod server
run mongo_client default ping
[[ "$output" =~ "bye" ]]
}
@test "Can't create root user without password" {
run create_container -e MONGODB_USER=$MONGODB_ROOT_USER
# create container without specifying MONGODB_PASSWORD for root user
run container_create default \
-e MONGODB_USER=$MONGODB_ROOT_USER
[[ "$output" =~ "you need to provide the MONGODB_PASSWORD" ]]
}
@test "Root user created with password" {
container_create default -d \
-e MONGODB_USER=$MONGODB_ROOT_USER \
-e MONGODB_PASSWORD=$MONGODB_PASSWORD
# auth as root without specifying password
run mongo_client default -u $MONGODB_ROOT_USER admin --eval "printjson(db.adminCommand('listDatabases'))"
[[ "$output" =~ "login failed" ]]
# auth as root and list all databases
run mongo_client default -u $MONGODB_ROOT_USER -p $MONGODB_PASSWORD admin --eval "printjson(db.adminCommand('listDatabases'))"
[[ "$output" =~ '"ok" : 1' ]]
[[ "$output" =~ '"name" : "local"' ]]
}
@test "Can't create a custom user without password" {
run create_container -e MONGODB_USER=$MONGODB_USER
# create custom user without specifying MONGODB_PASSWORD
run container_create default \
-e MONGODB_USER=$MONGODB_USER
[[ "$output" =~ "you need to provide the MONGODB_PASSWORD" ]]
}
@test "Can't create a custom user without database" {
run create_container -e MONGODB_USER=$MONGODB_USER -e MONGODB_PASSWORD=$MONGODB_PASSWORD
# create custom user without specifying MONGODB_DATABASE
run container_create default \
-e MONGODB_USER=$MONGODB_USER \
-e MONGODB_PASSWORD=$MONGODB_PASSWORD
[[ "$output" =~ "you need to provide the MONGODB_DATABASE" ]]
}
@test "Create custom user and database with password" {
create_full_container
# Cannot login without password
run mongo_client -u $MONGODB_USER $MONGODB_DATABASE --eval "printjson(db.adminCommand('listCollections'))"
container_create default -d \
-e MONGODB_USER=$MONGODB_USER \
-e MONGODB_DATABASE=$MONGODB_DATABASE \
-e MONGODB_PASSWORD=$MONGODB_PASSWORD
# auth as MONGODB_USER without specifying MONGODB_PASSWORD
run mongo_client default -u $MONGODB_USER $MONGODB_DATABASE --eval "printjson(db.adminCommand('listCollections'))"
[[ "$output" =~ "login failed" ]]
run mongo_client -u $MONGODB_USER -p $MONGODB_PASSWORD $MONGODB_DATABASE --eval "printjson(db.adminCommand('listCollections'))"
# auth as MONGODB_USER and list all Collections from MONGODB_DATABASE
run mongo_client default -u $MONGODB_USER -p $MONGODB_PASSWORD $MONGODB_DATABASE --eval "printjson(db.adminCommand('listCollections'))"
[[ "$output" =~ '"ok" : 1' ]]
}
@test "Custom user can't access admin database" {
create_full_container
run mongo_client -u $MONGODB_USER -p $MONGODB_PASSWORD admin --eval "printjson(db.adminCommand('listDatabases'))"
container_create default -d \
-e MONGODB_USER=$MONGODB_USER \
-e MONGODB_DATABASE=$MONGODB_DATABASE \
-e MONGODB_PASSWORD=$MONGODB_PASSWORD
# auth as MONGODB_USER and list all databases
run mongo_client default -u $MONGODB_USER -p $MONGODB_PASSWORD admin --eval "printjson(db.adminCommand('listDatabases'))"
[[ "$output" =~ 'login failed' ]]
}
@test "User and password settings are preserved after restart" {
create_full_container
docker stop $CONTAINER_NAME
docker start $CONTAINER_NAME
sleep $SLEEP_TIME
run docker logs $CONTAINER_NAME
[[ "$output" =~ "The credentials were set on first boot." ]]
run mongo_client -u $MONGODB_USER -p $MONGODB_PASSWORD $MONGODB_DATABASE --eval "printjson(db.adminCommand('listCollections'))"
[[ "$output" =~ '"ok" : 1' ]]
}
@test "If host mounted, password and settings are preserved after deletion" {
cleanup_volumes_content
create_full_container_mounted
docker rm -fv $CONTAINER_NAME
run docker run -d --name $CONTAINER_NAME\
-v $HOST_VOL_PREFIX/data:$VOL_PREFIX/data\
-v $HOST_VOL_PREFIX/conf:$VOL_PREFIX/conf\
$IMAGE_NAME
sleep $SLEEP_TIME
run mongo_client -u $MONGODB_USER -p $MONGODB_PASSWORD $MONGODB_DATABASE --eval "printjson(db.adminCommand('listCollections'))"
[[ "$output" =~ '"ok" : 1' ]]
cleanup_volumes_content
}
@test "All the volumes exposed" {
create_container -d
run docker inspect $CONTAINER_NAME
container_create default -d
# get container introspection details and check if volumes are exposed
run container_inspect default --format {{.Mounts}}
[[ "$output" =~ "$VOL_PREFIX/data" ]]
[[ "$output" =~ "$VOL_PREFIX/conf" ]]
[[ "$output" =~ "$VOL_PREFIX/logs" ]]
}
@test "Data gets generated in conf and data if bind mounted in the host" {
create_full_container_mounted
run docker run -v $HOST_VOL_PREFIX:$HOST_VOL_PREFIX --rm $IMAGE_NAME ls -l $HOST_VOL_PREFIX/conf/mongodb.conf $HOST_VOL_PREFIX/logs/mongodb.log
[ $status = 0 ]
cleanup_volumes_content
@test "Data gets generated in conf, data and logs if bind mounted in the host" {
container_create_with_host_volumes default -d \
-e MONGODB_USER=$MONGODB_USER \
-e MONGODB_DATABASE=$MONGODB_DATABASE \
-e MONGODB_PASSWORD=$MONGODB_PASSWORD
# files expected in conf volume
run container_exec default ls -la $VOL_PREFIX/conf/
[[ "$output" =~ "mongodb.conf" ]]
# files expected in data volume (subset)
run container_exec default ls -la $VOL_PREFIX/data/
[[ "$output" =~ "storage.bson" ]]
[[ "$output" =~ "local.0" ]]
[[ "$output" =~ "local.ns" ]]
# files expected in logs volume
run container_exec default ls -la $VOL_PREFIX/logs/
[[ "$output" =~ "mongodb.log" ]]
}
@test "User and password settings are preserved after restart" {
container_create default -d \
-e MONGODB_USER=$MONGODB_USER \
-e MONGODB_DATABASE=$MONGODB_DATABASE \
-e MONGODB_PASSWORD=$MONGODB_PASSWORD
# restart container
container_restart default
# get container logs
run container_logs default
[[ "$output" =~ "The credentials were set on first boot." ]]
# auth as MONGODB_USER and list all Collections from MONGODB_DATABASE
run mongo_client default -u $MONGODB_USER -p $MONGODB_PASSWORD $MONGODB_DATABASE --eval "printjson(db.adminCommand('listCollections'))"
[[ "$output" =~ '"ok" : 1' ]]
}
@test "If host mounted, password and settings are preserved after deletion" {
container_create_with_host_volumes default -d \
-e MONGODB_USER=$MONGODB_USER \
-e MONGODB_DATABASE=$MONGODB_DATABASE \
-e MONGODB_PASSWORD=$MONGODB_PASSWORD
# stop and remove container
container_remove default
# recreate container without specifying any env parameters
container_create_with_host_volumes default -d
# auth as MONGODB_USER and list all Collections from MONGODB_DATABASE
run mongo_client default -u $MONGODB_USER -p $MONGODB_PASSWORD $MONGODB_DATABASE --eval "printjson(db.adminCommand('listCollections'))"
[[ "$output" =~ '"ok" : 1' ]]
}

View 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}"
}