cloudflare/cloudflared

Public

mirrored from https://github.com/cloudflare/cloudflaredAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
2021.12.3

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

component-tests/test_logging.py

92lines · modeblame

f23e33c0cthuang5 years ago1#!/usr/bin/env python
2import json
3import os
4
61067372Nuno Diegues5 years ago5from util import start_cloudflared, wait_tunnel_ready, send_requests
f23e33c0cthuang5 years ago6
61067372Nuno Diegues5 years ago7# Rolling logger rotate log files after 1 MB
8rotate_after_size = 1000 * 1000
9default_log_file = "cloudflared.log"
bf068e72Nuno Diegues5 years ago10expect_message = "Starting Hello"
61067372Nuno Diegues5 years ago11
12
13def assert_log_to_terminal(cloudflared):
b25d38ddNuno Diegues5 years ago14stderr = cloudflared.stderr.read(1500)
61067372Nuno Diegues5 years ago15assert expect_message.encode() in stderr, f"{stderr} doesn't contain {expect_message}"
16
17
18def assert_log_in_file(file):
19with open(file, "r") as f:
738b4f8dNuno Diegues4 years ago20log = f.read(2000)
61067372Nuno Diegues5 years ago21assert expect_message in log, f"{log} doesn't contain {expect_message}"
22
23
24def assert_json_log(file):
25def assert_in_json(j, key):
26assert key in j, f"{key} is not in j"
27
28with open(file, "r") as f:
29line = f.readline()
30json_log = json.loads(line)
31assert_in_json(json_log, "level")
32assert_in_json(json_log, "time")
33assert_in_json(json_log, "message")
f23e33c0cthuang5 years ago34
61067372Nuno Diegues5 years ago35
36def assert_log_to_dir(config, log_dir):
37max_batches = 3
38batch_requests = 1000
39for _ in range(max_batches):
40send_requests(config.get_url(),
41batch_requests, require_ok=False)
42files = os.listdir(log_dir)
43if len(files) == 2:
44current_log_file_index = files.index(default_log_file)
45current_file = log_dir / files[current_log_file_index]
46stats = os.stat(current_file)
47assert stats.st_size > 0
48assert_json_log(current_file)
49
50# One file is the current log file, the other is the rotated log file
51rotated_log_file_index = 0 if current_log_file_index == 1 else 1
52rotated_file = log_dir / files[rotated_log_file_index]
53stats = os.stat(rotated_file)
54assert stats.st_size > rotate_after_size
55assert_log_in_file(rotated_file)
56assert_json_log(current_file)
57return
58
59raise Exception(
60f"Log file isn't rotated after sending {max_batches * batch_requests} requests")
61
62
63class TestLogging:
f23e33c0cthuang5 years ago64def test_logging_to_terminal(self, tmp_path, component_tests_config):
65config = component_tests_config()
66with start_cloudflared(tmp_path, config, new_process=True) as cloudflared:
6a9ba612cthuang5 years ago67wait_tunnel_ready(tunnel_url=config.get_url())
61067372Nuno Diegues5 years ago68assert_log_to_terminal(cloudflared)
f23e33c0cthuang5 years ago69
70def test_logging_to_file(self, tmp_path, component_tests_config):
61067372Nuno Diegues5 years ago71log_file = tmp_path / default_log_file
f23e33c0cthuang5 years ago72extra_config = {
73# Convert from pathlib.Path to str
74"logfile": str(log_file),
75}
76config = component_tests_config(extra_config)
77with start_cloudflared(tmp_path, config, new_process=True, capture_output=False):
794635fbNuno Diegues4 years ago78wait_tunnel_ready(tunnel_url=config.get_url(), cfd_logs=str(log_file))
61067372Nuno Diegues5 years ago79assert_log_in_file(log_file)
80assert_json_log(log_file)
f23e33c0cthuang5 years ago81
82def test_logging_to_dir(self, tmp_path, component_tests_config):
83log_dir = tmp_path / "logs"
84extra_config = {
85"loglevel": "debug",
86# Convert from pathlib.Path to str
87"log-directory": str(log_dir),
88}
89config = component_tests_config(extra_config)
90with start_cloudflared(tmp_path, config, new_process=True, capture_output=False):
794635fbNuno Diegues4 years ago91wait_tunnel_ready(tunnel_url=config.get_url(), cfd_logs=str(log_dir))
61067372Nuno Diegues5 years ago92assert_log_to_dir(config, log_dir)