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 · modecode

1#!/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):
13 return pytest.mark.skipif(
14 platform.system() != plat, reason=f"Only runs on {plat}")
15
16
17def default_config_dir():
18 return os.path.join(Path.home(), ".cloudflared")
19
20
21def default_config_file():
22 return 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")
28 def test_launchd_service(self, component_tests_config):
29 # On Darwin cloudflared service defaults to run classic tunnel command
30 additional_config = {
31 "hello-world": True,
32 }
33 config = component_tests_config(
34 additional_config=additional_config, named_tunnel=False)
35 with self.run_service(Path(default_config_dir()), config):
36 self.launchctl_cmd("list")
37 self.launchctl_cmd("start")
38 wait_tunnel_ready(tunnel_url=config.get_url())
39 self.launchctl_cmd("stop")
40
41 os.remove(default_config_file())
42 self.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")
46 def test_sysv_service(self, tmp_path, component_tests_config):
47 config = component_tests_config()
48 with self.run_service(tmp_path, config, root=True):
49 self.sysv_cmd("start")
50 self.sysv_cmd("status")
51 wait_tunnel_ready(tunnel_url=config.get_url())
52 self.sysv_cmd("stop")
53 # Service install copies config file to /etc/cloudflared/config.yml
54 subprocess.run(["sudo", "rm", "/etc/cloudflared/config.yml"])
55 self.sysv_cmd("status", success=False)
56
57 @contextmanager
58 def run_service(self, tmp_path, config, root=False):
59 try:
60 service = start_cloudflared(
61 tmp_path, config, cfd_args=["service", "install"], cfd_pre_args=[], capture_output=False, root=root)
62 yield service
63 finally:
64 start_cloudflared(
65 tmp_path, config, cfd_args=["service", "uninstall"], cfd_pre_args=[], capture_output=False, root=root)
66
67 def launchctl_cmd(self, action, success=True):
68 cmd = subprocess.run(
69 ["launchctl", action, "com.cloudflare.cloudflared"], check=success)
70 if not success:
71 assert cmd.returncode != 0, f"Expect {cmd.args} to fail, but it succeed"
72
73 def sysv_cmd(self, action, success=True):
74 cmd = subprocess.run(
75 ["sudo", "service", "cloudflared", action], check=success)
76 if not success:
77 assert cmd.returncode != 0, f"Expect {cmd.args} to fail, but it succeed"
78