11#!/usr/bin/env python
22import os
3+ import pathlib
34import platform
45import subprocess
56from contextlib import contextmanager
910
1011import test_logging
1112from conftest import CfdModes
12- from util import start_cloudflared , wait_tunnel_ready
13+ from util import start_cloudflared , wait_tunnel_ready , write_config
1314
1415
1516def select_platform (plat ):
@@ -43,6 +44,21 @@ def assert_log_file():
4344
4445 self .launchd_service_scenario (config , assert_log_file )
4546
47+ @select_platform ("Darwin" )
48+ @pytest .mark .skipif (os .path .exists (default_config_file ()), reason = f"There is already a config file in default path" )
49+ def test_launchd_service_with_token (self , tmp_path , component_tests_config ):
50+ log_file = tmp_path / test_logging .default_log_file
51+ additional_config = {
52+ "logfile" : str (log_file ),
53+ }
54+ config = component_tests_config (additional_config = additional_config )
55+
56+ # service install doesn't install the config file but in this case we want to use some default settings
57+ # so we write the base config without the tunnel credentials and ID
58+ write_config (pathlib .Path (default_config_dir ()), config .base_config ())
59+
60+ self .launchd_service_scenario (config , use_token = True )
61+
4662 @select_platform ("Darwin" )
4763 @pytest .mark .skipif (os .path .exists (default_config_file ()), reason = f"There is already a config file in default path" )
4864 def test_launchd_service_rotating_log (self , tmp_path , component_tests_config ):
@@ -60,12 +76,13 @@ def assert_rotating_log():
6076
6177 self .launchd_service_scenario (config , assert_rotating_log )
6278
63- def launchd_service_scenario (self , config , extra_assertions ):
64- with self .run_service (Path (default_config_dir ()), config ):
79+ def launchd_service_scenario (self , config , extra_assertions = None , use_token = False ):
80+ with self .run_service (Path (default_config_dir ()), config , use_token = use_token ):
6581 self .launchctl_cmd ("list" )
6682 self .launchctl_cmd ("start" )
6783 wait_tunnel_ready (tunnel_url = config .get_url ())
68- extra_assertions ()
84+ if extra_assertions is not None :
85+ extra_assertions ()
6986 self .launchctl_cmd ("stop" )
7087
7188 os .remove (default_config_file ())
@@ -105,27 +122,50 @@ def assert_rotating_log():
105122
106123 self .sysv_service_scenario (config , tmp_path , assert_rotating_log )
107124
108- def sysv_service_scenario (self , config , tmp_path , extra_assertions ):
109- with self .run_service (tmp_path , config , root = True ):
125+ @select_platform ("Linux" )
126+ @pytest .mark .skipif (os .path .exists ("/etc/cloudflared/config.yml" ),
127+ reason = f"There is already a config file in default path" )
128+ def test_sysv_service_with_token (self , tmp_path , component_tests_config ):
129+ additional_config = {
130+ "loglevel" : "debug" ,
131+ }
132+
133+ config = component_tests_config (additional_config = additional_config )
134+
135+ # service install doesn't install the config file but in this case we want to use some default settings
136+ # so we write the base config without the tunnel credentials and ID
137+ config_path = write_config (tmp_path , config .base_config ())
138+ subprocess .run (["sudo" , "cp" , config_path , "/etc/cloudflared/config.yml" ], check = True )
139+
140+ self .sysv_service_scenario (config , tmp_path , use_token = True )
141+
142+ def sysv_service_scenario (self , config , tmp_path , extra_assertions = None , use_token = False ):
143+ with self .run_service (tmp_path , config , root = True , use_token = use_token ):
110144 self .sysv_cmd ("start" )
111145 self .sysv_cmd ("status" )
112146 wait_tunnel_ready (tunnel_url = config .get_url ())
113- extra_assertions ()
147+ if extra_assertions is not None :
148+ extra_assertions ()
114149 self .sysv_cmd ("stop" )
115150
116151 # Service install copies config file to /etc/cloudflared/config.yml
117152 subprocess .run (["sudo" , "rm" , "/etc/cloudflared/config.yml" ])
118153 self .sysv_cmd ("status" , success = False )
119154
120155 @contextmanager
121- def run_service (self , tmp_path , config , root = False ):
156+ def run_service (self , tmp_path , config , root = False , use_token = False ):
157+ args = ["service" , "install" ]
158+
159+ if use_token :
160+ args .append (config .get_token ())
161+
122162 try :
123163 service = start_cloudflared (
124- tmp_path , config , cfd_args = [ "service" , "install" ], cfd_pre_args = [], capture_output = False , root = root )
164+ tmp_path , config , cfd_args = args , cfd_pre_args = [], capture_output = False , root = root , skip_config_flag = use_token )
125165 yield service
126166 finally :
127167 start_cloudflared (
128- tmp_path , config , cfd_args = ["service" , "uninstall" ], cfd_pre_args = [], capture_output = False , root = root )
168+ tmp_path , config , cfd_args = ["service" , "uninstall" ], cfd_pre_args = [], capture_output = False , root = root , skip_config_flag = use_token )
129169
130170 def launchctl_cmd (self , action , success = True ):
131171 cmd = subprocess .run (
0 commit comments