Skip to content

Commit

Permalink
[minigraph][port_config] Use imported config.main and add conditional…
Browse files Browse the repository at this point in the history
… patch (sonic-net#1724)

Signed-off-by: Jing Kan jika@microsoft.com
  • Loading branch information
Blueve authored Jul 26, 2021
1 parent acb5d84 commit b83d2bf
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions tests/config_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,50 +57,54 @@ def test_load_minigraph(self, get_cmd_module, setup_single_broadcom_asic):
assert "\n".join([l.rstrip() for l in result.output.split('\n')]) == load_minigraph_command_output
assert mock_run_command.call_count == 7

def test_load_minigraph_with_port_config_bad_format(self, setup_single_broadcom_asic):
def test_load_minigraph_with_port_config_bad_format(self, get_cmd_module, setup_single_broadcom_asic):
with mock.patch(
"utilities_common.cli.run_command",
mock.MagicMock(side_effect=mock_run_command_side_effect)) as mock_run_command:
(config, show) = get_cmd_module

# Not in an array
port_config = {"PORT": {"Ethernet0": {"admin_status": "up"}}}
self.check_port_config(None, port_config, "Failed to load port_config.json, Error: Bad format: port_config is not an array")
self.check_port_config(None, config, port_config, "Failed to load port_config.json, Error: Bad format: port_config is not an array")

# No PORT table
port_config = [{}]
self.check_port_config(None, port_config, "Failed to load port_config.json, Error: Bad format: PORT table not exists")
self.check_port_config(None, config, port_config, "Failed to load port_config.json, Error: Bad format: PORT table not exists")

def test_load_minigraph_with_port_config_inconsistent_port(self, setup_single_broadcom_asic):
def test_load_minigraph_with_port_config_inconsistent_port(self, get_cmd_module, setup_single_broadcom_asic):
with mock.patch(
"utilities_common.cli.run_command",
mock.MagicMock(side_effect=mock_run_command_side_effect)) as mock_run_command:
(config, show) = get_cmd_module

db = Db()
db.cfgdb.set_entry("PORT", "Ethernet1", {"admin_status": "up"})
port_config = [{"PORT": {"Eth1": {"admin_status": "up"}}}]
self.check_port_config(db, port_config, "Failed to load port_config.json, Error: Port Eth1 is not defined in current device")
self.check_port_config(db, config, port_config, "Failed to load port_config.json, Error: Port Eth1 is not defined in current device")

def test_load_minigraph_with_port_config(self, setup_single_broadcom_asic):
def test_load_minigraph_with_port_config(self, get_cmd_module, setup_single_broadcom_asic):
with mock.patch(
"utilities_common.cli.run_command",
mock.MagicMock(side_effect=mock_run_command_side_effect)) as mock_run_command:
(config, show) = get_cmd_module
db = Db()

# From up to down
db.cfgdb.set_entry("PORT", "Ethernet0", {"admin_status": "up"})
port_config = [{"PORT": {"Ethernet0": {"admin_status": "down"}}}]
self.check_port_config(db, port_config, "config interface shutdown Ethernet0")
self.check_port_config(db, config, port_config, "config interface shutdown Ethernet0")

# From down to up
db.cfgdb.set_entry("PORT", "Ethernet0", {"admin_status": "down"})
port_config = [{"PORT": {"Ethernet0": {"admin_status": "up"}}}]
self.check_port_config(db, port_config, "config interface startup Ethernet0")
self.check_port_config(db, config, port_config, "config interface startup Ethernet0")

def check_port_config(self, db, port_config, expected_output):
def check_port_config(self, db, config, port_config, expected_output):
def read_json_file_side_effect(filename):
return port_config
with mock.patch('config.main.read_json_file', mock.MagicMock(side_effect=read_json_file_side_effect)):
def is_file_side_effect(filename):
return True
return True if 'port_config' in filename else False
with mock.patch('os.path.isfile', mock.MagicMock(side_effect=is_file_side_effect)):
runner = CliRunner()
result = runner.invoke(config.config.commands["load_minigraph"], ["-y"], obj=db)
Expand Down

0 comments on commit b83d2bf

Please sign in to comment.