forked from cyber-dojo/saver
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.sh
executable file
·170 lines (141 loc) · 4.6 KB
/
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/bin/bash -Eeu
export ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
readonly SCRIPTS_DIR="${ROOT_DIR}/scripts"
source "${SCRIPTS_DIR}/config.sh"
source "${SCRIPTS_DIR}/echo_versioner_env_vars.sh"
source "${SCRIPTS_DIR}/exit_non_zero_unless_installed.sh"
source "${SCRIPTS_DIR}/exit_zero_if_show_help.sh"
# - - - - - - - - - - - - - - - - - - - - - - - - - -
containers_run_tests()
{
if [ "${1:-}" == server ]; then
shift
run_server_tests "$@"
elif [ "${1:-}" == client ]; then
shift
run_client_tests "$@"
else
run_server_tests "$@"
run_client_tests "$@"
fi
}
# - - - - - - - - - - - - - - - - - - - - - - - - - -
run_server_tests()
{
run_tests $(server_user) $(server_container) $(server_name) "${@:-}"
}
# - - - - - - - - - - - - - - - - - - - - - - - - - -
run_client_tests()
{
run_tests $(client_user) $(client_container) $(client_name) "${@:-}"
}
# - - - - - - - - - - - - - - - - - - - - - - - - - -
run_tests()
{
# Getting coverage data
# - - - - - - - - - - -
# I would like to do this in docker-compose.yml
#
# saver:
# volume:
# ./tmp:/app/tmp:rw
#
# and write the coverage off /app/tmp thus avoiding
# copying the coverage out of the container.
#
# This works locally, but not on the CircleCI pipeline
# which runs as the ubuntu user, and does not have
# permission to run this (before docker-compose up):
# $ chown -R 19663:65533 ./tmp
# See app/config/up.sh
#
# So coverage data is being written to /tmp inside the container
# and docker-compose.yml has a tmpfs: /tmp
# You can't [docker cp] from a tmpfs, so tar-piping coverage out.
reset_dirs_inside_containers
copy_in_saver_test_data
local -r user="${1}"
local -r cid="${2}"
local -r type="${3}" # client|server
echo
echo "Running ${type} tests"
echo
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Run tests and test-result metrics inside the container.
local -r CONTAINER_COVERAGE_DIR="/tmp/${type}"
local -r COVERAGE_CODE_TAB_NAME=app
local -r COVERAGE_TEST_TAB_NAME=test
local -r TEST_LOG=test.log
set +e
docker exec \
--env COVERAGE_ROOT=${CONTAINER_COVERAGE_DIR} \
--env COVERAGE_CODE_TAB_NAME=${COVERAGE_CODE_TAB_NAME} \
--env COVERAGE_TEST_TAB_NAME=${COVERAGE_TEST_TAB_NAME} \
-it \
--user "${user}" \
"${cid}" \
sh -c "/app/test/config/run.sh ${TEST_LOG} ${*:4}"
local status=$?
set -e
if [ "${status}" == 255 ]; then
exit 42 # ^C
fi
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Extract test-results and metrics data from the container.
# You can't [docker cp] from a tmpfs, so tar-piping coverage out
local -r HOST_COVERAGE_DIR="${ROOT_DIR}/tmp/coverage"
mkdir -p "${HOST_COVERAGE_DIR}"
rm -rf "${HOST_COVERAGE_DIR}/*"
docker exec \
"${cid}" \
tar Ccf \
"$(dirname "${CONTAINER_COVERAGE_DIR}")" \
- "$(basename "${CONTAINER_COVERAGE_DIR}")" \
| tar Cxf "${HOST_COVERAGE_DIR}/" -
echo Coverage written to
echo "${HOST_COVERAGE_DIR}/${type}/index.html"
if [ "${status}" == 0 ]; then
echo "Test status: PASSED"
else
echo "Test status: FAILED"
fi
echo
return ${status}
}
# - - - - - - - - - - - - - - - - - - - - - - - - - -
reset_dirs_inside_containers()
{
# See docker-compose.yml for tmpfs and external volume
local DIRS=''
# /cyber-dojo is a tmpfs
DIRS="${DIRS} /cyber-dojo/*"
# /one_k is an external volume
# See create_space_limited_volume() in ./up.sh
DIRS="${DIRS} /one_k/*"
# /tmp is a tmpfs
DIRS="${DIRS} /tmp/cyber-dojo/*"
docker exec "$(server_container)" bash -c "rm -rf ${DIRS}"
docker exec "$(client_container)" bash -c "rm -rf /tmp/*"
}
# - - - - - - - - - - - - - - - - - - - - - - - - - -
copy_in_saver_test_data()
{
local -r TEST_DATA_DIR="${ROOT_DIR}/app/test/data"
# You cannot docker cp to a tmpfs, so tar-piping...
cd "${TEST_DATA_DIR}/cyber-dojo" \
&& tar -c . \
| docker exec -i "$(server_container)" tar x -C /cyber-dojo
cat "${TEST_DATA_DIR}/almost_full_group.v0.AWCQdE.tgz" \
| docker exec -i "$(server_container)" tar -zxf - -C /
cat "${TEST_DATA_DIR}/almost_full_group.v1.X9UunP.tgz" \
| docker exec -i "$(server_container)" tar -zxf - -C /
cat "${TEST_DATA_DIR}/almost_full_group.v2.U8Tt6y.tgz" \
| docker exec -i "$(server_container)" tar -zxf - -C /
cat "${TEST_DATA_DIR}/rG63fy.tgz" \
| docker exec -i "$(server_container)" tar -zxf - -C /
}
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
export $(echo_versioner_env_vars)
exit_zero_if_show_help "$@"
exit_non_zero_unless_installed docker docker-compose
containers_run_tests "$@"