forked from fabric8-analytics/fabric8-analytics-worker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runtest.sh
executable file
·110 lines (93 loc) · 3.86 KB
/
runtest.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/bin/bash
# fail if smth fails
# the whole env will be running if test suite fails so you can debug
set -e
# for debugging this script, b/c I sometimes get
# unable to prepare context: The Dockerfile (Dockerfile.tests) must be within the build context (.)
set -x
here=$(cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd)
TIMESTAMP="$(date +%F-%H-%M-%S)"
IMAGE_NAME=${IMAGE_NAME:-bayesian/cucos-worker}
TEST_IMAGE_NAME="worker-tests"
POSTGRES_IMAGE_NAME="registry.centos.org/centos/postgresql-94-centos7:latest"
S3_IMAGE_NAME="minio/minio"
CVEDB_S3_DUMP_IMAGE_NAME="registry.devshift.net/bayesian/cvedb-s3-dump"
CONTAINER_NAME="worker-tests-${TIMESTAMP}"
# we don't want to wipe local "database" container, so we create a custom one just for tests
TESTDB_CONTAINER_NAME="worker-tests-db-${TIMESTAMP}"
TESTS3_CONTAINER_NAME="worker-tests-s3-${TIMESTAMP}"
TESTCVEDB_S3_DUMP_CONTAINER_NAME="worker-tests-cvedb-s3-dump-${TIMESTAMP}"
DOCKER_NETWORK="F8aWorkerTest"
gc() {
retval=$?
# FIXME: make this configurable
echo "Stopping test containers"
docker stop "${CONTAINER_NAME}" "${TESTDB_CONTAINER_NAME}" "${TESTS3_CONTAINER_NAME}" "${TESTCVEDB_S3_DUMP_CONTAINER_NAME}" || :
echo "Removing test containers"
docker rm -v "${CONTAINER_NAME}" "${TESTDB_CONTAINER_NAME}" "${TESTS3_CONTAINER_NAME}" "${TESTCVEDB_S3_DUMP_CONTAINER_NAME}" || :
echo "Removing network ${DOCKER_NETWORK}"
docker network rm "${DOCKER_NETWORK}" || :
exit $retval
}
trap gc EXIT SIGINT
if [ "$REBUILD" == "1" ] || \
!(docker inspect $IMAGE_NAME > /dev/null 2>&1); then
echo "Building $IMAGE_NAME for testing"
docker build --tag="$IMAGE_NAME" .
fi
if [ "$REBUILD" == "1" ] || \
!(docker inspect $TEST_IMAGE_NAME > /dev/null 2>&1); then
echo "Building $TEST_IMAGE_NAME test image"
docker build -f ./Dockerfile.tests --tag=$TEST_IMAGE_NAME .
fi
echo "Removing database"
docker kill "${TESTDB_CONTAINER_NAME}" "${TESTS3_CONTAINER_NAME}" || :
docker rm -vf "${TESTDB_CONTAINER_NAME}" "${TESTS3_CONTAINER_NAME}" || :
echo "Creating network ${DOCKER_NETWORK}"
docker network create ${DOCKER_NETWORK}
echo "Starting/creating containers:"
# first start the database under different name, so that we don't overwrite a non-testing db
# NOTE: we omit pgbouncer while running tests
docker run -d \
--env-file tests/postgres.env \
--network ${DOCKER_NETWORK} \
--name "${TESTDB_CONTAINER_NAME}" "${POSTGRES_IMAGE_NAME}"
DB_CONTAINER_IP=$(docker inspect --format "{{.NetworkSettings.Networks.${DOCKER_NETWORK}.IPAddress}}" ${TESTDB_CONTAINER_NAME})
# TODO: this is duplicating code with server's runtest, we should refactor
echo "Waiting for postgres to fully initialize"
set +x
for i in {1..10}; do
retcode=$(curl http://${DB_CONTAINER_IP}:5432 &>/dev/null || echo $?)
if test "$retcode" == "52"; then
break
fi;
sleep 1
done;
set -x
docker run -d \
--env-file tests/minio.env \
--name "${TESTS3_CONTAINER_NAME}" \
--network "${DOCKER_NETWORK}" \
${S3_IMAGE_NAME} server --address :33000 /export
S3_CONTAINER_IP=$(docker inspect --format "{{.NetworkSettings.Networks.${DOCKER_NETWORK}.IPAddress}}" ${TESTS3_CONTAINER_NAME})
S3_ENDPOINT_URL="http://${S3_CONTAINER_IP}:33000"
docker run \
-e S3_ENDPOINT_URL="${S3_ENDPOINT_URL}" \
--env-file tests/cvedb_s3_dump.env \
--network "${DOCKER_NETWORK}" \
--name "${TESTCVEDB_S3_DUMP_CONTAINER_NAME}" "${CVEDB_S3_DUMP_IMAGE_NAME}"
echo "Starting test suite"
docker run -t \
-v "${here}:/f8a_worker:ro,Z" \
--network "${DOCKER_NETWORK}" \
-u 9007 \
-e PGBOUNCER_SERVICE_HOST="${TESTDB_CONTAINER_NAME}" \
-e S3_ENDPOINT_URL="${S3_ENDPOINT_URL}" \
-e DEPLOYMENT_PREFIX='test' \
-e WORKER_ADMINISTRATION_REGION='api' \
-e F8A_UNCLOUDED_MODE='true' \
-e SENTRY_DSN='' \
--env-file tests/postgres.env \
--name="${CONTAINER_NAME}" \
${TEST_IMAGE_NAME} /f8a_worker/hack/exec_tests.sh $@ /f8a_worker/tests/
echo "Test suite passed \\o/"