X Tutup
Skip to content

Commit a708bf7

Browse files
committed
Cleanup machines only at the end
Signed-off-by: David Gageot <david@gageot.net>
1 parent 17b1e57 commit a708bf7

16 files changed

+77
-52
lines changed

test/integration/core/certs-extra-san.bats

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
load ${BASE_TEST_DIR}/helpers.bash
44

5+
if [[ -z "$NAME" ]]; then
6+
export NAME="$(unique_machine_name)"
7+
fi
58

69
@test "$DRIVER: create" {
710
run machine create --tls-san foo.bar.tld --tls-san 10.42.42.42 -d $DRIVER $NAME

test/integration/core/core-commands.bats

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,15 @@
22

33
load ${BASE_TEST_DIR}/helpers.bash
44

5+
if [[ -z "$NAME" ]]; then
6+
export NAME="$(shared_machine_name)"
7+
fi
8+
59
@test "$DRIVER: machine should not exist" {
6-
run machine inspect $NAME
10+
run machine inspect UNKNOWN
711
echo ${output}
812
[ "$status" -eq 1 ]
9-
[[ ${lines[0]} =~ "Host does not exist: \"$NAME\"" ]]
10-
}
11-
12-
@test "$DRIVER: create" {
13-
run machine create -d $DRIVER $NAME
14-
echo ${output}
15-
[ "$status" -eq 0 ]
13+
[[ ${lines[0]} =~ "Host does not exist: \"UNKNOWN\"" ]]
1614
}
1715

1816
@test "$DRIVER: appears with ls" {

test/integration/core/engine-options.bats

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
load ${BASE_TEST_DIR}/helpers.bash
44

5+
if [[ -z "$NAME" ]]; then
6+
export NAME="$(unique_machine_name)"
7+
fi
8+
59
@test "$DRIVER: create with supported engine options" {
610
run machine create -d $DRIVER \
711
--engine-label spam=eggs \

test/integration/core/env_shell.bats

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22

33
load ${BASE_TEST_DIR}/helpers.bash
44

5-
@test "$DRIVER: create" {
6-
run machine create -d $DRIVER $NAME
7-
[ "$status" -eq 0 ]
8-
}
5+
if [[ -z "$NAME" ]]; then
6+
export NAME="$(shared_machine_name)"
7+
fi
98

109
@test "$DRIVER: test basic bash / zsh notation" {
1110
run machine env $NAME

test/integration/core/inspect_format.bats

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22

33
load ${BASE_TEST_DIR}/helpers.bash
44

5-
@test "$DRIVER: create" {
6-
run machine create -d $DRIVER $NAME
7-
[ "$status" -eq 0 ]
8-
}
5+
if [[ -z "$NAME" ]]; then
6+
export NAME="$(shared_machine_name)"
7+
fi
98

109
@test "$DRIVER: inspect format template" {
1110
run machine inspect -f '{{.DriverName}}' $NAME

test/integration/core/regenerate-certs.bats

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
load ${BASE_TEST_DIR}/helpers.bash
44

5-
@test "$DRIVER: create" {
6-
run machine create -d $DRIVER $NAME
7-
}
5+
if [[ -z "$NAME" ]]; then
6+
export NAME="$(shared_machine_name)"
7+
fi
88

99
@test "$DRIVER: regenerate the certs" {
1010
run machine regenerate-certs -f $NAME

test/integration/core/scp.bats

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22

33
load ${BASE_TEST_DIR}/helpers.bash
44

5-
export SECOND_MACHINE="$NAME-2"
6-
7-
@test "$DRIVER: create" {
8-
run machine create -d $DRIVER $NAME
9-
[[ ${status} -eq 0 ]]
10-
}
5+
if [[ -z "$NAME" ]]; then
6+
export NAME="$(shared_machine_name)"
7+
export SECOND_MACHINE="$NAME-2"
8+
fi
119

1210
@test "$DRIVER: test machine scp command from remote to host" {
1311
machine ssh $NAME 'echo A file created remotely! >/tmp/foo.txt'

test/integration/core/ssh-backends.bats

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,9 @@
22

33
load ${BASE_TEST_DIR}/helpers.bash
44

5-
# Basic smoke test for SSH backends
6-
7-
@test "$DRIVER: create SSH test box" {
8-
run machine create -d $DRIVER $NAME
9-
[[ "$status" -eq 0 ]]
10-
}
5+
if [[ -z "$NAME" ]]; then
6+
export NAME="$(shared_machine_name)"
7+
fi
118

129
@test "$DRIVER: test external ssh backend" {
1310
run machine ssh $NAME df -h

test/integration/helpers.bash

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/bash
22

3-
echo_to_log() {
3+
function echo_to_log {
44
echo "$BATS_TEST_NAME
55
----------
66
$output
@@ -9,27 +9,38 @@ $output
99
" >> ${BATS_LOG}
1010
}
1111

12-
teardown() {
12+
function teardown {
1313
echo_to_log
1414
}
1515

16-
function errecho () {
16+
function errecho {
1717
>&2 echo "$@"
1818
}
1919

20-
function only_if_env () {
20+
function only_if_env {
2121
if [[ ${!1} != "$2" ]]; then
2222
errecho "This test requires the $1 environment variable to be set to $2. Skipping..."
2323
skip
2424
fi
2525
}
2626

27-
function require_env () {
27+
function require_env {
2828
if [[ -z ${!1} ]]; then
2929
errecho "This test requires the $1 environment variable to be set in order to run."
3030
exit 1
3131
fi
3232
}
3333

34+
function unique_machine_name {
35+
echo "bats-$DRIVER-test-$(date +%s)"
36+
}
37+
38+
function shared_machine_name {
39+
if [[ $(machine ls -q --filter name=$SHARED_NAME | wc -l) -eq 0 ]]; then
40+
machine create -d $DRIVER $SHARED_NAME &>/dev/null
41+
fi
42+
echo "$SHARED_NAME"
43+
}
44+
3445
# Make sure these aren't set while tests run (can cause confusing behavior)
3546
unset DOCKER_HOST DOCKER_TLS_VERIFY DOCKER_CERT_DIR

test/integration/run-bats.sh

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ function quiet_run () {
1414
}
1515

1616
function cleanup_machines() {
17-
if [[ $(machine ls -q | wc -l) -ne 0 ]]; then
18-
quiet_run machine rm -f $(machine ls -q)
19-
fi
17+
for MACHINE_NAME in $(machine ls -q); do
18+
if [[ "$MACHINE_NAME" != "$SHARED_NAME" ]] || [[ "$1" == "ALL" ]]; then
19+
quiet_run machine rm -f $MACHINE_NAME
20+
fi
21+
done
2022
}
2123

2224
function cleanup_store() {
@@ -26,18 +28,15 @@ function cleanup_store() {
2628
}
2729

2830
function machine() {
29-
export PATH="$MACHINE_ROOT"/bin:$PATH
3031
"$MACHINE_ROOT"/bin/"$MACHINE_BIN_NAME" "$@"
3132
}
3233

3334
function run_bats() {
3435
for bats_file in $(find "$1" -name \*.bats); do
35-
export NAME="bats-$DRIVER-test-$(date +%s)"
36-
37-
# BATS returns non-zero to indicate the tests have failed, we shouldn't
38-
# neccesarily bail in this case, so that's the reason for the e toggle.
3936
echo "=> $bats_file"
4037

38+
# BATS returns non-zero to indicate the tests have failed, we shouldn't
39+
# necessarily bail in this case, so that's the reason for the e toggle.
4140
set +e
4241
bats "$bats_file"
4342
if [[ $? -ne 0 ]]; then
@@ -88,13 +87,14 @@ export MACHINE_STORAGE_PATH="/tmp/machine-bats-test-$DRIVER"
8887
export MACHINE_BIN_NAME=docker-machine
8988
export BATS_LOG="$MACHINE_ROOT/bats.log"
9089
export B2D_LOCATION=~/.docker/machine/cache/boot2docker.iso
90+
export SHARED_NAME="bats-$DRIVER-test-shared-$(date +%s)"
9191

9292
# This function gets used in the integration tests, so export it.
9393
export -f machine
9494

9595
> "$BATS_LOG"
9696

97-
cleanup_machines
97+
cleanup_machines "ALL"
9898
cleanup_store
9999

100100
if [[ -f "$B2D_LOCATION" ]]; then
@@ -108,6 +108,7 @@ fi
108108

109109
run_bats "$BATS_FILE"
110110

111+
cleanup_machines "ALL"
111112
cleanup_store
112113

113114
exit ${EXIT_STATUS}

0 commit comments

Comments
 (0)
X Tutup