Skip to content
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

Add some restart tests #166

Merged
merged 9 commits into from
Mar 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ before_install:

script:
- bats test/test.full.bats
- bats test/test.mysql.bats

notifications:
email: false
7 changes: 2 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@ RUN mkdir -p /var/www/html && \
chown -R www-data /var/www

COPY conf/crontab /etc/cron.d/artisan-schedule
COPY entrypoint.sh /sbin/entrypoint.sh
RUN chown www-data /sbin/entrypoint.sh && \
chmod 755 /sbin/entrypoint.sh

RUN chmod 0644 /etc/cron.d/artisan-schedule && \
touch /var/log/cron.log
Expand Down Expand Up @@ -77,8 +74,8 @@ RUN wget https://github.com/cachethq/Cachet/archive/${cachet_ver}.tar.gz && \
rm -rf bootstrap/cache/*

COPY conf/.env.docker /var/www/html/.env
COPY entrypoint.sh /sbin/entrypoint.sh

EXPOSE 80

ENTRYPOINT ["/sbin/entrypoint.sh"]
CMD ["start"]
CMD ["/sbin/entrypoint.sh"]
101 changes: 64 additions & 37 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ set -eo pipefail
[[ $DEBUG == true ]] && set -x

check_database_connection() {
echo "Attempting to connect to database ..."
case ${DB_DRIVER} in
mysql)
prog="mysqladmin -h ${DB_HOST} -u ${DB_USERNAME} ${DB_PASSWORD:+-p$DB_PASSWORD} status"
Expand All @@ -19,29 +20,73 @@ check_database_connection() {
timeout=$(( $timeout - 1 ))
if [[ $timeout -eq 0 ]]; then
echo
echo "Could not connect to database server. Aborting..."
return 1
echo "Could not connect to database server! Aborting..."
exit 1
fi
echo -n "."
sleep 1
done
echo
}

checkdbinitmysql() {
table=sessions
if [ $(mysql -N -s -h ${DB_HOST} -u ${DB_USERNAME} ${DB_PASSWORD:+-p$DB_PASSWORD} -e \
"select count(*) from information_schema.tables where \
table_schema='${DB_DATABASE}' and table_name='${table}';") -eq 1 ]; then
echo "Table ${table} exists! ..."
else
echo "Table ${table} does not exist! ..."
init_db
fi

}

checkdbinitpsql() {
table=sessions
export PGPASSWORD=${DB_PASSWORD}
if [ "$(psql -h ${DB_HOST} -U ${DB_USERNAME} -c "SELECT to_regclass('${table}');" | grep -c "${table}")" -eq 1 ]; then
echo "Table ${table} exists! ..."
else
echo "Table ${table} does not exist! ..."
init_db
fi

}

check_configured() {
case ${DB_DRIVER} in
mysql)
checkdbinitmysql
;;
pgsql)
checkdbinitpsql
;;
esac
}

initialize_system() {
echo "Initializing Cachet container ..."
APP_ENV=${APP_ENV:-development}
APP_DEBUG=${APP_DEBUG:-true}
APP_URL=${APP_URL:-http://localhost}
APP_KEY=${APP_KEY:-SECRET}
APP_KEY=${APP_KEY:-base64:SGZXUdds0Qnbf55/7diaHMPPM2TXfOSxHtUAXz6POSw=}
Copy link
Contributor Author

@djdefi djdefi Mar 14, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I realize a key is being shipped here, but it needed some real default value for now. This should be set via -e APP_KEY="base64:<Your Real Key>" in production anyway.


DB_DRIVER=${DB_DRIVER:-pgsql}
DB_HOST=${DB_HOST:-postgres}
DB_DATABASE=${DB_DATABASE:-cachet}
DB_USERNAME=${DB_USERNAME:-postgres}
DB_PASSWORD=${DB_PASSWORD:-postgres}
DB_PORT=${DB_PORT:-5432}

DOCKER=true

if [ ${DB_DRIVER} = "pgsql" ]; then
DB_PORT=${DB_PORT:-5432}
fi

if [ ${DB_DRIVER} = "mysql" ]; then
DB_PORT=${DB_PORT:-3306}
fi

DB_PORT=${DB_PORT}

CACHE_DRIVER=${CACHE_DRIVER:-apc}
SESSION_DRIVER=${SESSION_DRIVER:-apc}
Expand Down Expand Up @@ -79,12 +124,12 @@ initialize_system() {
sed 's,{{DB_USERNAME}},'"${DB_USERNAME}"',g' -i /var/www/html/.env
sed 's,{{DB_PASSWORD}},'"${DB_PASSWORD}"',g' -i /var/www/html/.env
sed 's,{{DB_PORT}},'"${DB_PORT}"',g' -i /var/www/html/.env

sed 's,{{CACHE_DRIVER}},'"${CACHE_DRIVER}"',g' -i /var/www/html/.env
sed 's,{{SESSION_DRIVER}},'"${SESSION_DRIVER}"',g' -i /var/www/html/.env
sed 's,{{QUEUE_DRIVER}},'"${QUEUE_DRIVER}"',g' -i /var/www/html/.env
sed 's,{{CACHET_EMOJI}},'"${CACHET_EMOJI}"',g' -i /var/www/html/.env

sed 's,{{MAIL_DRIVER}},'"${MAIL_DRIVER}"',g' -i /var/www/html/.env
sed 's,{{MAIL_HOST}},'"${MAIL_HOST}"',g' -i /var/www/html/.env
sed 's,{{MAIL_PORT}},'"${MAIL_PORT}"',g' -i /var/www/html/.env
Expand All @@ -98,49 +143,31 @@ initialize_system() {
sed 's,{{REDIS_DATABASE}},'${REDIS_DATABASE}',g' -i /var/www/html/.env
sed 's,{{REDIS_PORT}},'${REDIS_PORT}',g' -i /var/www/html/.env
sed 's,{{REDIS_PASSWORD}},'${REDIS_PASSWORD}',g' -i /var/www/html/.env

sed 's,{{GITHUB_TOKEN}},'"${GITHUB_TOKEN}"',g' -i /var/www/html/.env

sed 's,{{GITHUB_TOKEN}},'"${GITHUB_TOKEN}"',g' -i /var/www/html/.env
sudo sed 's,{{PHP_MAX_CHILDREN}},'"${PHP_MAX_CHILDREN}"',g' -i /etc/php5/fpm/pool.d/www.conf

php artisan app:install
rm -rf bootstrap/cache/*
chmod -R 777 storage
touch /var/www/.cachet-installed
start_system
}

init_db() {
echo "Initializing Cachet database ..."
php artisan app:install
check_configured
}



start_system() {
initialize_system
check_database_connection
[ -f "/var/www/.cachet-installed" ] && echo "Starting Cachet" || initialize_system
check_configured
echo "Starting Cachet! ..."
php artisan config:cache
sudo /usr/bin/supervisord -n -c /etc/supervisor/supervisord.conf
}

case ${1} in
init|start)

case ${1} in
start)
start_system
;;
init)
initialize_system
;;
esac
;;
help)
echo "Available options:"
echo " start - Starts the Cachet server (default)"
echo " init - Initialize the Cachet server (e.g. create databases, compile assets)."
echo " help - Displays the help"
echo " [command] - Execute the specified command, eg. bash."
;;
*)
exec "$@"
;;
esac
start_system

exit 0
29 changes: 29 additions & 0 deletions test/docker-compose-mysql.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
version: "2"

services:
mysql:
image: mysql:5.7
environment:
- MYSQL_ROOT_PASSWORD=mysql
- MYSQL_USER=mysql
- MYSQL_PASSWORD=mysql
- MYSQL_DATABASE=mysql
cachet:
image: docker_cachet
ports:
- 80:80
links:
- mysql:mysql
environment:
- DB_DRIVER=mysql
- DB_HOST=mysql
- DB_DATABASE=mysql
- DB_USERNAME=mysql
- DB_PASSWORD=mysql
depends_on:
- mysql

networks:
default:
external:
name: docker_default
52 changes: 47 additions & 5 deletions test/test.full.bats
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,30 @@ load "lib/output"
command docker-compose up -d
}

@test "[$TEST_FILE] check for container init" {
docker_wait_for_log docker_cachet_1 15 "Initializing Cachet container ..."
}

@test "[$TEST_FILE] check for postgres database startup" {
docker_wait_for_log docker_postgres_1 120 "LOG: autovacuum launcher started"
}

@test "[$TEST_FILE] check for empty sessions table" {
docker_wait_for_log docker_cachet_1 15 "Table sessions does not exist! ..."
}

@test "[$TEST_FILE] check for DB init" {
docker_wait_for_log docker_cachet_1 15 "Initializing Cachet database ..."
}

@test "[$TEST_FILE] check for populated sessions table" {
docker_wait_for_log docker_cachet_1 15 "Table sessions exists! ..."
}

@test "[$TEST_FILE] check for container start message" {
docker_wait_for_log docker_cachet_1 15 "Starting Cachet! ..."
}

@test "[$TEST_FILE] check for nginx startup" {
docker_wait_for_log docker_cachet_1 15 "INFO success: nginx entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)"
}
Expand All @@ -20,10 +44,6 @@ load "lib/output"
docker_wait_for_log docker_cachet_1 15 "INFO success: php-fpm entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)"
}

@test "[$TEST_FILE] check for postgres startup" {
docker_wait_for_log docker_postgres_1 15 "LOG: autovacuum launcher started"
}

@test "[$TEST_FILE] php artisan cachet:seed" {
run docker exec docker_cachet_1 php artisan cachet:seed
assert_output -l 0 $'Database seeded with demo data successfully!'
Expand All @@ -39,7 +59,7 @@ load "lib/output"
assert_output -l 0 $'HTTP/1.1 200 OK\r'
}

@test "[$TEST_FILE] curl API ping" {
@test "[$TEST_FILE] check for curl API pong" {
run curl_container docker_cachet_1 /api/v1/ping
assert_output -l 0 $'{"data":"Pong!"}'
}
Expand All @@ -49,6 +69,28 @@ load "lib/output"
refute_output -l 5 $'pg_dump: aborting because of server version mismatch'
}

@test "[$TEST_FILE] ensure there are no volumes" {
run docker inspect -f '{{ .Mounts }}' docker_cachet_1
assert_output -l 0 $'[]'
}

@test "[$TEST_FILE] restart cachet" {
command docker-compose stop cachet
command docker-compose rm -f cachet
command docker-compose up -d
docker_wait_for_log docker_cachet_1 15 "INFO success: nginx entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)"
}

@test "[$TEST_FILE] post-restart API pong" {
run curl_container docker_cachet_1 /api/v1/ping
assert_output -l 0 $'{"data":"Pong!"}'
}

@test "[$TEST_FILE] post-restart login test" {
run curl_container docker_cachet_1 /auth/login --head --user test:test123
assert_output -l 0 $'HTTP/1.1 200 OK\r'
}

@test "[$TEST_FILE] stop all test containers" {
stop_bats_containers
}
Expand Down
87 changes: 87 additions & 0 deletions test/test.mysql.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#!/usr/bin/env bats
load test_helpers
load docker_helpers
load "lib/batslib"
load "lib/output"

@test "[$TEST_FILE] docker-compose up" {
command docker-compose -f test/docker-compose-mysql.yml up -d
}

@test "[$TEST_FILE] check for container init" {
docker_wait_for_log test_cachet_1 15 "Initializing Cachet container ..."
}

@test "[$TEST_FILE] check for database startup" {
docker_wait_for_log test_mysql_1 120 "[Note] mysqld: ready for connections."
}

@test "[$TEST_FILE] check for empty sessions table" {
docker_wait_for_log test_cachet_1 15 "Table sessions does not exist! ..."
}

@test "[$TEST_FILE] check for DB init" {
docker_wait_for_log test_cachet_1 15 "Initializing Cachet database ..."
}

@test "[$TEST_FILE] check for populated sessions table" {
docker_wait_for_log test_cachet_1 15 "Table sessions exists! ..."
}

@test "[$TEST_FILE] check for container start message" {
docker_wait_for_log test_cachet_1 15 "Starting Cachet! ..."
}

@test "[$TEST_FILE] check for nginx startup" {
docker_wait_for_log test_cachet_1 15 "INFO success: nginx entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)"
}

@test "[$TEST_FILE] check for php-fpm startup" {
docker_wait_for_log test_cachet_1 15 "INFO success: php-fpm entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)"
}

@test "[$TEST_FILE] php artisan cachet:seed" {
run docker exec test_cachet_1 php artisan cachet:seed
assert_output -l 0 $'Database seeded with demo data successfully!'
}

@test "[$TEST_FILE] curl 200 response test" {
run curl_container test_cachet_1 /auth/login --head
assert_output -l 0 $'HTTP/1.1 200 OK\r'
}

@test "[$TEST_FILE] login test" {
run curl_container test_cachet_1 /auth/login --head --user test:test123
assert_output -l 0 $'HTTP/1.1 200 OK\r'
}

@test "[$TEST_FILE] check for curl API pong" {
run curl_container test_cachet_1 /api/v1/ping
assert_output -l 0 $'{"data":"Pong!"}'
}

@test "[$TEST_FILE] restart cachet" {
command docker-compose -f test/docker-compose-mysql.yml stop cachet
command docker-compose -f test/docker-compose-mysql.yml rm -f cachet
command docker-compose -f test/docker-compose-mysql.yml up -d
docker_wait_for_log test_cachet_1 15 "INFO success: nginx entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)"
}

@test "[$TEST_FILE] post-restart API pong" {
run curl_container test_cachet_1 /api/v1/ping
assert_output -l 0 $'{"data":"Pong!"}'
}

@test "[$TEST_FILE] post-restart login test" {
run curl_container test_cachet_1 /auth/login --head --user test:test123
assert_output -l 0 $'HTTP/1.1 200 OK\r'
}

@test "[$TEST_FILE] stop all test containers" {
stop_bats_containers
}

@test "[$TEST_FILE] Cleanup test containers" {
docker_clean test_cachet_1
docker_clean test_mysql_1
}