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'''
3234class Test_remap_acl :
3335 """Configure a test to verify remap.config acl behavior."""
3436
35- _ts_counter : int = 0
37+ _ts : 'TestProcess' = None
38+ _ts_reload_counter : int = 0
39+ _ts_is_started : bool = False
40+
3641 _server_counter : int = 0
3742 _client_counter : int = 0
3843
@@ -50,49 +55,56 @@ def __init__(
5055 :param expect_responses: The in-order expected responses from the proxy.
5156 """
5257 self ._replay_file = replay_file
53- self ._ip_allow_content = ip_allow_content
58+ self ._ip_allow_lines = ip_allow_content . split ( " \n " )
5459 self ._deactivate_ip_allow = deactivate_ip_allow
5560 self ._acl_behavior_policy = acl_behavior_policy
5661 self ._acl_configuration = acl_configuration
5762 self ._named_acls = named_acls
5863 self ._expected_responses = expected_responses
5964
65+ # Usually we configure the server first and use the server port to
66+ # configure ATS to remap to it. In this case, though, we want a
67+ # long-lived ATS process that spans TestRuns. So we let ATS choose an
68+ # arbitrary availble server port, and then tell the TestRun-specific
69+ # server to use that port.
70+ server_port = self ._configure_traffic_server ()
6071 tr = Test .AddTestRun (name )
61- self ._configure_server (tr )
62- self ._configure_traffic_server (tr , proxy_protocol )
72+ self ._configure_server (tr , server_port )
6373 self ._configure_client (tr , proxy_protocol )
6474
65- def _configure_server (self , tr : 'TestRun' ) -> None :
75+ def _configure_server (self , tr : 'TestRun' , server_port : int ) -> None :
6676 """Configure the server.
67-
68- :param tr: The TestRun object to associate the server process with.
6977 """
7078 name = f"server-{ Test_remap_acl ._server_counter } "
71- server = tr .AddVerifierServerProcess (name , self ._replay_file )
79+ server = tr .AddVerifierServerProcess (name , self ._replay_file , http_ports = [ server_port ] )
7280 Test_remap_acl ._server_counter += 1
7381 self ._server = server
7482
75- def _configure_traffic_server (self , tr : 'TestRun' , proxy_protocol : bool ) -> None :
83+ def _configure_traffic_server (self ) -> int :
7684 """Configure Traffic Server.
7785
78- :param tr : The TestRun object to associate the Traffic Server process with .
86+ :return : The listening port that the server should use .
7987 """
8088
81- name = f"ts-{ Test_remap_acl ._ts_counter } "
82- ts = tr .MakeATSProcess (
83- name , enable_cache = False , enable_proxy_protocol = proxy_protocol , enable_uds = False , reload_quickly = True )
84- Test_remap_acl ._ts_counter += 1
89+ call_reload : bool = False
90+ if Test_remap_acl ._ts is not None :
91+ ts = Test_remap_acl ._ts
92+ call_reload = True
93+ else :
94+ ts = Test .MakeATSProcess ("ts" , enable_cache = False , enable_proxy_protocol = True , enable_uds = False , reload_quickly = True )
95+ Test_remap_acl ._ts = ts
8596 self ._ts = ts
86-
87- ts .Disk .records_config .update (
88- {
89- 'proxy.config.diags.debug.enabled' : 1 ,
90- 'proxy.config.diags.debug.tags' : 'http|url|remap|ip_allow|proxyprotocol' ,
91- 'proxy.config.http.push_method_enabled' : 1 ,
92- 'proxy.config.http.connect_ports' : self ._server .Variables .http_port ,
93- 'proxy.config.url_remap.acl_behavior_policy' : self ._acl_behavior_policy ,
94- 'proxy.config.acl.subjects' : 'PROXY,PEER' ,
95- })
97+ port_name = f'ServerPort-{ Test_remap_acl ._ts_reload_counter } '
98+ server_port : int = get_port (ts , port_name )
99+
100+ record_config = {
101+ 'proxy.config.diags.debug.enabled' : 1 ,
102+ 'proxy.config.diags.debug.tags' : 'http|url|remap|ip_allow|proxyprotocol' ,
103+ 'proxy.config.http.push_method_enabled' : 1 ,
104+ 'proxy.config.http.connect_ports' : server_port ,
105+ 'proxy.config.url_remap.acl_behavior_policy' : self ._acl_behavior_policy ,
106+ 'proxy.config.acl.subjects' : 'PROXY,PEER' ,
107+ }
96108
97109 remap_config_lines = []
98110 if self ._deactivate_ip_allow :
@@ -105,9 +117,56 @@ def _configure_traffic_server(self, tr: 'TestRun', proxy_protocol: bool) -> None
105117 for name , _ in self ._named_acls :
106118 remap_config_lines .append (f'.activatefilter { name } ' )
107119
108- remap_config_lines .append (f'map / http://127.0.0.1:{ self ._server .Variables .http_port } { self ._acl_configuration } ' )
109- ts .Disk .remap_config .AddLines (remap_config_lines )
110- ts .Disk .ip_allow_yaml .AddLines (self ._ip_allow_content .split ("\n " ))
120+ remap_config_lines .append (f'map / http://127.0.0.1:{ server_port } { self ._acl_configuration } ' )
121+ if call_reload :
122+ #
123+ # Update the ATS configuration.
124+ #
125+ tr = Test .AddTestRun ("Change the ATS configuration" )
126+ p = tr .Processes .Default
127+ p .Command = 'echo updating configuration'
128+ p .Env = ts .Env
129+ tr .StillRunningAfter = ts
130+
131+ records_path = os .path .join (ts .Variables .CONFIGDIR , 'records.yaml' )
132+ tr .Disk .File (records_path , typename = "ats:config:records" ).update (record_config )
133+
134+ remap_cfg_path = os .path .join (ts .Variables .CONFIGDIR , 'remap.config' )
135+ tr .Disk .File (remap_cfg_path ).WriteOn ("" )
136+ tr .Disk .File (remap_cfg_path , typename = "ats:config" ).AddLines (remap_config_lines )
137+
138+ ip_allow_path = os .path .join (ts .Variables .CONFIGDIR , 'ip_allow.yaml' )
139+ tr .Disk .File (ip_allow_path ).WriteOn ("" )
140+ tr .Disk .File (ip_allow_path , typename = "ats:config" ).AddLines (self ._ip_allow_lines )
141+
142+ #
143+ # Kick off the ATS config reload.
144+ #
145+ tr = Test .AddTestRun ("Reload the ATS configuration" )
146+ p = tr .Processes .Default
147+ p .Command = 'traffic_ctl config reload'
148+ p .Env = ts .Env
149+ tr .StillRunningAfter = ts
150+
151+ #
152+ # Await the config reload to finish.
153+ #
154+ tr = Test .AddTestRun ("Await config reload" )
155+ p = tr .Processes .Default
156+ p .Command = 'echo awaiting config reload'
157+ p .Env = ts .Env
158+ Test_remap_acl ._ts_reload_counter += 1
159+ count = Test_remap_acl ._ts_reload_counter
160+ await_config_reload = tr .Processes .Process (f'config_reload_succeeded_{ count } ' , 'sleep 30' )
161+ await_config_reload .Ready = When .FileContains (ts .Disk .diags_log .Name , "remap.config finished loading" , count )
162+ p .StartBefore (await_config_reload )
163+
164+ else :
165+ ts .Disk .records_config .update (record_config )
166+ ts .Disk .remap_config .AddLines (remap_config_lines )
167+ ts .Disk .ip_allow_yaml .AddLines (self ._ip_allow_lines )
168+
169+ return server_port
111170
112171 def _configure_client (self , tr : 'TestRun' , proxy_protocol : bool ) -> None :
113172 """Run the test.
@@ -116,11 +175,14 @@ def _configure_client(self, tr: 'TestRun', proxy_protocol: bool) -> None:
116175 """
117176
118177 name = f"client-{ Test_remap_acl ._client_counter } "
119- port = self ._ts .Variables .port if proxy_protocol == False else self ._ts .Variables .proxy_protocol_port
178+ ts = Test_remap_acl ._ts
179+ port = ts .Variables .port if proxy_protocol == False else ts .Variables .proxy_protocol_port
120180 p = tr .AddVerifierClientProcess (name , self ._replay_file , http_ports = [port ])
121181 Test_remap_acl ._client_counter += 1
122182 p .StartBefore (self ._server )
123- p .StartBefore (self ._ts )
183+ if not Test_remap_acl ._ts_is_started :
184+ p .StartBefore (ts )
185+ Test_remap_acl ._ts_is_started = True
124186
125187 if self ._expected_responses == [None , None ]:
126188 # If there are no expected responses, expect the Warning about the rejected ip.
0 commit comments