test_fallback.py 6.7 KB

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