Added mechanism to specify ip_hash

This commit is contained in:
tpcwang 2016-01-11 11:45:36 -06:00
parent f42bf9175d
commit f6aa0f55d3
6 changed files with 82 additions and 4 deletions

View file

@ -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 */}}

View file

@ -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

71
test/env-config.bats Normal file
View file

@ -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
}

View file

@ -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

View file

@ -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

View file

@ -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