From 5fe9411d8820e07ca2a5031828dd18f60532a894 Mon Sep 17 00:00:00 2001 From: Mike Dillon Date: Fri, 9 Oct 2015 23:07:46 -0700 Subject: [PATCH 1/9] Change line endings to Unix --- test/multiple-ports.bats | 108 +++++++++++++++---------------- test/wildcard-hosts.bats | 136 +++++++++++++++++++-------------------- 2 files changed, 122 insertions(+), 122 deletions(-) diff --git a/test/multiple-ports.bats b/test/multiple-ports.bats index a711056..a8ff314 100644 --- a/test/multiple-ports.bats +++ b/test/multiple-ports.bats @@ -1,54 +1,54 @@ -#!/usr/bin/env bats -load test_helpers -SUT_CONTAINER=bats-nginx-proxy-${TEST_FILE} - -function setup { - # 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 - docker ps -q --filter "label=bats-type=web" | xargs -r docker stop >&2 -} - - -@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 - 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 - - # 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 - - # 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 - - # 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 - run curl_container $SUT_CONTAINER /data --header "Host: web.bats" - assert_output "answer from port $port" -} - +#!/usr/bin/env bats +load test_helpers +SUT_CONTAINER=bats-nginx-proxy-${TEST_FILE} + +function setup { + # 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 + docker ps -q --filter "label=bats-type=web" | xargs -r docker stop >&2 +} + + +@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 + 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 + + # 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 + + # 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 + + # 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 + run curl_container $SUT_CONTAINER /data --header "Host: web.bats" + assert_output "answer from port $port" +} + diff --git a/test/wildcard-hosts.bats b/test/wildcard-hosts.bats index 8242fc1..4e1484f 100644 --- a/test/wildcard-hosts.bats +++ b/test/wildcard-hosts.bats @@ -1,68 +1,68 @@ -#!/usr/bin/env bats -load test_helpers -SUT_CONTAINER=bats-nginx-proxy-${TEST_FILE} - -function setup { - # 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 - docker ps -q --filter "label=bats-type=web" | xargs -r docker stop >&2 -} - - -@test "[$TEST_FILE] start a nginx-proxy container" { - # 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 - - # THEN - assert_200 f00.wildcard.bats - assert_200 bar.wildcard.bats - assert_503 unexpected.host.bats -} - -@test "[$TEST_FILE] VIRTUAL_HOST=wildcard.bats.*" { - # WHEN - prepare_web_container bats-wildcard-hosts-2 80 -e VIRTUAL_HOST=wildcard.bats.* - - # THEN - assert_200 wildcard.bats.f00 - assert_200 wildcard.bats.bar - assert_503 unexpected.host.bats -} - -@test "[$TEST_FILE] VIRTUAL_HOST=~^foo\.bar\..*\.bats" { - # WHEN - prepare_web_container bats-wildcard-hosts-2 80 -e VIRTUAL_HOST=~^foo\.bar\..*\.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 - - run curl_container $SUT_CONTAINER / --head --header "Host: $host" - assert_output -l 0 $'HTTP/1.1 200 OK\r' -} - -# 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 -function assert_503 { - 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' -} \ No newline at end of file +#!/usr/bin/env bats +load test_helpers +SUT_CONTAINER=bats-nginx-proxy-${TEST_FILE} + +function setup { + # 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 + docker ps -q --filter "label=bats-type=web" | xargs -r docker stop >&2 +} + + +@test "[$TEST_FILE] start a nginx-proxy container" { + # 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 + + # THEN + assert_200 f00.wildcard.bats + assert_200 bar.wildcard.bats + assert_503 unexpected.host.bats +} + +@test "[$TEST_FILE] VIRTUAL_HOST=wildcard.bats.*" { + # WHEN + prepare_web_container bats-wildcard-hosts-2 80 -e VIRTUAL_HOST=wildcard.bats.* + + # THEN + assert_200 wildcard.bats.f00 + assert_200 wildcard.bats.bar + assert_503 unexpected.host.bats +} + +@test "[$TEST_FILE] VIRTUAL_HOST=~^foo\.bar\..*\.bats" { + # WHEN + prepare_web_container bats-wildcard-hosts-2 80 -e VIRTUAL_HOST=~^foo\.bar\..*\.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 + + run curl_container $SUT_CONTAINER / --head --header "Host: $host" + assert_output -l 0 $'HTTP/1.1 200 OK\r' +} + +# 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 +function assert_503 { + 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' +} From 2cba8a8d5f525c1e9ba4b8c06e396d2ee14fc522 Mon Sep 17 00:00:00 2001 From: Mike Dillon Date: Fri, 9 Oct 2015 23:10:38 -0700 Subject: [PATCH 2/9] Avoid xargs -r The -r option is not available on OS X --- test/default-host.bats | 5 ++++- test/multiple-hosts.bats | 5 ++++- test/multiple-ports.bats | 5 ++++- test/wildcard-hosts.bats | 5 ++++- 4 files changed, 16 insertions(+), 4 deletions(-) diff --git a/test/default-host.bats b/test/default-host.bats index 503bb9f..4e9d84e 100644 --- a/test/default-host.bats +++ b/test/default-host.bats @@ -4,7 +4,10 @@ load test_helpers function setup { # 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 - 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 } diff --git a/test/multiple-hosts.bats b/test/multiple-hosts.bats index c5c5749..695aec1 100644 --- a/test/multiple-hosts.bats +++ b/test/multiple-hosts.bats @@ -5,7 +5,10 @@ SUT_CONTAINER=bats-nginx-proxy-${TEST_FILE} function setup { # 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 - 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 } diff --git a/test/multiple-ports.bats b/test/multiple-ports.bats index a8ff314..a520571 100644 --- a/test/multiple-ports.bats +++ b/test/multiple-ports.bats @@ -5,7 +5,10 @@ SUT_CONTAINER=bats-nginx-proxy-${TEST_FILE} function setup { # 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 - 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 } diff --git a/test/wildcard-hosts.bats b/test/wildcard-hosts.bats index 4e1484f..88ca1e7 100644 --- a/test/wildcard-hosts.bats +++ b/test/wildcard-hosts.bats @@ -5,7 +5,10 @@ SUT_CONTAINER=bats-nginx-proxy-${TEST_FILE} function setup { # 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 - 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 } From 0d2bc381791bf15746e363b8dcf9acc5a28fdd6c Mon Sep 17 00:00:00 2001 From: Mike Dillon Date: Fri, 9 Oct 2015 23:11:03 -0700 Subject: [PATCH 3/9] Run curl commands from a sibling container --- test/docker.bats | 2 +- test/test_helpers.bash | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/test/docker.bats b/test/docker.bats index 44d31b9..d5fde7c 100644 --- a/test/docker.bats +++ b/test/docker.bats @@ -62,7 +62,7 @@ load test_helpers -v /etc/nginx/certs/ \ nginx:latest assert_success - run retry 5 1s curl --silent --fail --head http://$(docker_ip bats-nginx)/ + run retry 5 1s docker run --rm appropriate/curl --silent --fail --head http://$(docker_ip bats-nginx)/ assert_output -l 0 $'HTTP/1.1 200 OK\r' # WHEN docker-gen runs on our docker host diff --git a/test/test_helpers.bash b/test/test_helpers.bash index 9063736..d4f0842 100644 --- a/test/test_helpers.bash +++ b/test/test_helpers.bash @@ -1,7 +1,7 @@ # Test if requirements are met ( type docker &>/dev/null || ( echo "docker is not available"; exit 1 ) - type curl &>/dev/null || ( echo "curl is not available"; exit 1 ) + #type curl &>/dev/null || ( echo "curl is not available"; exit 1 ) )>&2 @@ -67,7 +67,7 @@ function curl_container { local -r container=$1 local -r path=$2 shift 2 - curl --silent \ + docker run --rm appropriate/curl --silent \ --connect-timeout 5 \ --max-time 20 \ "$@" \ @@ -122,7 +122,7 @@ function prepare_web_container { # THEN querying directly port works 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 --rm appropriate/curl --silent --fail http://$(docker_ip $container_name):$port/data assert_output "answer from port $port" done } \ No newline at end of file From b1a08843da6cf2983dec7cebb490aee2cf086273 Mon Sep 17 00:00:00 2001 From: Mike Dillon Date: Sun, 11 Oct 2015 10:55:17 -0700 Subject: [PATCH 4/9] Avoid breakage from IFS being reset in "run" See https://github.com/sstephenson/bats/issues/89 --- test/test_helpers.bash | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/test_helpers.bash b/test/test_helpers.bash index d4f0842..c4a2d1c 100644 --- a/test/test_helpers.bash +++ b/test/test_helpers.bash @@ -87,6 +87,7 @@ function prepare_web_container { local -r options="$@" local expose_option="" + IFS=$' \t\n' # See https://github.com/sstephenson/bats/issues/89 for port in $ports; do expose_option="${expose_option}--expose=$port " done @@ -121,6 +122,7 @@ function prepare_web_container { assert_success # THEN querying directly port works + IFS=$' \t\n' # See https://github.com/sstephenson/bats/issues/89 for port in $ports; do run retry 5 1s docker run --rm appropriate/curl --silent --fail http://$(docker_ip $container_name):$port/data assert_output "answer from port $port" From 30e565f0ed62a2bf34ab62a356e432957f12ac10 Mon Sep 17 00:00:00 2001 From: Mike Dillon Date: Sun, 11 Oct 2015 11:04:00 -0700 Subject: [PATCH 5/9] Check "docker version" from a sibling container --- test/lib/docker_helpers.bash | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/lib/docker_helpers.bash b/test/lib/docker_helpers.bash index bbaf27e..c60c222 100644 --- a/test/lib/docker_helpers.bash +++ b/test/lib/docker_helpers.bash @@ -56,5 +56,5 @@ function docker_tcp { --expose 2375 \ -v /var/run/docker.sock:/var/run/docker.sock \ rancher/socat-docker - docker -H tcp://$(docker_ip $container_name):2375 version + docker run --rm --link "$container_name:docker" docker version } From ec393e3d180c9652bfa055d6b2bde7d96056f013 Mon Sep 17 00:00:00 2001 From: Mike Dillon Date: Sun, 11 Oct 2015 11:04:34 -0700 Subject: [PATCH 6/9] Pull additional images needed for Circle CI --- circle.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/circle.yml b/circle.yml index 8deabde..6fcd063 100644 --- a/circle.yml +++ b/circle.yml @@ -14,6 +14,8 @@ dependencies: - docker pull nginx - docker pull python:3 - docker pull rancher/socat-docker + - docker pull appropriate/curl + - docker pull docker test: override: From 6e7ff3451bcb453796b12ecb074f2f7a90321c31 Mon Sep 17 00:00:00 2001 From: Mike Dillon Date: Sun, 11 Oct 2015 11:27:38 -0700 Subject: [PATCH 7/9] Remove commented out line --- test/test_helpers.bash | 1 - 1 file changed, 1 deletion(-) diff --git a/test/test_helpers.bash b/test/test_helpers.bash index c4a2d1c..d8e7496 100644 --- a/test/test_helpers.bash +++ b/test/test_helpers.bash @@ -1,7 +1,6 @@ # Test if requirements are met ( type docker &>/dev/null || ( echo "docker is not available"; exit 1 ) - #type curl &>/dev/null || ( echo "curl is not available"; exit 1 ) )>&2 From 5c6a63568dfb32e53d8a3dee5a2c65c70090bb15 Mon Sep 17 00:00:00 2001 From: Mike Dillon Date: Mon, 12 Oct 2015 20:59:53 -0700 Subject: [PATCH 8/9] Remove --rm from docker run commands Trying to avoid "Failed to destroy btrfs snapshot" errors on CircleCI --- test/docker.bats | 2 +- test/lib/docker_helpers.bash | 2 +- test/test_helpers.bash | 6 +++--- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/test/docker.bats b/test/docker.bats index d5fde7c..f4ec665 100644 --- a/test/docker.bats +++ b/test/docker.bats @@ -62,7 +62,7 @@ load test_helpers -v /etc/nginx/certs/ \ nginx:latest assert_success - run retry 5 1s docker run --rm appropriate/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' # WHEN docker-gen runs on our docker host diff --git a/test/lib/docker_helpers.bash b/test/lib/docker_helpers.bash index c60c222..ae93409 100644 --- a/test/lib/docker_helpers.bash +++ b/test/lib/docker_helpers.bash @@ -56,5 +56,5 @@ function docker_tcp { --expose 2375 \ -v /var/run/docker.sock:/var/run/docker.sock \ rancher/socat-docker - docker run --rm --link "$container_name:docker" docker version + docker run --link "$container_name:docker" docker version } diff --git a/test/test_helpers.bash b/test/test_helpers.bash index d8e7496..9e4690c 100644 --- a/test/test_helpers.bash +++ b/test/test_helpers.bash @@ -66,7 +66,7 @@ function curl_container { local -r container=$1 local -r path=$2 shift 2 - docker run --rm appropriate/curl --silent \ + docker run appropriate/curl --silent \ --connect-timeout 5 \ --max-time 20 \ "$@" \ @@ -123,7 +123,7 @@ function prepare_web_container { # THEN querying directly port works IFS=$' \t\n' # See https://github.com/sstephenson/bats/issues/89 for port in $ports; do - run retry 5 1s docker run --rm appropriate/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" done -} \ No newline at end of file +} From 1d9db94535272b738d47a76f4c8dc838de0fc577 Mon Sep 17 00:00:00 2001 From: Mike Dillon Date: Mon, 12 Oct 2015 21:16:55 -0700 Subject: [PATCH 9/9] Use docker:1.7 image to match CircleCI's version --- circle.yml | 4 ++-- test/lib/docker_helpers.bash | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/circle.yml b/circle.yml index 6fcd063..d0c24ec 100644 --- a/circle.yml +++ b/circle.yml @@ -15,9 +15,9 @@ dependencies: - docker pull python:3 - docker pull rancher/socat-docker - docker pull appropriate/curl - - docker pull docker + - docker pull docker:1.7 test: override: - docker build -t jwilder/nginx-proxy:bats . - - bats test \ No newline at end of file + - bats test diff --git a/test/lib/docker_helpers.bash b/test/lib/docker_helpers.bash index ae93409..b5165af 100644 --- a/test/lib/docker_helpers.bash +++ b/test/lib/docker_helpers.bash @@ -56,5 +56,5 @@ function docker_tcp { --expose 2375 \ -v /var/run/docker.sock:/var/run/docker.sock \ rancher/socat-docker - docker run --link "$container_name:docker" docker version + docker run --link "$container_name:docker" docker:1.7 version }