2
0

test_fallback.py 6.6 KB

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