-
Notifications
You must be signed in to change notification settings - Fork 22
/
run-tests.sh
executable file
·281 lines (236 loc) · 9.49 KB
/
run-tests.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#!/bin/bash
# Test suite execution script for the gluster-coreutils project. The execution
# script is responsible for verifying the system environment in which the tests
# will be executed and setting up and validating (as appropriate) the test
# environment against which the tests will be executed.
#
# The tests are implemented using the Test Anything Protocol (testanything.org)
# In this case, the test consumer is the Perl `prove` test harness and the test
# producer is the `bats` utility (github.com/sstephenson/bats).
#
# There are three environment variables that the script tests for which will
# influence the behavior of its execution: GLUSTER_VOLUME, GLUSTER_BRICK_DIR,
# and GLUSTER_MOUNT_DIR. If all three variables are set, the script will skip
# setting up a temporary Gluster volume of its own and will instead use the
# provided values. GLUSTER_VOLUME must be a valid Gluster volume that is
# started, GLUSTER_BRICK_DIR should point to the directory that backs the
# volume, and GLUSTER_MOUNT_DIR should point to the mount point on which the
# volume is mounted (via FUSE). Note that as of this writing, the script does
# not do extensive validation of these provided values, so it is up to the
# user to ensure that they are sane values. Otherwise, the behavior of the
# script (and tests) is likely to be erratic and non-conclusive.
# prevent unnecessary headaches
set -u
DIR=$(cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd)
GLUSTER_DAEMON_NOT_RUNNING=true
GLUSTER_DAEMON_PID=-1
with_valgrind="no"
export BUILD_DIR="$DIR/build"
export CLI="gluster --mode=script --wignore"
export CMD_PREFIX=""
export HOST=$(hostname --fqdn)
export VALGRIND="valgrind --input-fd=3 --quiet --error-exitcode=2 \
--leak-check=full --suppressions=$DIR/tests/util/valgrind.suppressions"
source tests/util/log.rc
function check_root() {
if [[ $(id -u) -ne 0 ]]; then
error "Test needs to be executed with superuser privileges.\n"
exit 1
fi
}
function check_environment() {
highlight "==> Executing preflight\n"
MISSING=()
for cmd in bats gluster glusterd glusterfsd prove; do
command -v $cmd > /dev/null || MISSING+=($cmd)
done
if [[ ${#MISSING[@]} -ne 0 ]]; then
error "This system is missing the required tools:\n"
for pkg in $MISSING; do
echo "* $pkg"
done
exit 1
fi
if [[ ! -f $BUILD_DIR/bin/gfcli ]]; then
error "Utilities must be built before executing tests.\n"
exit 1
fi
if [[ ! -d $DIR/tests ]]; then
error "No tests directory present.\n"
exit 1
fi
info "Checking for running daemon: "
GLUSTER_DAEMON_PID=$(pgrep glusterd)
if [[ $? -eq 0 ]]; then
printf "yes\n"
GLUSTER_DAEMON_NOT_RUNNING=false
else
printf "no\n"
info "Starting gluster daemon: "
glusterd
if [[ $? -eq 0 ]]; then
printf "started\n"
else
printf "failed!\n"
error "failed to start gluster daemon\n"
exit 1
fi
fi
}
function setup_temporary_test_environment() {
highlight "==> Executing setup\n"
export ROOT_DIR=""
info "Creating temporary brick directory: "
export GLUSTER_BRICK_DIR=$(mktemp -d --tmpdir=/tmp BRICKXXXXXX)
printf "$GLUSTER_BRICK_DIR\n"
info "Creating temporary gluster volume: "
export GLUSTER_VOLUME=$(basename $GLUSTER_BRICK_DIR)
RESULT=$($CLI volume create $GLUSTER_VOLUME $HOST:$GLUSTER_BRICK_DIR force)
if [[ $? -ne 0 ]]; then
printf "failed!\n"
error "failed to create temporary volume $GLUSTER_VOLUME\n"
exit 1
else
printf "$GLUSTER_VOLUME\n"
fi
generate_data
info "Starting temporary gluster volume: "
RESULT=$($CLI volume start $GLUSTER_VOLUME force)
if [[ $? -ne 0 ]]; then
printf "failed!\n"
error "failed to start temporary volume $GLUSTER_VOLUME\n"
exit 1
else
printf "$GLUSTER_VOLUME\n"
fi
info "Creating temporary mount directory: "
export GLUSTER_MOUNT_DIR=$(mktemp -d --tmpdir=/tmp MNTXXXXXXX)
printf "$GLUSTER_MOUNT_DIR\n"
info "Mounting temporary volume: "
mount -t glusterfs "$HOST:/$GLUSTER_VOLUME" $GLUSTER_MOUNT_DIR
if [[ $? -eq 0 ]]; then
printf "$GLUSTER_MOUNT_DIR\n"
else
printf "failed!\n"
error "failed to mount temporary brick $GLUSTER_VOLUME\n"
exit 1
fi
}
function setup_test_environment() {
ROOT_DIR=$(mktemp -d --tmpdir=$GLUSTER_BRICK_DIR)
export ROOT_DIR="/$(basename $ROOT_DIR)"
generate_data
}
function generate_data() {
info "Generating test files and directories:\n"
TEST_DIR=$(mktemp -d --tmpdir="$GLUSTER_BRICK_DIR$ROOT_DIR")
export TEST_DIR=$(basename $TEST_DIR)
printf "* $TEST_DIR [directory]\n"
TEST_FILE_SMALL=$(mktemp --tmpdir="$GLUSTER_BRICK_DIR$ROOT_DIR")
RESULT=$(dd if=/dev/urandom of=$TEST_FILE_SMALL bs=1024 count=1 2>&1)
export TEST_FILE_SMALL_HASH=$(md5sum $TEST_FILE_SMALL | awk '{print $1}')
export TEST_FILE_SMALL=$(basename $TEST_FILE_SMALL)
printf "* $TEST_FILE_SMALL: $TEST_FILE_SMALL_HASH\n"
TEST_FILE_MEDIUM=$(mktemp --tmpdir="$GLUSTER_BRICK_DIR$ROOT_DIR")
RESULT=$(dd if=/dev/urandom of=$TEST_FILE_MEDIUM bs=1M count=10 2>&1)
export TEST_FILE_MEDIUM_HASH=$(md5sum $TEST_FILE_MEDIUM | awk '{print $1}')
export TEST_FILE_MEDIUM=$(basename $TEST_FILE_MEDIUM)
printf "* $TEST_FILE_MEDIUM: $TEST_FILE_MEDIUM_HASH\n"
TEST_FILE_LARGE=$(mktemp --tmpdir="$GLUSTER_BRICK_DIR$ROOT_DIR")
RESULT=$(dd if=/dev/urandom of=$TEST_FILE_LARGE bs=1M count=100 2>&1)
export TEST_FILE_LARGE_HASH=$(md5sum $TEST_FILE_LARGE | awk '{print $1}')
export TEST_FILE_LARGE=$(basename $TEST_FILE_LARGE)
printf "* $TEST_FILE_LARGE: $TEST_FILE_LARGE_HASH\n"
}
function run_tests() {
highlight "==> Executing tests\n"
if [ $with_valgrind == "yes" ]; then
CMD_PREFIX="$VALGRIND"
fi
prove -rf --timer tests
}
function cleanup_temporary_test_environment() {
highlight "==> Executing test environment cleanup\n"
info "Unmounting temporary mount directory: "
umount -fl $GLUSTER_MOUNT_DIR
if [[ $? -ne 0 ]]; then
printf "failed!\n"
error "failed to unmount temporary mount directory $GLUSTER_MOUNT_DIR\n"
else
printf "$GLUSTER_MOUNT_DIR\n"
fi
info "Removing temporary mount directory: "
rm -rf $GLUSTER_MOUNT_DIR
if [[ $? -ne 0 ]]; then
printf "failed!\n"
error "failed to remove temporary mount directory $GLUSTER_MOUNT_DIR\n"
else
printf "$GLUSTER_MOUNT_DIR\n"
fi
info "Stopping temporary gluster volume: "
RESULT=$($CLI volume stop $GLUSTER_VOLUME force)
if [[ $? -ne 0 ]]; then
printf "failed!\n"
error "failed to stop temporary volume $GLUSTER_VOLUME\n"
else
printf "$GLUSTER_VOLUME\n"
fi
info "Removing temporary gluster volume: "
RESULT=$($CLI volume delete $GLUSTER_VOLUME)
if [[ $? -ne 0 ]]; then
printf "failed!\n"
error "failed to remove temporary volume $GLUSTER_VOLUME\n"
else
printf "$GLUSTER_VOLUME\n"
fi
info "Removing temporary brick directory: "
rm -rf $GLUSTER_BRICK_DIR
if [[ $? -ne 0 ]]; then
printf "failed!\n"
error "failed to remove temporary brick directory $GLUSTER_BRICK_DIR\n"
else
printf "$GLUSTER_BRICK_DIR\n"
fi
if $GLUSTER_DAEMON_NOT_RUNNING; then
info "Stopping gluster daemon: "
RESULT=$(pkill glusterd)
if [[ $? -ne 0 ]]; then
printf "failed!\n"
error "failed to stop gluster daemon"
exit 1
else
printf "done\n"
fi
fi
}
function cleanup_test_environment() {
highlight "==> Executing test environment cleanup\n"
rm -rf "$GLUSTER_BRICK_DIR$ROOT_DIR"
if [[ $? -ne 0 ]]; then
error "failed to remove test data directory\n"
exit 1
fi
}
function main() {
args=`getopt g "$@"`
set -- $args
while [ $# -gt 0 ]; do
case "$1" in
-g) with_valgrind="yes" ;;
--) shift; break ;;
esac
shift
done
check_root
check_environment
if [[ -z ${GLUSTER_BRICK_DIR+x} || -z ${GLUSTER_MOUNT_DIR+x} || -z ${GLUSTER_VOLUME+x} ]]; then
setup_temporary_test_environment
run_tests
cleanup_temporary_test_environment
else
setup_test_environment
run_tests
cleanup_test_environment
fi
}
main "$@"