-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathunit_test.sh
executable file
·107 lines (83 loc) · 2.51 KB
/
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
#!/bin/bash
# The following are defined outside of this script:
# DOCKERFILE
# APP_ROOT
# IMAGE_TAG
cd $APP_ROOT
API_IMAGE="local/remediations-consumer-test-${IMAGE_TAG}"
API_CONTAINER_NAME="remediations-consumer-test-${IMAGE_TAG}"
DB_IMAGE="quay.io/cloudservices/postgresql-rds:16"
DB_CONTAINER_NAME="remediations-consumer-db-${IMAGE_TAG}"
NETWORK="remediations-consumer-test-${IMAGE_TAG}"
# cleanup function to tidy up after the test run
function tidy_up {
echo '----> Tidying up...'
podman rm -f $DB_CONTAINER_ID || true
podman rm -f $API_CONTAINER_ID || true
podman network rm -f $NETWORK || true
podman rmi -f $API_IMAGE || true
podman rmi -f $DB_IMAGE || true
podman container prune --force || true
}
trap "tidy_up" EXIT SIGINT SIGTERM
#---------------------
# create test network
#---------------------
echo '----> Create network...'
podman network create --driver bridge $NETWORK
if [ $? -ne 0 ]; then
echo '====> FAILED creating test network'
exit 1
fi
#--------------------
# start db container
#--------------------
echo '----> pull DB image...'
podman pull $DB_IMAGE
echo '----> start db container...'
DB_CONTAINER_ID=$(podman run -d \
--name "${DB_CONTAINER_NAME}" \
--network "${NETWORK}" \
-e POSTGRESQL_USER="postgres_user" \
-e POSTGRESQL_PASSWORD="remediations" \
-e POSTGRESQL_DATABASE="remediations" \
${DB_IMAGE} || echo "0")
if [[ "$DB_CONTAINER_ID" == "0" ]]; then
echo "====> FAILED to start DB container"
exit 1
fi
#-----------------------------------
# start remediations-test container
#-----------------------------------
echo '----> build api test image...'
podman build -f $DOCKERFILE --target test -t $API_IMAGE .
echo '----> start api test container...'
API_CONTAINER_ID=$(podman run -d \
--name "${API_CONTAINER_NAME}" \
--network "${NETWORK}" \
-e NODE_ENV="test" \
-e DB_HOST="${DB_CONTAINER_NAME}" \
$API_IMAGE \
/bin/bash -c 'sleep infinity' || echo "0")
if [[ "$API_CONTAINER_ID" == "0" ]]; then
echo "Failed to start api container"
exit 1
fi
#-------------------------------------
# run remediations-tests in container
#-------------------------------------
echo '----> run api tests...'
podman exec $API_CONTAINER_ID /bin/bash -c 'npm run test:ci'
TEST_RESULT=$?
if [ $TEST_RESULT -ne 0 ]; then
echo '====> unit tests FAILED'
exit 1
fi
#----------------
# report results
#----------------
echo '----> record results...'
mkdir -p $WORKSPACE/artifacts
podman cp $API_CONTAINER_ID:/opt/app-root/src/artifacts/. $WORKSPACE/artifacts
echo '====> unit tests PASSED'
tidy_up