#!/bin/bash set -euo pipefail IMAGE="quay-ci" CACHE_DIR="${HOME}/docker" SHORT_SHA="${TRAVIS_COMMIT:0:7}" IMAGE_TAG="${SHORT_SHA}-${TRAVIS_BUILD_NUMBER}" IMAGE_TAR="${CACHE_DIR}/${IMAGE}-${IMAGE_TAG}.tar.gz" MYSQL_IMAGE="mysql:5.7" POSTGRES_IMAGE="postgres:9.6" POSTGRES_CONTAINER="test_postgres" export MYSQL_ROOT_PASSWORD="quay" export MYSQL_USER="quay" export MYSQL_PASSWORD="quay" export MYSQL_DATABASE="quay_ci" export POSTGRES_USER="quay" export POSTGRES_PASSWORD="quay" export POSTGRES_DB="quay_ci" build_image() { # Build the image and save it to the shared cache. docker build -t "${IMAGE}:${IMAGE_TAG}" . echo "Exporting Docker image to cache..." time (docker save "${IMAGE}:${IMAGE_TAG}" | gzip -2 > "${IMAGE_TAR}") } load_image() { # Load our cached Docker image. echo "Loading Docker image from cache..." time (zcat "${IMAGE_TAR}" | docker load) } clean_cache() { rm -f "${IMAGE_TAR}" } fail_clean() { if [[ $TRAVIS_TEST_RESULT -ne 0 ]]; then echo "Job failed. Cleaning cache..." clean_cache fi } quay_run() { docker run --net=host -e TEST_DATABASE_URI -ti "${IMAGE}:${IMAGE_TAG}" "$@" } unit() { MARK=${1:-"shard_1_of_1"} load_image && quay_run "make -f Makefile.ci unit-test PYTEST_MARK=$MARK" } registry() { MARK=${1:-"shard_1_of_1"} load_image && quay_run "make -f Makefile.ci registry-test PYTEST_MARK=$MARK" } registry_old() { load_image && quay_run "make -f Makefile.ci registry-test-old" } quay_ping() { curl --fail http://localhost:8080/v1/_internal_ping } gunicorn_test() { load_image docker tag "${IMAGE}:${IMAGE_TAG}" quay-ci-base echo "Building CI run image" docker build -t "${IMAGE}:${IMAGE_TAG}-ci-run" -f Dockerfile.cirun . echo "Running CI run image" docker run -d --name=quay-gunicorn-test --net=host -ti "${IMAGE}:${IMAGE_TAG}-ci-run" echo "Sleeping for CI image start" if ! (sleep 120 && quay_ping); then echo "Quay container logs:" docker log quay-gunicorn-test echo "Quay failed to respond in time." exit 1 fi echo "" echo "Testing gunicorn" echo "" ./test/test_gunicorn_running.sh echo "Done testing gunicorn" echo "" docker kill quay-gunicorn-test } certs_test() { load_image && quay_run "make -f Makefile.ci certs-test" } mysql_ping() { mysqladmin --connect-timeout=2 --wait=60 --host=127.0.0.1 \ --user=root --password="${MYSQL_ROOT_PASSWORD}" ping } mysql_start() { docker run --net=host -d -e MYSQL_ROOT_PASSWORD -e MYSQL_USER \ -e MYSQL_PASSWORD -e MYSQL_DATABASE "${MYSQL_IMAGE}" if ! (sleep 20 && mysql_ping); then echo "MySQL failed to respond in time." exit 1 fi } mysql() { MARK=${1:-"shard_1_of_1"} TEST_DATABASE_URI="mysql+pymysql://" TEST_DATABASE_URI+="${MYSQL_USER}:${MYSQL_PASSWORD}" TEST_DATABASE_URI+="@127.0.0.1/${MYSQL_DATABASE}" export TEST_DATABASE_URI load_image mysql_start quay_run "make -f Makefile.ci full-db-test PYTEST_MARK=$MARK" } postgres_ping() { pg_isready --timeout=30 --dbname="${TEST_DATABASE_URI}" } postgres_start() { docker run --name="${POSTGRES_CONTAINER}" --net=host -d -e POSTGRES_USER -e POSTGRES_PASSWORD \ -e POSTGRES_DB "${POSTGRES_IMAGE}" if ! (sleep 10 && postgres_ping); then echo "PostgreSQL failed to respond in time." exit 1 fi } postgres_init() { docker exec "${POSTGRES_CONTAINER}" psql -U "${POSTGRES_USER}" -d "${POSTGRES_DB}" -c 'CREATE EXTENSION IF NOT EXISTS pg_trgm;' } postgres() { MARK=${1:-"shard_1_of_1"} TEST_DATABASE_URI="postgresql://" TEST_DATABASE_URI+="${POSTGRES_USER}:${POSTGRES_PASSWORD}" TEST_DATABASE_URI+="@127.0.0.1/${POSTGRES_DB}" export TEST_DATABASE_URI load_image postgres_start postgres_init quay_run "make -f Makefile.ci full-db-test PYTEST_MARK=$MARK" } case "$1" in build) build_image ;; unit) shift unit "$@" ;; registry) shift registry "$@" ;; registry_old) registry_old ;; certs_test) certs_test ;; gunicorn_test) gunicorn_test ;; mysql) shift mysql "$@" ;; postgres) shift postgres "$@" ;; fail-clean) fail_clean ;; clean) clean_cache ;; *) echo "Usage: $0 {build|unit|registry|registry_old|mysql|postgres|clean}" exit 1 ;; esac