2424from yaml import CLoader as Loader
2525from typing import List , Tuple
2626
27+ from ports import get_port
28+
2729Test .Summary = '''
2830Verify remap.config acl behavior.
2931'''
3032
3133
34+ def update_config_file (path1 : str , content1 : str , path2 : str , content2 : str ) -> None :
35+ """Update two config files.
36+
37+ This is used for some of the updates to the config files between test runs.
38+
39+ :param path1: The path to the first config file.
40+ :param content1: The content to write to the first config file.
41+ :param path2: The path to the second config file.
42+ :param content2: The content to write to the second config file.
43+ """
44+ with open (path1 , 'w' ) as f :
45+ f .write (content1 + '\n ' )
46+ with open (path2 , 'w' ) as f :
47+ f .write (content2 + '\n ' )
48+
49+
3250class Test_remap_acl :
3351 """Configure a test to verify remap.config acl behavior."""
3452
35- _ts_counter : int = 0
53+ _ts : 'TestProcess' = None
54+ _ts_reload_counter : int = 0
55+ _ts_is_started : bool = False
56+
3657 _server_counter : int = 0
3758 _client_counter : int = 0
3859
@@ -50,48 +71,47 @@ def __init__(
5071 :param expect_responses: The in-order expected responses from the proxy.
5172 """
5273 self ._replay_file = replay_file
53- self ._ip_allow_content = ip_allow_content
74+ self ._ip_allow_lines = ip_allow_content . split ( " \n " )
5475 self ._deactivate_ip_allow = deactivate_ip_allow
5576 self ._acl_behavior_policy = acl_behavior_policy
5677 self ._acl_configuration = acl_configuration
5778 self ._named_acls = named_acls
5879 self ._expected_responses = expected_responses
5980
81+ # Usually we configure the server first and use the server port to
82+ # configure ATS to remap to it. In this case, though, we want a
83+ # long-lived ATS process that spans TestRuns. So we let ATS choose an
84+ # arbitrary availble server port, and then tell the TestRun-specific
85+ # server to use that port.
86+ server_port = self ._configure_traffic_server ()
6087 tr = Test .AddTestRun (name )
61- self ._configure_server (tr )
62- self ._configure_traffic_server (tr , proxy_protocol )
88+ self ._configure_server (tr , server_port )
6389 self ._configure_client (tr , proxy_protocol )
6490
65- def _configure_server (self , tr : 'TestRun' ) -> None :
91+ def _configure_server (self , tr : 'TestRun' , server_port : int ) -> None :
6692 """Configure the server.
67-
68- :param tr: The TestRun object to associate the server process with.
6993 """
7094 name = f"server-{ Test_remap_acl ._server_counter } "
71- server = tr .AddVerifierServerProcess (name , self ._replay_file )
95+ server = tr .AddVerifierServerProcess (name , self ._replay_file , http_ports = [ server_port ] )
7296 Test_remap_acl ._server_counter += 1
7397 self ._server = server
7498
75- def _configure_traffic_server (self , tr : 'TestRun' , proxy_protocol : bool ) -> None :
99+ def _configure_traffic_server (self ) -> int :
76100 """Configure Traffic Server.
77101
78- :param tr : The TestRun object to associate the Traffic Server process with .
102+ :return : The listening port that the server should use .
79103 """
80104
81- name = f"ts-{ Test_remap_acl ._ts_counter } "
82- ts = tr .MakeATSProcess (name , enable_cache = False , enable_proxy_protocol = proxy_protocol , enable_uds = False )
83- Test_remap_acl ._ts_counter += 1
105+ call_reload : bool = False
106+ if Test_remap_acl ._ts is not None :
107+ ts = Test_remap_acl ._ts
108+ call_reload = True
109+ else :
110+ ts = Test .MakeATSProcess ("ts" , enable_cache = False , enable_proxy_protocol = True , enable_uds = False )
111+ Test_remap_acl ._ts = ts
84112 self ._ts = ts
85-
86- ts .Disk .records_config .update (
87- {
88- 'proxy.config.diags.debug.enabled' : 1 ,
89- 'proxy.config.diags.debug.tags' : 'http|url|remap|ip_allow|proxyprotocol' ,
90- 'proxy.config.http.push_method_enabled' : 1 ,
91- 'proxy.config.http.connect_ports' : self ._server .Variables .http_port ,
92- 'proxy.config.url_remap.acl_behavior_policy' : self ._acl_behavior_policy ,
93- 'proxy.config.acl.subjects' : 'PROXY,PEER' ,
94- })
113+ port_name = f'ServerPort-{ Test_remap_acl ._ts_reload_counter } '
114+ server_port : int = get_port (ts , port_name )
95115
96116 remap_config_lines = []
97117 if self ._deactivate_ip_allow :
@@ -104,9 +124,64 @@ def _configure_traffic_server(self, tr: 'TestRun', proxy_protocol: bool) -> None
104124 for name , _ in self ._named_acls :
105125 remap_config_lines .append (f'.activatefilter { name } ' )
106126
107- remap_config_lines .append (f'map / http://127.0.0.1:{ self ._server .Variables .http_port } { self ._acl_configuration } ' )
108- ts .Disk .remap_config .AddLines (remap_config_lines )
109- ts .Disk .ip_allow_yaml .AddLines (self ._ip_allow_content .split ("\n " ))
127+ remap_config_lines .append (f'map / http://127.0.0.1:{ server_port } { self ._acl_configuration } ' )
128+
129+ if call_reload :
130+ #
131+ # Update the ATS configuration.
132+ #
133+ tr = Test .AddTestRun ("Change the ATS configuration" )
134+ p = tr .Processes .Default
135+ p .Command = (
136+ f'traffic_ctl config set proxy.config.http.connect_ports { server_port } && '
137+ f'traffic_ctl config set proxy.config.url_remap.acl_behavior_policy { self ._acl_behavior_policy } ' )
138+
139+ p .Env = ts .Env
140+ tr .StillRunningAfter = ts
141+
142+ remap_cfg_path = os .path .join (ts .Variables .CONFIGDIR , 'remap.config' )
143+ ip_allow_path = os .path .join (ts .Variables .CONFIGDIR , 'ip_allow.yaml' )
144+ p .Setup .Lambda (
145+ lambda : update_config_file (
146+ remap_cfg_path , '\n ' .join (remap_config_lines ), ip_allow_path , '\n ' .join (self ._ip_allow_lines )))
147+
148+ #
149+ # Kick off the ATS config reload.
150+ #
151+ tr = Test .AddTestRun ("Reload the ATS configuration" )
152+ p = tr .Processes .Default
153+ p .Command = 'traffic_ctl config reload'
154+ p .Env = ts .Env
155+ tr .StillRunningAfter = ts
156+
157+ #
158+ # Await the config reload to finish.
159+ #
160+ tr = Test .AddTestRun ("Await config reload" )
161+ p = tr .Processes .Default
162+ p .Command = 'echo awaiting config reload'
163+ p .Env = ts .Env
164+ Test_remap_acl ._ts_reload_counter += 1
165+ count = Test_remap_acl ._ts_reload_counter
166+ await_config_reload = tr .Processes .Process (f'config_reload_succeeded_{ count } ' , 'sleep 30' )
167+ await_config_reload .Ready = When .FileContains (ts .Disk .diags_log .Name , "remap.config finished loading" , count )
168+ p .StartBefore (await_config_reload )
169+
170+ else :
171+ record_config = {
172+ 'proxy.config.diags.debug.enabled' : 1 ,
173+ 'proxy.config.diags.debug.tags' : 'http|url|remap|ip_allow|proxyprotocol' ,
174+ 'proxy.config.http.push_method_enabled' : 1 ,
175+ 'proxy.config.http.connect_ports' : server_port ,
176+ 'proxy.config.url_remap.acl_behavior_policy' : self ._acl_behavior_policy ,
177+ 'proxy.config.acl.subjects' : 'PROXY,PEER' ,
178+ }
179+
180+ ts .Disk .records_config .update (record_config )
181+ ts .Disk .remap_config .AddLines (remap_config_lines )
182+ ts .Disk .ip_allow_yaml .AddLines (self ._ip_allow_lines )
183+
184+ return server_port
110185
111186 def _configure_client (self , tr : 'TestRun' , proxy_protocol : bool ) -> None :
112187 """Run the test.
@@ -115,11 +190,14 @@ def _configure_client(self, tr: 'TestRun', proxy_protocol: bool) -> None:
115190 """
116191
117192 name = f"client-{ Test_remap_acl ._client_counter } "
118- port = self ._ts .Variables .port if proxy_protocol == False else self ._ts .Variables .proxy_protocol_port
193+ ts = Test_remap_acl ._ts
194+ port = ts .Variables .port if proxy_protocol == False else ts .Variables .proxy_protocol_port
119195 p = tr .AddVerifierClientProcess (name , self ._replay_file , http_ports = [port ])
120196 Test_remap_acl ._client_counter += 1
121197 p .StartBefore (self ._server )
122- p .StartBefore (self ._ts )
198+ if not Test_remap_acl ._ts_is_started :
199+ p .StartBefore (ts )
200+ Test_remap_acl ._ts_is_started = True
123201
124202 if self ._expected_responses == [None , None ]:
125203 # If there are no expected responses, expect the Warning about the rejected ip.
0 commit comments