test_fallback.py 2.6 KB

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