cloudflare/cloudflared

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
2021.3.2

Branches

Tags

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

Clone

HTTPS

Download ZIP

component-tests/test_logging.py

93lines · modeblame

f23e33c0cthuang5 years ago1#!/usr/bin/env python
2import json
3import os
4from util import start_cloudflared, wait_tunnel_ready, send_requests, LOGGER
5
6
7class TestLogging:
9df60276cthuang5 years ago8# TODO: Test logging when running as a service https://jira.cfops.it/browse/TUN-4082
f23e33c0cthuang5 years ago9# Rolling logger rotate log files after 1 MB
10rotate_after_size = 1000 * 1000
11default_log_file = "cloudflared.log"
12expect_message = "Starting tunnel"
13
14def test_logging_to_terminal(self, tmp_path, component_tests_config):
15config = component_tests_config()
16with start_cloudflared(tmp_path, config, new_process=True) as cloudflared:
6a9ba612cthuang5 years ago17wait_tunnel_ready(tunnel_url=config.get_url())
f23e33c0cthuang5 years ago18self.assert_log_to_terminal(cloudflared)
19
20def test_logging_to_file(self, tmp_path, component_tests_config):
21log_file = tmp_path / self.default_log_file
22extra_config = {
23# Convert from pathlib.Path to str
24"logfile": str(log_file),
25}
26config = component_tests_config(extra_config)
27with start_cloudflared(tmp_path, config, new_process=True, capture_output=False):
6a9ba612cthuang5 years ago28wait_tunnel_ready(tunnel_url=config.get_url())
f23e33c0cthuang5 years ago29self.assert_log_in_file(log_file)
30self.assert_json_log(log_file)
31
32def test_logging_to_dir(self, tmp_path, component_tests_config):
33log_dir = tmp_path / "logs"
34extra_config = {
35"loglevel": "debug",
36# Convert from pathlib.Path to str
37"log-directory": str(log_dir),
38}
39config = component_tests_config(extra_config)
40with start_cloudflared(tmp_path, config, new_process=True, capture_output=False):
6a9ba612cthuang5 years ago41wait_tunnel_ready(tunnel_url=config.get_url())
f23e33c0cthuang5 years ago42self.assert_log_to_dir(config, log_dir)
43
44def assert_log_to_terminal(self, cloudflared):
45stderr = cloudflared.stderr.read(200)
46# cloudflared logs the following when it first starts
47# 2021-03-10T12:30:39Z INF Starting tunnel tunnelID=<tunnel ID>
48assert self.expect_message.encode(
49) in stderr, f"{stderr} doesn't contain {self.expect_message}"
50
51def assert_log_in_file(self, file, expect_message=""):
52with open(file, "r") as f:
53log = f.read(200)
54# cloudflared logs the following when it first starts
55# {"level":"info","tunnelID":"<tunnel ID>","time":"2021-03-10T12:21:13Z","message":"Starting tunnel"}
56assert self.expect_message in log, f"{log} doesn't contain {self.expect_message}"
57
58def assert_json_log(self, file):
59with open(file, "r") as f:
60line = f.readline()
61json_log = json.loads(line)
62self.assert_in_json(json_log, "level")
63self.assert_in_json(json_log, "time")
64self.assert_in_json(json_log, "message")
65
66def assert_in_json(self, j, key):
67assert key in j, f"{key} is not in j"
68
69def assert_log_to_dir(self, config, log_dir):
70max_batches = 3
71batch_requests = 1000
72for _ in range(max_batches):
25cfbec0cthuang5 years ago73send_requests(config.get_url(),
f23e33c0cthuang5 years ago74batch_requests, require_ok=False)
75files = os.listdir(log_dir)
76if len(files) == 2:
77current_log_file_index = files.index(self.default_log_file)
78current_file = log_dir / files[current_log_file_index]
79stats = os.stat(current_file)
80assert stats.st_size > 0
81self.assert_json_log(current_file)
82
83# One file is the current log file, the other is the rotated log file
84rotated_log_file_index = 0 if current_log_file_index == 1 else 1
85rotated_file = log_dir / files[rotated_log_file_index]
86stats = os.stat(rotated_file)
87assert stats.st_size > self.rotate_after_size
88self.assert_log_in_file(rotated_file)
89self.assert_json_log(current_file)
90return
91
92raise Exception(
93f"Log file isn't rotated after sending {max_batches * batch_requests} requests")