55from .sync_data import process_sync_data
66from aikido_zen .background_process .routes import Routes
77from aikido_zen .helpers .iplist import IPList
8+ from ...storage .hostnames import Hostnames
89
910
1011@pytest .fixture
1112def setup_connection_manager ():
1213 """Fixture to set up a mock connection manager."""
1314 connection_manager = MagicMock ()
1415 connection_manager .routes = Routes ()
16+ connection_manager .hostnames = Hostnames ()
1517 connection_manager .conf .endpoints = ["endpoint1" , "endpoint2" ]
1618
1719 connection_manager .conf .bypassed_ips = IPList ()
@@ -26,6 +28,10 @@ def setup_connection_manager():
2628def test_process_sync_data_initialization (setup_connection_manager ):
2729 """Test the initialization of routes and hits update."""
2830 connection_manager = setup_connection_manager
31+ test_hostnames = Hostnames ()
32+ test_hostnames .add ("example2.com" , 443 , 15 )
33+ test_hostnames .add ("bumblebee.com" , 8080 )
34+
2935 data = {
3036 "current_routes" : {
3137 "route1" : {
@@ -43,6 +49,7 @@ def test_process_sync_data_initialization(setup_connection_manager):
4349 },
4450 "reqs" : 10 , # Total requests to be added
4551 "middleware_installed" : False ,
52+ "hostnames" : test_hostnames .as_array (),
4653 }
4754
4855 result = process_sync_data (connection_manager , data , None )
@@ -69,6 +76,10 @@ def test_process_sync_data_initialization(setup_connection_manager):
6976 assert result ["routes" ] == dict (connection_manager .routes .routes )
7077 assert result ["config" ] == connection_manager .conf
7178 assert connection_manager .middleware_installed == False
79+ assert connection_manager .hostnames .as_array () == [
80+ {"hits" : 15 , "hostname" : "example2.com" , "port" : 443 },
81+ {"hits" : 1 , "hostname" : "bumblebee.com" , "port" : 8080 },
82+ ]
7283
7384
7485def test_process_sync_data_with_last_updated_at_below_zero (setup_connection_manager ):
@@ -114,13 +125,22 @@ def test_process_sync_data_with_last_updated_at_below_zero(setup_connection_mana
114125 # Check that the total requests were updated
115126 assert connection_manager .statistics .requests ["total" ] == 10
116127 assert connection_manager .middleware_installed == True
128+ assert len (connection_manager .hostnames .as_array ()) == 0
117129 # Check that the return value is correct
118130 assert result == {}
119131
120132
121- def test_process_sync_data_existing_route (setup_connection_manager ):
133+ def test_process_sync_data_existing_route_and_hostnames (setup_connection_manager ):
122134 """Test updating an existing route's hit count."""
123135 connection_manager = setup_connection_manager
136+ connection_manager .hostnames .add ("example.com" , 443 , 200 )
137+ connection_manager .hostnames .add ("example.org" , 443 )
138+ connection_manager .hostnames .add ("a.com" , 443 )
139+
140+ hostnames_sync = Hostnames ()
141+ hostnames_sync .add ("example.com" , 443 , 15 )
142+ hostnames_sync .add ("c.com" , 443 )
143+
124144 data = {
125145 "current_routes" : {
126146 "route1" : {
@@ -131,6 +151,7 @@ def test_process_sync_data_existing_route(setup_connection_manager):
131151 }
132152 },
133153 "reqs" : 5 , # Total requests to be added
154+ "hostnames" : hostnames_sync .as_array (),
134155 }
135156
136157 # First call to initialize the route
@@ -162,6 +183,12 @@ def test_process_sync_data_existing_route(setup_connection_manager):
162183 # Check that the total requests were updated
163184 assert connection_manager .statistics .requests ["total" ] == 20 # 5 + 15
164185 assert connection_manager .middleware_installed == False
186+ assert connection_manager .hostnames .as_array () == [
187+ {"hits" : 215 , "hostname" : "example.com" , "port" : 443 },
188+ {"hits" : 1 , "hostname" : "example.org" , "port" : 443 },
189+ {"hits" : 1 , "hostname" : "a.com" , "port" : 443 },
190+ {"hits" : 1 , "hostname" : "c.com" , "port" : 443 },
191+ ]
165192
166193 # Check that the return value is correct
167194 assert result ["routes" ] == dict (connection_manager .routes .routes )
0 commit comments