Skip to content

Commit

Permalink
Add CLI for bmp configdb entity Enable/Disable (#3286)
Browse files Browse the repository at this point in the history
* Add CLI for bmp configdb entity Enable/Disable.

* Add CLI for bmp configdb entity Enable/Disable.

* Use pytest.mark.parametrize to reduce duplicated code

* Use pytest.mark.parametrize to reduce duplicated code
  • Loading branch information
FengPan-Frank authored Oct 30, 2024
1 parent 5b37ee6 commit 0ae2ec1
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 0 deletions.
99 changes: 99 additions & 0 deletions config/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4248,6 +4248,105 @@ def del_user(db, user):
click.echo("Restart service snmp failed with error {}".format(e))
raise click.Abort()


#
# 'bmp' group ('config bmp ...')
#
@config.group()
@clicommon.pass_db
def bmp(db):
"""BMP-related configuration"""
pass


#
# common function to update bmp config table
#
@clicommon.pass_db
def update_bmp_table(db, table_name, value):
log.log_info(f"'bmp {value} {table_name}' executing...")
bmp_table = db.cfgdb.get_table('BMP')
if not bmp_table:
bmp_table = {'table': {table_name: value}}
else:
bmp_table['table'][table_name] = value
db.cfgdb.mod_entry('BMP', 'table', bmp_table['table'])


#
# 'enable' subgroup ('config bmp enable ...')
#
@bmp.group()
@clicommon.pass_db
def enable(db):
"""Enable BMP table dump """
pass


#
# 'bgp-neighbor-table' command ('config bmp enable bgp-neighbor-table')
#
@enable.command('bgp-neighbor-table')
@clicommon.pass_db
def enable_bgp_neighbor_table(db):
update_bmp_table('bgp_neighbor_table', 'true')


#
# 'bgp-rib-out-table' command ('config bmp enable bgp-rib-out-table')
#
@enable.command('bgp-rib-out-table')
@clicommon.pass_db
def enable_bgp_rib_out_table(db):
update_bmp_table('bgp_rib_out_table', 'true')


#
# 'bgp-rib-in-table' command ('config bmp enable bgp-rib-in-table')
#
@enable.command('bgp-rib-in-table')
@clicommon.pass_db
def enable_bgp_rib_in_table(db):
update_bmp_table('bgp_rib_in_table', 'true')


#
# 'disable' subgroup ('config bmp disable ...')
#
@bmp.group()
@clicommon.pass_db
def disable(db):
"""Disable BMP table dump """
pass


#
# 'bgp-neighbor-table' command ('config bmp disable bgp-neighbor-table')
#
@disable.command('bgp-neighbor-table')
@clicommon.pass_db
def disable_bgp_neighbor_table(db):
update_bmp_table('bgp_neighbor_table', 'false')


#
# 'bgp-rib-out-table' command ('config bmp disable bgp-rib-out-table')
#
@disable.command('bgp-rib-out-table')
@clicommon.pass_db
def diable_bgp_rib_out_table(db):
update_bmp_table('bgp_rib_out_table', 'false')


#
# 'bgp-rib-in-table' command ('config bmp disable bgp-rib-in-table')
#
@disable.command('bgp-rib-in-table')
@clicommon.pass_db
def disable_bgp_rib_in_table(db):
update_bmp_table('bgp_rib_in_table', 'false')


#
# 'bgp' group ('config bgp ...')
#
Expand Down
9 changes: 9 additions & 0 deletions tests/bmp_input/bmp.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"BMP": {
"table": {
"bgp_neighbor_table": "false",
"bgp_rib_in_table": "false",
"bgp_rib_out_table": "false"
}
}
}
6 changes: 6 additions & 0 deletions tests/bmp_input/bmp_invalid.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"BMP": {
"table": {
}
}
}
48 changes: 48 additions & 0 deletions tests/config_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@
# Config Reload input Path
mock_db_path = os.path.join(test_path, "config_reload_input")

mock_bmp_db_path = os.path.join(test_path, "bmp_input")


# Load minigraph input Path
load_minigraph_input_path = os.path.join(test_path, "load_minigraph_input")
load_minigraph_platform_path = os.path.join(load_minigraph_input_path, "platform")
Expand Down Expand Up @@ -702,6 +705,51 @@ def teardown_class(cls):
dbconnector.load_namespace_config()


class TestBMPConfig(object):
@classmethod
def setup_class(cls):
print("SETUP")
os.environ['UTILITIES_UNIT_TESTING'] = "1"
yield
print("TEARDOWN")
os.environ["UTILITIES_UNIT_TESTING"] = "0"

@pytest.mark.parametrize("table_name", [
"bgp-neighbor-table",
"bgp-rib-in-table",
"bgp-rib-out-table"
])
@pytest.mark.parametrize("enabled", ["true", "false"])
@pytest.mark.parametrize("filename", ["bmp_invalid.json", "bmp.json"])
def test_enable_disable_table(
self,
get_cmd_module,
setup_single_broadcom_asic,
table_name,
enabled,
filename):
(config, show) = get_cmd_module
jsonfile_config = os.path.join(mock_bmp_db_path, filename)
config.DEFAULT_CONFIG_DB_FILE = jsonfile_config
runner = CliRunner()
db = Db()

# Enable table
result = runner.invoke(config.config.commands["bmp"].commands["enable"],
[table_name], obj=db)
assert result.exit_code == 0

# Disable table
result = runner.invoke(config.config.commands["bmp"].commands["disable"],
[table_name], obj=db)
assert result.exit_code == 0

# Enable table again
result = runner.invoke(config.config.commands["bmp"].commands["enable"],
[table_name], obj=db)
assert result.exit_code == 0


class TestConfigReloadMasic(object):
@classmethod
def setup_class(cls):
Expand Down

0 comments on commit 0ae2ec1

Please sign in to comment.