Skip to content

Commit 5ca4ca7

Browse files
committed
refactor: clean up formatting in user-config.json and test_port_logic.py; streamline port handling in fn_setupApps.py and generator.py
1 parent 535610c commit 5ca4ca7

4 files changed

Lines changed: 50 additions & 78 deletions

File tree

template/user-config.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,11 @@
118118
"userid": "yourPROXYBASEUserID"
119119
},
120120
"wipter": {
121-
"enabled": false,
122-
"docker_platform": "linux/amd64",
123-
"email": "yourWIPTERMail",
124-
"password": "yourWIPTERPw",
125-
"ports": [5900, 6080]
121+
"enabled": false,
122+
"docker_platform": "linux/amd64",
123+
"email": "yourWIPTERMail",
124+
"password": "yourWIPTERPw",
125+
"ports": [5900, 6080]
126126
}
127127
},
128128
"m4b_dashboard": {

tests/test_port_logic.py

Lines changed: 40 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -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:

utils/fn_setupApps.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -737,12 +737,13 @@ def setup_multiproxy_instances(
737737
if has_ports:
738738
# Get base port from user config or default
739739
base_port = app_config_entry.get(
740-
"ports", [DEFAULT_PORT_BASE + app_index * PORT_OFFSET_PER_APP]
740+
"ports",
741+
[DEFAULT_PORT_BASE + app_index * PORT_OFFSET_PER_APP],
741742
)
742743
# Ensure we have a list (handles legacy configs with int)
743744
if not isinstance(base_port, list):
744745
base_port = [base_port]
745-
746+
746747
# Update all ports with unique values for this instance
747748
app_config_entry["ports"] = [
748749
find_next_available_port(

utils/generator.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -513,12 +513,8 @@ def generate_env_file(
513513
env_lines.append(f"{app_name.upper()}_PORT_{i + 1}={port}")
514514
# For backward compatibility, also add non-indexed variable for single port
515515
if len(ports) == 1:
516-
env_lines.append(
517-
f"{app_name.upper()}_PORT={port}"
518-
)
519-
env_lines.append(
520-
f"{app_lower.upper()}_PORT={port}"
521-
)
516+
env_lines.append(f"{app_name.upper()}_PORT={port}")
517+
env_lines.append(f"{app_lower.upper()}_PORT={port}")
522518

523519
# Write to .env file
524520
with open(env_output_path, "w") as f:

0 commit comments

Comments
 (0)