@@ -18,18 +18,14 @@ class TestPortLogic(unittest.TestCase):
1818 def test_assign_app_ports_single_port (self ):
1919 """Test assigning a single port to an app."""
2020 app_name = "dawn"
21- app = {
22- "compose_config" : {
23- "ports" : ["${DAWN_PORT}:5000" ]
24- }
25- }
21+ app = {"compose_config" : {"ports" : ["${DAWN_PORT}:5000" ]}}
2622 config = {"ports" : [5000 ]}
27-
23+
2824 with patch ("utils.fn_setupApps.find_next_available_port" ) as mock_find_port :
2925 mock_find_port .side_effect = lambda x : x # Return the same port
30-
26+
3127 result = assign_app_ports (app_name , app , config )
32-
28+
3329 self .assertIsInstance (result , list )
3430 self .assertEqual (len (result ), 1 )
3531 self .assertEqual (result , [5000 ])
@@ -39,38 +35,31 @@ def test_assign_app_ports_multiple_ports(self):
3935 app_name = "wipter"
4036 app = {
4137 "compose_config" : {
42- "ports" : [
43- "${WIPTER_PORT_1}:5900" ,
44- "${WIPTER_PORT_2}:6080"
45- ]
38+ "ports" : ["${WIPTER_PORT_1}:5900" , "${WIPTER_PORT_2}:6080" ]
4639 }
4740 }
4841 config = {"ports" : [5900 , 6080 ]}
49-
42+
5043 with patch ("utils.fn_setupApps.find_next_available_port" ) as mock_find_port :
5144 mock_find_port .side_effect = lambda x : x # Return the same port
52-
45+
5346 result = assign_app_ports (app_name , app , config )
54-
47+
5548 self .assertIsInstance (result , list )
5649 self .assertEqual (len (result ), 2 )
5750 self .assertEqual (result , [5900 , 6080 ])
5851
5952 def test_assign_app_ports_default_when_no_config (self ):
6053 """Test default port assignment when config doesn't have ports."""
6154 app_name = "mystnode"
62- app = {
63- "compose_config" : {
64- "ports" : ["${MYSTNODE_PORT}:4449" ]
65- }
66- }
55+ app = {"compose_config" : {"ports" : ["${MYSTNODE_PORT}:4449" ]}}
6756 config = {} # No ports in config
68-
57+
6958 with patch ("utils.fn_setupApps.find_next_available_port" ) as mock_find_port :
7059 mock_find_port .side_effect = lambda x : x # Return the same port
71-
60+
7261 result = assign_app_ports (app_name , app , config )
73-
62+
7463 self .assertIsInstance (result , list )
7564 self .assertEqual (len (result ), 1 )
7665 self .assertEqual (result [0 ], 50000 ) # Default starting port
@@ -79,65 +68,58 @@ def test_substitute_port_placeholders_single(self):
7968 """Test substituting a single port placeholder."""
8069 port_placeholders = ["${DAWN_PORT}:5000" ]
8170 actual_ports = [8080 ]
82-
71+
8372 result = substitute_port_placeholders (port_placeholders , actual_ports )
84-
73+
8574 self .assertEqual (result , ["8080:5000" ])
8675
8776 def test_substitute_port_placeholders_multiple (self ):
8877 """Test substituting multiple port placeholders."""
89- port_placeholders = [
90- "${WIPTER_PORT_1}:5900" ,
91- "${WIPTER_PORT_2}:6080"
92- ]
78+ port_placeholders = ["${WIPTER_PORT_1}:5900" , "${WIPTER_PORT_2}:6080" ]
9379 actual_ports = [5901 , 6081 ]
94-
80+
9581 result = substitute_port_placeholders (port_placeholders , actual_ports )
96-
82+
9783 self .assertEqual (result , ["5901:5900" , "6081:6080" ])
9884
9985 def test_substitute_port_placeholders_uses_first_when_insufficient (self ):
10086 """Test that substitute uses first port when actual_ports list is too short."""
10187 port_placeholders = [
10288 "${APP_PORT_1}:5000" ,
10389 "${APP_PORT_2}:5001" ,
104- "${APP_PORT_3}:5002"
90+ "${APP_PORT_3}:5002" ,
10591 ]
10692 actual_ports = [8080 ] # Only one port provided
107-
93+
10894 result = substitute_port_placeholders (port_placeholders , actual_ports )
109-
95+
11096 self .assertEqual (result , ["8080:5000" , "8080:5001" , "8080:5002" ])
11197
11298 def test_substitute_port_placeholders_empty_raises_error (self ):
11399 """Test that empty actual_ports raises ValueError."""
114100 port_placeholders = ["${APP_PORT}:5000" ]
115101 actual_ports = [] # Empty list
116-
102+
117103 with self .assertRaises (ValueError ) as context :
118104 substitute_port_placeholders (port_placeholders , actual_ports )
119-
105+
120106 self .assertIn ("cannot be empty" , str (context .exception ))
121107
122108 def test_config_ports_always_list (self ):
123109 """Test that ports are always stored as list in config."""
124110 # Simulate the setup flow
125111 app_name = "dawn"
126- app = {
127- "compose_config" : {
128- "ports" : ["${DAWN_PORT}:5000" ]
129- }
130- }
112+ app = {"compose_config" : {"ports" : ["${DAWN_PORT}:5000" ]}}
131113 config = {"ports" : [5000 ]} # Should be list
132-
114+
133115 with patch ("utils.fn_setupApps.find_next_available_port" ) as mock_find_port :
134116 mock_find_port .return_value = 5000
135-
117+
136118 assigned_ports = assign_app_ports (app_name , app , config )
137-
119+
138120 # Verify it returns a list
139121 self .assertIsInstance (assigned_ports , list )
140-
122+
141123 # Verify we would store it as list
142124 config ["ports" ] = assigned_ports
143125 self .assertIsInstance (config ["ports" ], list )
@@ -147,31 +129,26 @@ def test_generate_env_file_handles_list_ports(self):
147129 # Mock configurations
148130 m4b_config = {
149131 "network" : {"subnet" : "172.19.7.0" , "netmask" : "27" },
150- "system" : {"sleep_time" : 3 }
132+ "system" : {"sleep_time" : 3 },
151133 }
152-
134+
153135 app_config = {
154136 "apps" : [
155137 {
156138 "name" : "DAWN" ,
157139 "flags" : {"email" : {}, "password" : {}},
158- "compose_config" : {
159- "ports" : ["${DAWN_PORT}:5000" ]
160- }
140+ "compose_config" : {"ports" : ["${DAWN_PORT}:5000" ]},
161141 },
162142 {
163143 "name" : "WIPTER" ,
164144 "flags" : {"email" : {}, "password" : {}},
165145 "compose_config" : {
166- "ports" : [
167- "${WIPTER_PORT_1}:5900" ,
168- "${WIPTER_PORT_2}:6080"
169- ]
170- }
171- }
146+ "ports" : ["${WIPTER_PORT_1}:5900" , "${WIPTER_PORT_2}:6080" ]
147+ },
148+ },
172149 ]
173150 }
174-
151+
175152 user_config = {
176153 "device_info" : {"device_name" : "test_device" },
177154 "resource_limits" : {},
@@ -180,20 +157,18 @@ def test_generate_env_file_handles_list_ports(self):
180157 "enabled" : True ,
181158 "email" : "test@test.com" ,
182159 "password" : "pass" ,
183- "ports" : [8080 ] # List with single element
160+ "ports" : [8080 ], # List with single element
184161 },
185162 "wipter" : {
186163 "enabled" : True ,
187164 "email" : "test@test.com" ,
188165 "password" : "pass" ,
189- "ports" : [5901 , 6081 ] # List with multiple elements
190- }
191- }
166+ "ports" : [5901 , 6081 ], # List with multiple elements
167+ },
168+ },
192169 }
193-
194- with tempfile .NamedTemporaryFile (
195- mode = "w" , delete = False , suffix = ".env"
196- ) as f :
170+
171+ with tempfile .NamedTemporaryFile (mode = "w" , delete = False , suffix = ".env" ) as f :
197172 env_path = f .name
198173
199174 try :
0 commit comments