test_fallback.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import os.path
  2. import backoff
  3. import pytest
  4. import requests
  5. @pytest.fixture
  6. def data_dir():
  7. return f"{os.path.splitext(__file__)[0]}.data"
  8. @pytest.fixture
  9. def docker_compose_file(data_dir, compose_file):
  10. return os.path.join(data_dir, compose_file)
  11. @pytest.fixture
  12. def get(docker_compose, nginxproxy, want_err_re):
  13. @backoff.on_exception(
  14. backoff.constant,
  15. requests.exceptions.RequestException,
  16. giveup=lambda e: want_err_re and want_err_re.search(str(e)),
  17. interval=.3,
  18. max_tries=30,
  19. jitter=None)
  20. def _get(url):
  21. return nginxproxy.get(url, allow_redirects=False)
  22. return _get
  23. @pytest.mark.parametrize("compose_file,url,want_code,want_err_re", [
  24. # Has default.crt.
  25. ("withdefault.yml", "http://https-and-http.nginx-proxy.test/", 301, None),
  26. ("withdefault.yml", "https://https-and-http.nginx-proxy.test/", 200, None),
  27. ("withdefault.yml", "http://https-only.nginx-proxy.test/", 503, None),
  28. ("withdefault.yml", "https://https-only.nginx-proxy.test/", 200, None),
  29. ("withdefault.yml", "http://http-only.nginx-proxy.test/", 200, None),
  30. ("withdefault.yml", "https://http-only.nginx-proxy.test/", 503, None),
  31. ("withdefault.yml", "http://missing-cert.nginx-proxy.test/", 200, None),
  32. ("withdefault.yml", "https://missing-cert.nginx-proxy.test/", 500, None),
  33. ("withdefault.yml", "http://unknown.nginx-proxy.test/", 503, None),
  34. ("withdefault.yml", "https://unknown.nginx-proxy.test/", 503, None),
  35. ])
  36. def test_fallback(get, url, want_code, want_err_re):
  37. if want_err_re is None:
  38. r = get(url)
  39. assert r.status_code == want_code
  40. else:
  41. with pytest.raises(requests.exceptions.RequestException, match=want_err_re):
  42. get(url)