Skip to content

Commit 5580035

Browse files
wen587mssonicbld
authored andcommitted
[show] Update show run all to cover all asic config in multiasic (#3148)
* [show] Update show run all to cover all asic config in masic * per comment
1 parent 45c4409 commit 5580035

File tree

2 files changed

+90
-19
lines changed

2 files changed

+90
-19
lines changed

show/main.py

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,24 @@ def get_cmd_output(cmd):
140140
proc = subprocess.Popen(cmd, text=True, stdout=subprocess.PIPE)
141141
return proc.communicate()[0], proc.returncode
142142

143+
def get_config_json_by_namespace(namespace):
144+
cmd = ['sonic-cfggen', '-d', '--print-data']
145+
if namespace is not None and namespace != multi_asic.DEFAULT_NAMESPACE:
146+
cmd += ['-n', namespace]
147+
148+
stdout, rc = get_cmd_output(cmd)
149+
if rc:
150+
click.echo("Failed to get cmd output '{}':rc {}".format(cmd, rc))
151+
raise click.Abort()
152+
153+
try:
154+
config_json = json.loads(stdout)
155+
except JSONDecodeError as e:
156+
click.echo("Failed to load output '{}':{}".format(cmd, e))
157+
raise click.Abort()
158+
159+
return config_json
160+
143161
# Lazy global class instance for SONiC interface name to alias conversion
144162
iface_alias_converter = lazy_object_proxy.Proxy(lambda: clicommon.InterfaceAliasConverter())
145163

@@ -1403,25 +1421,24 @@ def runningconfiguration():
14031421
@click.option('--verbose', is_flag=True, help="Enable verbose output")
14041422
def all(verbose):
14051423
"""Show full running configuration"""
1406-
cmd = ['sonic-cfggen', '-d', '--print-data']
1407-
stdout, rc = get_cmd_output(cmd)
1408-
if rc:
1409-
click.echo("Failed to get cmd output '{}':rc {}".format(cmd, rc))
1410-
raise click.Abort()
1424+
output = {}
1425+
bgpraw_cmd = "show running-config"
14111426

1412-
try:
1413-
output = json.loads(stdout)
1414-
except JSONDecodeError as e:
1415-
click.echo("Failed to load output '{}':{}".format(cmd, e))
1416-
raise click.Abort()
1427+
import utilities_common.bgp_util as bgp_util
1428+
# In multiaisc, the namespace is changed to 'localhost' by design
1429+
host_config = get_config_json_by_namespace(multi_asic.DEFAULT_NAMESPACE)
1430+
output['localhost'] = host_config
14171431

1418-
if not multi_asic.is_multi_asic():
1419-
bgpraw_cmd = [constants.RVTYSH_COMMAND, '-c', 'show running-config']
1420-
bgpraw, rc = get_cmd_output(bgpraw_cmd)
1421-
if rc:
1422-
bgpraw = ""
1423-
output['bgpraw'] = bgpraw
1424-
click.echo(json.dumps(output, indent=4))
1432+
if multi_asic.is_multi_asic():
1433+
ns_list = multi_asic.get_namespace_list()
1434+
for ns in ns_list:
1435+
ns_config = get_config_json_by_namespace(ns)
1436+
ns_config['bgpraw'] = bgp_util.run_bgp_show_command(bgpraw_cmd, ns)
1437+
output[ns] = ns_config
1438+
click.echo(json.dumps(output, indent=4))
1439+
else:
1440+
host_config['bgpraw'] = bgp_util.run_bgp_show_command(bgpraw_cmd)
1441+
click.echo(json.dumps(output['localhost'], indent=4))
14251442

14261443

14271444
# 'acl' subcommand ("show runningconfiguration acl")

tests/show_test.py

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
import sys
33
import click
44
import pytest
5+
import importlib
56
import subprocess
67
import show.main as show
8+
import utilities_common.bgp_util as bgp_util
79
from unittest import mock
810
from click.testing import CliRunner
911
from utilities_common import constants
@@ -32,6 +34,12 @@ class TestShowRunAllCommands(object):
3234
def setup_class(cls):
3335
print("SETUP")
3436
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 ""
3543

3644
def test_show_runningconfiguration_all_json_loads_failure(self):
3745
def get_cmd_output_side_effect(*args, **kwargs):
@@ -55,16 +63,62 @@ def get_cmd_output_side_effect(*args, **kwargs):
5563
with mock.patch('show.main.get_cmd_output',
5664
mock.MagicMock(side_effect=get_cmd_output_side_effect)) as mock_get_cmd_output:
5765
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
59105
assert mock_get_cmd_output.call_args_list == [
60106
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'])]
62109

63110
@classmethod
64111
def teardown_class(cls):
65112
print("TEARDOWN")
113+
bgp_util.run_bgp_command = cls._old_run_bgp_command
66114
os.environ["PATH"] = os.pathsep.join(os.environ["PATH"].split(os.pathsep)[:-1])
67115
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+
68122

69123
@patch('show.main.run_command')
70124
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)