1+ import filecmp
2+ import imp
13import os
24import traceback
5+ import json
36from unittest import mock
47
58import click
69from click .testing import CliRunner
710
11+ from sonic_py_common import device_info
812from utilities_common .db import Db
913
1014load_minigraph_command_output = """\
@@ -61,8 +65,10 @@ class TestLoadMinigraph(object):
6165 def setup_class (cls ):
6266 os .environ ['UTILITIES_UNIT_TESTING' ] = "1"
6367 print ("SETUP" )
68+ import config .main
69+ imp .reload (config .main )
6470
65- def test_load_minigraph (self , get_cmd_module , setup_single_broacom_asic ):
71+ def test_load_minigraph (self , get_cmd_module , setup_single_broadcom_asic ):
6672 with mock .patch ("utilities_common.cli.run_command" , mock .MagicMock (side_effect = mock_run_command_side_effect )) as mock_run_command :
6773 (config , show ) = get_cmd_module
6874 runner = CliRunner ()
@@ -74,7 +80,7 @@ def test_load_minigraph(self, get_cmd_module, setup_single_broacom_asic):
7480 assert "\n " .join ([l .rstrip () for l in result .output .split ('\n ' )]) == load_minigraph_command_output
7581 assert mock_run_command .call_count == 38
7682
77- def test_load_minigraph_with_disabled_telemetry (self , get_cmd_module , setup_single_broacom_asic ):
83+ def test_load_minigraph_with_disabled_telemetry (self , get_cmd_module , setup_single_broadcom_asic ):
7884 with mock .patch ("utilities_common.cli.run_command" , mock .MagicMock (side_effect = mock_run_command_side_effect )) as mock_run_command :
7985 (config , show ) = get_cmd_module
8086 db = Db ()
@@ -95,3 +101,98 @@ def test_load_minigraph_with_disabled_telemetry(self, get_cmd_module, setup_sing
95101 def teardown_class (cls ):
96102 os .environ ['UTILITIES_UNIT_TESTING' ] = "0"
97103 print ("TEARDOWN" )
104+
105+
106+ class TestConfigQos (object ):
107+ @classmethod
108+ def setup_class (cls ):
109+ print ("SETUP" )
110+ os .environ ['UTILITIES_UNIT_TESTING' ] = "2"
111+ import config .main
112+ imp .reload (config .main )
113+
114+ def test_qos_reload_single (
115+ self , get_cmd_module , setup_qos_mock_apis ,
116+ setup_single_broadcom_asic
117+ ):
118+ (config , show ) = get_cmd_module
119+ runner = CliRunner ()
120+ output_file = os .path .join (os .sep , "tmp" , "qos_config_output.json" )
121+ print ("Saving output in {}" .format (output_file ))
122+ try :
123+ os .remove (output_file )
124+ except OSError :
125+ pass
126+ json_data = '{"DEVICE_METADATA": {"localhost": {}}}'
127+ result = runner .invoke (
128+ config .config .commands ["qos" ],
129+ ["reload" , "--dry_run" , output_file , "--json-data" , json_data ]
130+ )
131+ print (result .exit_code )
132+ print (result .output )
133+ assert result .exit_code == 0
134+
135+ cwd = os .path .dirname (os .path .realpath (__file__ ))
136+ expected_result = os .path .join (
137+ cwd , "qos_config_input" , "config_qos.json"
138+ )
139+ assert filecmp .cmp (output_file , expected_result , shallow = False )
140+
141+ @classmethod
142+ def teardown_class (cls ):
143+ print ("TEARDOWN" )
144+ os .environ ['UTILITIES_UNIT_TESTING' ] = "0"
145+
146+
147+ class TestConfigQosMasic (object ):
148+ @classmethod
149+ def setup_class (cls ):
150+ print ("SETUP" )
151+ os .environ ['UTILITIES_UNIT_TESTING' ] = "2"
152+ os .environ ["UTILITIES_UNIT_TESTING_TOPOLOGY" ] = "multi_asic"
153+ import config .main
154+ imp .reload (config .main )
155+
156+ def test_qos_reload_masic (
157+ self , get_cmd_module , setup_qos_mock_apis ,
158+ setup_multi_broadcom_masic
159+ ):
160+ (config , show ) = get_cmd_module
161+ runner = CliRunner ()
162+ output_file = os .path .join (os .sep , "tmp" , "qos_config_output.json" )
163+ print ("Saving output in {}<0,1,2..>" .format (output_file ))
164+ num_asic = device_info .get_num_npus ()
165+ for asic in range (num_asic ):
166+ try :
167+ file = "{}{}" .format (output_file , asic )
168+ os .remove (file )
169+ except OSError :
170+ pass
171+ json_data = '{"DEVICE_METADATA": {"localhost": {}}}'
172+ result = runner .invoke (
173+ config .config .commands ["qos" ],
174+ ["reload" , "--dry_run" , output_file , "--json-data" , json_data ]
175+ )
176+ print (result .exit_code )
177+ print (result .output )
178+ assert result .exit_code == 0
179+
180+ cwd = os .path .dirname (os .path .realpath (__file__ ))
181+
182+ for asic in range (num_asic ):
183+ expected_result = os .path .join (
184+ cwd , "qos_config_input" , str (asic ), "config_qos.json"
185+ )
186+ file = "{}{}" .format (output_file , asic )
187+ assert filecmp .cmp (file , expected_result , shallow = False )
188+
189+ @classmethod
190+ def teardown_class (cls ):
191+ print ("TEARDOWN" )
192+ os .environ ['UTILITIES_UNIT_TESTING' ] = "0"
193+ os .environ ["UTILITIES_UNIT_TESTING_TOPOLOGY" ] = ""
194+ # change back to single asic config
195+ from .mock_tables import dbconnector
196+ from .mock_tables import mock_single_asic
197+ imp .reload (mock_single_asic )
198+ dbconnector .load_namespace_config ()
0 commit comments