Skip to content

Commit f09da99

Browse files
authored
[show] Add bgpraw to show run all (#2537)
#### What I did Add bgpraw output to `show runningconfiguration all` ``` Requirements: 1. current `show runningconfig` will print all the ConfigDB in a json format, we need to add a new key-value into the json output "bgpraw" with a long string value 2. The long string value should be the output of `vtysh -c "show run"`. It is normally multiline string, may include special characters like \". Need to make sure the escaping properly 3. We do not need to insert the key-value into ConfigDB is not existing there 4. If ConfigDB already has the key-value, we do not need to override it by vtysh command output 5. Not break multi-asic use ``` #### How I did it Generate bgpraw output then append it to `show runnningconfiguration all`'s output #### How to verify it Mannual test #### Previous command output (if the output of a command-line utility has changed) ``` admin@vlab-01:~$ show run all { "ACL_TABLE": { ...... "WRED_PROFILE": { "AZURE_LOSSLESS": { "ecn": "ecn_all", "green_drop_probability": "5", "green_max_threshold": "2097152", "green_min_threshold": "1048576", "red_drop_probability": "5", "red_max_threshold": "2097152", "red_min_threshold": "1048576", "wred_green_enable": "true", "wred_red_enable": "true", "wred_yellow_enable": "true", "yellow_drop_probability": "5", "yellow_max_threshold": "2097152", "yellow_min_threshold": "1048576" } } } ``` #### New command output (if the output of a command-line utility has changed) ``` admin@vlab-01:~$ show run all { "ACL_TABLE": { ...... "WRED_PROFILE": { "AZURE_LOSSLESS": { "ecn": "ecn_all", "green_drop_probability": "5", "green_max_threshold": "2097152", "green_min_threshold": "1048576", "red_drop_probability": "5", "red_max_threshold": "2097152", "red_min_threshold": "1048576", "wred_green_enable": "true", "wred_red_enable": "true", "wred_yellow_enable": "true", "yellow_drop_probability": "5", "yellow_max_threshold": "2097152", "yellow_min_threshold": "1048576" } }, "bgpraw": "Building configuration...\n\nCurrent configuration......end\n" } ```
1 parent 39ac564 commit f09da99

File tree

2 files changed

+75
-2
lines changed

2 files changed

+75
-2
lines changed

show/main.py

+24-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from datetime import datetime
2020
import utilities_common.constants as constants
2121
from utilities_common.general import load_db_config
22+
from json.decoder import JSONDecodeError
2223

2324
# mock the redis for unit test purposes #
2425
try:
@@ -129,6 +130,10 @@ def run_command(command, display_cmd=False, return_cmd=False):
129130
if rc != 0:
130131
sys.exit(rc)
131132

133+
def get_cmd_output(cmd):
134+
proc = subprocess.Popen(cmd, text=True, stdout=subprocess.PIPE)
135+
return proc.communicate()[0], proc.returncode
136+
132137
# Lazy global class instance for SONiC interface name to alias conversion
133138
iface_alias_converter = lazy_object_proxy.Proxy(lambda: clicommon.InterfaceAliasConverter())
134139

@@ -1383,8 +1388,25 @@ def runningconfiguration():
13831388
@click.option('--verbose', is_flag=True, help="Enable verbose output")
13841389
def all(verbose):
13851390
"""Show full running configuration"""
1386-
cmd = "sonic-cfggen -d --print-data"
1387-
run_command(cmd, display_cmd=verbose)
1391+
cmd = ['sonic-cfggen', '-d', '--print-data']
1392+
stdout, rc = get_cmd_output(cmd)
1393+
if rc:
1394+
click.echo("Failed to get cmd output '{}':rc {}".format(cmd, rc))
1395+
raise click.Abort()
1396+
1397+
try:
1398+
output = json.loads(stdout)
1399+
except JSONDecodeError as e:
1400+
click.echo("Failed to load output '{}':{}".format(cmd, e))
1401+
raise click.Abort()
1402+
1403+
if not multi_asic.is_multi_asic():
1404+
bgpraw_cmd = [constants.RVTYSH_COMMAND, '-c', 'show running-config']
1405+
bgpraw, rc = get_cmd_output(bgpraw_cmd)
1406+
if rc:
1407+
bgpraw = ""
1408+
output['bgpraw'] = bgpraw
1409+
click.echo(json.dumps(output, indent=4))
13881410

13891411

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

tests/show_test.py

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import os
2+
import sys
3+
import show.main as show
4+
from click.testing import CliRunner
5+
from unittest import mock
6+
from unittest.mock import call, MagicMock
7+
8+
test_path = os.path.dirname(os.path.abspath(__file__))
9+
modules_path = os.path.dirname(test_path)
10+
sys.path.insert(0, test_path)
11+
sys.path.insert(0, modules_path)
12+
13+
14+
class TestShowRunAllCommands(object):
15+
@classmethod
16+
def setup_class(cls):
17+
print("SETUP")
18+
os.environ["UTILITIES_UNIT_TESTING"] = "1"
19+
20+
def test_show_runningconfiguration_all_json_loads_failure(self):
21+
def get_cmd_output_side_effect(*args, **kwargs):
22+
return "", 0
23+
with mock.patch('show.main.get_cmd_output',
24+
mock.MagicMock(side_effect=get_cmd_output_side_effect)) as mock_get_cmd_output:
25+
result = CliRunner().invoke(show.cli.commands['runningconfiguration'].commands['all'], [])
26+
assert result.exit_code != 0
27+
28+
def test_show_runningconfiguration_all_get_cmd_ouput_failure(self):
29+
def get_cmd_output_side_effect(*args, **kwargs):
30+
return "{}", 2
31+
with mock.patch('show.main.get_cmd_output',
32+
mock.MagicMock(side_effect=get_cmd_output_side_effect)) as mock_get_cmd_output:
33+
result = CliRunner().invoke(show.cli.commands['runningconfiguration'].commands['all'], [])
34+
assert result.exit_code != 0
35+
36+
def test_show_runningconfiguration_all(self):
37+
def get_cmd_output_side_effect(*args, **kwargs):
38+
return "{}", 0
39+
with mock.patch('show.main.get_cmd_output',
40+
mock.MagicMock(side_effect=get_cmd_output_side_effect)) as mock_get_cmd_output:
41+
result = CliRunner().invoke(show.cli.commands['runningconfiguration'].commands['all'], [])
42+
assert mock_get_cmd_output.call_count == 2
43+
assert mock_get_cmd_output.call_args_list == [
44+
call(['sonic-cfggen', '-d', '--print-data']),
45+
call(['rvtysh', '-c', 'show running-config'])]
46+
47+
@classmethod
48+
def teardown_class(cls):
49+
print("TEARDOWN")
50+
os.environ["PATH"] = os.pathsep.join(os.environ["PATH"].split(os.pathsep)[:-1])
51+
os.environ["UTILITIES_UNIT_TESTING"] = "0"

0 commit comments

Comments
 (0)