2021-03-12 13:37:53 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
import os
|
2022-03-02 15:56:32 +00:00
|
|
|
import pathlib
|
2022-06-14 23:07:27 +00:00
|
|
|
import platform
|
2021-03-12 13:37:53 +00:00
|
|
|
import subprocess
|
2021-03-23 14:28:29 +00:00
|
|
|
from contextlib import contextmanager
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
import pytest
|
2021-03-12 13:37:53 +00:00
|
|
|
|
2021-03-23 14:28:29 +00:00
|
|
|
import test_logging
|
2021-07-30 16:44:11 +00:00
|
|
|
from conftest import CfdModes
|
2022-06-14 23:07:27 +00:00
|
|
|
from util import start_cloudflared, wait_tunnel_ready, write_config
|
|
|
|
|
|
|
|
|
|
|
|
def select_platform(plat):
|
|
|
|
return pytest.mark.skipif(
|
|
|
|
platform.system() != plat, reason=f"Only runs on {plat}")
|
2021-03-12 13:37:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
def default_config_dir():
|
|
|
|
return os.path.join(Path.home(), ".cloudflared")
|
|
|
|
|
|
|
|
|
|
|
|
def default_config_file():
|
|
|
|
return os.path.join(default_config_dir(), "config.yml")
|
|
|
|
|
|
|
|
|
2021-03-23 14:28:29 +00:00
|
|
|
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_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),
|
|
|
|
}
|
2021-07-30 16:44:11 +00:00
|
|
|
config = component_tests_config(additional_config=additional_config, cfd_mode=CfdModes.CLASSIC)
|
2021-03-23 14:28:29 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2022-03-02 15:56:32 +00:00
|
|
|
@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_with_token(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)
|
|
|
|
|
|
|
|
# service install doesn't install the config file but in this case we want to use some default settings
|
|
|
|
# so we write the base config without the tunnel credentials and ID
|
|
|
|
write_config(pathlib.Path(default_config_dir()), config.base_config())
|
|
|
|
|
|
|
|
self.launchd_service_scenario(config, use_token=True)
|
|
|
|
|
2021-03-12 13:37:53 +00:00
|
|
|
@select_platform("Darwin")
|
|
|
|
@pytest.mark.skipif(os.path.exists(default_config_file()), reason=f"There is already a config file in default path")
|
2021-03-23 14:28:29 +00:00
|
|
|
def test_launchd_service_rotating_log(self, tmp_path, component_tests_config):
|
|
|
|
log_dir = tmp_path / "logs"
|
2021-03-12 13:37:53 +00:00
|
|
|
additional_config = {
|
2021-03-23 14:28:29 +00:00
|
|
|
# On Darwin cloudflared service defaults to run classic tunnel command
|
2021-03-12 13:37:53 +00:00
|
|
|
"hello-world": True,
|
2021-03-23 14:28:29 +00:00
|
|
|
"loglevel": "debug",
|
|
|
|
"log-directory": str(log_dir),
|
2021-03-12 13:37:53 +00:00
|
|
|
}
|
2021-07-30 16:44:11 +00:00
|
|
|
config = component_tests_config(additional_config=additional_config, cfd_mode=CfdModes.CLASSIC)
|
2021-03-23 14:28:29 +00:00
|
|
|
|
|
|
|
def assert_rotating_log():
|
|
|
|
test_logging.assert_log_to_dir(config, log_dir)
|
|
|
|
|
|
|
|
self.launchd_service_scenario(config, assert_rotating_log)
|
|
|
|
|
2022-03-02 15:56:32 +00:00
|
|
|
def launchd_service_scenario(self, config, extra_assertions=None, use_token=False):
|
|
|
|
with self.run_service(Path(default_config_dir()), config, use_token=use_token):
|
2021-03-12 13:37:53 +00:00
|
|
|
self.launchctl_cmd("list")
|
|
|
|
self.launchctl_cmd("start")
|
|
|
|
wait_tunnel_ready(tunnel_url=config.get_url())
|
2022-03-02 15:56:32 +00:00
|
|
|
if extra_assertions is not None:
|
|
|
|
extra_assertions()
|
2021-03-12 13:37:53 +00:00
|
|
|
self.launchctl_cmd("stop")
|
|
|
|
|
|
|
|
os.remove(default_config_file())
|
|
|
|
self.launchctl_cmd("list", success=False)
|
|
|
|
|
|
|
|
@select_platform("Linux")
|
2021-03-23 14:28:29 +00:00
|
|
|
@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)
|
|
|
|
|
2022-03-02 15:56:32 +00:00
|
|
|
@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_with_token(self, tmp_path, component_tests_config):
|
|
|
|
additional_config = {
|
|
|
|
"loglevel": "debug",
|
|
|
|
}
|
|
|
|
|
|
|
|
config = component_tests_config(additional_config=additional_config)
|
|
|
|
|
|
|
|
# service install doesn't install the config file but in this case we want to use some default settings
|
|
|
|
# so we write the base config without the tunnel credentials and ID
|
|
|
|
config_path = write_config(tmp_path, config.base_config())
|
|
|
|
subprocess.run(["sudo", "cp", config_path, "/etc/cloudflared/config.yml"], check=True)
|
|
|
|
|
|
|
|
self.sysv_service_scenario(config, tmp_path, use_token=True)
|
|
|
|
|
|
|
|
def sysv_service_scenario(self, config, tmp_path, extra_assertions=None, use_token=False):
|
|
|
|
with self.run_service(tmp_path, config, root=True, use_token=use_token):
|
2021-03-12 13:37:53 +00:00
|
|
|
self.sysv_cmd("status")
|
|
|
|
wait_tunnel_ready(tunnel_url=config.get_url())
|
2022-03-02 15:56:32 +00:00
|
|
|
if extra_assertions is not None:
|
|
|
|
extra_assertions()
|
2021-03-23 14:28:29 +00:00
|
|
|
|
2021-03-12 13:37:53 +00:00
|
|
|
# Service install copies config file to /etc/cloudflared/config.yml
|
|
|
|
subprocess.run(["sudo", "rm", "/etc/cloudflared/config.yml"])
|
|
|
|
self.sysv_cmd("status", success=False)
|
|
|
|
|
|
|
|
@contextmanager
|
2022-03-02 15:56:32 +00:00
|
|
|
def run_service(self, tmp_path, config, root=False, use_token=False):
|
|
|
|
args = ["service", "install"]
|
|
|
|
|
|
|
|
if use_token:
|
|
|
|
args.append(config.get_token())
|
|
|
|
|
2021-03-12 13:37:53 +00:00
|
|
|
try:
|
|
|
|
service = start_cloudflared(
|
2022-03-02 15:56:32 +00:00
|
|
|
tmp_path, config, cfd_args=args, cfd_pre_args=[], capture_output=False, root=root, skip_config_flag=use_token)
|
2021-03-12 13:37:53 +00:00
|
|
|
yield service
|
|
|
|
finally:
|
|
|
|
start_cloudflared(
|
2022-03-02 15:56:32 +00:00
|
|
|
tmp_path, config, cfd_args=["service", "uninstall"], cfd_pre_args=[], capture_output=False, root=root, skip_config_flag=use_token)
|
2021-03-12 13:37:53 +00:00
|
|
|
|
|
|
|
def launchctl_cmd(self, action, success=True):
|
|
|
|
cmd = subprocess.run(
|
|
|
|
["launchctl", action, "com.cloudflare.cloudflared"], check=success)
|
|
|
|
if not success:
|
|
|
|
assert cmd.returncode != 0, f"Expect {cmd.args} to fail, but it succeed"
|
|
|
|
|
|
|
|
def sysv_cmd(self, action, success=True):
|
|
|
|
cmd = subprocess.run(
|
|
|
|
["sudo", "service", "cloudflared", action], check=success)
|
|
|
|
if not success:
|
|
|
|
assert cmd.returncode != 0, f"Expect {cmd.args} to fail, but it succeed"
|