Skip to content

Commit

Permalink
[show] Update show run all to cover all asic config in masic
Browse files Browse the repository at this point in the history
  • Loading branch information
wen587 committed Feb 2, 2024
1 parent 3d45c0c commit 817b886
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 11 deletions.
40 changes: 29 additions & 11 deletions show/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,24 @@ def get_cmd_output(cmd):
proc = subprocess.Popen(cmd, text=True, stdout=subprocess.PIPE)
return proc.communicate()[0], proc.returncode

def get_config_json_by_namespace(namespace):
cmd = ['sonic-cfggen', '-d', '--print-data']
if namespace is not None and namespace != multi_asic.DEFAULT_NAMESPACE:
cmd += ['-n', namespace]

stdout, rc = get_cmd_output(cmd)
if rc:
click.echo("Failed to get cmd output '{}':rc {}".format(cmd, rc))
raise click.Abort()

try:
config_json = json.loads(stdout)
except JSONDecodeError as e:
click.echo("Failed to load output '{}':{}".format(cmd, e))
raise click.Abort()

return config_json

# Lazy global class instance for SONiC interface name to alias conversion
iface_alias_converter = lazy_object_proxy.Proxy(lambda: clicommon.InterfaceAliasConverter())

Expand Down Expand Up @@ -1407,24 +1425,24 @@ def runningconfiguration():
@click.option('--verbose', is_flag=True, help="Enable verbose output")
def all(verbose):
"""Show full running configuration"""
cmd = ['sonic-cfggen', '-d', '--print-data']
stdout, rc = get_cmd_output(cmd)
if rc:
click.echo("Failed to get cmd output '{}':rc {}".format(cmd, rc))
raise click.Abort()

try:
output = json.loads(stdout)
except JSONDecodeError as e:
click.echo("Failed to load output '{}':{}".format(cmd, e))
raise click.Abort()
if multi_asic.is_multi_asic():
output = {}
# In multiaisc, the namespace is changed to localhost by design
output['localhost'] = get_config_json_by_namespace(multi_asic.DEFAULT_NAMESPACE)

ns_list = multi_asic.get_namespace_list()
for ns in ns_list:
output[ns] = get_config_json_by_namespace(ns)
else:
output = get_config_json_by_namespace(None)

if not multi_asic.is_multi_asic():
bgpraw_cmd = [constants.RVTYSH_COMMAND, '-c', 'show running-config']
bgpraw, rc = get_cmd_output(bgpraw_cmd)
if rc:
bgpraw = ""
output['bgpraw'] = bgpraw

click.echo(json.dumps(output, indent=4))


Expand Down
38 changes: 38 additions & 0 deletions tests/show_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import sys
import click
import pytest
import importlib
import subprocess
import show.main as show
from unittest import mock
Expand Down Expand Up @@ -66,6 +67,43 @@ def teardown_class(cls):
os.environ["PATH"] = os.pathsep.join(os.environ["PATH"].split(os.pathsep)[:-1])
os.environ["UTILITIES_UNIT_TESTING"] = "0"


class TestShowRunAllCommandsMasic(object):
@classmethod
def setup_class(cls):
print("SETUP")
os.environ['UTILITIES_UNIT_TESTING'] = "2"
os.environ["UTILITIES_UNIT_TESTING_TOPOLOGY"] = "multi_asic"
# change to multi asic config
from .mock_tables import dbconnector
from .mock_tables import mock_multi_asic
importlib.reload(mock_multi_asic)
dbconnector.load_namespace_config()

def test_show_runningconfiguration_all_masic(self):
def get_cmd_output_side_effect(*args, **kwargs):
return "{}", 0
with mock.patch('show.main.get_cmd_output',
mock.MagicMock(side_effect=get_cmd_output_side_effect)) as mock_get_cmd_output:
result = CliRunner().invoke(show.cli.commands['runningconfiguration'].commands['all'], [])
assert mock_get_cmd_output.call_count == 3
assert mock_get_cmd_output.call_args_list == [
call(['sonic-cfggen', '-d', '--print-data']),
call(['sonic-cfggen', '-d', '--print-data', '-n', 'asic0']),
call(['sonic-cfggen', '-d', '--print-data', '-n', 'asic1'])]

@classmethod
def teardown_class(cls):
print("TEARDOWN")
os.environ["PATH"] = os.pathsep.join(os.environ["PATH"].split(os.pathsep)[:-1])
os.environ["UTILITIES_UNIT_TESTING"] = "0"
# change back to single asic config
from .mock_tables import dbconnector
from .mock_tables import mock_single_asic
importlib.reload(mock_single_asic)
dbconnector.load_namespace_config()


@patch('show.main.run_command')
@pytest.mark.parametrize(
"cli_arguments0,expected0",
Expand Down

0 comments on commit 817b886

Please sign in to comment.