This repository has been archived by the owner on Nov 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathephemeral_unit_test.sh
122 lines (96 loc) · 3.2 KB
/
ephemeral_unit_test.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
111
112
113
114
115
116
117
118
119
120
121
122
#!/bin/bash
cd $APP_ROOT
# pre-commit -- do not run in the container so that it has access to .git data
echo '===================================='
echo '=== Running Pre-commit ===='
echo '===================================='
#Start Python venv
python3.8 -m venv venv
source venv/bin/activate
pip install --upgrade pip setuptools
pip install pre-commit
set +e
pre-commit run --all-files
$TEST_RESULT=$?
set -e
if [ $TEST_RESULT -ne 0 ]; then
echo '====================================='
echo '==== ✖ ERROR: PRECOMMIT FAILED ===='
echo '====================================='
exit 1
fi
pip install --force-reinstall poetry~=1.8.2
# Move back out of the pre-commit virtual env
source .bonfire_venv/bin/activate
# run unit tests in containers
DB_CONTAINER_NAME="hsp-db-${IMAGE_TAG}"
NETWORK="hsp-test-${IMAGE_TAG}"
POSTGRES_IMAGE="quay.io/cloudservices/postgresql-rds:cyndi-13"
function teardown_docker {
docker rm -f $DB_CONTAINER_ID || true
docker rm -f $TEST_CONTAINER_ID || true
docker network rm $NETWORK || true
}
trap "teardown_docker" EXIT SIGINT SIGTERM
docker network create --driver bridge $NETWORK
DB_CONTAINER_ID=$(docker run -d \
--name "${DB_CONTAINER_NAME}" \
--network "${NETWORK}" \
-e POSTGRESQL_USER="hsp-test" \
-e POSTGRESQL_PASSWORD="hsp-test" \
-e POSTGRESQL_DATABASE="hsp-test" \
${POSTGRES_IMAGE} || echo "0")
if [[ "$DB_CONTAINER_ID" == "0" ]]; then
echo "Failed to start DB container"
exit 1
fi
DB_IP_ADDR=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $DB_CONTAINER_ID)
# Do tests
TEST_CONTAINER_ID=$(docker run -d \
--network ${NETWORK} \
-e HSP_DB_NAME="hsp-test" \
-e HSP_DB_HOST="${DB_IP_ADDR}" \
-e HSP_DB_PORT="5432" \
-e HSP_DB_USER="hsp-test" \
-e HSP_DB_PASS="hsp-test" \
$IMAGE:$IMAGE_TAG \
/bin/bash -c 'sleep infinity' || echo "0")
if [[ "$TEST_CONTAINER_ID" == "0" ]]; then
echo "Failed to start test container"
exit 1
fi
ARTIFACTS_DIR="$WORKSPACE/artifacts"
mkdir -p $ARTIFACTS_DIR
# pip install
echo '===================================='
echo '=== Installing Pip Dependencies ===='
echo '===================================='
set +e
docker exec $TEST_CONTAINER_ID /bin/bash -c 'poetry install --with dev'
TEST_RESULT=$?
set -e
if [ $TEST_RESULT -ne 0 ]; then
echo '====================================='
echo '==== ✖ ERROR: PIP INSTALL FAILED ===='
echo '====================================='
exit 1
fi
# pytest
echo '===================================='
echo '==== Running Tests ===='
echo '===================================='
set +e
docker exec $TEST_CONTAINER_ID /bin/bash -c 'TEMP_DIR=`mktemp -d` && FLASK_APP=historical_system_profiles.app:get_flask_app_with_migration flask db upgrade && prometheus_multiproc_dir=$TEMPDIR pytest . "$@" --junitxml=junit-unittest.xml && rm -rf $TEMPDIR'
TEST_RESULT=$?
set -e
docker cp $TEST_CONTAINER_ID:junit-unittest.xml $WORKSPACE/artifacts
if [ $TEST_RESULT -ne 0 ]; then
echo '====================================='
echo '==== ✖ ERROR: TEST FAILED ===='
echo '====================================='
exit 1
fi
echo '====================================='
echo '==== ✔ SUCCESS: PASSED TESTS ===='
echo '====================================='
teardown_docker