test_build.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. """
  2. Test that nginx-proxy-tester can build successfully
  3. """
  4. import pathlib
  5. import re
  6. import docker
  7. import pytest
  8. client = docker.from_env()
  9. @pytest.fixture(scope = "session")
  10. def docker_build(request):
  11. # Define Dockerfile path
  12. current_file_path = pathlib.Path(__file__)
  13. dockerfile_path = current_file_path.parent.parent.joinpath("requirements")
  14. dockerfile_name = "Dockerfile-nginx-proxy-tester"
  15. # Build the Docker image
  16. image, logs = client.images.build(
  17. path = dockerfile_path.as_posix(),
  18. dockerfile = dockerfile_name,
  19. rm = True, # Remove intermediate containers
  20. tag = "nginx-proxy-tester-ci", # Tag for the built image
  21. )
  22. # Check for build success
  23. for log in logs:
  24. if "stream" in log:
  25. print(log["stream"].strip())
  26. if "error" in log:
  27. raise Exception(log["error"])
  28. def teardown():
  29. # Clean up after teardown
  30. client.images.remove(image.id, force=True)
  31. request.addfinalizer(teardown)
  32. # Return the image name
  33. return "nginx-proxy-tester-ci"
  34. def test_build_nginx_proxy_tester(docker_build):
  35. assert docker_build == "nginx-proxy-tester-ci"
  36. def test_run_nginx_proxy_tester(docker_build):
  37. # Run the container with 'pytest -v' command to output version info
  38. container = client.containers.run("nginx-proxy-tester-ci",
  39. command = "pytest -V",
  40. detach = True,
  41. )
  42. # Wait for the container to finish and get the exit code
  43. result = container.wait()
  44. exit_code = result.get("StatusCode", 1) # Default to 1 (error) if not found
  45. # Get the output logs from the container
  46. output = container.logs().decode("utf-8").strip()
  47. # Clean up: Remove the container
  48. container.remove()
  49. # Assertions
  50. assert exit_code == 0, "Container exited with a non-zero exit code"
  51. assert re.search(r"pytest\s\d+\.\d+\.\d+", output)