mirror of
https://github.com/cloudflare/cloudflared.git
synced 2025-07-27 18:39:58 +00:00
TUN-4082: Test logging when running as a service
This commit is contained in:
@@ -1,12 +1,14 @@
|
||||
#!/usr/bin/env python
|
||||
from contextlib import contextmanager
|
||||
import os
|
||||
from pathlib import Path
|
||||
import platform
|
||||
import pytest
|
||||
import subprocess
|
||||
from contextlib import contextmanager
|
||||
from pathlib import Path
|
||||
|
||||
from util import start_cloudflared, cloudflared_cmd, wait_tunnel_ready, LOGGER
|
||||
import pytest
|
||||
|
||||
import test_logging
|
||||
from util import start_cloudflared, wait_tunnel_ready
|
||||
|
||||
|
||||
def select_platform(plat):
|
||||
@@ -22,34 +24,94 @@ def default_config_file():
|
||||
return os.path.join(default_config_dir(), "config.yml")
|
||||
|
||||
|
||||
class TestServiceMode():
|
||||
class TestServiceMode:
|
||||
@select_platform("Darwin")
|
||||
@pytest.mark.skipif(os.path.exists(default_config_file()), reason=f"There is already a config file in default path")
|
||||
def test_launchd_service(self, component_tests_config):
|
||||
# On Darwin cloudflared service defaults to run classic tunnel command
|
||||
def test_launchd_service_log_to_file(self, tmp_path, component_tests_config):
|
||||
log_file = tmp_path / test_logging.default_log_file
|
||||
additional_config = {
|
||||
# On Darwin cloudflared service defaults to run classic tunnel command
|
||||
"hello-world": True,
|
||||
"logfile": str(log_file),
|
||||
}
|
||||
config = component_tests_config(
|
||||
additional_config=additional_config, named_tunnel=False)
|
||||
config = component_tests_config(additional_config=additional_config, named_tunnel=False)
|
||||
|
||||
def assert_log_file():
|
||||
test_logging.assert_log_in_file(log_file)
|
||||
test_logging.assert_json_log(log_file)
|
||||
|
||||
self.launchd_service_scenario(config, assert_log_file)
|
||||
|
||||
@select_platform("Darwin")
|
||||
@pytest.mark.skipif(os.path.exists(default_config_file()), reason=f"There is already a config file in default path")
|
||||
def test_launchd_service_rotating_log(self, tmp_path, component_tests_config):
|
||||
log_dir = tmp_path / "logs"
|
||||
additional_config = {
|
||||
# On Darwin cloudflared service defaults to run classic tunnel command
|
||||
"hello-world": True,
|
||||
"loglevel": "debug",
|
||||
"log-directory": str(log_dir),
|
||||
}
|
||||
config = component_tests_config(additional_config=additional_config, named_tunnel=False)
|
||||
|
||||
def assert_rotating_log():
|
||||
test_logging.assert_log_to_dir(config, log_dir)
|
||||
|
||||
self.launchd_service_scenario(config, assert_rotating_log)
|
||||
|
||||
def launchd_service_scenario(self, config, extra_assertions):
|
||||
with self.run_service(Path(default_config_dir()), config):
|
||||
self.launchctl_cmd("list")
|
||||
self.launchctl_cmd("start")
|
||||
wait_tunnel_ready(tunnel_url=config.get_url())
|
||||
extra_assertions()
|
||||
self.launchctl_cmd("stop")
|
||||
|
||||
os.remove(default_config_file())
|
||||
self.launchctl_cmd("list", success=False)
|
||||
|
||||
@select_platform("Linux")
|
||||
@pytest.mark.skipif(os.path.exists("/etc/cloudflared/config.yml"), reason=f"There is already a config file in default path")
|
||||
def test_sysv_service(self, tmp_path, component_tests_config):
|
||||
config = component_tests_config()
|
||||
@pytest.mark.skipif(os.path.exists("/etc/cloudflared/config.yml"),
|
||||
reason=f"There is already a config file in default path")
|
||||
def test_sysv_service_log_to_file(self, tmp_path, component_tests_config):
|
||||
log_file = tmp_path / test_logging.default_log_file
|
||||
additional_config = {
|
||||
"logfile": str(log_file),
|
||||
}
|
||||
config = component_tests_config(additional_config=additional_config)
|
||||
|
||||
def assert_log_file():
|
||||
test_logging.assert_log_in_file(log_file)
|
||||
test_logging.assert_json_log(log_file)
|
||||
|
||||
self.sysv_service_scenario(config, tmp_path, assert_log_file)
|
||||
|
||||
@select_platform("Linux")
|
||||
@pytest.mark.skipif(os.path.exists("/etc/cloudflared/config.yml"),
|
||||
reason=f"There is already a config file in default path")
|
||||
def test_sysv_service_rotating_log(self, tmp_path, component_tests_config):
|
||||
log_dir = tmp_path / "logs"
|
||||
additional_config = {
|
||||
"loglevel": "debug",
|
||||
"log-directory": str(log_dir),
|
||||
}
|
||||
config = component_tests_config(additional_config=additional_config)
|
||||
|
||||
def assert_rotating_log():
|
||||
# We need the folder to have executable permissions for the "stat" command in the assertions to work.
|
||||
subprocess.check_call(['sudo', 'chmod', 'o+x', log_dir])
|
||||
test_logging.assert_log_to_dir(config, log_dir)
|
||||
|
||||
self.sysv_service_scenario(config, tmp_path, assert_rotating_log)
|
||||
|
||||
def sysv_service_scenario(self, config, tmp_path, extra_assertions):
|
||||
with self.run_service(tmp_path, config, root=True):
|
||||
self.sysv_cmd("start")
|
||||
self.sysv_cmd("status")
|
||||
wait_tunnel_ready(tunnel_url=config.get_url())
|
||||
extra_assertions()
|
||||
self.sysv_cmd("stop")
|
||||
|
||||
# Service install copies config file to /etc/cloudflared/config.yml
|
||||
subprocess.run(["sudo", "rm", "/etc/cloudflared/config.yml"])
|
||||
self.sysv_cmd("status", success=False)
|
||||
|
Reference in New Issue
Block a user