Merge pull request #257 from appropriate/osx-fix-test-suite

Fix running test suite under Docker Machine
This commit is contained in:
Jason Wilder 2015-10-12 22:43:19 -06:00
commit f819a4e2e7
8 changed files with 146 additions and 131 deletions

View file

@ -14,8 +14,10 @@ dependencies:
- docker pull nginx - docker pull nginx
- docker pull python:3 - docker pull python:3
- docker pull rancher/socat-docker - docker pull rancher/socat-docker
- docker pull appropriate/curl
- docker pull docker:1.7
test: test:
override: override:
- docker build -t jwilder/nginx-proxy:bats . - docker build -t jwilder/nginx-proxy:bats .
- bats test - bats test

View file

@ -4,7 +4,10 @@ load test_helpers
function setup { function setup {
# make sure to stop any web container before each test so we don't # make sure to stop any web container before each test so we don't
# have any unexpected contaiener running with VIRTUAL_HOST or VIRUTAL_PORT set # have any unexpected contaiener running with VIRTUAL_HOST or VIRUTAL_PORT set
docker ps -q --filter "label=bats-type=web" | xargs -r docker stop >&2 CIDS=( $(docker ps -q --filter "label=bats-type=web") )
if [ ${#CIDS[@]} -gt 0 ]; then
docker stop ${CIDS[@]} >&2
fi
} }

View file

@ -62,7 +62,7 @@ load test_helpers
-v /etc/nginx/certs/ \ -v /etc/nginx/certs/ \
nginx:latest nginx:latest
assert_success assert_success
run retry 5 1s curl --silent --fail --head http://$(docker_ip bats-nginx)/ run retry 5 1s docker run appropriate/curl --silent --fail --head http://$(docker_ip bats-nginx)/
assert_output -l 0 $'HTTP/1.1 200 OK\r' assert_output -l 0 $'HTTP/1.1 200 OK\r'
# WHEN docker-gen runs on our docker host # WHEN docker-gen runs on our docker host

View file

@ -56,5 +56,5 @@ function docker_tcp {
--expose 2375 \ --expose 2375 \
-v /var/run/docker.sock:/var/run/docker.sock \ -v /var/run/docker.sock:/var/run/docker.sock \
rancher/socat-docker rancher/socat-docker
docker -H tcp://$(docker_ip $container_name):2375 version docker run --link "$container_name:docker" docker:1.7 version
} }

View file

@ -5,7 +5,10 @@ SUT_CONTAINER=bats-nginx-proxy-${TEST_FILE}
function setup { function setup {
# make sure to stop any web container before each test so we don't # make sure to stop any web container before each test so we don't
# have any unexpected contaiener running with VIRTUAL_HOST or VIRUTAL_PORT set # have any unexpected contaiener running with VIRTUAL_HOST or VIRUTAL_PORT set
docker ps -q --filter "label=bats-type=web" | xargs -r docker stop >&2 CIDS=( $(docker ps -q --filter "label=bats-type=web") )
if [ ${#CIDS[@]} -gt 0 ]; then
docker stop ${CIDS[@]} >&2
fi
} }

View file

@ -1,54 +1,57 @@
#!/usr/bin/env bats #!/usr/bin/env bats
load test_helpers load test_helpers
SUT_CONTAINER=bats-nginx-proxy-${TEST_FILE} SUT_CONTAINER=bats-nginx-proxy-${TEST_FILE}
function setup { function setup {
# make sure to stop any web container before each test so we don't # make sure to stop any web container before each test so we don't
# have any unexpected contaiener running with VIRTUAL_HOST or VIRUTAL_PORT set # have any unexpected contaiener running with VIRTUAL_HOST or VIRUTAL_PORT set
docker ps -q --filter "label=bats-type=web" | xargs -r docker stop >&2 CIDS=( $(docker ps -q --filter "label=bats-type=web") )
} if [ ${#CIDS[@]} -gt 0 ]; then
docker stop ${CIDS[@]} >&2
fi
@test "[$TEST_FILE] start a nginx-proxy container" { }
# GIVEN nginx-proxy
run nginxproxy $SUT_CONTAINER -v /var/run/docker.sock:/tmp/docker.sock:ro
assert_success @test "[$TEST_FILE] start a nginx-proxy container" {
docker_wait_for_log $SUT_CONTAINER 3 "Watching docker events" # GIVEN nginx-proxy
} run nginxproxy $SUT_CONTAINER -v /var/run/docker.sock:/tmp/docker.sock:ro
assert_success
docker_wait_for_log $SUT_CONTAINER 3 "Watching docker events"
@test "[$TEST_FILE] nginx-proxy defaults to the service running on port 80" { }
# WHEN
prepare_web_container bats-web-${TEST_FILE}-1 "80 90" -e VIRTUAL_HOST=web.bats
@test "[$TEST_FILE] nginx-proxy defaults to the service running on port 80" {
# THEN # WHEN
assert_response_is_from_port 80 prepare_web_container bats-web-${TEST_FILE}-1 "80 90" -e VIRTUAL_HOST=web.bats
}
# THEN
assert_response_is_from_port 80
@test "[$TEST_FILE] VIRTUAL_PORT=90 while port 80 is also exposed" { }
# GIVEN
prepare_web_container bats-web-${TEST_FILE}-2 "80 90" -e VIRTUAL_HOST=web.bats -e VIRTUAL_PORT=90
@test "[$TEST_FILE] VIRTUAL_PORT=90 while port 80 is also exposed" {
# THEN # GIVEN
assert_response_is_from_port 90 prepare_web_container bats-web-${TEST_FILE}-2 "80 90" -e VIRTUAL_HOST=web.bats -e VIRTUAL_PORT=90
}
# THEN
assert_response_is_from_port 90
@test "[$TEST_FILE] single exposed port != 80" { }
# GIVEN
prepare_web_container bats-web-${TEST_FILE}-3 1234 -e VIRTUAL_HOST=web.bats
@test "[$TEST_FILE] single exposed port != 80" {
# THEN # GIVEN
assert_response_is_from_port 1234 prepare_web_container bats-web-${TEST_FILE}-3 1234 -e VIRTUAL_HOST=web.bats
}
# THEN
assert_response_is_from_port 1234
# assert querying nginx-proxy provides a response from the expected port of the web container }
# $1 port we are expecting an response from
function assert_response_is_from_port {
local -r port=$1 # assert querying nginx-proxy provides a response from the expected port of the web container
run curl_container $SUT_CONTAINER /data --header "Host: web.bats" # $1 port we are expecting an response from
assert_output "answer from port $port" function assert_response_is_from_port {
} local -r port=$1
run curl_container $SUT_CONTAINER /data --header "Host: web.bats"
assert_output "answer from port $port"
}

View file

@ -1,7 +1,6 @@
# Test if requirements are met # Test if requirements are met
( (
type docker &>/dev/null || ( echo "docker is not available"; exit 1 ) type docker &>/dev/null || ( echo "docker is not available"; exit 1 )
type curl &>/dev/null || ( echo "curl is not available"; exit 1 )
)>&2 )>&2
@ -67,7 +66,7 @@ function curl_container {
local -r container=$1 local -r container=$1
local -r path=$2 local -r path=$2
shift 2 shift 2
curl --silent \ docker run appropriate/curl --silent \
--connect-timeout 5 \ --connect-timeout 5 \
--max-time 20 \ --max-time 20 \
"$@" \ "$@" \
@ -87,6 +86,7 @@ function prepare_web_container {
local -r options="$@" local -r options="$@"
local expose_option="" local expose_option=""
IFS=$' \t\n' # See https://github.com/sstephenson/bats/issues/89
for port in $ports; do for port in $ports; do
expose_option="${expose_option}--expose=$port " expose_option="${expose_option}--expose=$port "
done done
@ -121,8 +121,9 @@ function prepare_web_container {
assert_success assert_success
# THEN querying directly port works # THEN querying directly port works
IFS=$' \t\n' # See https://github.com/sstephenson/bats/issues/89
for port in $ports; do for port in $ports; do
run retry 5 1s curl --silent --fail http://$(docker_ip $container_name):$port/data run retry 5 1s docker run appropriate/curl --silent --fail http://$(docker_ip $container_name):$port/data
assert_output "answer from port $port" assert_output "answer from port $port"
done done
} }

View file

@ -1,68 +1,71 @@
#!/usr/bin/env bats #!/usr/bin/env bats
load test_helpers load test_helpers
SUT_CONTAINER=bats-nginx-proxy-${TEST_FILE} SUT_CONTAINER=bats-nginx-proxy-${TEST_FILE}
function setup { function setup {
# make sure to stop any web container before each test so we don't # make sure to stop any web container before each test so we don't
# have any unexpected contaiener running with VIRTUAL_HOST or VIRUTAL_PORT set # have any unexpected contaiener running with VIRTUAL_HOST or VIRUTAL_PORT set
docker ps -q --filter "label=bats-type=web" | xargs -r docker stop >&2 CIDS=( $(docker ps -q --filter "label=bats-type=web") )
} if [ ${#CIDS[@]} -gt 0 ]; then
docker stop ${CIDS[@]} >&2
fi
@test "[$TEST_FILE] start a nginx-proxy container" { }
# GIVEN
run nginxproxy $SUT_CONTAINER -v /var/run/docker.sock:/tmp/docker.sock:ro
assert_success @test "[$TEST_FILE] start a nginx-proxy container" {
docker_wait_for_log $SUT_CONTAINER 3 "Watching docker events" # GIVEN
} run nginxproxy $SUT_CONTAINER -v /var/run/docker.sock:/tmp/docker.sock:ro
assert_success
docker_wait_for_log $SUT_CONTAINER 3 "Watching docker events"
@test "[$TEST_FILE] VIRTUAL_HOST=*.wildcard.bats" { }
# WHEN
prepare_web_container bats-wildcard-hosts-1 80 -e VIRTUAL_HOST=*.wildcard.bats
@test "[$TEST_FILE] VIRTUAL_HOST=*.wildcard.bats" {
# THEN # WHEN
assert_200 f00.wildcard.bats prepare_web_container bats-wildcard-hosts-1 80 -e VIRTUAL_HOST=*.wildcard.bats
assert_200 bar.wildcard.bats
assert_503 unexpected.host.bats # THEN
} assert_200 f00.wildcard.bats
assert_200 bar.wildcard.bats
@test "[$TEST_FILE] VIRTUAL_HOST=wildcard.bats.*" { assert_503 unexpected.host.bats
# WHEN }
prepare_web_container bats-wildcard-hosts-2 80 -e VIRTUAL_HOST=wildcard.bats.*
@test "[$TEST_FILE] VIRTUAL_HOST=wildcard.bats.*" {
# THEN # WHEN
assert_200 wildcard.bats.f00 prepare_web_container bats-wildcard-hosts-2 80 -e VIRTUAL_HOST=wildcard.bats.*
assert_200 wildcard.bats.bar
assert_503 unexpected.host.bats # THEN
} assert_200 wildcard.bats.f00
assert_200 wildcard.bats.bar
@test "[$TEST_FILE] VIRTUAL_HOST=~^foo\.bar\..*\.bats" { assert_503 unexpected.host.bats
# WHEN }
prepare_web_container bats-wildcard-hosts-2 80 -e VIRTUAL_HOST=~^foo\.bar\..*\.bats
@test "[$TEST_FILE] VIRTUAL_HOST=~^foo\.bar\..*\.bats" {
# THEN # WHEN
assert_200 foo.bar.whatever.bats prepare_web_container bats-wildcard-hosts-2 80 -e VIRTUAL_HOST=~^foo\.bar\..*\.bats
assert_200 foo.bar.why.not.bats
assert_503 unexpected.host.bats # THEN
assert_200 foo.bar.whatever.bats
} assert_200 foo.bar.why.not.bats
assert_503 unexpected.host.bats
# assert that querying nginx-proxy with the given Host header produces a `HTTP 200` response }
# $1 Host HTTP header to use when querying nginx-proxy
function assert_200 {
local -r host=$1 # assert that querying nginx-proxy with the given Host header produces a `HTTP 200` response
# $1 Host HTTP header to use when querying nginx-proxy
run curl_container $SUT_CONTAINER / --head --header "Host: $host" function assert_200 {
assert_output -l 0 $'HTTP/1.1 200 OK\r' local -r host=$1
}
run curl_container $SUT_CONTAINER / --head --header "Host: $host"
# assert that querying nginx-proxy with the given Host header produces a `HTTP 503` response assert_output -l 0 $'HTTP/1.1 200 OK\r'
# $1 Host HTTP header to use when querying nginx-proxy }
function assert_503 {
local -r host=$1 # assert that querying nginx-proxy with the given Host header produces a `HTTP 503` response
# $1 Host HTTP header to use when querying nginx-proxy
run curl_container $SUT_CONTAINER / --head --header "Host: $host" function assert_503 {
assert_output -l 0 $'HTTP/1.1 503 Service Temporarily Unavailable\r' local -r host=$1
}
run curl_container $SUT_CONTAINER / --head --header "Host: $host"
assert_output -l 0 $'HTTP/1.1 503 Service Temporarily Unavailable\r'
}