Skip to content

Commit

Permalink
Merge branch 'master' into tk_tof3_prof_nam_bf
Browse files Browse the repository at this point in the history
  • Loading branch information
Taras Keryk committed Jan 17, 2023
2 parents 9895ee8 + c2d746d commit 48055b0
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 9 deletions.
7 changes: 6 additions & 1 deletion scripts/db_migrator.py
Original file line number Diff line number Diff line change
Expand Up @@ -884,7 +884,12 @@ def common_migration_ops(self):
new_cfg = {**init_cfg, **curr_cfg}
self.configDB.set_entry(init_cfg_table, key, new_cfg)

self.migrate_copp_table()
# Avoiding copp table migration is platform specific at the moment as I understood this might cause issues for some
# vendors, probably Broadcom. This change can be checked with any specific vendor and if this works fine the platform
# condition can be modified and extend. If no vendor has an issue with not clearing copp tables the condition can be
# removed together with calling to migrate_copp_table function.
if self.asic_type != "mellanox":
self.migrate_copp_table()
if self.asic_type == "broadcom" and 'Force10-S6100' in self.hwsku:
self.migrate_mgmt_ports_on_s6100()
else:
Expand Down
4 changes: 1 addition & 3 deletions scripts/fast-reboot
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ case "$REBOOT_TYPE" in
check_warm_restart_in_progress
BOOT_TYPE_ARG=$REBOOT_TYPE
trap clear_boot EXIT HUP INT QUIT TERM KILL ABRT ALRM
sonic-db-cli STATE_DB SET "FAST_REBOOT|system" "1" "EX" "180" &>/dev/null
sonic-db-cli STATE_DB SET "FAST_REBOOT|system" "1" "EX" "210" &>/dev/null
config warm_restart enable system
;;
"warm-reboot")
Expand Down Expand Up @@ -727,8 +727,6 @@ for service in ${SERVICES_TO_STOP}; do
sonic-db-cli FLEX_COUNTER_DB FLUSHDB > /dev/null
fi
# TODO: backup_database preserves FDB_TABLE
# need to cleanup as well for fastfast boot case
backup_database
fi
Expand Down
26 changes: 24 additions & 2 deletions show/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from datetime import datetime
import utilities_common.constants as constants
from utilities_common.general import load_db_config
from json.decoder import JSONDecodeError

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

def get_cmd_output(cmd):
proc = subprocess.Popen(cmd, text=True, stdout=subprocess.PIPE)
return proc.communicate()[0], proc.returncode

# 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 @@ -1383,8 +1388,25 @@ 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"
run_command(cmd, display_cmd=verbose)
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 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))


# 'acl' subcommand ("show runningconfiguration acl")
Expand Down
51 changes: 48 additions & 3 deletions tests/barefoot_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,19 @@ def test_show_profile(self):

def test_get_chip_family1(self):
with patch('show.plugins.barefoot.device_info.get_path_to_hwsku_dir', return_value=""):
chip_family = json.dumps({"chip_list": [{"instance": 0,"chip_family": "tofino3"}]})
chip_family = json.dumps({"chip_list": [{"instance": 0,"chip_family": "tofino"}]})
with patch('show.plugins.barefoot.open', mock_open(read_data=chip_family)):
result = bfshow.get_chip_family()
assert result == "tofino3"
assert result == "tofino"

def test_get_chip_family2(self):
with patch('config.plugins.barefoot.device_info.get_path_to_hwsku_dir', return_value=""):
chip_family = json.dumps({"chip_list": [{"instance": 0,"chip_family": "tofino2"}]})
with patch('show.plugins.barefoot.open', mock_open(read_data=chip_family)):
result = bfconfig.get_chip_family()
assert result == "tofino2"

def test_get_chip_family3(self):
with patch('config.plugins.barefoot.device_info.get_path_to_hwsku_dir', return_value=""):
chip_family = json.dumps({"chip_list": [{"instance": 0,"chip_family": "tofino3"}]})
with patch('show.plugins.barefoot.open', mock_open(read_data=chip_family)):
Expand Down Expand Up @@ -149,7 +156,7 @@ def test_get_available_profiles(self):
def test_show_profile(self):
runner = CliRunner()
expected_output = """\
Current profile: y2
Current profile: x2
Available profile(s):
x1
x2
Expand All @@ -158,9 +165,47 @@ def test_show_profile(self):
"""
with patch("show.plugins.barefoot.check_profile", return_value=False):
with patch("show.plugins.barefoot.get_chip_family", return_value="tofino"):
with patch("show.plugins.barefoot.get_current_profile", return_value="x2\n"):
with patch("show.plugins.barefoot.get_available_profiles", return_value="x1\nx2\ny2\ny3\n"):
result = runner.invoke(bfshow.barefoot.commands['profile'], [])
print("result.exit_code:", result.exit_code)
print("result.output:", result.output)
assert result.output == expected_output

def test_show_profile2(self):
runner = CliRunner()
expected_output = """\
Current profile: y2
Available profile(s):
x1
x2
y2
y3
"""
with patch("show.plugins.barefoot.check_profile", return_value=False):
with patch("show.plugins.barefoot.get_chip_family", return_value="tofino2"):
with patch("show.plugins.barefoot.get_current_profile", return_value="y2\n"):
with patch("show.plugins.barefoot.get_available_profiles", return_value="x1\nx2\ny2\ny3\n"):
result = runner.invoke(bfshow.barefoot.commands['profile'], [])
print("result.exit_code:", result.exit_code)
print("result.output:", result.output)
assert result.output == expected_output

def test_show_profile3(self):
runner = CliRunner()
expected_output = """\
Current profile: y2
Available profile(s):
x1
x2
y2
y3
"""
with patch("show.plugins.barefoot.check_profile", return_value=False):
with patch("show.plugins.barefoot.get_chip_family", return_value="tofino3"):
with patch("show.plugins.barefoot.get_current_profile", return_value="y2\n"):
with patch("show.plugins.barefoot.get_available_profiles", return_value="x1\nx2\ny2\ny3\n"):
result = runner.invoke(bfshow.barefoot.commands['profile'], [])
print("result.exit_code:", result.exit_code)
print("result.output:", result.output)
assert result.output == expected_output
51 changes: 51 additions & 0 deletions tests/show_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import os
import sys
import show.main as show
from click.testing import CliRunner
from unittest import mock
from unittest.mock import call, MagicMock

test_path = os.path.dirname(os.path.abspath(__file__))
modules_path = os.path.dirname(test_path)
sys.path.insert(0, test_path)
sys.path.insert(0, modules_path)


class TestShowRunAllCommands(object):
@classmethod
def setup_class(cls):
print("SETUP")
os.environ["UTILITIES_UNIT_TESTING"] = "1"

def test_show_runningconfiguration_all_json_loads_failure(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 result.exit_code != 0

def test_show_runningconfiguration_all_get_cmd_ouput_failure(self):
def get_cmd_output_side_effect(*args, **kwargs):
return "{}", 2
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 result.exit_code != 0

def test_show_runningconfiguration_all(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 == 2
assert mock_get_cmd_output.call_args_list == [
call(['sonic-cfggen', '-d', '--print-data']),
call(['rvtysh', '-c', 'show running-config'])]

@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"

0 comments on commit 48055b0

Please sign in to comment.