test_fallback.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import pathlib
  2. import re
  3. from typing import List, Callable
  4. import backoff
  5. import pytest
  6. import requests
  7. from requests import Response
  8. @pytest.fixture
  9. def docker_compose_files(compose_file) -> List[str]:
  10. data_dir = pathlib.Path(__file__).parent.joinpath("test_fallback.data")
  11. return [
  12. data_dir.joinpath("compose.base.yml"),
  13. data_dir.joinpath(compose_file).as_posix()
  14. ]
  15. @pytest.fixture
  16. def get(docker_compose, nginxproxy, want_err_re: re.Pattern[str]) -> Callable[[str], Response]:
  17. @backoff.on_exception(
  18. backoff.constant,
  19. requests.exceptions.SSLError,
  20. giveup=lambda e: want_err_re and bool(want_err_re.search(str(e))),
  21. interval=.3,
  22. max_tries=30,
  23. jitter=None)
  24. def _get(url) -> Response:
  25. return nginxproxy.get(url, allow_redirects=False)
  26. return _get
  27. INTERNAL_ERR_RE = re.compile("TLSV1_UNRECOGNIZED_NAME")
  28. @pytest.mark.parametrize("compose_file,url,want_code,want_err_re", [
  29. # Has default.crt.
  30. ("withdefault.yml", "http://https-and-http.nginx-proxy.test/", 301, None),
  31. ("withdefault.yml", "https://https-and-http.nginx-proxy.test/", 200, None),
  32. ("withdefault.yml", "http://https-only.nginx-proxy.test/", 503, None),
  33. ("withdefault.yml", "https://https-only.nginx-proxy.test/", 200, None),
  34. ("withdefault.yml", "http://http-only.nginx-proxy.test/", 200, None),
  35. ("withdefault.yml", "https://http-only.nginx-proxy.test/", 503, None),
  36. ("withdefault.yml", "http://missing-cert.nginx-proxy.test/", 301, None),
  37. ("withdefault.yml", "https://missing-cert.nginx-proxy.test/", 200, None),
  38. ("withdefault.yml", "http://missing-cert.default-untrusted.nginx-proxy.test/", 200, None),
  39. ("withdefault.yml", "https://missing-cert.default-untrusted.nginx-proxy.test/", None, INTERNAL_ERR_RE),
  40. ("withdefault.yml", "http://unknown.nginx-proxy.test/", 503, None),
  41. ("withdefault.yml", "https://unknown.nginx-proxy.test/", 503, None),
  42. # Same as withdefault.yml, except default.crt is not trusted (TRUST_DEFAULT_CERT=false).
  43. ("untrusteddefault.yml", "http://https-and-http.nginx-proxy.test/", 301, None),
  44. ("untrusteddefault.yml", "https://https-and-http.nginx-proxy.test/", 200, None),
  45. ("untrusteddefault.yml", "http://https-only.nginx-proxy.test/", 503, None),
  46. ("untrusteddefault.yml", "https://https-only.nginx-proxy.test/", 200, None),
  47. ("untrusteddefault.yml", "http://http-only.nginx-proxy.test/", 200, None),
  48. ("untrusteddefault.yml", "https://http-only.nginx-proxy.test/", 503, None),
  49. ("untrusteddefault.yml", "http://missing-cert.nginx-proxy.test/", 200, None),
  50. ("untrusteddefault.yml", "https://missing-cert.nginx-proxy.test/", None, INTERNAL_ERR_RE),
  51. ("untrusteddefault.yml", "http://unknown.nginx-proxy.test/", 503, None),
  52. ("untrusteddefault.yml", "https://unknown.nginx-proxy.test/", 503, None),
  53. # Same as withdefault.yml, except there is no default.crt.
  54. ("nodefault.yml", "http://https-and-http.nginx-proxy.test/", 301, None),
  55. ("nodefault.yml", "https://https-and-http.nginx-proxy.test/", 200, None),
  56. ("nodefault.yml", "http://https-only.nginx-proxy.test/", 503, None),
  57. ("nodefault.yml", "https://https-only.nginx-proxy.test/", 200, None),
  58. ("nodefault.yml", "http://http-only.nginx-proxy.test/", 200, None),
  59. ("nodefault.yml", "https://http-only.nginx-proxy.test/", None, INTERNAL_ERR_RE),
  60. ("nodefault.yml", "http://missing-cert.nginx-proxy.test/", 200, None),
  61. ("nodefault.yml", "https://missing-cert.nginx-proxy.test/", None, INTERNAL_ERR_RE),
  62. ("nodefault.yml", "http://unknown.nginx-proxy.test/", 503, None),
  63. ("nodefault.yml", "https://unknown.nginx-proxy.test/", None, INTERNAL_ERR_RE),
  64. # HTTPS_METHOD=nohttp on nginx-proxy, HTTPS_METHOD unset on the app container.
  65. ("nohttp.yml", "http://https-only.nginx-proxy.test/", 503, None),
  66. ("nohttp.yml", "https://https-only.nginx-proxy.test/", 200, None),
  67. ("nohttp.yml", "http://unknown.nginx-proxy.test/", 503, None),
  68. ("nohttp.yml", "https://unknown.nginx-proxy.test/", 503, None),
  69. # HTTPS_METHOD=redirect on nginx-proxy, HTTPS_METHOD=nohttp on the app container.
  70. ("nohttp-on-app.yml", "http://https-only.nginx-proxy.test/", 503, None),
  71. ("nohttp-on-app.yml", "https://https-only.nginx-proxy.test/", 200, None),
  72. ("nohttp-on-app.yml", "http://unknown.nginx-proxy.test/", 503, None),
  73. ("nohttp-on-app.yml", "https://unknown.nginx-proxy.test/", 503, None),
  74. # Same as nohttp.yml, except there are two vhosts with a missing cert, the second
  75. # one being configured not to trust the default certificate. This causes its
  76. # HTTPS_METHOD=nohttp setting to effectively become HTTPS_METHOD=noredirect.
  77. ("nohttp-with-missing-cert.yml", "http://https-only.nginx-proxy.test/", 503, None),
  78. ("nohttp-with-missing-cert.yml", "https://https-only.nginx-proxy.test/", 200, None),
  79. ("nohttp-with-missing-cert.yml", "http://missing-cert.nginx-proxy.test/", 503, None),
  80. ("nohttp-with-missing-cert.yml", "https://missing-cert.nginx-proxy.test/", 200, None),
  81. ("nohttp-with-missing-cert.yml", "http://missing-cert.default-untrusted.nginx-proxy.test/", 200, None),
  82. ("nohttp-with-missing-cert.yml", "https://missing-cert.default-untrusted.nginx-proxy.test/", None, INTERNAL_ERR_RE),
  83. ("nohttp-with-missing-cert.yml", "http://unknown.nginx-proxy.test/", 503, None),
  84. ("nohttp-with-missing-cert.yml", "https://unknown.nginx-proxy.test/", 503, None),
  85. # HTTPS_METHOD=nohttps on nginx-proxy, HTTPS_METHOD unset on the app container.
  86. ("nohttps.yml", "http://http-only.nginx-proxy.test/", 200, None),
  87. ("nohttps.yml", "https://http-only.nginx-proxy.test/", None, INTERNAL_ERR_RE),
  88. ("nohttps.yml", "http://unknown.nginx-proxy.test/", 503, None),
  89. ("nohttps.yml", "https://unknown.nginx-proxy.test/", None, INTERNAL_ERR_RE),
  90. # HTTPS_METHOD=redirect on nginx-proxy, HTTPS_METHOD=nohttps on the app container.
  91. ("nohttps-on-app.yml", "http://http-only.nginx-proxy.test/", 200, None),
  92. ("nohttps-on-app.yml", "https://http-only.nginx-proxy.test/", None, INTERNAL_ERR_RE),
  93. ("nohttps-on-app.yml", "http://unknown.nginx-proxy.test/", 503, None),
  94. ("nohttps-on-app.yml", "https://unknown.nginx-proxy.test/", None, INTERNAL_ERR_RE),
  95. # Custom nginx config that has a `server` directive that uses `default_server` and simply
  96. # returns 418. Nginx should successfully start (in particular, the `default_server` in the
  97. # custom config should not conflict with the fallback server generated by nginx-proxy) and nginx
  98. # should prefer that server for handling requests for unknown vhosts.
  99. ("custom-fallback.yml", "http://unknown.nginx-proxy.test/", 418, None),
  100. ])
  101. def test_fallback(get, compose_file, url, want_code, want_err_re):
  102. if want_err_re is None:
  103. r = get(url)
  104. assert r.status_code == want_code
  105. else:
  106. with pytest.raises(requests.exceptions.SSLError, match=want_err_re):
  107. get(url)