Skip to content

Commit 6b88365

Browse files
Merge remote-tracking branch 'upstream/master'
2 parents 74fb2ac + 6adc04b commit 6b88365

File tree

8 files changed

+94
-31
lines changed

8 files changed

+94
-31
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
.PHONY : test
33

44
update-dependencies:
5-
docker pull jwilder/docker-gen:latest
6-
docker pull nginx:latest
5+
docker pull jwilder/docker-gen:0.7.0
6+
docker pull nginx:1.9.12
77
docker pull python:3
88
docker pull rancher/socat-docker:latest
99
docker pull appropriate/curl:latest

test/default-host.bats

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@ load test_helpers
44
function setup {
55
# make sure to stop any web container before each test so we don't
66
# have any unexpected contaiener running with VIRTUAL_HOST or VIRUTAL_PORT set
7-
CIDS=( $(docker ps -q --filter "label=bats-type=web") )
8-
if [ ${#CIDS[@]} -gt 0 ]; then
9-
docker stop ${CIDS[@]} >&2
10-
fi
7+
stop_bats_containers web
118
}
129

1310

@@ -20,7 +17,7 @@ function setup {
2017
# WHEN nginx-proxy runs with DEFAULT_HOST set to web.bats
2118
run nginxproxy $SUT_CONTAINER -v /var/run/docker.sock:/tmp/docker.sock:ro -e DEFAULT_HOST=web.bats
2219
assert_success
23-
docker_wait_for_log $SUT_CONTAINER 3 "Watching docker events"
20+
docker_wait_for_log $SUT_CONTAINER 9 "Watching docker events"
2421

2522
# THEN querying the proxy without Host header → 200
2623
run curl_container $SUT_CONTAINER / --head
@@ -30,3 +27,7 @@ function setup {
3027
run curl_container $SUT_CONTAINER / --head --header "Host: something.I.just.made.up"
3128
assert_output -l 0 $'HTTP/1.1 200 OK\r'
3229
}
30+
31+
@test "[$TEST_FILE] stop all bats containers" {
32+
stop_bats_containers
33+
}

test/docker.bats

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ load test_helpers
1414
# WHEN nginx-proxy runs on our docker host using the default unix socket
1515
run nginxproxy $SUT_CONTAINER -v /var/run/docker.sock:/tmp/docker.sock:ro
1616
assert_success
17-
docker_wait_for_log $SUT_CONTAINER 3 "Watching docker events"
17+
docker_wait_for_log $SUT_CONTAINER 9 "Watching docker events"
1818

1919
# THEN
2020
assert_nginxproxy_behaves $SUT_CONTAINER
@@ -27,7 +27,7 @@ load test_helpers
2727
# WHEN nginx-proxy runs on our docker host using a custom unix socket
2828
run nginxproxy $SUT_CONTAINER -v /var/run/docker.sock:/f00.sock:ro -e DOCKER_HOST=unix:///f00.sock
2929
assert_success
30-
docker_wait_for_log $SUT_CONTAINER 3 "Watching docker events"
30+
docker_wait_for_log $SUT_CONTAINER 9 "Watching docker events"
3131

3232
# THEN
3333
assert_nginxproxy_behaves $SUT_CONTAINER
@@ -44,7 +44,7 @@ load test_helpers
4444
# WHEN nginx-proxy runs on our docker host using tcp to connect to our docker host
4545
run nginxproxy $SUT_CONTAINER -e DOCKER_HOST="tcp://bats-docker-tcp:2375" --link bats-docker-tcp:bats-docker-tcp
4646
assert_success
47-
docker_wait_for_log $SUT_CONTAINER 3 "Watching docker events"
47+
docker_wait_for_log $SUT_CONTAINER 9 "Watching docker events"
4848

4949
# THEN
5050
assert_nginxproxy_behaves $SUT_CONTAINER
@@ -57,28 +57,30 @@ load test_helpers
5757

5858
# GIVEN a simple nginx container
5959
run docker run -d \
60+
--label bats-type="nginx" \
6061
--name bats-nginx \
6162
-v /etc/nginx/conf.d/ \
6263
-v /etc/nginx/certs/ \
6364
nginx:latest
6465
assert_success
65-
run retry 5 1s docker run appropriate/curl --silent --fail --head http://$(docker_ip bats-nginx)/
66+
run retry 5 1s docker run --label bats-type="curl" appropriate/curl --silent --fail --head http://$(docker_ip bats-nginx)/
6667
assert_output -l 0 $'HTTP/1.1 200 OK\r'
6768

6869
# WHEN docker-gen runs on our docker host
6970
run docker run -d \
71+
--label bats-type="docker-gen" \
7072
--name bats-docker-gen \
7173
-v /var/run/docker.sock:/tmp/docker.sock:ro \
7274
-v $BATS_TEST_DIRNAME/../nginx.tmpl:/etc/docker-gen/templates/nginx.tmpl:ro \
7375
--volumes-from bats-nginx \
74-
jwilder/docker-gen:latest \
76+
jwilder/docker-gen:0.7.0 \
7577
-notify-sighup bats-nginx \
7678
-watch \
7779
-only-exposed \
7880
/etc/docker-gen/templates/nginx.tmpl \
7981
/etc/nginx/conf.d/default.conf
8082
assert_success
81-
docker_wait_for_log bats-docker-gen 6 "Watching docker events"
83+
docker_wait_for_log bats-docker-gen 9 "Watching docker events"
8284

8385
# Give some time to the docker-gen container to notify bats-nginx so it
8486
# reloads its config
@@ -94,6 +96,10 @@ load test_helpers
9496
assert_nginxproxy_behaves bats-nginx
9597
}
9698

99+
@test "[$TEST_FILE] stop all bats containers" {
100+
stop_bats_containers
101+
}
102+
97103

98104
# $1 nginx-proxy container
99105
function assert_nginxproxy_behaves {

test/lib/docker_helpers.bash

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ function docker_ip {
1313
docker inspect --format '{{ .NetworkSettings.IPAddress }}' $1
1414
}
1515

16+
# get the ip of docker container $1
17+
function docker_id {
18+
docker inspect --format '{{ .ID }}' $1
19+
}
20+
1621
# get the running state of container $1
1722
# → true/false
1823
# fails if the container does not exist
@@ -52,9 +57,10 @@ function docker_tcp {
5257
local container_name="$1"
5358
docker_clean $container_name
5459
docker run -d \
60+
--label bats-type="socat" \
5561
--name $container_name \
5662
--expose 2375 \
5763
-v /var/run/docker.sock:/var/run/docker.sock \
5864
rancher/socat-docker
59-
docker run --link "$container_name:docker" docker:1.9 version
65+
docker run --label bats-type="docker" --link "$container_name:docker" docker:1.9 version
6066
}

test/multiple-hosts.bats

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,21 @@ SUT_CONTAINER=bats-nginx-proxy-${TEST_FILE}
55
function setup {
66
# make sure to stop any web container before each test so we don't
77
# have any unexpected contaiener running with VIRTUAL_HOST or VIRUTAL_PORT set
8-
CIDS=( $(docker ps -q --filter "label=bats-type=web") )
9-
if [ ${#CIDS[@]} -gt 0 ]; then
10-
docker stop ${CIDS[@]} >&2
11-
fi
8+
stop_bats_containers web
129
}
1310

1411

1512
@test "[$TEST_FILE] start a nginx-proxy container" {
1613
run nginxproxy $SUT_CONTAINER -v /var/run/docker.sock:/tmp/docker.sock:ro
1714
assert_success
18-
docker_wait_for_log $SUT_CONTAINER 3 "Watching docker events"
15+
docker_wait_for_log $SUT_CONTAINER 9 "Watching docker events"
1916
}
2017

2118
@test "[$TEST_FILE] nginx-proxy forwards requests for 2 hosts" {
2219
# WHEN a container runs a web server with VIRTUAL_HOST set for multiple hosts
2320
prepare_web_container bats-multiple-hosts-1 80 -e VIRTUAL_HOST=multiple-hosts-1-A.bats,multiple-hosts-1-B.bats
21+
dockergen_wait_for_event $SUT_CONTAINER start bats-multiple-hosts-1
22+
sleep 1
2423

2524
# THEN querying the proxy without Host header → 503
2625
run curl_container $SUT_CONTAINER / --head
@@ -38,3 +37,7 @@ function setup {
3837
run curl_container $SUT_CONTAINER /data --header 'Host: multiple-hosts-1-B.bats'
3938
assert_output "answer from port 80"
4039
}
40+
41+
@test "[$TEST_FILE] stop all bats containers" {
42+
stop_bats_containers
43+
}

test/multiple-ports.bats

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,23 @@ SUT_CONTAINER=bats-nginx-proxy-${TEST_FILE}
55
function setup {
66
# make sure to stop any web container before each test so we don't
77
# have any unexpected contaiener running with VIRTUAL_HOST or VIRUTAL_PORT set
8-
CIDS=( $(docker ps -q --filter "label=bats-type=web") )
9-
if [ ${#CIDS[@]} -gt 0 ]; then
10-
docker stop ${CIDS[@]} >&2
11-
fi
8+
stop_bats_containers web
129
}
1310

1411

1512
@test "[$TEST_FILE] start a nginx-proxy container" {
1613
# GIVEN nginx-proxy
1714
run nginxproxy $SUT_CONTAINER -v /var/run/docker.sock:/tmp/docker.sock:ro
1815
assert_success
19-
docker_wait_for_log $SUT_CONTAINER 3 "Watching docker events"
16+
docker_wait_for_log $SUT_CONTAINER 9 "Watching docker events"
2017
}
2118

2219

2320
@test "[$TEST_FILE] nginx-proxy defaults to the service running on port 80" {
2421
# WHEN
2522
prepare_web_container bats-web-${TEST_FILE}-1 "80 90" -e VIRTUAL_HOST=web.bats
23+
dockergen_wait_for_event $SUT_CONTAINER start bats-web-${TEST_FILE}-1
24+
sleep 1
2625

2726
# THEN
2827
assert_response_is_from_port 80
@@ -32,6 +31,8 @@ function setup {
3231
@test "[$TEST_FILE] VIRTUAL_PORT=90 while port 80 is also exposed" {
3332
# GIVEN
3433
prepare_web_container bats-web-${TEST_FILE}-2 "80 90" -e VIRTUAL_HOST=web.bats -e VIRTUAL_PORT=90
34+
dockergen_wait_for_event $SUT_CONTAINER start bats-web-${TEST_FILE}-2
35+
sleep 1
3536

3637
# THEN
3738
assert_response_is_from_port 90
@@ -41,11 +42,17 @@ function setup {
4142
@test "[$TEST_FILE] single exposed port != 80" {
4243
# GIVEN
4344
prepare_web_container bats-web-${TEST_FILE}-3 1234 -e VIRTUAL_HOST=web.bats
45+
dockergen_wait_for_event $SUT_CONTAINER start bats-web-${TEST_FILE}-3
46+
sleep 1
4447

4548
# THEN
4649
assert_response_is_from_port 1234
4750
}
4851

52+
@test "[$TEST_FILE] stop all bats containers" {
53+
stop_bats_containers
54+
}
55+
4956

5057
# assert querying nginx-proxy provides a response from the expected port of the web container
5158
# $1 port we are expecting an response from

test/test_helpers.bash

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ function nginxproxy {
3333
shift
3434
docker_clean $container_name \
3535
&& docker run -d \
36+
--label bats-type="nginx-proxy" \
3637
--name $container_name \
3738
"$@" \
3839
$SUT_IMAGE \
@@ -66,7 +67,7 @@ function curl_container {
6667
local -r container=$1
6768
local -r path=$2
6869
shift 2
69-
docker run appropriate/curl --silent \
70+
docker run --label bats-type="curl" appropriate/curl --silent \
7071
--connect-timeout 5 \
7172
--max-time 20 \
7273
"$@" \
@@ -128,7 +129,39 @@ function prepare_web_container {
128129
# THEN querying directly port works
129130
IFS=$' \t\n' # See https://github.com/sstephenson/bats/issues/89
130131
for port in $ports; do
131-
run retry 5 1s docker run appropriate/curl --silent --fail http://$(docker_ip $container_name):$port/data
132+
run retry 5 1s docker run --label bats-type="curl" appropriate/curl --silent --fail http://$(docker_ip $container_name):$port/data
132133
assert_output "answer from port $port"
133134
done
134135
}
136+
137+
# stop all containers with the "bats-type" label (matching the optionally supplied value)
138+
#
139+
# $1 optional label value
140+
function stop_bats_containers {
141+
local -r value=$1
142+
143+
if [ -z "$value" ]; then
144+
CIDS=( $(docker ps -q --filter "label=bats-type") )
145+
else
146+
CIDS=( $(docker ps -q --filter "label=bats-type=$value") )
147+
fi
148+
149+
if [ ${#CIDS[@]} -gt 0 ]; then
150+
docker stop ${CIDS[@]} >&2
151+
fi
152+
}
153+
154+
# wait for a docker-gen container to receive a specified event from a
155+
# container with the specified ID/name
156+
#
157+
# $1 docker-gen container name
158+
# $2 event
159+
# $3 ID/name of container to receive event from
160+
function dockergen_wait_for_event {
161+
local -r container=$1
162+
local -r event=$2
163+
local -r other=$3
164+
local -r did=$(docker_id "$other")
165+
docker_wait_for_log "$container" 9 "Received event $event for container ${did:0:12}"
166+
}
167+

test/wildcard-hosts.bats

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,23 @@ SUT_CONTAINER=bats-nginx-proxy-${TEST_FILE}
55
function setup {
66
# make sure to stop any web container before each test so we don't
77
# have any unexpected contaiener running with VIRTUAL_HOST or VIRUTAL_PORT set
8-
CIDS=( $(docker ps -q --filter "label=bats-type=web") )
9-
if [ ${#CIDS[@]} -gt 0 ]; then
10-
docker stop ${CIDS[@]} >&2
11-
fi
8+
stop_bats_containers web
129
}
1310

1411

1512
@test "[$TEST_FILE] start a nginx-proxy container" {
1613
# GIVEN
1714
run nginxproxy $SUT_CONTAINER -v /var/run/docker.sock:/tmp/docker.sock:ro
1815
assert_success
19-
docker_wait_for_log $SUT_CONTAINER 3 "Watching docker events"
16+
docker_wait_for_log $SUT_CONTAINER 9 "Watching docker events"
2017
}
2118

2219

2320
@test "[$TEST_FILE] VIRTUAL_HOST=*.wildcard.bats" {
2421
# WHEN
2522
prepare_web_container bats-wildcard-hosts-1 80 -e VIRTUAL_HOST=*.wildcard.bats
23+
dockergen_wait_for_event $SUT_CONTAINER start bats-wildcard-hosts-1
24+
sleep 1
2625

2726
# THEN
2827
assert_200 f00.wildcard.bats
@@ -33,6 +32,8 @@ function setup {
3332
@test "[$TEST_FILE] VIRTUAL_HOST=wildcard.bats.*" {
3433
# WHEN
3534
prepare_web_container bats-wildcard-hosts-2 80 -e VIRTUAL_HOST=wildcard.bats.*
35+
dockergen_wait_for_event $SUT_CONTAINER start bats-wildcard-hosts-2
36+
sleep 1
3637

3738
# THEN
3839
assert_200 wildcard.bats.f00
@@ -43,6 +44,8 @@ function setup {
4344
@test "[$TEST_FILE] VIRTUAL_HOST=~^foo\.bar\..*\.bats" {
4445
# WHEN
4546
prepare_web_container bats-wildcard-hosts-2 80 -e VIRTUAL_HOST=~^foo\.bar\..*\.bats
47+
dockergen_wait_for_event $SUT_CONTAINER start bats-wildcard-hosts-2
48+
sleep 1
4649

4750
# THEN
4851
assert_200 foo.bar.whatever.bats
@@ -51,6 +54,10 @@ function setup {
5154

5255
}
5356

57+
@test "[$TEST_FILE] stop all bats containers" {
58+
stop_bats_containers
59+
}
60+
5461

5562
# assert that querying nginx-proxy with the given Host header produces a `HTTP 200` response
5663
# $1 Host HTTP header to use when querying nginx-proxy

0 commit comments

Comments
 (0)