test_virtual-paths.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. from time import sleep
  2. import pytest
  3. from docker.errors import NotFound
  4. @pytest.mark.parametrize("stub,expected_port", [
  5. ("nginx-proxy.test/web1", 81),
  6. ("nginx-proxy.test/web2", 82),
  7. ("nginx-proxy.test", 83),
  8. ("foo.nginx-proxy.test", 42),
  9. ])
  10. def test_valid_path(docker_compose, nginxproxy, stub, expected_port):
  11. r = nginxproxy.get(f"http://{stub}/port")
  12. assert r.status_code == 200
  13. assert r.text == f"answer from port {expected_port}\n"
  14. @pytest.mark.parametrize("stub", [
  15. "nginx-proxy.test/foo",
  16. "bar.nginx-proxy.test",
  17. ])
  18. def test_invalid_path(docker_compose, nginxproxy, stub):
  19. r = nginxproxy.get(f"http://{stub}/port")
  20. assert r.status_code in [404, 503]
  21. @pytest.fixture
  22. def web4(docker_compose):
  23. """
  24. pytest fixture creating a web container with `VIRTUAL_HOST=nginx-proxy.test`, `VIRTUAL_PATH=/web4/` and `VIRTUAL_DEST=/` listening on port 84.
  25. """
  26. container = docker_compose.containers.run(
  27. name="web4",
  28. image="web",
  29. detach=True,
  30. environment={
  31. "WEB_PORTS": "84",
  32. "VIRTUAL_HOST": "nginx-proxy.test",
  33. "VIRTUAL_PATH": "/web4/",
  34. "VIRTUAL_DEST": "/",
  35. },
  36. ports={"84/tcp": None}
  37. )
  38. docker_compose.networks.get("test_virtual-path-net").connect(container)
  39. sleep(2) # give it some time to initialize and for docker-gen to detect it
  40. yield container
  41. try:
  42. docker_compose.containers.get("web4").remove(force=True)
  43. except NotFound:
  44. pass
  45. """
  46. Test if we can add and remove a single virtual_path from multiple ones on the same subdomain.
  47. """
  48. def test_container_hotplug(web4, nginxproxy):
  49. r = nginxproxy.get(f"http://nginx-proxy.test/web4/port")
  50. assert r.status_code == 200
  51. assert r.text == f"answer from port 84\n"
  52. web4.remove(force=True)
  53. sleep(2)
  54. r = nginxproxy.get(f"http://nginx-proxy.test/web4/port")
  55. assert r.status_code == 404