test_logging.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #!/usr/bin/env python
  2. import json
  3. import os
  4. from constants import MAX_LOG_LINES
  5. from util import start_cloudflared, wait_tunnel_ready, send_requests
  6. # Rolling logger rotate log files after 1 MB
  7. rotate_after_size = 1000 * 1000
  8. default_log_file = "cloudflared.log"
  9. expect_message = "Starting Hello"
  10. def assert_log_to_terminal(cloudflared):
  11. for _ in range(0, MAX_LOG_LINES):
  12. line = cloudflared.stderr.readline()
  13. if not line:
  14. break
  15. if expect_message.encode() in line:
  16. return
  17. raise Exception(f"terminal log doesn't contain {expect_message}")
  18. def assert_log_in_file(file):
  19. with open(file, "r") as f:
  20. for _ in range(0, MAX_LOG_LINES):
  21. line = f.readline()
  22. if not line:
  23. break
  24. if expect_message in line:
  25. return
  26. raise Exception(f"log file doesn't contain {expect_message}")
  27. def assert_json_log(file):
  28. def assert_in_json(j, key):
  29. assert key in j, f"{key} is not in j"
  30. with open(file, "r") as f:
  31. line = f.readline()
  32. json_log = json.loads(line)
  33. assert_in_json(json_log, "level")
  34. assert_in_json(json_log, "time")
  35. assert_in_json(json_log, "message")
  36. def assert_log_to_dir(config, log_dir):
  37. max_batches = 3
  38. batch_requests = 1000
  39. for _ in range(max_batches):
  40. send_requests(config.get_url(),
  41. batch_requests, require_ok=False)
  42. files = os.listdir(log_dir)
  43. if len(files) == 2:
  44. current_log_file_index = files.index(default_log_file)
  45. current_file = log_dir / files[current_log_file_index]
  46. stats = os.stat(current_file)
  47. assert stats.st_size > 0
  48. assert_json_log(current_file)
  49. # One file is the current log file, the other is the rotated log file
  50. rotated_log_file_index = 0 if current_log_file_index == 1 else 1
  51. rotated_file = log_dir / files[rotated_log_file_index]
  52. stats = os.stat(rotated_file)
  53. assert stats.st_size > rotate_after_size
  54. assert_log_in_file(rotated_file)
  55. assert_json_log(current_file)
  56. return
  57. raise Exception(
  58. f"Log file isn't rotated after sending {max_batches * batch_requests} requests")
  59. class TestLogging:
  60. def test_logging_to_terminal(self, tmp_path, component_tests_config):
  61. config = component_tests_config()
  62. with start_cloudflared(tmp_path, config, cfd_pre_args=["tunnel", "--ha-connections", "1"], new_process=True) as cloudflared:
  63. wait_tunnel_ready(tunnel_url=config.get_url())
  64. assert_log_to_terminal(cloudflared)
  65. def test_logging_to_file(self, tmp_path, component_tests_config):
  66. log_file = tmp_path / default_log_file
  67. extra_config = {
  68. # Convert from pathlib.Path to str
  69. "logfile": str(log_file),
  70. }
  71. config = component_tests_config(extra_config)
  72. with start_cloudflared(tmp_path, config, cfd_pre_args=["tunnel", "--ha-connections", "1"], new_process=True, capture_output=False):
  73. wait_tunnel_ready(tunnel_url=config.get_url(), cfd_logs=str(log_file))
  74. assert_log_in_file(log_file)
  75. assert_json_log(log_file)
  76. def test_logging_to_dir(self, tmp_path, component_tests_config):
  77. log_dir = tmp_path / "logs"
  78. extra_config = {
  79. "loglevel": "debug",
  80. # Convert from pathlib.Path to str
  81. "log-directory": str(log_dir),
  82. }
  83. config = component_tests_config(extra_config)
  84. with start_cloudflared(tmp_path, config, cfd_pre_args=["tunnel", "--ha-connections", "1"], new_process=True, capture_output=False):
  85. wait_tunnel_ready(tunnel_url=config.get_url(), cfd_logs=str(log_dir))
  86. assert_log_to_dir(config, log_dir)