4
4
import traceback
5
5
import json
6
6
import jsonpatch
7
- import shutil
8
7
import sys
9
8
import unittest
10
9
import ipaddress
89
88
Reloading Monit configuration ...
90
89
"""
91
90
92
- RELOAD_CONFIG_DB_OUTPUT_INVALID_MSG = """\
93
- Bad format: json file"""
94
-
95
- RELOAD_CONFIG_DB_OUTPUT_INVALID_ERROR = """\
96
- Expecting ',' delimiter: line 12 column 5 (char 321)"""
97
-
98
91
RELOAD_YANG_CFG_OUTPUT = """\
99
92
Stopping SONiC target ...
100
93
Running command: /usr/local/bin/sonic-cfggen -Y /tmp/config.json --write-to-db
111
104
Reloading Monit configuration ...
112
105
"""
113
106
114
- RELOAD_MASIC_CONFIG_DB_OUTPUT_FILE_NOT_EXIST = """\
115
- Stopping SONiC target ...
116
- Running command: /usr/local/bin/sonic-cfggen -j /tmp/config.json --write-to-db
117
- The config file non_exist.json doesn't exist
118
- Running command: /usr/local/bin/sonic-cfggen -j /tmp/config.json -n asic1 --write-to-db
119
- Restarting SONiC target ...
120
- Reloading Monit configuration ...
121
- """
122
-
123
107
reload_config_with_sys_info_command_output = """\
124
108
Running command: /usr/local/bin/sonic-cfggen -H -k Seastone-DX010-25-50 --write-to-db"""
125
109
@@ -211,7 +195,6 @@ def mock_run_command_side_effect_gnmi(*args, **kwargs):
211
195
212
196
class TestConfigReload (object ):
213
197
dummy_cfg_file = os .path .join (os .sep , "tmp" , "config.json" )
214
- dummy_cfg_file_contents = os .path .join (mock_db_path , "config_db.json" )
215
198
216
199
@classmethod
217
200
def setup_class (cls ):
@@ -223,8 +206,7 @@ def setup_class(cls):
223
206
224
207
import config .main
225
208
importlib .reload (config .main )
226
- shutil .copyfile (cls .dummy_cfg_file_contents , cls .dummy_cfg_file )
227
- open (cls .dummy_cfg_file , 'r' ).close ()
209
+ open (cls .dummy_cfg_file , 'w' ).close ()
228
210
229
211
def test_config_reload (self , get_cmd_module , setup_single_broadcom_asic ):
230
212
with mock .patch ("utilities_common.cli.run_command" , mock .MagicMock (side_effect = mock_run_command_side_effect )) as mock_run_command :
@@ -497,17 +479,14 @@ def teardown_class(cls):
497
479
498
480
class TestReloadConfig (object ):
499
481
dummy_cfg_file = os .path .join (os .sep , "tmp" , "config.json" )
500
- dummy_cfg_file_contents = os .path .join (mock_db_path , "config_db.json" )
501
- dummy_cfg_file_invalid = os .path .join (mock_db_path , "config_db_invalid.json" )
502
482
503
483
@classmethod
504
484
def setup_class (cls ):
505
485
os .environ ['UTILITIES_UNIT_TESTING' ] = "1"
506
486
print ("SETUP" )
507
487
import config .main
508
488
importlib .reload (config .main )
509
- shutil .copyfile (cls .dummy_cfg_file_contents , cls .dummy_cfg_file )
510
- open (cls .dummy_cfg_file , 'r' ).close ()
489
+ open (cls .dummy_cfg_file , 'w' ).close ()
511
490
512
491
def test_reload_config (self , get_cmd_module , setup_single_broadcom_asic ):
513
492
with mock .patch (
@@ -528,27 +507,6 @@ def test_reload_config(self, get_cmd_module, setup_single_broadcom_asic):
528
507
assert "\n " .join ([l .rstrip () for l in result .output .split ('\n ' )]) \
529
508
== RELOAD_CONFIG_DB_OUTPUT
530
509
531
- def test_reload_config_invalid_config_file (self , get_cmd_module , setup_single_broadcom_asic ):
532
- with mock .patch (
533
- "utilities_common.cli.run_command" ,
534
- mock .MagicMock (side_effect = mock_run_command_side_effect )
535
- ) as mock_run_command :
536
- (config , show ) = get_cmd_module
537
- runner = CliRunner ()
538
-
539
- result = runner .invoke (
540
- config .config .commands ["reload" ],
541
- [self .dummy_cfg_file_invalid , '-y' , '-f' ])
542
-
543
- print (result .exit_code )
544
- print (result .output )
545
- traceback .print_tb (result .exc_info [2 ])
546
- assert result .exit_code == 1
547
-
548
- output = "\n " .join ([l .rstrip () for l in result .output .split ('\n ' )])
549
- assert RELOAD_CONFIG_DB_OUTPUT_INVALID_MSG in output
550
- assert RELOAD_CONFIG_DB_OUTPUT_INVALID_ERROR in output
551
-
552
510
def test_config_reload_disabled_service (self , get_cmd_module , setup_single_broadcom_asic ):
553
511
with mock .patch (
554
512
"utilities_common.cli.run_command" ,
@@ -591,54 +549,6 @@ def test_reload_config_masic(self, get_cmd_module, setup_multi_broadcom_masic):
591
549
assert "\n " .join ([l .rstrip () for l in result .output .split ('\n ' )]) \
592
550
== RELOAD_MASIC_CONFIG_DB_OUTPUT
593
551
594
- def test_reload_config_masic_invalid (self , get_cmd_module , setup_multi_broadcom_masic ):
595
- with mock .patch (
596
- "utilities_common.cli.run_command" ,
597
- mock .MagicMock (side_effect = mock_run_command_side_effect )
598
- ) as mock_run_command :
599
- (config , show ) = get_cmd_module
600
- runner = CliRunner ()
601
- # 3 config files: 1 for host and 2 for asic
602
- cfg_files = "{},{},{}" .format (
603
- self .dummy_cfg_file ,
604
- self .dummy_cfg_file_invalid ,
605
- self .dummy_cfg_file )
606
- result = runner .invoke (
607
- config .config .commands ["reload" ],
608
- [cfg_files , '-y' , '-f' ])
609
-
610
- print (result .exit_code )
611
- print (result .output )
612
- traceback .print_tb (result .exc_info [2 ])
613
- assert result .exit_code == 1
614
-
615
- output = "\n " .join ([l .rstrip () for l in result .output .split ('\n ' )])
616
- assert RELOAD_CONFIG_DB_OUTPUT_INVALID_MSG in output
617
- assert RELOAD_CONFIG_DB_OUTPUT_INVALID_ERROR in output
618
-
619
- def test_reload_config_masic_non_exist_file (self , get_cmd_module , setup_multi_broadcom_masic ):
620
- with mock .patch (
621
- "utilities_common.cli.run_command" ,
622
- mock .MagicMock (side_effect = mock_run_command_side_effect )
623
- ) as mock_run_command :
624
- (config , show ) = get_cmd_module
625
- runner = CliRunner ()
626
- # 3 config files: 1 for host and 2 for asic
627
- cfg_files = "{},{},{}" .format (
628
- self .dummy_cfg_file ,
629
- "non_exist.json" ,
630
- self .dummy_cfg_file )
631
- result = runner .invoke (
632
- config .config .commands ["reload" ],
633
- [cfg_files , '-y' , '-f' ])
634
-
635
- print (result .exit_code )
636
- print (result .output )
637
- traceback .print_tb (result .exc_info [2 ])
638
- assert result .exit_code == 0
639
- assert "\n " .join ([l .rstrip () for l in result .output .split ('\n ' )]) \
640
- == RELOAD_MASIC_CONFIG_DB_OUTPUT_FILE_NOT_EXIST
641
-
642
552
def test_reload_yang_config (self , get_cmd_module ,
643
553
setup_single_broadcom_asic ):
644
554
with mock .patch (
@@ -658,27 +568,6 @@ def test_reload_yang_config(self, get_cmd_module,
658
568
assert "\n " .join ([l .rstrip () for l in result .output .split ('\n ' )]) \
659
569
== RELOAD_YANG_CFG_OUTPUT
660
570
661
- def test_reload_yang_config_invalid (self , get_cmd_module ,
662
- setup_single_broadcom_asic ):
663
- with mock .patch (
664
- "utilities_common.cli.run_command" ,
665
- mock .MagicMock (side_effect = mock_run_command_side_effect )
666
- ) as mock_run_command :
667
- (config , show ) = get_cmd_module
668
- runner = CliRunner ()
669
-
670
- result = runner .invoke (config .config .commands ["reload" ],
671
- [self .dummy_cfg_file_invalid , '-y' , '-f' , '-t' , 'config_yang' ])
672
-
673
- print (result .exit_code )
674
- print (result .output )
675
- traceback .print_tb (result .exc_info [2 ])
676
- assert result .exit_code == 1
677
-
678
- output = "\n " .join ([l .rstrip () for l in result .output .split ('\n ' )])
679
- assert RELOAD_CONFIG_DB_OUTPUT_INVALID_MSG in output
680
- assert RELOAD_CONFIG_DB_OUTPUT_INVALID_ERROR in output
681
-
682
571
@classmethod
683
572
def teardown_class (cls ):
684
573
os .environ ['UTILITIES_UNIT_TESTING' ] = "0"
0 commit comments