Skip to content

Commit

Permalink
[sonic-cfggen] Allow cfggen to work on system without ports (sonic-ne…
Browse files Browse the repository at this point in the history
…t#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
  • Loading branch information
liorghub committed Aug 23, 2021
1 parent 26c6e7f commit f5ec889
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
8 changes: 4 additions & 4 deletions src/sonic-config-engine/portconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,9 +261,9 @@ def parse_platform_json_file(hwsku_json_file, platform_json_file):
port_dict = readJson(platform_json_file)
hwsku_dict = readJson(hwsku_json_file)

if not port_dict:
if port_dict is None:
raise Exception("port_dict is none")
if not hwsku_dict:
if hwsku_dict is None:
raise Exception("hwsku_dict is none")

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

ports.update(child_ports)

if not ports:
raise Exception("Ports dictionary is empty")
if ports is None:
raise Exception("Ports dictionary is None")

for i in ports.keys():
port_alias_map[ports[i]["alias"]]= i
Expand Down
2 changes: 1 addition & 1 deletion src/sonic-config-engine/sonic-cfggen
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ def main():
if args.port_config is None:
args.port_config = device_info.get_path_to_port_config_file(hwsku)
(ports, _, _) = get_port_config(hwsku, platform, args.port_config, asic_id)
if not ports:
if ports is None:
print('Failed to get port config', file=sys.stderr)
sys.exit(1)
deep_update(data, {'PORT': ports})
Expand Down
13 changes: 13 additions & 0 deletions src/sonic-config-engine/tests/test_cfggen_platformJson.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@
import json
import os
import subprocess
import sys

import tests.common_utils as utils

from unittest import TestCase
from portconfig import get_port_config, INTF_KEY

if sys.version_info.major == 3:
from unittest import mock
else:
import mock

# Global Variable
PLATFORM_OUTPUT_FILE = "platform_output.json"
Expand Down Expand Up @@ -85,3 +91,10 @@ def test_platform_json_all_ethernet_interfaces(self):
output_dict = ast.literal_eval(output.strip())
expected = ast.literal_eval(json.dumps(fh_data))
self.assertDictEqual(output_dict, expected)

@mock.patch('portconfig.readJson', mock.MagicMock(return_value={INTF_KEY:{}}))
@mock.patch('os.path.isfile', mock.MagicMock(return_value=True))
def test_platform_json_no_interfaces(self):
(ports, _, _) = get_port_config(port_config_file=self.platform_json)
self.assertNotEqual(ports, None)
self.assertEqual(ports, {})

0 comments on commit f5ec889

Please sign in to comment.