test_http.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. from __future__ import annotations
  2. from typing import Any
  3. from django.test.utils import override_settings
  4. from sentry.services.http import SentryHTTPServer, convert_options_to_env
  5. from sentry.testutils.cases import TestCase
  6. class HTTPServiceTest(TestCase):
  7. def test_convert(self):
  8. options = {
  9. "true": True,
  10. "false": False,
  11. "string": "foo",
  12. "int": 1,
  13. "none": None,
  14. "hy-phen": "foo",
  15. }
  16. expected = [
  17. ("UWSGI_TRUE", "true"),
  18. ("UWSGI_FALSE", "false"),
  19. ("UWSGI_STRING", "foo"),
  20. ("UWSGI_INT", "1"),
  21. ("UWSGI_HY_PHEN", "foo"),
  22. ]
  23. assert set(convert_options_to_env(options)) == set(expected)
  24. def test_options(self):
  25. cls = SentryHTTPServer
  26. server = cls(host="1.1.1.1", port=80)
  27. assert server.options["http-socket"] == "1.1.1.1:80"
  28. with override_settings(SENTRY_WEB_HOST="1.1.1.1", SENTRY_WEB_PORT=80):
  29. assert server.options["http-socket"] == "1.1.1.1:80"
  30. server = cls(workers=10)
  31. assert server.options["workers"] == 10
  32. # Make sure that changing `protocol` to uwsgi sets the right socket
  33. options: dict[str, Any] = {"protocol": "uwsgi"}
  34. with override_settings(SENTRY_WEB_OPTIONS=options):
  35. server = cls()
  36. assert "http-socket" not in server.options
  37. assert "uwsgi-socket" in server.options
  38. options = {
  39. "bind": "1.1.1.1:80",
  40. "accesslog": "/tmp/access.log",
  41. "errorlog": "/tmp/error.log",
  42. "timeout": 69,
  43. "proc_name": "LOL",
  44. "secure_scheme_headers": {},
  45. "loglevel": "info",
  46. }
  47. with override_settings(SENTRY_WEB_OPTIONS=options):
  48. server = cls()
  49. assert server.options["http-socket"] == "1.1.1.1:80"
  50. assert "bind" not in server.options
  51. assert server.options["logto"] == "/tmp/access.log"
  52. assert "accesslog" not in server.options
  53. assert server.options["logto2"] == "/tmp/error.log"
  54. assert "errorlog" not in server.options
  55. assert server.options["http-timeout"] == 69
  56. assert "timeout" not in server.options
  57. assert server.options["procname-prefix-spaced"] == "LOL"
  58. assert "proc_name" not in server.options
  59. assert "secure_scheme_headers" not in server.options
  60. assert "loglevel" not in server.options
  61. def test_format_logs(self):
  62. with self.options({"system.logging-format": "human"}):
  63. server = SentryHTTPServer()
  64. assert server.options["disable-logging"] is False
  65. with self.options({"system.logging-format": "machine"}):
  66. server = SentryHTTPServer()
  67. assert server.options["disable-logging"] is True