2
0

test_fallback.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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.SSLError,
  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_UNRECOGNIZED_NAME")
  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/", 301, None),
  34. ("withdefault.yml", "https://missing-cert.nginx-proxy.test/", 200, None),
  35. ("withdefault.yml", "http://missing-cert.default-untrusted.nginx-proxy.test/", 200, None),
  36. ("withdefault.yml", "https://missing-cert.default-untrusted.nginx-proxy.test/", None, INTERNAL_ERR_RE),
  37. ("withdefault.yml", "http://unknown.nginx-proxy.test/", 503, None),
  38. ("withdefault.yml", "https://unknown.nginx-proxy.test/", 503, None),
  39. # Same as withdefault.yml, except there is no default.crt.
  40. ("nodefault.yml", "http://https-and-http.nginx-proxy.test/", 301, None),
  41. ("nodefault.yml", "https://https-and-http.nginx-proxy.test/", 200, None),
  42. ("nodefault.yml", "http://https-only.nginx-proxy.test/", 503, None),
  43. ("nodefault.yml", "https://https-only.nginx-proxy.test/", 200, None),
  44. ("nodefault.yml", "http://http-only.nginx-proxy.test/", 200, None),
  45. ("nodefault.yml", "https://http-only.nginx-proxy.test/", None, INTERNAL_ERR_RE),
  46. ("nodefault.yml", "http://missing-cert.nginx-proxy.test/", 200, None),
  47. ("nodefault.yml", "https://missing-cert.nginx-proxy.test/", None, INTERNAL_ERR_RE),
  48. ("nodefault.yml", "http://unknown.nginx-proxy.test/", 503, None),
  49. ("nodefault.yml", "https://unknown.nginx-proxy.test/", None, INTERNAL_ERR_RE),
  50. # HTTPS_METHOD=nohttp on nginx-proxy, HTTPS_METHOD unset on the app container.
  51. ("nohttp.yml", "http://https-only.nginx-proxy.test/", 503, None),
  52. ("nohttp.yml", "https://https-only.nginx-proxy.test/", 200, None),
  53. ("nohttp.yml", "http://unknown.nginx-proxy.test/", 503, None),
  54. ("nohttp.yml", "https://unknown.nginx-proxy.test/", 503, None),
  55. # HTTPS_METHOD=redirect on nginx-proxy, HTTPS_METHOD=nohttp on the app container.
  56. ("nohttp-on-app.yml", "http://https-only.nginx-proxy.test/", 503, None),
  57. ("nohttp-on-app.yml", "https://https-only.nginx-proxy.test/", 200, None),
  58. ("nohttp-on-app.yml", "http://unknown.nginx-proxy.test/", 503, None),
  59. ("nohttp-on-app.yml", "https://unknown.nginx-proxy.test/", 503, None),
  60. # Same as nohttp.yml, except there are two vhosts with a missing cert, the second
  61. # one being configured not to trust the default certificate. This causes its
  62. # HTTPS_METHOD=nohttp setting to effectively become HTTPS_METHOD=noredirect.
  63. ("nohttp-with-missing-cert.yml", "http://https-only.nginx-proxy.test/", 503, None),
  64. ("nohttp-with-missing-cert.yml", "https://https-only.nginx-proxy.test/", 200, None),
  65. ("nohttp-with-missing-cert.yml", "http://missing-cert.nginx-proxy.test/", 503, None),
  66. ("nohttp-with-missing-cert.yml", "https://missing-cert.nginx-proxy.test/", 200, None),
  67. ("nohttp-with-missing-cert.yml", "http://missing-cert.default-untrusted.nginx-proxy.test/", 200, None),
  68. ("nohttp-with-missing-cert.yml", "https://missing-cert.default-untrusted.nginx-proxy.test/", None, INTERNAL_ERR_RE),
  69. ("nohttp-with-missing-cert.yml", "http://unknown.nginx-proxy.test/", 503, None),
  70. ("nohttp-with-missing-cert.yml", "https://unknown.nginx-proxy.test/", 503, None),
  71. # HTTPS_METHOD=nohttps on nginx-proxy, HTTPS_METHOD unset on the app container.
  72. ("nohttps.yml", "http://http-only.nginx-proxy.test/", 200, None),
  73. ("nohttps.yml", "https://http-only.nginx-proxy.test/", None, INTERNAL_ERR_RE),
  74. ("nohttps.yml", "http://unknown.nginx-proxy.test/", 503, None),
  75. ("nohttps.yml", "https://unknown.nginx-proxy.test/", None, INTERNAL_ERR_RE),
  76. # HTTPS_METHOD=redirect on nginx-proxy, HTTPS_METHOD=nohttps on the app container.
  77. ("nohttps-on-app.yml", "http://http-only.nginx-proxy.test/", 200, None),
  78. ("nohttps-on-app.yml", "https://http-only.nginx-proxy.test/", None, INTERNAL_ERR_RE),
  79. ("nohttps-on-app.yml", "http://unknown.nginx-proxy.test/", 503, None),
  80. ("nohttps-on-app.yml", "https://unknown.nginx-proxy.test/", None, INTERNAL_ERR_RE),
  81. # Custom nginx config that has a `server` directive that uses `default_server` and simply
  82. # returns 418. Nginx should successfully start (in particular, the `default_server` in the
  83. # custom config should not conflict with the fallback server generated by nginx-proxy) and nginx
  84. # should prefer that server for handling requests for unknown vhosts.
  85. ("custom-fallback.yml", "http://unknown.nginx-proxy.test/", 418, None),
  86. ])
  87. def test_fallback(get, url, want_code, want_err_re):
  88. if want_err_re is None:
  89. r = get(url)
  90. assert r.status_code == want_code
  91. else:
  92. with pytest.raises(requests.exceptions.SSLError, match=want_err_re):
  93. get(url)