Skip to content

Commit f5ec889

Browse files
authored
[sonic-cfggen] Allow cfggen to work on system without ports (sonic-net#7999)
#### Why I did it Allow cfggen to work on system without ports in platform.json or in port_config.ini #### How I did it Add json write of PORT section only if the dictionary that contains the ports is not empty. #### How to verify it sonic-cfggen -k ACS-MSN3700 -H -j /etc/sonic/init_cfg.json --print-data
1 parent 26c6e7f commit f5ec889

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed

src/sonic-config-engine/portconfig.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -261,9 +261,9 @@ def parse_platform_json_file(hwsku_json_file, platform_json_file):
261261
port_dict = readJson(platform_json_file)
262262
hwsku_dict = readJson(hwsku_json_file)
263263

264-
if not port_dict:
264+
if port_dict is None:
265265
raise Exception("port_dict is none")
266-
if not hwsku_dict:
266+
if hwsku_dict is None:
267267
raise Exception("hwsku_dict is none")
268268

269269
if INTF_KEY not in port_dict or INTF_KEY not in hwsku_dict:
@@ -285,8 +285,8 @@ def parse_platform_json_file(hwsku_json_file, platform_json_file):
285285

286286
ports.update(child_ports)
287287

288-
if not ports:
289-
raise Exception("Ports dictionary is empty")
288+
if ports is None:
289+
raise Exception("Ports dictionary is None")
290290

291291
for i in ports.keys():
292292
port_alias_map[ports[i]["alias"]]= i

src/sonic-config-engine/sonic-cfggen

+1-1
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ def main():
304304
if args.port_config is None:
305305
args.port_config = device_info.get_path_to_port_config_file(hwsku)
306306
(ports, _, _) = get_port_config(hwsku, platform, args.port_config, asic_id)
307-
if not ports:
307+
if ports is None:
308308
print('Failed to get port config', file=sys.stderr)
309309
sys.exit(1)
310310
deep_update(data, {'PORT': ports})

src/sonic-config-engine/tests/test_cfggen_platformJson.py

+13
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,17 @@
22
import json
33
import os
44
import subprocess
5+
import sys
56

67
import tests.common_utils as utils
78

89
from unittest import TestCase
10+
from portconfig import get_port_config, INTF_KEY
911

12+
if sys.version_info.major == 3:
13+
from unittest import mock
14+
else:
15+
import mock
1016

1117
# Global Variable
1218
PLATFORM_OUTPUT_FILE = "platform_output.json"
@@ -85,3 +91,10 @@ def test_platform_json_all_ethernet_interfaces(self):
8591
output_dict = ast.literal_eval(output.strip())
8692
expected = ast.literal_eval(json.dumps(fh_data))
8793
self.assertDictEqual(output_dict, expected)
94+
95+
@mock.patch('portconfig.readJson', mock.MagicMock(return_value={INTF_KEY:{}}))
96+
@mock.patch('os.path.isfile', mock.MagicMock(return_value=True))
97+
def test_platform_json_no_interfaces(self):
98+
(ports, _, _) = get_port_config(port_config_file=self.platform_json)
99+
self.assertNotEqual(ports, None)
100+
self.assertEqual(ports, {})

0 commit comments

Comments
 (0)