Skip to content

Commit b8421e1

Browse files
author
Anfernee Gui
committed
Basic integration test for unix
1 parent b7a89cc commit b8421e1

File tree

5 files changed

+177
-0
lines changed

5 files changed

+177
-0
lines changed

Makefile

+7
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,10 @@ test:
2424
check:
2525
gofmt -l -s -d pkg/ cmd/
2626
go tool vet pkg/ cmd/
27+
28+
.PHONY: integration
29+
integration:
30+
ifeq ($(GOOS),windows)
31+
else
32+
hack/integration.sh
33+
endif

hack/integration.sh

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/bin/bash
2+
3+
set -ex
4+
5+
ROOT=$(git rev-parse --show-toplevel)
6+
7+
check_tool() {
8+
echo "Checking tool $1 ..."
9+
type "$1" >/dev/null || ( echo "error: missing $1" && exit 1 )
10+
}
11+
12+
check_tool bats
13+
check_tool docker-machine
14+
check_tool docker-machine-driver-vmware
15+
16+
bats $ROOT/test/*.bats

test/docker-machine.bats

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/usr/bin/env bats
2+
3+
load test_helper
4+
load env
5+
6+
@test "lifecycle" {
7+
id="vm-$$"
8+
run docker-machine create --driver="${DRIVER}" "${id}"
9+
assert_success
10+
11+
run docker-machine status "${id}"
12+
assert_output "Running"
13+
14+
run docker-machine url "${id}"
15+
assert_matches "^[a-z]+://([0-9]{1,3}\.){3}[0-9]{1,3}:[0-9]+$"
16+
17+
run docker-machine ip "${id}"
18+
assert_matches "^([0-9]{1,3}\.){3}[0-9]{1,3}$"
19+
20+
run docker-machine ssh "$id" ls
21+
assert_matches "boot2docker"
22+
23+
run docker-machine restart "${id}"
24+
assert_success
25+
26+
run docker-machine stop "${id}"
27+
assert_success
28+
29+
run docker-machine status "${id}"
30+
assert_output "Stopped"
31+
32+
run docker-machine start "${id}"
33+
assert_success
34+
35+
run docker-machine kill "${id}"
36+
assert_success
37+
38+
run docker-machine status "${id}"
39+
assert_output "Stopped"
40+
41+
run docker-machine rm "${id}" -f
42+
assert_success
43+
44+
run docker-machine status "${id}"
45+
assert_failure
46+
}

test/env.bash

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DRIVER=vmware

test/test_helper.bash

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# the following helpers are borrowed from the test_helper.bash in https://github.com/sstephenson/rbenv
2+
3+
flunk() {
4+
{ if [ "$#" -eq 0 ]; then cat -
5+
else echo "$@"
6+
fi
7+
} >&2
8+
return 1
9+
}
10+
11+
assert_success() {
12+
if [ "$status" -ne 0 ]; then
13+
flunk "command failed with exit status $status: $output"
14+
elif [ "$#" -gt 0 ]; then
15+
assert_output "$1"
16+
fi
17+
}
18+
19+
assert_failure() {
20+
if [ "$status" -ne 1 ]; then
21+
flunk $(printf "expected failed exit status=1, got status=%d" $status)
22+
elif [ "$#" -gt 0 ]; then
23+
assert_output "$1"
24+
fi
25+
}
26+
27+
assert_equal() {
28+
if [ "$1" != "$2" ]; then
29+
{ echo "expected: $1"
30+
echo "actual: $2"
31+
} | flunk
32+
fi
33+
}
34+
35+
assert_output() {
36+
local expected
37+
if [ $# -eq 0 ]; then expected="$(cat -)"
38+
else expected="$1"
39+
fi
40+
assert_equal "$expected" "$output"
41+
}
42+
43+
assert_matches() {
44+
local pattern="${1}"
45+
local actual="${2}"
46+
47+
if [ $# -eq 1 ]; then
48+
actual="$output"
49+
fi
50+
51+
if ! grep -E -q "${pattern}" <<<"${actual}"; then
52+
{ echo "pattern: ${pattern}"
53+
echo "actual: ${actual}"
54+
} | flunk
55+
fi
56+
}
57+
58+
assert_number() {
59+
assert_matches "^-?[0-9]+$" "$output"
60+
}
61+
62+
assert_empty() {
63+
local actual="${1}"
64+
65+
if [ $# -eq 0 ]; then
66+
actual="$(cat -)"
67+
fi
68+
69+
if [ -n "${actual}" ]; then
70+
{ echo "actual: ${actual}"
71+
} | flunk
72+
fi
73+
}
74+
75+
assert_line() {
76+
if [ "$1" -ge 0 ] 2>/dev/null; then
77+
assert_equal "$2" "$(collapse_ws ${lines[$1]})"
78+
else
79+
local line
80+
for line in "${lines[@]}"; do
81+
if [ "$(collapse_ws $line)" = "$1" ]; then return 0; fi
82+
done
83+
flunk "expected line \`$1'"
84+
fi
85+
}
86+
87+
refute_line() {
88+
if [ "$1" -ge 0 ] 2>/dev/null; then
89+
local num_lines="${#lines[@]}"
90+
if [ "$1" -lt "$num_lines" ]; then
91+
flunk "output has $num_lines lines"
92+
fi
93+
else
94+
local line
95+
for line in "${lines[@]}"; do
96+
if [ "$line" = "$1" ]; then
97+
flunk "expected to not find line \`$line'"
98+
fi
99+
done
100+
fi
101+
}
102+
103+
assert() {
104+
if ! "$@"; then
105+
flunk "failed: $*"
106+
fi
107+
}

0 commit comments

Comments
 (0)