From 26b0b05f730cd6dfdb8c77f9ee18fabce3a82019 Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Mon, 16 Jan 2023 19:22:20 -0500 Subject: [PATCH 1/6] tests: Fix `test_debug/*` tests when IPv6 is enabled --- test/test_debug/test_proxy-debug-flag.py | 8 ++++---- test/test_debug/test_server-debug-flag.py | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/test/test_debug/test_proxy-debug-flag.py b/test/test_debug/test_proxy-debug-flag.py index af7f73a..6430d84 100644 --- a/test/test_debug/test_proxy-debug-flag.py +++ b/test/test_debug/test_proxy-debug-flag.py @@ -3,10 +3,10 @@ import re def test_debug_info_is_present_in_nginx_generated_conf(docker_compose, nginxproxy): conf = nginxproxy.get_conf().decode('ASCII') - assert re.search(r"# Exposed ports: \[\{\d+\.\d+\.\d+\.\d+\s+80\s+tcp \} \{\d+\.\d+\.\d+\.\d+\s+81\s+tcp \}\]", conf) or \ - re.search(r"# Exposed ports: \[\{\d+\.\d+\.\d+\.\d+\s+81\s+tcp \} \{\d+\.\d+\.\d+\.\d+\s+80\s+tcp \}\]", conf) - assert re.search(r"# Exposed ports: \[\{\d+\.\d+\.\d+\.\d+\s+82\s+tcp \} \{\d+\.\d+\.\d+\.\d+\s+83\s+tcp \}\]", conf) or \ - re.search(r"# Exposed ports: \[\{\d+\.\d+\.\d+\.\d+\s+83\s+tcp \} \{\d+\.\d+\.\d+\.\d+\s+82\s+tcp \}\]", conf) + assert re.search(r"# Exposed ports: \[\{[^}]+\s+80\s+tcp \} \{[^}]+\s+81\s+tcp \}\]", conf) or \ + re.search(r"# Exposed ports: \[\{[^}]+\s+81\s+tcp \} \{[^}]+\s+80\s+tcp \}\]", conf) + assert re.search(r"# Exposed ports: \[\{[^}]+\s+82\s+tcp \} \{[^}]+\s+83\s+tcp \}\]", conf) or \ + re.search(r"# Exposed ports: \[\{[^}]+\s+83\s+tcp \} \{[^}]+\s+82\s+tcp \}\]", conf) assert "# Default virtual port: 80" in conf assert "# VIRTUAL_PORT: 82" in conf assert conf.count("# /!\\ Virtual port not exposed") == 1 diff --git a/test/test_debug/test_server-debug-flag.py b/test/test_debug/test_server-debug-flag.py index 50ae737..c635175 100644 --- a/test/test_debug/test_server-debug-flag.py +++ b/test/test_debug/test_server-debug-flag.py @@ -3,6 +3,6 @@ import re def test_debug_info_is_present_in_nginx_generated_conf(docker_compose, nginxproxy): conf = nginxproxy.get_conf().decode('ASCII') - assert re.search(r"# Exposed ports: \[\{\d+\.\d+\.\d+\.\d+\s+80\s+tcp \} \{\d+\.\d+\.\d+\.\d+\s+81\s+tcp \}\]", conf) or \ - re.search(r"# Exposed ports: \[\{\d+\.\d+\.\d+\.\d+\s+81\s+tcp \} \{\d+\.\d+\.\d+\.\d+\s+80\s+tcp \}\]", conf) + assert re.search(r"# Exposed ports: \[\{[^}]+\s+80\s+tcp \} \{[^}]+\s+81\s+tcp \}\]", conf) or \ + re.search(r"# Exposed ports: \[\{[^}]+\s+81\s+tcp \} \{[^}]+\s+80\s+tcp \}\]", conf) assert conf.count("# Exposed ports: [{") == 1 From d56b5b370d8aef801ec2c1d7c13dfcf2a6f23c03 Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Mon, 16 Jan 2023 23:37:48 -0500 Subject: [PATCH 2/6] tests: Whitespace fixes --- test/pytest.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/pytest.sh b/test/pytest.sh index 28275e5..beac0af 100755 --- a/test/pytest.sh +++ b/test/pytest.sh @@ -16,10 +16,10 @@ ARGS=("$@") echo "> Building nginx-proxy-tester image..." docker build -t nginx-proxy-tester -f "${DIR}/requirements/Dockerfile-nginx-proxy-tester" "${DIR}/requirements" -# run the nginx-proxy-tester container setting the correct value for the working dir in order for +# 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 --name "nginx-proxy-pytest" \ --volume "/var/run/docker.sock:/var/run/docker.sock" \ --volume "${DIR}:${DIR}" \ --workdir "${DIR}" \ - nginx-proxy-tester "${ARGS[@]}" \ No newline at end of file + nginx-proxy-tester "${ARGS[@]}" From 55cfae963689f3b583c17ca22831361e5b8c901d Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Mon, 16 Jan 2023 23:39:20 -0500 Subject: [PATCH 3/6] tests: Avoid unnecessary bashisms --- test/pytest.sh | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/test/pytest.sh b/test/pytest.sh index beac0af..2ac281d 100755 --- a/test/pytest.sh +++ b/test/pytest.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/bin/sh ############################################################################### # # # This script is meant to run the test suite from a Docker container. # @@ -9,8 +9,7 @@ ############################################################################### # Returns the absolute directory path to this script -DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" -ARGS=("$@") +DIR=$(cd "${0%/*}" && pwd) # check requirements echo "> Building nginx-proxy-tester image..." @@ -22,4 +21,4 @@ exec docker run --rm -it --name "nginx-proxy-pytest" \ --volume "/var/run/docker.sock:/var/run/docker.sock" \ --volume "${DIR}:${DIR}" \ --workdir "${DIR}" \ - nginx-proxy-tester "${ARGS[@]}" + nginx-proxy-tester "$@" From 569953521aa5eca5261d97f08785b5a9edf03880 Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Mon, 16 Jan 2023 23:49:32 -0500 Subject: [PATCH 4/6] tests: Exit non-zero if creation of nginx-proxy-tester image fails --- test/pytest.sh | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/test/pytest.sh b/test/pytest.sh index 2ac281d..8e46ce0 100755 --- a/test/pytest.sh +++ b/test/pytest.sh @@ -9,11 +9,14 @@ ############################################################################### # Returns the absolute directory path to this script -DIR=$(cd "${0%/*}" && pwd) +DIR=$(cd "${0%/*}" && pwd) || exit 1 # check requirements echo "> Building nginx-proxy-tester image..." -docker build -t nginx-proxy-tester -f "${DIR}/requirements/Dockerfile-nginx-proxy-tester" "${DIR}/requirements" +docker build -t nginx-proxy-tester \ + -f "${DIR}/requirements/Dockerfile-nginx-proxy-tester" \ + "${DIR}/requirements" \ + || exit 1 # 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. From 486addd14403f016ebd829e4611d81e6b830c9f8 Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Mon, 16 Jan 2023 23:53:51 -0500 Subject: [PATCH 5/6] tests: Bind-mount the entire nginx-proxy directory in the container --- test/pytest.sh | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/test/pytest.sh b/test/pytest.sh index 8e46ce0..9088d10 100755 --- a/test/pytest.sh +++ b/test/pytest.sh @@ -9,13 +9,14 @@ ############################################################################### # Returns the absolute directory path to this script -DIR=$(cd "${0%/*}" && pwd) || exit 1 +TESTDIR=$(cd "${0%/*}" && pwd) || exit 1 +DIR=$(cd "${TESTDIR}/.." && pwd) || exit 1 # check requirements echo "> Building nginx-proxy-tester image..." docker build -t nginx-proxy-tester \ - -f "${DIR}/requirements/Dockerfile-nginx-proxy-tester" \ - "${DIR}/requirements" \ + -f "${TESTDIR}/requirements/Dockerfile-nginx-proxy-tester" \ + "${TESTDIR}/requirements" \ || exit 1 # run the nginx-proxy-tester container setting the correct value for the working dir in order for @@ -23,5 +24,5 @@ docker build -t nginx-proxy-tester \ exec docker run --rm -it --name "nginx-proxy-pytest" \ --volume "/var/run/docker.sock:/var/run/docker.sock" \ --volume "${DIR}:${DIR}" \ - --workdir "${DIR}" \ + --workdir "${TESTDIR}" \ nginx-proxy-tester "$@" From 92e1a6567ead9e2cdb7dea17774b0a20d187af6a Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Tue, 17 Jan 2023 00:02:02 -0500 Subject: [PATCH 6/6] tests: Remove extraction of `nginx.tmpl` (now unnecessary) --- test/test_dockergen/test_dockergen_v2.py | 36 ++--------------------- test/test_dockergen/test_dockergen_v2.yml | 2 +- test/test_dockergen/test_dockergen_v3.py | 33 ++------------------- test/test_dockergen/test_dockergen_v3.yml | 2 +- 4 files changed, 6 insertions(+), 67 deletions(-) diff --git a/test/test_dockergen/test_dockergen_v2.py b/test/test_dockergen/test_dockergen_v2.py index 43b1431..dbb15d4 100644 --- a/test/test_dockergen/test_dockergen_v2.py +++ b/test/test_dockergen/test_dockergen_v2.py @@ -1,41 +1,9 @@ -import os -import docker -import logging -import pytest - - -@pytest.fixture(scope="module") -def nginx_tmpl(): - """ - pytest fixture which extracts the the nginx config template from - the nginxproxy/nginx-proxy:test image - """ - script_dir = os.path.dirname(__file__) - logging.info("extracting nginx.tmpl from nginxproxy/nginx-proxy:test") - docker_client = docker.from_env() - print( - docker_client.containers.run( - image="nginxproxy/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): +def test_unknown_virtual_host_is_503(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): +def test_forwards_to_whoami(docker_compose, nginxproxy): r = nginxproxy.get("http://whoami.nginx.container.docker/") assert r.status_code == 200 whoami_container = docker_compose.containers.get("whoami") diff --git a/test/test_dockergen/test_dockergen_v2.yml b/test/test_dockergen/test_dockergen_v2.yml index b1f443c..36ee1c1 100644 --- a/test/test_dockergen/test_dockergen_v2.yml +++ b/test/test_dockergen/test_dockergen_v2.yml @@ -14,7 +14,7 @@ services: - nginx volumes: - /var/run/docker.sock:/tmp/docker.sock:ro - - ./nginx.tmpl:/etc/docker-gen/templates/nginx.tmpl + - ../../nginx.tmpl:/etc/docker-gen/templates/nginx.tmpl web: image: web diff --git a/test/test_dockergen/test_dockergen_v3.py b/test/test_dockergen/test_dockergen_v3.py index 67561bf..b696e6c 100644 --- a/test/test_dockergen/test_dockergen_v3.py +++ b/test/test_dockergen/test_dockergen_v3.py @@ -1,8 +1,5 @@ -import os import docker -import logging import pytest -import re from distutils.version import LooseVersion @@ -13,38 +10,12 @@ pytestmark = pytest.mark.skipif( ) -@pytest.fixture(scope="module") -def nginx_tmpl(): - """ - pytest fixture which extracts the the nginx config template from - the nginxproxy/nginx-proxy:test image - """ - script_dir = os.path.dirname(__file__) - logging.info("extracting nginx.tmpl from nginxproxy/nginx-proxy:test") - docker_client = docker.from_env() - print( - docker_client.containers.run( - image="nginxproxy/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): +def test_unknown_virtual_host_is_503(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): +def test_forwards_to_whoami(docker_compose, nginxproxy): r = nginxproxy.get("http://whoami.nginx.container.docker/") assert r.status_code == 200 whoami_container = docker_compose.containers.get("whoami") diff --git a/test/test_dockergen/test_dockergen_v3.yml b/test/test_dockergen/test_dockergen_v3.yml index 8339273..8b0411c 100644 --- a/test/test_dockergen/test_dockergen_v3.yml +++ b/test/test_dockergen/test_dockergen_v3.yml @@ -11,7 +11,7 @@ services: 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.tmpl:/etc/docker-gen/templates/nginx.tmpl - nginx_conf:/etc/nginx/conf.d web: