168168Reloading Monit configuration ...
169169"""
170170
171+ save_config_output = """\
172+ Running command: /usr/local/bin/sonic-cfggen -d --print-data > /etc/sonic/config_db.json
173+ """
174+
175+ save_config_filename_output = """\
176+ Running command: /usr/local/bin/sonic-cfggen -d --print-data > /tmp/config_db.json
177+ """
178+
179+ save_config_masic_output = """\
180+ Running command: /usr/local/bin/sonic-cfggen -d --print-data > /etc/sonic/config_db.json
181+ Running command: /usr/local/bin/sonic-cfggen -n asic0 -d --print-data > /etc/sonic/config_db0.json
182+ Running command: /usr/local/bin/sonic-cfggen -n asic1 -d --print-data > /etc/sonic/config_db1.json
183+ """
184+
185+ save_config_filename_masic_output = """\
186+ Running command: /usr/local/bin/sonic-cfggen -d --print-data > config_db.json
187+ Running command: /usr/local/bin/sonic-cfggen -n asic0 -d --print-data > config_db0.json
188+ Running command: /usr/local/bin/sonic-cfggen -n asic1 -d --print-data > config_db1.json
189+ """
190+
191+ save_config_onefile_masic_output = """\
192+ Integrate each ASIC's config into a single JSON file /tmp/all_config_db.json.
193+ """
194+
171195config_temp = {
172196 "scope" : {
173197 "ACL_TABLE" : {
@@ -333,6 +357,191 @@ def test_plattform_fw_update(self, mock_check_call):
333357 assert result .exit_code == 0
334358 mock_check_call .assert_called_with (["fwutil" , "update" , 'update' , 'module' , 'Module1' , 'component' , 'BIOS' , 'fw' ])
335359
360+
361+ class TestConfigSave (object ):
362+ @classmethod
363+ def setup_class (cls ):
364+ os .environ ['UTILITIES_UNIT_TESTING' ] = "1"
365+ print ("SETUP" )
366+ import config .main
367+ importlib .reload (config .main )
368+
369+ def test_config_save (self , get_cmd_module , setup_single_broadcom_asic ):
370+ def read_json_file_side_effect (filename ):
371+ return {}
372+
373+ with mock .patch ("utilities_common.cli.run_command" ,
374+ mock .MagicMock (side_effect = mock_run_command_side_effect )),\
375+ mock .patch ('config.main.read_json_file' ,
376+ mock .MagicMock (side_effect = read_json_file_side_effect )),\
377+ mock .patch ('config.main.open' ,
378+ mock .MagicMock ()):
379+ (config , show ) = get_cmd_module
380+
381+ runner = CliRunner ()
382+
383+ result = runner .invoke (config .config .commands ["save" ], ["-y" ])
384+
385+ print (result .exit_code )
386+ print (result .output )
387+ traceback .print_tb (result .exc_info [2 ])
388+
389+ assert result .exit_code == 0
390+ assert "\n " .join ([li .rstrip () for li in result .output .split ('\n ' )]) == save_config_output
391+
392+ def test_config_save_filename (self , get_cmd_module , setup_single_broadcom_asic ):
393+ def read_json_file_side_effect (filename ):
394+ return {}
395+
396+ with mock .patch ("utilities_common.cli.run_command" ,
397+ mock .MagicMock (side_effect = mock_run_command_side_effect )),\
398+ mock .patch ('config.main.read_json_file' ,
399+ mock .MagicMock (side_effect = read_json_file_side_effect )),\
400+ mock .patch ('config.main.open' ,
401+ mock .MagicMock ()):
402+
403+ (config , show ) = get_cmd_module
404+
405+ runner = CliRunner ()
406+
407+ output_file = os .path .join (os .sep , "tmp" , "config_db.json" )
408+ result = runner .invoke (config .config .commands ["save" ], ["-y" , output_file ])
409+
410+ print (result .exit_code )
411+ print (result .output )
412+ traceback .print_tb (result .exc_info [2 ])
413+
414+ assert result .exit_code == 0
415+ assert "\n " .join ([li .rstrip () for li in result .output .split ('\n ' )]) == save_config_filename_output
416+
417+ @classmethod
418+ def teardown_class (cls ):
419+ print ("TEARDOWN" )
420+ os .environ ['UTILITIES_UNIT_TESTING' ] = "0"
421+
422+
423+ class TestConfigSaveMasic (object ):
424+ @classmethod
425+ def setup_class (cls ):
426+ print ("SETUP" )
427+ os .environ ['UTILITIES_UNIT_TESTING' ] = "2"
428+ os .environ ["UTILITIES_UNIT_TESTING_TOPOLOGY" ] = "multi_asic"
429+ import config .main
430+ importlib .reload (config .main )
431+ # change to multi asic config
432+ from .mock_tables import dbconnector
433+ from .mock_tables import mock_multi_asic
434+ importlib .reload (mock_multi_asic )
435+ dbconnector .load_namespace_config ()
436+
437+ def test_config_save_masic (self ):
438+ def read_json_file_side_effect (filename ):
439+ return {}
440+
441+ with mock .patch ("utilities_common.cli.run_command" ,
442+ mock .MagicMock (side_effect = mock_run_command_side_effect )),\
443+ mock .patch ('config.main.read_json_file' ,
444+ mock .MagicMock (side_effect = read_json_file_side_effect )),\
445+ mock .patch ('config.main.open' ,
446+ mock .MagicMock ()):
447+
448+ runner = CliRunner ()
449+
450+ result = runner .invoke (config .config .commands ["save" ], ["-y" ])
451+
452+ print (result .exit_code )
453+ print (result .output )
454+ traceback .print_tb (result .exc_info [2 ])
455+
456+ assert result .exit_code == 0
457+ assert "\n " .join ([li .rstrip () for li in result .output .split ('\n ' )]) == save_config_masic_output
458+
459+ def test_config_save_filename_masic (self ):
460+ def read_json_file_side_effect (filename ):
461+ return {}
462+
463+ with mock .patch ("utilities_common.cli.run_command" ,
464+ mock .MagicMock (side_effect = mock_run_command_side_effect )),\
465+ mock .patch ('config.main.read_json_file' ,
466+ mock .MagicMock (side_effect = read_json_file_side_effect )),\
467+ mock .patch ('config.main.open' ,
468+ mock .MagicMock ()):
469+
470+ runner = CliRunner ()
471+
472+ result = runner .invoke (
473+ config .config .commands ["save" ],
474+ ["-y" , "config_db.json,config_db0.json,config_db1.json" ]
475+ )
476+
477+ print (result .exit_code )
478+ print (result .output )
479+ traceback .print_tb (result .exc_info [2 ])
480+
481+ assert result .exit_code == 0
482+ assert "\n " .join ([li .rstrip () for li in result .output .split ('\n ' )]) == save_config_filename_masic_output
483+
484+ def test_config_save_filename_wrong_cnt_masic (self ):
485+ def read_json_file_side_effect (filename ):
486+ return {}
487+
488+ with mock .patch ('config.main.read_json_file' , mock .MagicMock (side_effect = read_json_file_side_effect )):
489+
490+ runner = CliRunner ()
491+
492+ result = runner .invoke (
493+ config .config .commands ["save" ],
494+ ["-y" , "config_db.json,config_db0.json" ]
495+ )
496+
497+ print (result .exit_code )
498+ print (result .output )
499+ traceback .print_tb (result .exc_info [2 ])
500+
501+ assert "Input 3 config file(s) separated by comma for multiple files" in result .output
502+
503+ def test_config_save_onefile_masic (self ):
504+ def get_config_side_effect ():
505+ return {}
506+
507+ with mock .patch ('swsscommon.swsscommon.ConfigDBConnector.get_config' ,
508+ mock .MagicMock (side_effect = get_config_side_effect )):
509+ runner = CliRunner ()
510+
511+ output_file = os .path .join (os .sep , "tmp" , "all_config_db.json" )
512+ print ("Saving output in {}" .format (output_file ))
513+ try :
514+ os .remove (output_file )
515+ except OSError :
516+ pass
517+ result = runner .invoke (
518+ config .config .commands ["save" ],
519+ ["-y" , output_file ]
520+ )
521+
522+ print (result .exit_code )
523+ print (result .output )
524+ assert result .exit_code == 0
525+ assert "\n " .join ([li .rstrip () for li in result .output .split ('\n ' )]) == save_config_onefile_masic_output
526+
527+ cwd = os .path .dirname (os .path .realpath (__file__ ))
528+ expected_result = os .path .join (
529+ cwd , "config_save_output" , "all_config_db.json"
530+ )
531+ assert filecmp .cmp (output_file , expected_result , shallow = False )
532+
533+ @classmethod
534+ def teardown_class (cls ):
535+ print ("TEARDOWN" )
536+ os .environ ['UTILITIES_UNIT_TESTING' ] = "0"
537+ os .environ ["UTILITIES_UNIT_TESTING_TOPOLOGY" ] = ""
538+ # change back to single asic config
539+ from .mock_tables import dbconnector
540+ from .mock_tables import mock_single_asic
541+ importlib .reload (mock_single_asic )
542+ dbconnector .load_namespace_config ()
543+
544+
336545class TestConfigReload (object ):
337546 dummy_cfg_file = os .path .join (os .sep , "tmp" , "config.json" )
338547
@@ -2889,4 +3098,4 @@ def teardown_class(cls):
28893098 from .mock_tables import dbconnector
28903099 from .mock_tables import mock_single_asic
28913100 importlib .reload (mock_single_asic )
2892- dbconnector .load_database_config ()
3101+ dbconnector .load_database_config ()
0 commit comments