2
0

test_build.py 1.8 KB

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