diff --git a/.travis.yml b/.travis.yml index 2a93ddd..9f7fb89 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,6 +10,6 @@ before_install: - curl -L https://github.com/docker/compose/releases/download/1.11.2/docker-compose-`uname -s`-`uname -m` > docker-compose - chmod +x docker-compose - sudo mv docker-compose /usr/local/bin - + script: - - docker-compose build + - test/run_tests.sh diff --git a/test/integration/.env b/test/integration/.env new file mode 100644 index 0000000..caf8c39 --- /dev/null +++ b/test/integration/.env @@ -0,0 +1,8 @@ +MYSQL_PASSWORD=db_password +MAILGUN_PASSWORD= +RECAPTCHA_SECRET_KEY= +WG_SECRET_KEY= +SITE_UPGRADE_KEY= +SERVER_PORT= +SERVER_NAME= +DROPBOX_ACCESS_TOKEN= diff --git a/test/integration/docker-compose.test.yml b/test/integration/docker-compose.test.yml new file mode 100644 index 0000000..2353908 --- /dev/null +++ b/test/integration/docker-compose.test.yml @@ -0,0 +1,22 @@ +version: '2.1' + +services: + mysql: + volumes: + - db-test-volume:/var/lib/mysql + nginx: + volumes: + - mediawiki-test-volume:/srv/mediawiki + ports: + - "80" + php: + volumes: + - mediawiki-test-volume:/srv/mediawiki + mediawiki: + volumes: + - mediawiki-test-volume:/srv/mediawiki + +volumes: + mediawiki-test-volume: + db-test-volume: + images-test-volume: diff --git a/test/integration/run_tests.sh b/test/integration/run_tests.sh new file mode 100755 index 0000000..7e574a3 --- /dev/null +++ b/test/integration/run_tests.sh @@ -0,0 +1,98 @@ +#!/usr/bin/env bash + +RED='\033[1;31m' +GREEN='\033[1;32m' +YELLOW='\033[1;33m' +NC='\033[0m' # No Color + +set -xe + +REPO_ROOT=$(git rev-parse --show-toplevel) +TEST_ROOT="$REPO_ROOT/test/integration" +DOCKER_CONFIG="-f $REPO_ROOT/docker-compose.yml -f $TEST_ROOT/docker-compose.test.yml" +DOCKER_COMPOSE="docker-compose $DOCKER_CONFIG" +WIKI="/srv/mediawiki" + +cd $TEST_ROOT +source .env + +function info { + echo -e "${YELLOW}$1${NC}" +} + +function error { + echo -e "${RED}$1${NC}" + exit 1 +} + +function cleanup { + EXIT_CODE=$? + + if [[ $EXIT_CODE != 0 && $($DOCKER_COMPOSE top) != "" ]]; then + info "Dumping logs" + $DOCKER_COMPOSE logs + fi + + info "Cleaning up" + $DOCKER_COMPOSE down --volumes + + if [[ $EXIT_CODE == 0 ]]; then + echo -e "${GREEN}All tests passed.${NC}" + else + echo -e "${RED}Test failure(s)!${NC}" + fi + + exit $EXIT_CODE +} + +info "Making sure no services are running" +if [[ $($DOCKER_COMPOSE top) != "" ]]; then + error "Cannot run integration tests while services are running. Run 'docker-compose down' and try again." +fi + +# About to start tests; make sure we clean up afterwards +trap cleanup EXIT + +info "Starting integration test services" +$DOCKER_COMPOSE up --build -d 1>/dev/null +info "Waiting for mysql to initialize" +for i in {1..24}; do + if [[ $($DOCKER_COMPOSE logs mysql | grep "ready for connections") != "" ]]; then + sleep 15 # to be extra-sure that mysql is ready + break + fi + sleep 5 + if [[ $i == 24 ]]; then + error "mysql failed to initialise within 120 seconds" + fi +done + +# Find the random port that nginx is mapped to +NGINX_ADDR=$(docker-compose port nginx 80) + +info "Initializing database" +# Move LocalSettings.php out of the way otherwise the installer complains +$DOCKER_COMPOSE exec php mv $WIKI/LocalSettings.php $WIKI/LocalSettings.php.bak +$DOCKER_COMPOSE exec php php $WIKI/maintenance/install.php \ + --confpath /tmp \ + --dbname metakgp_wiki_db \ + --dbserver mysql-docker \ + --dbuser metakgp_user \ + --dbpass $MYSQL_PASSWORD \ + --installdbuser metakgp_user \ + --installdbpass $MYSQL_PASSWORD \ + --pass admin_password \ + --scriptpath "" \ + Metakgp Test Wiki \ + Admin + +# Move LocalSettings.php back in place +$DOCKER_COMPOSE exec php mv $WIKI/LocalSettings.php.bak $WIKI/LocalSettings.php + +# Sample test only; more comprehensive tests to follow +CURL_OUTPUT=$(curl -sSL $NGINX_ADDR) +if [[ $CURL_OUTPUT != *"Powered by MediaWiki"* ]]; then + error "Main page failed to load properly: "$CURL_OUTPUT +fi + +info "Tests complete" diff --git a/test/run_tests.sh b/test/run_tests.sh new file mode 100755 index 0000000..1ca5bf7 --- /dev/null +++ b/test/run_tests.sh @@ -0,0 +1,11 @@ +#!/usr/bin/env bash + +set -xe + +REPO_ROOT=$(git rev-parse --show-toplevel) + +# Verify that builds succeed +docker-compose build + +# Run integration tests +$REPO_ROOT/test/integration/run_tests.sh