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_service.py

77lines · modeblame

9df60276cthuang5 years ago1#!/usr/bin/env python
2from contextlib import contextmanager
3import os
4from pathlib import Path
5import platform
6import pytest
7import subprocess
8
9from util import start_cloudflared, cloudflared_cmd, wait_tunnel_ready, LOGGER
10
11
12def select_platform(plat):
13return pytest.mark.skipif(
14platform.system() != plat, reason=f"Only runs on {plat}")
15
16
17def default_config_dir():
18return os.path.join(Path.home(), ".cloudflared")
19
20
21def default_config_file():
22return os.path.join(default_config_dir(), "config.yml")
23
24
25class TestServiceMode():
26@select_platform("Darwin")
27@pytest.mark.skipif(os.path.exists(default_config_file()), reason=f"There is already a config file in default path")
28def test_launchd_service(self, component_tests_config):
29# On Darwin cloudflared service defaults to run classic tunnel command
30additional_config = {
31"hello-world": True,
32}
33config = component_tests_config(
34additional_config=additional_config, named_tunnel=False)
35with self.run_service(Path(default_config_dir()), config):
36self.launchctl_cmd("list")
37self.launchctl_cmd("start")
38wait_tunnel_ready(tunnel_url=config.get_url())
39self.launchctl_cmd("stop")
40
41os.remove(default_config_file())
42self.launchctl_cmd("list", success=False)
43
44@select_platform("Linux")
45@pytest.mark.skipif(os.path.exists("/etc/cloudflared/config.yml"), reason=f"There is already a config file in default path")
46def test_sysv_service(self, tmp_path, component_tests_config):
47config = component_tests_config()
48with self.run_service(tmp_path, config, root=True):
49self.sysv_cmd("start")
50self.sysv_cmd("status")
51wait_tunnel_ready(tunnel_url=config.get_url())
52self.sysv_cmd("stop")
53# Service install copies config file to /etc/cloudflared/config.yml
54subprocess.run(["sudo", "rm", "/etc/cloudflared/config.yml"])
55self.sysv_cmd("status", success=False)
56
57@contextmanager
58def run_service(self, tmp_path, config, root=False):
59try:
60service = start_cloudflared(
61tmp_path, config, cfd_args=["service", "install"], cfd_pre_args=[], capture_output=False, root=root)
62yield service
63finally:
64start_cloudflared(
65tmp_path, config, cfd_args=["service", "uninstall"], cfd_pre_args=[], capture_output=False, root=root)
66
67def launchctl_cmd(self, action, success=True):
68cmd = subprocess.run(
69["launchctl", action, "com.cloudflare.cloudflared"], check=success)
70if not success:
71assert cmd.returncode != 0, f"Expect {cmd.args} to fail, but it succeed"
72
73def sysv_cmd(self, action, success=True):
74cmd = subprocess.run(
75["sudo", "service", "cloudflared", action], check=success)
76if not success:
77assert cmd.returncode != 0, f"Expect {cmd.args} to fail, but it succeed"