test_build.py 1.8 KB

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