2
0

test_virtual_paths.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. sleep(2) # give it some time to initialize and for docker-gen to detect it
  39. yield container
  40. try:
  41. docker_compose.containers.get("web4").remove(force=True)
  42. except NotFound:
  43. pass
  44. """
  45. Test if we can add and remove a single virtual_path from multiple ones on the same subdomain.
  46. """
  47. def test_container_hotplug(web4, nginxproxy):
  48. r = nginxproxy.get(f"http://nginx-proxy.test/web4/port")
  49. assert r.status_code == 200
  50. assert r.text == f"answer from port 84\n"
  51. web4.remove(force=True)
  52. sleep(2)
  53. r = nginxproxy.get(f"http://nginx-proxy.test/web4/port")
  54. assert r.status_code == 404