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