Skip to content

Commit

Permalink
Add integration test scaffolding
Browse files Browse the repository at this point in the history
- Define integration test config override, which basically renames all
  the volumes to avoid clashing with any existing volumes.
- Add test setup, which starts all the services and installs mediawiki
  using the CLI installer.
- Add an example test, which loads the Main Page. More to come.
- Add integration tests to the Travis CI config.
  • Loading branch information
amrav committed Jun 18, 2017
1 parent d9f99e0 commit 1bd18fd
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 2 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 8 additions & 0 deletions test/integration/.env
Original file line number Diff line number Diff line change
@@ -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=
22 changes: 22 additions & 0 deletions test/integration/docker-compose.test.yml
Original file line number Diff line number Diff line change
@@ -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:
98 changes: 98 additions & 0 deletions test/integration/run_tests.sh
Original file line number Diff line number Diff line change
@@ -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"
11 changes: 11 additions & 0 deletions test/run_tests.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 1bd18fd

Please sign in to comment.