From 43d8ba0e78a9e069ca96f74a5f0c75901ba0bee0 Mon Sep 17 00:00:00 2001 From: Thomas LEVEIL Date: Sat, 11 Feb 2017 22:42:24 +0100 Subject: [PATCH] TESTS: add tests for using nginx and docker-gen in separated containers --- test2/test.sh | 2 +- test2/test_dockergen/.gitignore | 1 + test2/test_dockergen/test_dockergen_v2.py | 38 ++++++++++++++++++ test2/test_dockergen/test_dockergen_v2.yml | 23 +++++++++++ test2/test_dockergen/test_dockergen_v3.py | 46 ++++++++++++++++++++++ test2/test_dockergen/test_dockergen_v3.yml | 24 +++++++++++ 6 files changed, 133 insertions(+), 1 deletion(-) create mode 100644 test2/test_dockergen/.gitignore create mode 100644 test2/test_dockergen/test_dockergen_v2.py create mode 100644 test2/test_dockergen/test_dockergen_v2.yml create mode 100644 test2/test_dockergen/test_dockergen_v3.py create mode 100644 test2/test_dockergen/test_dockergen_v3.yml diff --git a/test2/test.sh b/test2/test.sh index 03e29a6..a9745f5 100755 --- a/test2/test.sh +++ b/test2/test.sh @@ -18,7 +18,7 @@ docker build -t nginx-proxy-tester -f $DIR/requirements/Dockerfile-nginx-proxy-t # run the nginx-proxy-tester container setting the correct value for the working dir in order for # docker-compose to work properly when run from within that container. exec docker run --rm -it \ - -v ${DIR}:/${DIR}:ro \ + -v ${DIR}:/${DIR} \ -w ${DIR} \ -v /var/run/docker.sock:/var/run/docker.sock \ nginx-proxy-tester ${ARGS} diff --git a/test2/test_dockergen/.gitignore b/test2/test_dockergen/.gitignore new file mode 100644 index 0000000..98c0b06 --- /dev/null +++ b/test2/test_dockergen/.gitignore @@ -0,0 +1 @@ +nginx.tmpl \ No newline at end of file diff --git a/test2/test_dockergen/test_dockergen_v2.py b/test2/test_dockergen/test_dockergen_v2.py new file mode 100644 index 0000000..a83af57 --- /dev/null +++ b/test2/test_dockergen/test_dockergen_v2.py @@ -0,0 +1,38 @@ +import os +import docker +import logging +import pytest + + +@pytest.yield_fixture(scope="module") +def nginx_tmpl(): + """ + pytest fixture which extracts the the nginx config template from + the jwilder/nginx-proxy:test image + """ + script_dir = os.path.dirname(__file__) + logging.info("extracting nginx.tmpl from jwilder/nginx-proxy:test") + docker_client = docker.from_env() + print(docker_client.containers.run( + image='jwilder/nginx-proxy:test', + remove=True, + volumes=['{current_dir}:{current_dir}'.format(current_dir=script_dir)], + entrypoint='sh', + command='-xc "cp /app/nginx.tmpl {current_dir} && chmod 777 {current_dir}/nginx.tmpl"'.format(current_dir=script_dir), + stderr=True)) + yield + logging.info("removing nginx.tmpl") + os.remove(os.path.join(script_dir, "nginx.tmpl")) + + +def test_unknown_virtual_host_is_503(nginx_tmpl, docker_compose, nginxproxy): + r = nginxproxy.get("http://unknown.nginx.container.docker/") + assert r.status_code == 503 + + +def test_forwards_to_whoami(nginx_tmpl, docker_compose, nginxproxy): + r = nginxproxy.get("http://whoami.nginx.container.docker/") + assert r.status_code == 200 + whoami_container = docker_compose.containers.get("whoami") + assert r.text == "I'm %s\n" % whoami_container.id[:12] + diff --git a/test2/test_dockergen/test_dockergen_v2.yml b/test2/test_dockergen/test_dockergen_v2.yml new file mode 100644 index 0000000..145c763 --- /dev/null +++ b/test2/test_dockergen/test_dockergen_v2.yml @@ -0,0 +1,23 @@ +version: '2' + +services: + nginx: + image: nginx + container_name: nginx + volumes: + - /etc/nginx/conf.d + + dockergen: + image: jwilder/docker-gen + command: -notify-sighup nginx -watch /etc/docker-gen/templates/nginx.tmpl /etc/nginx/conf.d/default.conf + volumes_from: + - nginx + volumes: + - /var/run/docker.sock:/tmp/docker.sock:ro + - ./nginx.tmpl:/etc/docker-gen/templates/nginx.tmpl + + whoami: + image: jwilder/whoami + container_name: whoami + environment: + - VIRTUAL_HOST=whoami.nginx.container.docker \ No newline at end of file diff --git a/test2/test_dockergen/test_dockergen_v3.py b/test2/test_dockergen/test_dockergen_v3.py new file mode 100644 index 0000000..8388223 --- /dev/null +++ b/test2/test_dockergen/test_dockergen_v3.py @@ -0,0 +1,46 @@ +import os +import docker +import logging +import pytest + + +def versiontuple(v): + return tuple(map(int, (v.split(".")))) + +docker_version = docker.from_env().version()['Version'] +pytestmark = pytest.mark.skipif(versiontuple(docker_version) < versiontuple('1.13'), + reason="Docker compose syntax v3 requires docker engine v1.13") + + +@pytest.yield_fixture(scope="module") +def nginx_tmpl(): + """ + pytest fixture which extracts the the nginx config template from + the jwilder/nginx-proxy:test image + """ + script_dir = os.path.dirname(__file__) + logging.info("extracting nginx.tmpl from jwilder/nginx-proxy:test") + docker_client = docker.from_env() + print(docker_client.containers.run( + image='jwilder/nginx-proxy:test', + remove=True, + volumes=['{current_dir}:{current_dir}'.format(current_dir=script_dir)], + entrypoint='sh', + command='-xc "cp /app/nginx.tmpl {current_dir} && chmod 777 {current_dir}/nginx.tmpl"'.format(current_dir=script_dir), + stderr=True)) + yield + logging.info("removing nginx.tmpl") + os.remove(os.path.join(script_dir, "nginx.tmpl")) + + +def test_unknown_virtual_host_is_503(nginx_tmpl, docker_compose, nginxproxy): + r = nginxproxy.get("http://unknown.nginx.container.docker/") + assert r.status_code == 503 + + +def test_forwards_to_whoami(nginx_tmpl, docker_compose, nginxproxy): + r = nginxproxy.get("http://whoami.nginx.container.docker/") + assert r.status_code == 200 + whoami_container = docker_compose.containers.get("whoami") + assert r.text == "I'm %s\n" % whoami_container.id[:12] + diff --git a/test2/test_dockergen/test_dockergen_v3.yml b/test2/test_dockergen/test_dockergen_v3.yml new file mode 100644 index 0000000..b94b09b --- /dev/null +++ b/test2/test_dockergen/test_dockergen_v3.yml @@ -0,0 +1,24 @@ +version: '3' +services: + nginx: + image: nginx + container_name: nginx + volumes: + - nginx_conf:/etc/nginx/conf.d + + dockergen: + image: jwilder/docker-gen + command: -notify-sighup nginx -watch /etc/docker-gen/templates/nginx.tmpl /etc/nginx/conf.d/default.conf + volumes: + - /var/run/docker.sock:/tmp/docker.sock:ro + - ./nginx.tmpl:/etc/docker-gen/templates/nginx.tmpl + - nginx_conf:/etc/nginx/conf.d + + whoami: + image: jwilder/whoami + container_name: whoami + environment: + - VIRTUAL_HOST=whoami.nginx.container.docker + +volumes: + nginx_conf: {} \ No newline at end of file