cloudflare/cloudflared
Publicmirrored from https://github.com/cloudflare/cloudflaredAvailable
component-tests/test_logging.py
92lines · modeblame
f23e33c0cthuang5 years ago | 1 | #!/usr/bin/env python |
| 2 | import json | |
| 3 | import os | |
| 4 | | |
61067372Nuno Diegues5 years ago | 5 | from util import start_cloudflared, wait_tunnel_ready, send_requests |
f23e33c0cthuang5 years ago | 6 | |
61067372Nuno Diegues5 years ago | 7 | # Rolling logger rotate log files after 1 MB |
| 8 | rotate_after_size = 1000 * 1000 | |
| 9 | default_log_file = "cloudflared.log" | |
bf068e72Nuno Diegues5 years ago | 10 | expect_message = "Starting Hello" |
61067372Nuno Diegues5 years ago | 11 | |
| 12 | | |
| 13 | def assert_log_to_terminal(cloudflared): | |
b25d38ddNuno Diegues5 years ago | 14 | stderr = cloudflared.stderr.read(1500) |
61067372Nuno Diegues5 years ago | 15 | assert expect_message.encode() in stderr, f"{stderr} doesn't contain {expect_message}" |
| 16 | | |
| 17 | | |
| 18 | def assert_log_in_file(file): | |
| 19 | with open(file, "r") as f: | |
738b4f8dNuno Diegues4 years ago | 20 | log = f.read(2000) |
61067372Nuno Diegues5 years ago | 21 | assert expect_message in log, f"{log} doesn't contain {expect_message}" |
| 22 | | |
| 23 | | |
| 24 | def assert_json_log(file): | |
| 25 | def assert_in_json(j, key): | |
| 26 | assert key in j, f"{key} is not in j" | |
| 27 | | |
| 28 | with open(file, "r") as f: | |
| 29 | line = f.readline() | |
| 30 | json_log = json.loads(line) | |
| 31 | assert_in_json(json_log, "level") | |
| 32 | assert_in_json(json_log, "time") | |
| 33 | assert_in_json(json_log, "message") | |
f23e33c0cthuang5 years ago | 34 | |
61067372Nuno Diegues5 years ago | 35 | |
| 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 | | |
| 50 | # One file is the current log file, the other is the rotated log file | |
| 51 | rotated_log_file_index = 0 if current_log_file_index == 1 else 1 | |
| 52 | rotated_file = log_dir / files[rotated_log_file_index] | |
| 53 | stats = os.stat(rotated_file) | |
| 54 | assert stats.st_size > rotate_after_size | |
| 55 | assert_log_in_file(rotated_file) | |
| 56 | assert_json_log(current_file) | |
| 57 | return | |
| 58 | | |
| 59 | raise Exception( | |
| 60 | f"Log file isn't rotated after sending {max_batches * batch_requests} requests") | |
| 61 | | |
| 62 | | |
| 63 | class TestLogging: | |
f23e33c0cthuang5 years ago | 64 | def test_logging_to_terminal(self, tmp_path, component_tests_config): |
| 65 | config = component_tests_config() | |
| 66 | with start_cloudflared(tmp_path, config, new_process=True) as cloudflared: | |
6a9ba612cthuang5 years ago | 67 | wait_tunnel_ready(tunnel_url=config.get_url()) |
61067372Nuno Diegues5 years ago | 68 | assert_log_to_terminal(cloudflared) |
f23e33c0cthuang5 years ago | 69 | |
| 70 | def test_logging_to_file(self, tmp_path, component_tests_config): | |
61067372Nuno Diegues5 years ago | 71 | log_file = tmp_path / default_log_file |
f23e33c0cthuang5 years ago | 72 | extra_config = { |
| 73 | # Convert from pathlib.Path to str | |
| 74 | "logfile": str(log_file), | |
| 75 | } | |
| 76 | config = component_tests_config(extra_config) | |
| 77 | with start_cloudflared(tmp_path, config, new_process=True, capture_output=False): | |
6a9ba612cthuang5 years ago | 78 | wait_tunnel_ready(tunnel_url=config.get_url()) |
61067372Nuno Diegues5 years ago | 79 | assert_log_in_file(log_file) |
| 80 | assert_json_log(log_file) | |
f23e33c0cthuang5 years ago | 81 | |
| 82 | def test_logging_to_dir(self, tmp_path, component_tests_config): | |
| 83 | log_dir = tmp_path / "logs" | |
| 84 | extra_config = { | |
| 85 | "loglevel": "debug", | |
| 86 | # Convert from pathlib.Path to str | |
| 87 | "log-directory": str(log_dir), | |
| 88 | } | |
| 89 | config = component_tests_config(extra_config) | |
| 90 | with start_cloudflared(tmp_path, config, new_process=True, capture_output=False): | |
6a9ba612cthuang5 years ago | 91 | wait_tunnel_ready(tunnel_url=config.get_url()) |
61067372Nuno Diegues5 years ago | 92 | assert_log_to_dir(config, log_dir) |