@@ -37,7 +37,6 @@ def mock_run_command_side_effect(*args, **kwargs):
3737 if kwargs .get ('return_cmd' ):
3838 return ''
3939
40-
4140class TestLoadMinigraph (object ):
4241 @classmethod
4342 def setup_class (cls ):
@@ -58,6 +57,58 @@ def test_load_minigraph(self, get_cmd_module, setup_single_broadcom_asic):
5857 assert "\n " .join ([l .rstrip () for l in result .output .split ('\n ' )]) == load_minigraph_command_output
5958 assert mock_run_command .call_count == 7
6059
60+ def test_load_minigraph_with_port_config_bad_format (self , setup_single_broadcom_asic ):
61+ with mock .patch (
62+ "utilities_common.cli.run_command" ,
63+ mock .MagicMock (side_effect = mock_run_command_side_effect )) as mock_run_command :
64+
65+ # Not in an array
66+ port_config = {"PORT" : {"Ethernet0" : {"admin_status" : "up" }}}
67+ self .check_port_config (None , port_config , "Failed to load port_config.json, Error: Bad format: port_config is not an array" )
68+
69+ # No PORT table
70+ port_config = [{}]
71+ self .check_port_config (None , port_config , "Failed to load port_config.json, Error: Bad format: PORT table not exists" )
72+
73+ def test_load_minigraph_with_port_config_inconsistent_port (self , setup_single_broadcom_asic ):
74+ with mock .patch (
75+ "utilities_common.cli.run_command" ,
76+ mock .MagicMock (side_effect = mock_run_command_side_effect )) as mock_run_command :
77+ db = Db ()
78+ db .cfgdb .set_entry ("PORT" , "Ethernet1" , {"admin_status" : "up" })
79+ port_config = [{"PORT" : {"Eth1" : {"admin_status" : "up" }}}]
80+ self .check_port_config (db , port_config , "Failed to load port_config.json, Error: Port Eth1 is not defined in current device" )
81+
82+ def test_load_minigraph_with_port_config (self , setup_single_broadcom_asic ):
83+ with mock .patch (
84+ "utilities_common.cli.run_command" ,
85+ mock .MagicMock (side_effect = mock_run_command_side_effect )) as mock_run_command :
86+ db = Db ()
87+
88+ # From up to down
89+ db .cfgdb .set_entry ("PORT" , "Ethernet0" , {"admin_status" : "up" })
90+ port_config = [{"PORT" : {"Ethernet0" : {"admin_status" : "down" }}}]
91+ self .check_port_config (db , port_config , "config interface shutdown Ethernet0" )
92+
93+ # From down to up
94+ db .cfgdb .set_entry ("PORT" , "Ethernet0" , {"admin_status" : "down" })
95+ port_config = [{"PORT" : {"Ethernet0" : {"admin_status" : "up" }}}]
96+ self .check_port_config (db , port_config , "config interface startup Ethernet0" )
97+
98+ def check_port_config (self , db , port_config , expected_output ):
99+ def read_json_file_side_effect (filename ):
100+ return port_config
101+ with mock .patch ('config.main.read_json_file' , mock .MagicMock (side_effect = read_json_file_side_effect )):
102+ def is_file_side_effect (filename ):
103+ return True
104+ with mock .patch ('os.path.isfile' , mock .MagicMock (side_effect = is_file_side_effect )):
105+ runner = CliRunner ()
106+ result = runner .invoke (config .config .commands ["load_minigraph" ], ["-y" ], obj = db )
107+ print (result .exit_code )
108+ print (result .output )
109+ assert result .exit_code == 0
110+ assert expected_output in result .output
111+
61112 @classmethod
62113 def teardown_class (cls ):
63114 os .environ ['UTILITIES_UNIT_TESTING' ] = "0"
0 commit comments