forked from jlebon/atomic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.sh
executable file
·189 lines (156 loc) · 4.84 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/bin/bash
set -euo pipefail
IFS=$'\n\t'
#
# Test harness for the atomic CLI.
#
export PYTHONPATH=${PYTHONPATH:-$(pwd)}
export WORK_DIR=$(mktemp -p $(pwd) -d -t .tmp.XXXXXXXXXX)
export DOCKER=${DOCKER:-"/usr/bin/docker"}
export SECRET=`dd if=/dev/urandom bs=4096 count=1 2> /dev/null | sha256sum`
export ATOMIC_LIBEXEC="$(pwd)"
# This image contains the secret, so it needs to be rebuilt each time.
cat > tests/test-images/Dockerfile.secret <<EOF
FROM scratch
LABEL "Name"="atomic-test-secret"
LABEL "Secret"="${SECRET}"
ADD secret /secret
EOF
LOG=${LOG:-"$(pwd)/tests.log"}
echo -n '' > ${LOG}
cleanup () {
rm -rf ${WORK_DIR}
}
trap cleanup EXIT
_checksum () {
if [[ -d "${1}" ]]; then
CHK=`find "${1}" -type f -exec sha256sum {} \; | sha256sum`
echo "${CHK}"
return 0
fi
if [[ -e "${1}" ]]; then
echo "$(sha256sum ${1})"
fi
}
# Ensure the test-environment has a standard set of images.
# This function will not rebuild images if the dockerfile
# is the same as its last build.
make_docker_images () {
echo "${SECRET}" > ${WORK_DIR}/secret
echo "Pulling standard images from Docker Hub..." | tee -a ${LOG}
${DOCKER} pull busybox >> ${LOG}
${DOCKER} pull centos >> ${LOG}
${DOCKER} pull fedora >> ${LOG}
echo "Building images from tests/test-images..." | tee -a ${LOG}
for df in `find ./tests/test-images/ -name Dockerfile.*`; do
# Don't include directories for dockerfile data
if [[ -d "${df}" ]]; then
continue
fi
BASE_NAME="$(basename ${df})"
chksum=$(_checksum ${df})
IFS=$'.' read -a split <<< "${BASE_NAME}"
iname="atomic-test-${split[1]}"
# If there is a matching Dockerfile.X.d, then include its contents
# in the checksum data.
chksum="${chksum}$(_checksum ${df}.d)"
set +e
i_chksum=`${DOCKER} inspect -f '{{ .Config.Labels.Checksum }}' \
${iname} 2> /dev/null`
if [[ ${i_chksum} = "<no-value>" ]] || \
[[ "${i_chksum}" = "${chksum}" ]]; then
printf "\tSkipped : ${iname}\n"
continue
fi
set -e
# Copy the dockerfile into the build directory, then label the image
# with the original Dockerfile's checksum. This allows us to prevent
# rebuilding images
df_cp=${WORK_DIR}/${BASE_NAME}
cp ${df} ${df_cp}
printf "\nLABEL \"Checksum\"=\"${chksum}" >> ${df_cp}
# Copy help.1 into atomic-test-1
if [[ ${iname} = "atomic-test-1" ]]; then
cp ./tests/test-images/help.1 ${WORK_DIR}
fi
# Copy help.sh into atomic-test-3
if [[ ${iname} = "atomic-test-3" ]]; then
cp ./tests/test-images/help.sh ${WORK_DIR}
fi
# Remove the old image... Though there may not be one.
set +e
${DOCKER} rmi ${iname} &>> ${LOG}
set -e
if [[ -d "${df}.d" ]]; then
cp -r "${df}.d" "${WORK_DIR}/${BASE_NAME}.d"
fi
${DOCKER} build -t ${iname} -f ${df_cp} ${WORK_DIR} >> ${LOG}
# Clean up build files.
rm "${df_cp}"
if [[ -d "${WORK_DIR}/${BASE_NAME}.d" ]]; then
rm -r "${WORK_DIR}/${BASE_NAME}.d"
fi
printf "\tBuilt : ${iname}\n"
done
}
make_docker_images
# Python unit tests.
echo "UNIT TESTS:"
COVERAGE_BIN="/usr/bin/coverage"
if [[ ! -x "${COVERAGE_BIN}" ]]; then
# Check to see if it is in local instead.
COVERAGE_BIN="/usr/local/bin/coverage"
fi
if [[ ! -x "${COVERAGE_BIN}" ]]; then
# The executable is "coverage2" on systems with default python3 and no
# python3 install.
COVERAGE_BIN="/usr/bin/coverage2"
fi
set +e
COVERAGE="${COVERAGE_BIN}
run
--source=./Atomic/
--branch"
${COVERAGE} -m unittest discover ./tests/unit/ | tee -a ${LOG}
_UNIT_FAIL="$?"
set -e
# CLI integration tests.
let failures=0 || true
printf "\nINTEGRATION TESTS:\n" | tee -a ${LOG}
export ATOMIC="${COVERAGE}
--append
atomic"
for tf in `find ./tests/integration/ -name test_*.sh`; do
if [ -n "${TEST_INTEGRATION+ }" ]; then
tfbn=$(basename "$tf" .sh)
tfbn="${tfbn#test_}"
if [[ " $TEST_INTEGRATION " != *" $tfbn "* ]]; then
continue
fi
fi
printf "Running test $(basename ${tf})...\t\t"
printf "\n==== ${tf} ====\n" >> ${LOG}
if ${tf} &>> ${LOG}; then
printf "PASS\n";
else
printf "FAIL\n";
let "failures += 1"
fi
done
echo "Coverage report:" | tee -a ${LOG}
${COVERAGE_BIN} report | tee -a ${LOG}
if [[ "${failures}" -eq "0" ]]; then
if [[ $_UNIT_FAIL -eq 0 ]]; then
echo "ALL TESTS PASSED"
exit 0
else
echo "Unit tests failed."
fi
else
if [[ $_UNIT_FAIL -ne 0 ]]; then
echo "Unit tests failed."
fi
echo "Integration test failures: ${failures}"
echo "See ${LOG} for more information."
fi
exit 1