@@ -37,7 +37,6 @@ def mock_run_command_side_effect(*args, **kwargs):
37
37
if kwargs .get ('return_cmd' ):
38
38
return ''
39
39
40
-
41
40
class TestLoadMinigraph (object ):
42
41
@classmethod
43
42
def setup_class (cls ):
@@ -58,6 +57,58 @@ def test_load_minigraph(self, get_cmd_module, setup_single_broadcom_asic):
58
57
assert "\n " .join ([l .rstrip () for l in result .output .split ('\n ' )]) == load_minigraph_command_output
59
58
assert mock_run_command .call_count == 7
60
59
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
+
61
112
@classmethod
62
113
def teardown_class (cls ):
63
114
os .environ ['UTILITIES_UNIT_TESTING' ] = "0"
0 commit comments