forked from adamlaska/machine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun-bats.sh
More file actions
executable file
·120 lines (96 loc) · 2.94 KB
/
run-bats.sh
File metadata and controls
executable file
·120 lines (96 loc) · 2.94 KB
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
#!/usr/bin/env bash
set -e
# Wrapper script to run bats tests for various drivers.
# Usage: DRIVER=[driver] ./run-bats.sh [subtest]
function quiet_run () {
if [[ "$VERBOSE" == "1" ]]; then
"$@"
else
"$@" &>/dev/null
fi
}
function cleanup_machines() {
for MACHINE_NAME in $(machine ls -q); do
if [[ "$MACHINE_NAME" != "$SHARED_NAME" ]] || [[ "$1" == "ALL" ]]; then
quiet_run machine rm -f $MACHINE_NAME
fi
done
}
function cleanup_store() {
if [[ -d "$MACHINE_STORAGE_PATH" ]]; then
rm -r "$MACHINE_STORAGE_PATH"
fi
}
function machine() {
"$MACHINE_ROOT"/bin/"$MACHINE_BIN_NAME" "$@"
}
function run_bats() {
for bats_file in $(find "$1" -name \*.bats); do
echo "=> $bats_file"
# BATS returns non-zero to indicate the tests have failed, we shouldn't
# necessarily bail in this case, so that's the reason for the e toggle.
set +e
bats "$bats_file"
if [[ $? -ne 0 ]]; then
EXIT_STATUS=1
fi
set -e
echo
if [[ "$NO_SHARE_MACHINES" == "1" ]]; then
cleanup_machines "ALL"
else
cleanup_machines "NON-SHARED"
fi
done
}
# Set this ourselves in case bats call fails
EXIT_STATUS=0
export BATS_FILE="$1"
# Check we're not running bash 3.x
if [ "${BASH_VERSINFO[0]}" -lt 4 ]; then
echo "Bash 4.1 or later is required to run these tests"
exit 1
fi
# If bash 4.x, check the minor version is 1 or later
if [ "${BASH_VERSINFO[0]}" -eq 4 ] && [ "${BASH_VERSINFO[1]}" -lt 1 ]; then
echo "Bash 4.1 or later is required to run these tests"
exit 1
fi
if [[ -z "$DRIVER" ]]; then
echo "You must specify the DRIVER environment variable."
exit 1
fi
if [[ -z "$BATS_FILE" ]]; then
echo "You must specify a bats test to run."
exit 1
fi
if [[ ! -e "$BATS_FILE" ]]; then
echo "Requested bats file or directory not found: $BATS_FILE"
exit 1
fi
# TODO: Should the script bail out if these are set already?
export BASE_TEST_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
export MACHINE_ROOT="$BASE_TEST_DIR/../.."
export MACHINE_STORAGE_PATH="/tmp/machine-bats-test-$DRIVER"
export MACHINE_BIN_NAME=docker-machine
export BATS_LOG="$MACHINE_ROOT/bats.log"
export B2D_LOCATION=~/.docker/machine/cache/boot2docker.iso
export SHARED_NAME="bats-$DRIVER-test-shared-$(date +%s)"
export MACHINE_BUGSNAG_API_TOKEN=no-report
# This function gets used in the integration tests, so export it.
export -f machine
> "$BATS_LOG"
cleanup_machines "ALL"
cleanup_store
if [[ -f "$B2D_LOCATION" ]]; then
if [[ "$B2D_CACHE" == "1" ]]; then
mkdir -p "${MACHINE_STORAGE_PATH}/cache"
cp $B2D_LOCATION "${MACHINE_STORAGE_PATH}/cache/boot2docker.iso"
else
echo "INFO: Run the tests with B2D_CACHE=1 to avoid downloading the boot2docker iso each time."
fi
fi
run_bats "$BATS_FILE"
cleanup_machines "ALL"
cleanup_store
exit ${EXIT_STATUS}