From 46724e7cb62e37f44e54af19d0655beb6401054e Mon Sep 17 00:00:00 2001 From: Niek <100143256+SchoNie@users.noreply.github.com> Date: Fri, 3 May 2024 11:18:43 +0200 Subject: [PATCH 1/3] tests: add test if nginx-proxy-builder can be build successfully --- test/test_build.py | 63 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 test/test_build.py diff --git a/test/test_build.py b/test/test_build.py new file mode 100644 index 0000000..3371f80 --- /dev/null +++ b/test/test_build.py @@ -0,0 +1,63 @@ +""" +Test that nginx-proxy-tester can build successfully +""" +import pytest +import docker +import re + +client = docker.from_env() + +@pytest.fixture(scope="session") +def docker_build(request): + # Define Dockerfile path + dockerfile_path = "requirements/" + dockerfile_name="Dockerfile-nginx-proxy-tester" + + # Build the Docker image + image, logs = client.images.build( + path=dockerfile_path, + dockerfile=dockerfile_name, + rm=True, # Remove intermediate containers + tag="nginx-proxy-tester-ci", # Tag for the built image + ) + + # Check for build success + for log in logs: + if "stream" in log: + print(log["stream"].strip()) + if "error" in log: + raise Exception(log["error"]) + + def teardown(): + # Clean up after teardown + client.images.remove(image.id, force=True) + + request.addfinalizer(teardown) + + # Return the image name + return "nginx-proxy-tester-ci" + +def test_build_nginx_proxy_tester(docker_build): + assert docker_build == "nginx-proxy-tester-ci" + +def test_run_nginx_proxy_tester(docker_build): + # Run the container with 'pytest -v' command to output version info + container = client.containers.run("nginx-proxy-tester-ci", + command="pytest -V", + detach=True, + # auto_remove=True + ) + + # Wait for the container to finish and get the exit code + result = container.wait() + exit_code = result.get("StatusCode", 1) # Default to 1 (error) if not found + + # Get the output logs from the container + output = container.logs().decode("utf-8").strip() + + # Clean up: Remove the container + container.remove() + + # Assertions + assert exit_code == 0, "Container exited with a non-zero exit code" + assert re.search(r"pytest\s\d+\.\d+\.\d+", output) From c678cfdddfae0b73336108e3d2ceaa932e887266 Mon Sep 17 00:00:00 2001 From: Niek <100143256+SchoNie@users.noreply.github.com> Date: Fri, 3 May 2024 12:09:40 +0200 Subject: [PATCH 2/3] Apply suggestions from code review Commited code style consistency suggestions. Co-authored-by: Nicolas Duchon --- test/test_build.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/test/test_build.py b/test/test_build.py index 3371f80..3f19183 100644 --- a/test/test_build.py +++ b/test/test_build.py @@ -7,18 +7,18 @@ import re client = docker.from_env() -@pytest.fixture(scope="session") +@pytest.fixture(scope = "session") def docker_build(request): # Define Dockerfile path dockerfile_path = "requirements/" - dockerfile_name="Dockerfile-nginx-proxy-tester" + dockerfile_name = "Dockerfile-nginx-proxy-tester" # Build the Docker image image, logs = client.images.build( - path=dockerfile_path, - dockerfile=dockerfile_name, - rm=True, # Remove intermediate containers - tag="nginx-proxy-tester-ci", # Tag for the built image + path = dockerfile_path, + dockerfile = dockerfile_name, + rm = True, # Remove intermediate containers + tag = "nginx-proxy-tester-ci", # Tag for the built image ) # Check for build success @@ -43,9 +43,8 @@ def test_build_nginx_proxy_tester(docker_build): def test_run_nginx_proxy_tester(docker_build): # Run the container with 'pytest -v' command to output version info container = client.containers.run("nginx-proxy-tester-ci", - command="pytest -V", - detach=True, - # auto_remove=True + command = "pytest -V", + detach = True, ) # Wait for the container to finish and get the exit code From 6602769d9ad14888674ce29838812a6844bb15db Mon Sep 17 00:00:00 2001 From: Nicolas Duchon Date: Fri, 3 May 2024 12:37:53 +0200 Subject: [PATCH 3/3] style: linting --- test/test_build.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/test_build.py b/test/test_build.py index 3f19183..9c79808 100644 --- a/test/test_build.py +++ b/test/test_build.py @@ -43,8 +43,8 @@ def test_build_nginx_proxy_tester(docker_build): def test_run_nginx_proxy_tester(docker_build): # Run the container with 'pytest -v' command to output version info container = client.containers.run("nginx-proxy-tester-ci", - command = "pytest -V", - detach = True, + command = "pytest -V", + detach = True, ) # Wait for the container to finish and get the exit code