forked from tbal/vagrant-provision-script-collection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.sh
executable file
·149 lines (121 loc) · 4.44 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
#!/usr/bin/env bash
CURRENT_DIR="$(readlink -f `dirname $0`)"
[ -z "$VERBOSE" ] && VERBOSE=0
[ -z "$SCRIPT_SEARCH_DIR" ] && SCRIPT_SEARCH_DIR=$CURRENT_DIR
[ -z "$DOCKER_CONTAINER" ] && DOCKER_CONTAINER="debian:wheezy"
[ -z "$DOCKER_MOUNT_HOST" ] && DOCKER_MOUNT_HOST=$CURRENT_DIR
[ -z "$DOCKER_MOUNT_GUEST" ] && DOCKER_MOUNT_GUEST="/tmp/vpsc-test"
[ -z "$DOCKER_CMD_BEFORE" ] && DOCKER_CMD_BEFORE=
[ -z "$DOCKER_CMD_AFTER" ] && DOCKER_CMD_AFTER=
[ -z "$DOCKER_UPDATE_APT_INDEX" ] && DOCKER_UPDATE_APT_INDEX=0
[ -z "$EXIT_ON_FIRST_FAIL" ] && EXIT_ON_FIRST_FAIL=0
function show_help() {
echo "Usage: $(basename "$0") [options] [<files>]
-v Show complete output (stdout and stderr) of the test runs.
-b Set command which should be executed before any other files.
Example: ./$(basename "$0") -b 'cat /etc/debian_version && uname -a' [<files>]
-a Set command which should be executed after any other files.
This e.g. allows you to start a shell and attach to the container.
Example: ./$(basename "$0") -a '/bin/bash' [<files>]
-u Runs 'apt-get update' before executing actual files.
Same as: ./$(basename "$0") -b 'apt-get update > /dev/null' [<files>]
-h Print this help."
}
HAS_FAILED_TEST=0
TEST_SUMMARY=
function print_summary() {
echo -e "\n\n########## TEST SUMMARY ##########"
echo -e $TEST_SUMMARY
}
OPTIND=1 # Reset is necessary if getopts was used previously in the script. It is a good idea to make this local in a function.
while getopts ":hvb:a:u" opt; do
case "$opt" in
h)
show_help
exit 0
;;
v)
VERBOSE=1
;;
b)
DOCKER_CMD_BEFORE=$OPTARG
;;
a)
DOCKER_CMD_AFTER=$OPTARG
;;
u)
DOCKER_UPDATE_APT_INDEX=1
;;
esac
done
shift "$((OPTIND-1))" # Shift off the options and optional --.
if [ ! -z "$SCRIPTS" ]; then
# env variable was set
SCRIPTS=$SCRIPTS
elif [ $# -gt 0 ]; then
# use given arguments
SCRIPTS=$@
else
# no arguments given, test all scripts (exclude current script)
SCRIPTS=`find $SCRIPT_SEARCH_DIR -not -name "$(basename "$0")" -type f -name "*.sh"`
fi
# process every script
for SCRIPT in $SCRIPTS; do
# transform absolute to realative path
SCRIPT=$(echo "$SCRIPT" | sed "s#$CURRENT_DIR/##")
echo "########## Testing: $SCRIPT"
# check if script file exists
if [ ! -f $SCRIPT ]; then
echo -e "\033[31mERROR: '$SCRIPT' does not exist.\033[m"
exit 1
fi
# bash syntax check
if ! /usr/bin/env bash -n $SCRIPT; then
echo -e "\033[31mERROR: '$SCRIPT' contains syntax errors.\033[m"
exit 1
fi
# prepare execute command
DOCKER_EXECUTE_CMD="$DOCKER_MOUNT_GUEST/$SCRIPT"
if [ ! -z "$DOCKER_CMD_BEFORE" ]; then
DOCKER_EXECUTE_CMD="$DOCKER_CMD_BEFORE && $DOCKER_EXECUTE_CMD"
fi
if [ ! -z "$DOCKER_CMD_AFTER" ]; then
DOCKER_EXECUTE_CMD="$DOCKER_EXECUTE_CMD && $DOCKER_CMD_AFTER"
fi
if [ "$DOCKER_UPDATE_APT_INDEX" -eq 1 ]; then
DOCKER_EXECUTE_CMD="echo -n '>>> Updating apt index... ' && apt-get update > /dev/null && echo 'Done.' && $DOCKER_EXECUTE_CMD"
fi
# set up a subshell, so the exec does only affect the content of this subshell
(
# show output depending on VERBOSE variable
[ "$VERBOSE" -eq 1 ] || exec >/dev/null 2>&1
# fix error "cannot enable tty mode on non tty input" when running in jenkins
[ ! -z "$TERM" ] && [ "$TERM" != "dumb" ] && DOCKER_RUN_OPTS="-t -i"
# actually run prepared command in docker container
docker run \
$DOCKER_RUN_OPTS \
--rm \
-v "$DOCKER_MOUNT_HOST":"$DOCKER_MOUNT_GUEST" \
$DOCKER_CONTAINER \
/usr/bin/env bash -c "$DOCKER_EXECUTE_CMD"
)
# print test result
if [ $? -eq 0 ]; then
TEST_PASS="\033[32mTest for '$SCRIPT' was successful.\033[m"
TEST_SUMMARY="${TEST_SUMMARY}${TEST_PASS}\n"
echo -e $TEST_PASS
else
HAS_FAILED_TEST=1
TEST_FAIL="\033[31mERROR: Test execution of '$SCRIPT' failed!\033[m" 1>&2
TEST_SUMMARY="${TEST_SUMMARY}${TEST_FAIL}\n"
echo -e $TEST_FAIL
if [ $EXIT_ON_FIRST_FAIL -eq 1 ]; then
print_summary
exit 1
fi
fi
done
print_summary
if [ $HAS_FAILED_TEST -eq 1 ]; then
exit 1
fi