forked from RedHatInsights/compliance-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunit_test.sh
executable file
·149 lines (132 loc) · 4.47 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/bin/bash
cd "$APP_ROOT"
export IMAGE="quay.io/cloudservices/compliance-backend"
IMAGE_TAG=$(git rev-parse --short=7 HEAD)
export IMAGE_TAG
DB_CONTAINER_NAME="compliance-db-${IMAGE_TAG}"
TEST_CONTAINER_NAME="compliance-test-${IMAGE_TAG}"
NETWORK="compliance-test-${IMAGE_TAG}"
POSTGRES_IMAGE="quay.io/cloudservices/postgresql-rds:cyndi-12-1"
IMAGE="quay.io/cloudservices/compliance-backend"
IMAGE_TAG=$(git rev-parse --short=7 HEAD)
DATABASE_USER="compliance"
DATABASE_PASSWORD="changeme"
DATABASE_NAME="compliance-test"
function teardown_docker {
docker rm -f "$DB_CONTAINER_NAME" || true
docker rm -f "$TEST_CONTAINER_NAME" || true
docker network rm "$NETWORK" || true
}
trap "teardown_docker" EXIT SIGINT SIGTERM
docker network rm "$NETWORK" || echo "network likely doesn't exist"
docker network create --driver bridge "$NETWORK"
DB_CONTAINER_ID=$(docker run -d \
--name "${DB_CONTAINER_NAME}" \
--network "$NETWORK" \
--rm \
-e POSTGRESQL_USER="$DATABASE_USER" \
-e POSTGRESQL_PASSWORD="$DATABASE_PASSWORD" \
-e POSTGRESQL_DATABASE="$DATABASE_NAME" \
"$POSTGRES_IMAGE" || echo "0")
if [[ "$DB_CONTAINER_ID" == "0" ]]; then
echo "Failed to start DB container"
exit 1
fi
# Do tests
TEST_CONTAINER_ID=$(docker run -d \
--name "${TEST_CONTAINER_NAME}" \
--network "$NETWORK" \
--rm \
-e DATABASE_SERVICE_NAME=postgresql \
-e POSTGRESQL_SERVICE_HOST="$DB_CONTAINER_NAME" \
-e POSTGRESQL_USER="$DATABASE_USER" \
-e POSTGRESQL_PASSWORD="$DATABASE_PASSWORD" \
-e POSTGRESQL_DATABASE="$DATABASE_NAME" \
-e RAILS_ENV=test \
-e CI=true \
-e CODECOV_TOKEN="$CODECOV_TOKEN" \
-e JENKINS_URL="$JENKINS_URL" \
-e ghprbSourceBranch="$ghprbSourceBranch" \
-e GIT_BRANCH="$GIT_BRANCH" \
-e ghprbActualCommit="$ghprbActualCommit" \
-e GIT_COMMIT="$GIT_COMMIT" \
-e BUILD_NUMBER="$BUILD_NUMBER" \
-e ghprbPullId="$ghprbPullId" \
-e BUILD_URL="$BUILD_URL" \
"$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
WORKSPACE=${WORKSPACE:-./}
ARTIFACTS_DIR="$WORKSPACE/artifacts"
mkdir -p "$ARTIFACTS_DIR"
# gem install
echo '===================================='
echo '=== Installing Gem Dependencies ===='
echo '===================================='
set +e
docker exec "$TEST_CONTAINER_ID" /bin/bash -c 'bundle install --with test'
TEST_RESULT=$?
set -e
if [[ $TEST_RESULT -ne 0 ]]; then
echo '====================================='
echo '==== ✖ ERROR: GEM INSTALL FAILED ===='
echo '====================================='
exit 1
fi
# setup db
echo '===================================='
echo '=== Setting Up Database ===='
echo '===================================='
set +e
docker exec "$TEST_CONTAINER_ID" /bin/bash -c 'bundle exec rake db:test:prepare'
TEST_RESULT=$?
set -e
if [[ $TEST_RESULT -ne 0 ]]; then
echo '====================================='
echo '==== ✖ ERROR: DB SETUP FAILED ===='
echo '====================================='
exit 1
fi
# setup cyndi
echo '===================================='
echo '=== Setting Up Cyndi ===='
echo '===================================='
set +e
docker cp "$TEST_CONTAINER_ID":/opt/app-root/src/db/cyndi_setup_test.sql "$WORKSPACE/"
docker cp "$WORKSPACE/cyndi_setup_test.sql" "$DB_CONTAINER_ID":/var/lib/pgsql/
rm "$WORKSPACE/cyndi_setup_test.sql"
docker exec "$DB_CONTAINER_ID" /bin/bash -c 'psql -d $POSTGRESQL_DATABASE < cyndi_setup_test.sql'
TEST_RESULT=$?
set -e
if [[ $TEST_RESULT -ne 0 ]]; then
echo '====================================='
echo '==== ✖ ERROR: CYNDI SETUP FAILED ===='
echo '====================================='
exit 1
fi
# unit tests
echo '===================================='
echo '=== Running Unit Tests ===='
echo '===================================='
set +e
docker exec "$TEST_CONTAINER_ID" /bin/bash -c 'bundle exec rake test:validate'
TEST_RESULT=$?
set -e
# Copy test reports
docker cp "$TEST_CONTAINER_ID":/opt/app-root/src/test/reports/. "$WORKSPACE"/artifacts
# Prefix name of reports with 'junit-' so jenkins analysis picks them up
cd "$WORKSPACE/artifacts"
for FILENAME in TEST-*.xml; do mv "$FILENAME" "junit-$FILENAME"; done
cd -
if [[ $TEST_RESULT -ne 0 ]]; then
echo '====================================='
echo '==== ✖ ERROR: UNIT TEST FAILED ===='
echo '====================================='
exit 1
fi
echo '====================================='
echo '==== ✔ SUCCESS: PASSED TESTS ===='
echo '====================================='