Logging handler to send logs to your OpenSearch cluster with bulk SSL. Forked from https://github.com/logzio/logzio-python-handler
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

144 lines
4.7 KiB

  1. import fnmatch
  2. import logging.config
  3. import os
  4. import time
  5. from unittest import TestCase
  6. from .mockLogzioListener import listener
  7. def _find(pattern, path):
  8. result = []
  9. for root, dirs, files in os.walk(path):
  10. for name in files:
  11. if fnmatch.fnmatch(name, pattern):
  12. result.append(os.path.join(root, name))
  13. break # Not descending recursively
  14. return result
  15. class TestLogzioSender(TestCase):
  16. def setUp(self):
  17. self.logzio_listener = listener.MockLogzioListener()
  18. self.logzio_listener.clear_logs_buffer()
  19. self.logzio_listener.clear_server_error()
  20. self.logs_drain_timeout = 1
  21. logging_configuration = {
  22. "version": 1,
  23. "formatters": {
  24. "logzio": {
  25. "format": '{"key": "value"}'
  26. }
  27. },
  28. "handlers": {
  29. "LogzioHandler": {
  30. "class": "logzio.handler.LogzioHandler",
  31. "formatter": "logzio",
  32. "level": "DEBUG",
  33. "token": "token",
  34. 'logzio_type': "type",
  35. 'logs_drain_timeout': self.logs_drain_timeout,
  36. 'url': "http://" + self.logzio_listener.get_host() + ":" + str(self.logzio_listener.get_port()),
  37. 'debug': True
  38. }
  39. },
  40. "loggers": {
  41. "test": {
  42. "handlers": ["LogzioHandler"],
  43. "level": "DEBUG"
  44. }
  45. }
  46. }
  47. logging.config.dictConfig(logging_configuration)
  48. self.logger = logging.getLogger('test')
  49. for curr_file in _find("logzio-failures-*.txt", "."):
  50. os.remove(curr_file)
  51. def test_simple_log_drain(self):
  52. log_message = "Test simple log drain"
  53. self.logger.info(log_message)
  54. time.sleep(self.logs_drain_timeout * 2)
  55. self.assertTrue(self.logzio_listener.find_log(log_message))
  56. def test_multiple_lines_drain(self):
  57. logs_num = 50
  58. for counter in range(0, logs_num):
  59. self.logger.info("Test " + str(counter))
  60. time.sleep(self.logs_drain_timeout * 2)
  61. for counter in range(0, logs_num):
  62. self.logger.info("Test " + str(counter))
  63. time.sleep(self.logs_drain_timeout * 2)
  64. self.assertEqual(self.logzio_listener.get_number_of_logs(), logs_num * 2)
  65. def test_server_failure(self):
  66. log_message = "Failing log message"
  67. self.logzio_listener.set_server_error()
  68. self.logger.info(log_message)
  69. time.sleep(self.logs_drain_timeout * 2)
  70. self.assertFalse(self.logzio_listener.find_log(log_message))
  71. self.logzio_listener.clear_server_error()
  72. time.sleep(self.logs_drain_timeout * 2 * 4) # Longer, because of the retry
  73. self.assertTrue(self.logzio_listener.find_log(log_message))
  74. def test_local_file_backup(self):
  75. log_message = "Backup to local filesystem"
  76. self.logzio_listener.set_server_error()
  77. self.logger.info(log_message)
  78. # Make sure no file is present
  79. self.assertEqual(len(_find("logzio-failures-*.txt", ".")), 0)
  80. time.sleep(2 * 2 * 2 * 2 * 2) # All of the retries
  81. failure_files = _find("logzio-failures-*.txt", ".")
  82. self.assertEqual(len(failure_files), 1)
  83. with open(failure_files[0], "r") as f:
  84. line = f.readline()
  85. self.assertTrue(log_message in line)
  86. def test_local_file_backup_disabled(self):
  87. log_message = "Backup to local filesystem"
  88. self.logzio_listener.set_server_error()
  89. self.logger.handlers[0].logzio_sender.backup_logs = False
  90. self.logger.info(log_message)
  91. # Make sure no file is present
  92. self.assertEqual(len(_find("logzio-failures-*.txt", ".")), 0)
  93. time.sleep(2 * 2 * 2 * 2 * 2) # All of the retries
  94. # Make sure no file was created
  95. self.assertEqual(len(_find("logzio-failures-*.txt", ".")), 0)
  96. def test_can_send_after_fork(self):
  97. childpid = os.fork()
  98. child_log_message = 'logged from child process'
  99. parent_log_message = 'logged from parent process'
  100. if childpid == 0:
  101. # Log from the child process
  102. self.logger.info(child_log_message)
  103. time.sleep(self.logs_drain_timeout * 2)
  104. os._exit(0)
  105. # Wait for the child process to finish
  106. os.waitpid(childpid, 0)
  107. # log from the parent process
  108. self.logger.info(parent_log_message)
  109. time.sleep(self.logs_drain_timeout * 2)
  110. # Ensure listener receive all log messages
  111. self.assertTrue(self.logzio_listener.find_log(child_log_message))
  112. self.assertTrue(self.logzio_listener.find_log(parent_log_message))