forked from eficode/wait-for
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wait-for.bats
167 lines (124 loc) · 4.34 KB
/
wait-for.bats
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
#!/usr/bin/env bats
@test "throws an error when wget is missing when using http" {
# Run the script in a shell with an empty PATH, such that it's not able to find wget
run sh -c "PATH='' ./wait-for http://google.com"
[ "$status" -ne 0 ]
[ "$output" = "wget command is missing!" ]
}
@test "throws an error when wget is missing when using https" {
# Run the script in a shell with an empty PATH, such that it's not able to find wget
run sh -c "PATH='' ./wait-for https://google.com"
[ "$status" -ne 0 ]
[ "$output" = "wget command is missing!" ]
}
@test "google should be immediately found" {
run ./wait-for google.com:80 -- echo 'success'
[ "$output" = "success" ]
}
@test "nonexistent server should not start command" {
run ./wait-for -t 1 noserver:9999 -- echo 'success'
[ "$status" -ne 0 ]
[ "$output" != "success" ]
}
@test "support condensed option style" {
run ./wait-for -qt1 google.com:80 -- echo 'success'
[ "$output" = "success" ]
}
@test "timeout cannot be negative" {
run ./wait-for -t -1 google.com:80 -- echo 'success'
[ "$status" -ne 0 ]
[ "$output" != "success" ]
}
@test "timeout cannot be empty" {
run ./wait-for -t -- google.com:80 -- echo 'success'
[ "$status" -ne 0 ]
[ "$output" != "success" ]
}
@test "wget timeout zero does not immediately timeout" {
# Arrange
timeout=0 # The timeout we will use to invoke the wait-for command with
delay=5 # The amount of seconds to wait to see if our indefinite timeout works
# Act
# - Invoke non-existing local webserver and record duration
start_time=$(date +%s)
run timeout $delay ./wait-for -t ${timeout} http://localhost/
end_time=$(date +%s)
# Assert
# - Assert that the script should't have printed that it timed-out
[ "$output" != "Operation timed out" ]
# - Expect a non-zero exit code
[ "$status" != 0 ]
# - Assert that wait-for waited at least as long as we we're going to test delaying for.
elapsed=$((end_time - start_time))
[ ${elapsed} -ge ${delay} ]
}
@test "wget timeout does not double" {
timeout=10
cat >delay <<-EOF
#!/usr/bin/env bash
sleep $((timeout + 1))
EOF
chmod +x delay
nc -lk -p 80 -e $(pwd)/delay & ncpid=$!
start_time=$(date +%s)
run ./wait-for -t ${timeout} http://localhost/
end_time=$(date +%s)
kill $ncpid
rm -f delay
[ "$status" != 0 ]
[ "$output" = "Operation timed out" ]
elapsed=$((end_time - start_time))
[ ${elapsed} -ge ${timeout} ]
limit=$((timeout * 3 / 2))
[ ${elapsed} -lt ${limit} ]
}
@test "environment variable HOST should be restored for command invocation" {
HOST=success run ./wait-for -t 1 google.com:80 -- sh -c 'echo "$HOST"'
[ "$output" = "success" ]
}
@test "unset environment variable HOST should be restored as unset for command invocation" {
run ./wait-for -t 1 google.com:80 -- sh -uc 'echo "$HOST"'
[ "$status" -ne 0 ]
[ "$output" != "google.com" ]
}
@test "environment variable PROTOCOL should be restored for command invocation" {
PROTOCOL=success run ./wait-for -t 1 google.com:80 -- sh -c 'echo "$PROTOCOL"'
[ "$output" = "success" ]
}
@test "unset environment variables PROTOCOL should be restored as unset for command invocation" {
run ./wait-for -t 1 google.com:80 -- sh -uc 'echo "$PROTOCOL"'
[ "$status" -ne 0 ]
[ "$output" != "google.com" ]
}
@test "http://duckduckgo.com should be immediately found" {
run ./wait-for http://duckduckgo.com -- echo 'success'
[ "$output" = "success" ]
}
@test "https://duckduckgo.com should be immediately found" {
run ./wait-for https://duckduckgo.com -- echo 'success'
[ "$output" = "success" ]
}
@test "connection error in HTTP test should not start command" {
run ./wait-for -t 1 http://google.com:8080 -- echo 'success'
[ "$status" -ne 0 ]
[ "$output" != "success" ]
}
@test "not found HTTP status should not start command" {
run ./wait-for -t 1 http://google.com/ping -- echo 'success'
[ "$status" -ne 0 ]
[ "$output" != "success" ]
}
@test "--version option returns same version as in package.json" {
expected="$(node -p "require('./package.json').version")"
output="$(./wait-for --version)"
[ "$output" = "$expected" ]
}
@test "--version option returns 0 status code" {
run ./wait-for --version
[ "$status" -eq 0 ]
}
@test "--version response matches shorthand -v" {
long_form="$(./wait-for --version)"
short_form="$(./wait-for -v)"
[ "$long_form" = "$short_form" ]
}