From f6aa0f55d3d6a2ab14bc3adad811eaef8f314914 Mon Sep 17 00:00:00 2001 From: tpcwang Date: Mon, 11 Jan 2016 11:45:36 -0600 Subject: [PATCH] Added mechanism to specify ip_hash --- nginx.tmpl | 7 ++++ test/default-host.bats | 2 +- test/env-config.bats | 71 ++++++++++++++++++++++++++++++++++++++++ test/multiple-hosts.bats | 2 +- test/multiple-ports.bats | 2 +- test/wildcard-hosts.bats | 2 +- 6 files changed, 82 insertions(+), 4 deletions(-) create mode 100644 test/env-config.bats diff --git a/nginx.tmpl b/nginx.tmpl index 255cc35..6e707ce 100644 --- a/nginx.tmpl +++ b/nginx.tmpl @@ -72,7 +72,14 @@ server { {{ range $host, $containers := groupByMulti $ "Env.VIRTUAL_HOST" "," }} +{{ $ip_hash := or (first (groupByKeys $containers "Env.USE_IP_HASH")) "0" }} + upstream {{ $host }} { +{{/* If the container requests to load-balance by connection IP */}} +{{ if ne $ip_hash "0" }} + ip_hash; +{{ end }} + {{ range $container := $containers }} {{ $addrLen := len $container.Addresses }} {{/* If only 1 port exposed, use that */}} diff --git a/test/default-host.bats b/test/default-host.bats index 4e9d84e..7966575 100644 --- a/test/default-host.bats +++ b/test/default-host.bats @@ -3,7 +3,7 @@ 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 + # have any unexpected container running with VIRTUAL_HOST or VIRUTAL_PORT set CIDS=( $(docker ps -q --filter "label=bats-type=web") ) if [ ${#CIDS[@]} -gt 0 ]; then docker stop ${CIDS[@]} >&2 diff --git a/test/env-config.bats b/test/env-config.bats new file mode 100644 index 0000000..51a28cd --- /dev/null +++ b/test/env-config.bats @@ -0,0 +1,71 @@ +#!/usr/bin/env bats +load test_helpers +SUT_CONTAINER=bats-nginx-proxy-${TEST_FILE} +HOST=web.bats +MOUNT="-v /var/run/docker.sock:/tmp/docker.sock:ro" + +function setup { + # make sure to stop any web container before each test so we don't + # have any unexpected container running with VIRTUAL_HOST or VIRUTAL_PORT set + CIDS=( $(docker ps -q --filter "label=bats-type=web") ) + if [ ${#CIDS[@]} -gt 0 ]; then + docker stop ${CIDS[@]} >&2 + fi +} + + +@test "[$TEST_FILE] run nginx-proxy without USE_IP_HASH" { + # WHEN multiple containers runs without USE_IP_HASH + run nginxproxy $SUT_CONTAINER $MOUNT + assert_success + docker_wait_for_log $SUT_CONTAINER 3 "Watching docker events" + prepare_web_container bats-web-${TEST_FILE}-1 80 -e VIRTUAL_HOST=$HOST + prepare_web_container bats-web-${TEST_FILE}-2 90 -e VIRTUAL_HOST=$HOST + + # THEN querying nginx-proxy twice should return different responses + query_nginx; local response1="$output" + query_nginx; local response2="$output" + refute_equal "$response1" "$response2" +} + +@test "[$TEST_FILE] run nginx-proxy with USE_IP_HASH=1" { + # WHEN multiple containers runs with USE_IP_HASH=1 + run nginxproxy $SUT_CONTAINER $MOUNT + assert_success + docker_wait_for_log $SUT_CONTAINER 3 "Watching docker events" + prepare_web_container bats-web-${TEST_FILE}-1 80 -e VIRTUAL_HOST=$HOST -e USE_IP_HASH=1 + prepare_web_container bats-web-${TEST_FILE}-2 90 -e VIRTUAL_HOST=$HOST -e USE_IP_HASH=1 + + # THEN querying nginx-proxy twice should return the same response + query_nginx; local response1="$output" + query_nginx; local response2="$output" + assert_equal "$response1" "$response2" +} + +# helper functions from jasonkarns/bats-assert + +function flunk { + { if [ "$#" -eq 0 ]; then cat - + else echo "$@" + fi + } | sed "s:${BATS_TMPDIR}:\${BATS_TMPDIR}:g" >&2 + return 1 +} + +function query_nginx { + run curl_container $SUT_CONTAINER /data --header "Host: $HOST" +} + +function assert_equal { + if [ "$1" != "$2" ]; then + { echo "expected: $1" + echo "actual : $2" + } | flunk + fi +} + +function refute_equal { + if [ "$1" = "$2" ]; then + flunk "unexpectedly equal: $1" + fi +} diff --git a/test/multiple-hosts.bats b/test/multiple-hosts.bats index 695aec1..6f56e00 100644 --- a/test/multiple-hosts.bats +++ b/test/multiple-hosts.bats @@ -4,7 +4,7 @@ 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 + # have any unexpected container running with VIRTUAL_HOST or VIRUTAL_PORT set CIDS=( $(docker ps -q --filter "label=bats-type=web") ) if [ ${#CIDS[@]} -gt 0 ]; then docker stop ${CIDS[@]} >&2 diff --git a/test/multiple-ports.bats b/test/multiple-ports.bats index a520571..6b94d95 100644 --- a/test/multiple-ports.bats +++ b/test/multiple-ports.bats @@ -4,7 +4,7 @@ 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 + # have any unexpected container running with VIRTUAL_HOST or VIRUTAL_PORT set CIDS=( $(docker ps -q --filter "label=bats-type=web") ) if [ ${#CIDS[@]} -gt 0 ]; then docker stop ${CIDS[@]} >&2 diff --git a/test/wildcard-hosts.bats b/test/wildcard-hosts.bats index 88ca1e7..f7239fe 100644 --- a/test/wildcard-hosts.bats +++ b/test/wildcard-hosts.bats @@ -4,7 +4,7 @@ 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 + # have any unexpected container running with VIRTUAL_HOST or VIRUTAL_PORT set CIDS=( $(docker ps -q --filter "label=bats-type=web") ) if [ ${#CIDS[@]} -gt 0 ]; then docker stop ${CIDS[@]} >&2