Skip to content

Commit

Permalink
Merge commit 'upstream/master' into 'origin/master'
Browse files Browse the repository at this point in the history
* commit '205b3916776c97aca267dc7f4868d4ff7feb1240':
  Typo fix.
  Update email address
  Update to nginx 1.9.6
  makefile: update docker images before running tests
  Use make test from circle.yml
  Add Makefile
  Update .dockerignore to recent file additions
  Use latest docker-gen
  trim whitespace from host and port
  Trap TERM to allow quicker exit of web containers
  Use docker:1.7 image to match CircleCI's version
  Remove --rm from docker run commands
  Remove commented out line
  Pull additional images needed for Circle CI
  Check "docker version" from a sibling container
  Avoid breakage from IFS being reset in "run"
  Run curl commands from a sibling container
  Avoid xargs -r
  Change line endings to Unix

Conflicts:
	test/multiple-ports.bats
  • Loading branch information
ammmze committed Nov 2, 2015
2 parents 3e46053 + 205b391 commit c87ce66
Show file tree
Hide file tree
Showing 13 changed files with 185 additions and 143 deletions.
4 changes: 4 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
.git
.dockerignore
circle.yml
Makefile
README.md
test
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
FROM nginx:1.9.5
MAINTAINER Jason Wilder jwilder@litl.com
FROM nginx:1.9.6
MAINTAINER Jason Wilder mail@jasonwilder.com

# Install wget and install/updates certificates
RUN apt-get update \
Expand Down
14 changes: 14 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
.SILENT :
.PHONY : test

update-dependencies:
docker pull jwilder/docker-gen:latest
docker pull nginx:latest
docker pull python:3
docker pull rancher/socat-docker:latest
docker pull appropriate/curl:latest
docker pull docker:1.7

test:
docker build -t jwilder/nginx-proxy:bats .
bats test
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
![nginx 1.9.5](https://img.shields.io/badge/nginx-1.9.5-brightgreen.svg) ![License MIT](https://img.shields.io/badge/license-MIT-blue.svg) [![Build](https://circleci.com/gh/jwilder/nginx-proxy.svg?&style=shield&circle-token=2da3ee844076a47371bd45da81cf27409ca7306a)](https://circleci.com/gh/jwilder/nginx-proxy)
![nginx 1.9.6](https://img.shields.io/badge/nginx-1.9.6-brightgreen.svg) ![License MIT](https://img.shields.io/badge/license-MIT-blue.svg) [![Build](https://circleci.com/gh/jwilder/nginx-proxy.svg?&style=shield&circle-token=2da3ee844076a47371bd45da81cf27409ca7306a)](https://circleci.com/gh/jwilder/nginx-proxy)

nginx-proxy sets up a container running nginx and [docker-gen][1]. docker-gen generates reverse proxy configs for nginx and reloads nginx when containers are started and stopped.

Expand Down Expand Up @@ -93,7 +93,7 @@ should have a `foo.bar.com.dhparam.pem` file in the certs directory.

#### Wildcard Certificates

Wildcard certificates and keys should be name after the domain name with a `.crt` and `.key` extension.
Wildcard certificates and keys should be named after the domain name with a `.crt` and `.key` extension.
For example `VIRTUAL_HOST=foo.bar.com` would use cert name `bar.com.crt` and `bar.com.key`.

#### SNI
Expand Down Expand Up @@ -218,3 +218,13 @@ If you are using multiple hostnames for a single container (e.g. `VIRTUAL_HOST=e
If you want most of your virtual hosts to use a default single `location` block configuration and then override on a few specific ones, add those settings to the `/etc/nginx/vhost.d/default_location` file. This file
will be used on any virtual host which does not have a `/etc/nginx/vhost.d/{VIRTUAL_HOST}` file associated with it.

### Contributing

Before submitting pull requests or issues, please check github to make sure an existing issue or pull request is not already open.

#### Running Tests Locally

To run tests, you'll need to install [bats 0.4.0](https://github.com/sstephenson/bats).

make test

8 changes: 2 additions & 6 deletions circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,8 @@ dependencies:
- sudo add-apt-repository ppa:duggan/bats --yes
- sudo apt-get update -qq
- sudo apt-get install -qq bats
- docker pull jwilder/docker-gen
- docker pull nginx
- docker pull python:3
- docker pull rancher/socat-docker
- make update-dependencies

test:
override:
- docker build -t jwilder/nginx-proxy:bats .
- bats test
- make test
4 changes: 2 additions & 2 deletions nginx.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ server {
{{ end }}

location / {
proxy_pass {{ $proto }}://{{ $host }};
proxy_pass {{ trim $proto }}://{{ trim $host }};
{{ if (exists (printf "/etc/nginx/htpasswd/%s" $host)) }}
auth_basic "Restricted {{ $host }}";
auth_basic_user_file {{ (printf "/etc/nginx/htpasswd/%s" $host) }};
Expand All @@ -176,7 +176,7 @@ server {
{{ end }}

location / {
proxy_pass {{ $proto }}://{{ $host }};
proxy_pass {{ trim $proto }}://{{ trim $host }};
{{ if (exists (printf "/etc/nginx/htpasswd/%s" $host)) }}
auth_basic "Restricted {{ $host }}";
auth_basic_user_file {{ (printf "/etc/nginx/htpasswd/%s" $host) }};
Expand Down
5 changes: 4 additions & 1 deletion test/default-host.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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
}


Expand Down
2 changes: 1 addition & 1 deletion test/docker.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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 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
Expand Down
2 changes: 1 addition & 1 deletion test/lib/docker_helpers.bash
Original file line number Diff line number Diff line change
Expand Up @@ -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 --link "$container_name:docker" docker:1.7 version
}
5 changes: 4 additions & 1 deletion test/multiple-hosts.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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
}


Expand Down
109 changes: 56 additions & 53 deletions test/multiple-ports.bats
Original file line number Diff line number Diff line change
@@ -1,40 +1,43 @@
#!/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
}


#!/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
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
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] VIRTUAL_HOST proxy syntax" {
# GIVEN
prepare_web_container bats-web-${TEST_FILE}-2 "80 90" -e VIRTUAL_HOST='web.bats=>http:80,web1.bats=>http:90'
Expand All @@ -45,21 +48,21 @@ function setup {
}


@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
@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
local -r host=${2:-web.bats}
run curl_container $SUT_CONTAINER /data --header "Host: $host"
assert_output "answer from port $port"
}

assert_output "answer from port $port"
}

18 changes: 12 additions & 6 deletions test/test_helpers.bash
Original file line number Diff line number Diff line change
@@ -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


Expand Down Expand Up @@ -67,7 +66,7 @@ function curl_container {
local -r container=$1
local -r path=$2
shift 2
curl --silent \
docker run appropriate/curl --silent \
--connect-timeout 5 \
--max-time 20 \
"$@" \
Expand All @@ -87,6 +86,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
Expand All @@ -108,21 +108,27 @@ function prepare_web_container {
-w /var/www/ \
$options \
-e PYTHON_PORTS="$ports" \
python:3 sh -c "
python:3 bash -c "
trap '[ \${#PIDS[@]} -gt 0 ] && kill -TERM \${PIDS[@]}' TERM
declare -a PIDS
for port in \$PYTHON_PORTS; do
echo starting a web server listening on port \$port;
mkdir /var/www/\$port
cd /var/www/\$port
echo \"answer from port \$port\" > data
python -m http.server \$port &
PIDS+=(\$!)
done
wait
wait \${PIDS[@]}
trap - TERM
wait \${PIDS[@]}
"
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 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
}
}
Loading

0 comments on commit c87ce66

Please sign in to comment.