2
2
import sys
3
3
import click
4
4
import pytest
5
+ import importlib
5
6
import subprocess
6
7
import show .main as show
8
+ import utilities_common .bgp_util as bgp_util
7
9
from unittest import mock
8
10
from click .testing import CliRunner
9
11
from utilities_common import constants
@@ -32,6 +34,12 @@ class TestShowRunAllCommands(object):
32
34
def setup_class (cls ):
33
35
print ("SETUP" )
34
36
os .environ ["UTILITIES_UNIT_TESTING" ] = "1"
37
+ cls ._old_run_bgp_command = bgp_util .run_bgp_command
38
+ bgp_util .run_bgp_command = mock .MagicMock (
39
+ return_value = cls .mock_run_bgp_command ())
40
+
41
+ def mock_run_bgp_command ():
42
+ return ""
35
43
36
44
def test_show_runningconfiguration_all_json_loads_failure (self ):
37
45
def get_cmd_output_side_effect (* args , ** kwargs ):
@@ -55,16 +63,62 @@ def get_cmd_output_side_effect(*args, **kwargs):
55
63
with mock .patch ('show.main.get_cmd_output' ,
56
64
mock .MagicMock (side_effect = get_cmd_output_side_effect )) as mock_get_cmd_output :
57
65
result = CliRunner ().invoke (show .cli .commands ['runningconfiguration' ].commands ['all' ], [])
58
- assert mock_get_cmd_output .call_count == 2
66
+ assert result .exit_code == 0
67
+ assert mock_get_cmd_output .call_count == 1
68
+ assert mock_get_cmd_output .call_args_list == [
69
+ call (['sonic-cfggen' , '-d' , '--print-data' ])]
70
+
71
+ @classmethod
72
+ def teardown_class (cls ):
73
+ print ("TEARDOWN" )
74
+ bgp_util .run_bgp_command = cls ._old_run_bgp_command
75
+ os .environ ["PATH" ] = os .pathsep .join (os .environ ["PATH" ].split (os .pathsep )[:- 1 ])
76
+ os .environ ["UTILITIES_UNIT_TESTING" ] = "0"
77
+
78
+
79
+ class TestShowRunAllCommandsMasic (object ):
80
+ @classmethod
81
+ def setup_class (cls ):
82
+ print ("SETUP" )
83
+ os .environ ['UTILITIES_UNIT_TESTING' ] = "2"
84
+ os .environ ["UTILITIES_UNIT_TESTING_TOPOLOGY" ] = "multi_asic"
85
+ cls ._old_run_bgp_command = bgp_util .run_bgp_command
86
+ bgp_util .run_bgp_command = mock .MagicMock (
87
+ return_value = cls .mock_run_bgp_command ())
88
+ # change to multi asic config
89
+ from .mock_tables import dbconnector
90
+ from .mock_tables import mock_multi_asic
91
+ importlib .reload (mock_multi_asic )
92
+ dbconnector .load_namespace_config ()
93
+
94
+ def mock_run_bgp_command ():
95
+ return ""
96
+
97
+ def test_show_runningconfiguration_all_masic (self ):
98
+ def get_cmd_output_side_effect (* args , ** kwargs ):
99
+ return "{}" , 0
100
+ with mock .patch ('show.main.get_cmd_output' ,
101
+ mock .MagicMock (side_effect = get_cmd_output_side_effect )) as mock_get_cmd_output :
102
+ result = CliRunner ().invoke (show .cli .commands ['runningconfiguration' ].commands ['all' ], [])
103
+ assert result .exit_code == 0
104
+ assert mock_get_cmd_output .call_count == 3
59
105
assert mock_get_cmd_output .call_args_list == [
60
106
call (['sonic-cfggen' , '-d' , '--print-data' ]),
61
- call (['rvtysh' , '-c' , 'show running-config' ])]
107
+ call (['sonic-cfggen' , '-d' , '--print-data' , '-n' , 'asic0' ]),
108
+ call (['sonic-cfggen' , '-d' , '--print-data' , '-n' , 'asic1' ])]
62
109
63
110
@classmethod
64
111
def teardown_class (cls ):
65
112
print ("TEARDOWN" )
113
+ bgp_util .run_bgp_command = cls ._old_run_bgp_command
66
114
os .environ ["PATH" ] = os .pathsep .join (os .environ ["PATH" ].split (os .pathsep )[:- 1 ])
67
115
os .environ ["UTILITIES_UNIT_TESTING" ] = "0"
116
+ # change back to single asic config
117
+ from .mock_tables import dbconnector
118
+ from .mock_tables import mock_single_asic
119
+ importlib .reload (mock_single_asic )
120
+ dbconnector .load_namespace_config ()
121
+
68
122
69
123
@patch ('show.main.run_command' )
70
124
@pytest .mark .parametrize (
0 commit comments