From 3779113ed34bc0e00676af26ce3266260efae5f7 Mon Sep 17 00:00:00 2001 From: Feng Pan Date: Thu, 16 May 2024 11:18:22 +0000 Subject: [PATCH] Not4Review --- config/main.py | 150 ++++++++++++++++++++++++++++ tests/bmp_input/bmp.json | 9 ++ tests/bmp_input/bmp_invalid.json | 6 ++ tests/config_test.py | 161 +++++++++++++++++++++++++++++++ 4 files changed, 326 insertions(+) create mode 100644 tests/bmp_input/bmp.json create mode 100644 tests/bmp_input/bmp_invalid.json diff --git a/config/main.py b/config/main.py index 41a9f48121..17a664adc8 100644 --- a/config/main.py +++ b/config/main.py @@ -3975,6 +3975,156 @@ def del_user(db, user): click.echo("Restart service snmp failed with error {}".format(e)) raise click.Abort() + +# +# 'bmp' group ('config bmp ...') +# +@config.group() +@click.pass_context +def bmp(ctx): + """BMP-related configuration""" + + +# +# 'enable' subgroup ('config bmp enable ...') +# +@bmp.group() +@click.pass_context +def enable(ctx): + """Enable BMP table dump """ + + +# +# 'bgp-neighbor-table' command ('config bmp enable bgp-neighbor-table') +# +@enable.command('bgp-neighbor-table') +@click.pass_context +def enable_bgp_neighbor_table(ctx): + """enable bgp-neighbor-table sessions + In the case of Multi-Asic platform, corresponding database instance of neighbor will be operated. + """ + log.log_info("'bmp enable bgp-neighbor-table' executing...") + config_db = ConfigDBConnector() + config_db.connect() + bmp_table = config_db.get_table('BMP') + if not bmp_table: + bmp_table = {'table': {'bgp_neighbor_table': 'true'}} + else: + bmp_table['table']['bgp_neighbor_table'] = 'true' + config_db.mod_entry('BMP', 'table', bmp_table['table']) + + +# +# 'bgp-rib-out-table' command ('config bmp enable bgp-rib-out-table') +# +@enable.command('bgp-rib-out-table') +@click.pass_context +def enable_bgp_rib_out_table(ctx): + """enable bgp-rib-out-table sessions + In the case of Multi-Asic platform, corresponding database instance of neighbor will be operated. + """ + log.log_info("'bmp enable bgp-rib-out-table' executing...") + config_db = ConfigDBConnector() + config_db.connect() + bmp_table = config_db.get_table('BMP') + if not bmp_table: + bmp_table = {'table': {'bgp_rib_out_table': 'true'}} + else: + bmp_table['table']['bgp_rib_out_table'] = 'true' + + config_db.mod_entry('BMP', 'table', bmp_table['table']) + + +# +# 'bgp-rib-in-table' command ('config bmp enable bgp-rib-in-table') +# +@enable.command('bgp-rib-in-table') +@click.pass_context +def enable_bgp_rib_in_table(ctx): + """enable bgp-rib-in-table sessions + In the case of Multi-Asic platform, corresponding database instance of neighbor will be operated. + """ + log.log_info("'bmp enable bgp-rib-in-table' executing...") + config_db = ConfigDBConnector() + config_db.connect() + bmp_table = config_db.get_table('BMP') + if not bmp_table: + bmp_table = {'table': {'bgp_rib_in_table': 'true'}} + else: + bmp_table['table']['bgp_rib_in_table'] = 'true' + + config_db.mod_entry('BMP', 'table', bmp_table['table']) + + +# +# 'disable' subgroup ('config bmp disable ...') +# +@bmp.group() +@click.pass_context +def disable(ctx): + """Disable BMP table dump """ + + +# +# 'bgp-neighbor-table' command ('config bmp disable bgp-neighbor-table') +# +@disable.command('bgp-neighbor-table') +@click.pass_context +def disable_bgp_neighbor_table(ctx): + """disable bgp-neighbor-table sessions + In the case of Multi-Asic platform, corresponding database instance of neighbor will be operated. + """ + log.log_info("'bmp disable bgp-neighbor-table' executing...") + config_db = ConfigDBConnector() + config_db.connect() + bmp_table = config_db.get_table('BMP') + if not bmp_table: + bmp_table = {'table': {'bgp_neighbor_table': 'false'}} + else: + bmp_table['table']['bgp_neighbor_table'] = 'false' + config_db.mod_entry('BMP', 'table', bmp_table['table']) + + +# +# 'bgp-rib-out-table' command ('config bmp disable bgp-rib-out-table') +# +@disable.command('bgp-rib-out-table') +@click.pass_context +def diable_bgp_rib_out_table(ctx): + """disable bgp-rib-out-table sessions + In the case of Multi-Asic platform, corresponding database instance of neighbor will be operated. + """ + log.log_info("'bmp disable bgp-rib-out-table' executing...") + config_db = ConfigDBConnector() + config_db.connect() + bmp_table = config_db.get_table('BMP') + if not bmp_table: + bmp_table = {'table': {'bgp_rib_out_table': 'false'}} + else: + bmp_table['table']['bgp_rib_out_table'] = 'false' + config_db.mod_entry('BMP', 'table', bmp_table['table']) + + +# +# 'bgp-rib-in-table' command ('config bmp disable bgp-rib-in-table') +# +@disable.command('bgp-rib-in-table') +@click.pass_context +def disable_bgp_rib_in_table(ctx): + """disable bgp-rib-in-table sessions + In the case of Multi-Asic platform, corresponding database instance of neighbor will be operated. + """ + log.log_info("'bmp disable bgp-rib-in-table' executing...") + config_db = ConfigDBConnector() + config_db.connect() + bmp_table = config_db.get_table('BMP') + if not bmp_table: + bmp_table = {'table': {'bgp_rib_in_table': 'false'}} + else: + bmp_table['table']['bgp_rib_in_table'] = 'false' + config_db.mod_entry('BMP', 'table', bmp_table['table']) + + # # 'bgp' group ('config bgp ...') # diff --git a/tests/bmp_input/bmp.json b/tests/bmp_input/bmp.json new file mode 100644 index 0000000000..6f3583f549 --- /dev/null +++ b/tests/bmp_input/bmp.json @@ -0,0 +1,9 @@ +{ + "BMP": { + "table": { + "bgp_neighbor_table": "false", + "bgp_rib_in_table": "false", + "bgp_rib_out_table": "false" + } + } +} diff --git a/tests/bmp_input/bmp_invalid.json b/tests/bmp_input/bmp_invalid.json new file mode 100644 index 0000000000..87a4f937da --- /dev/null +++ b/tests/bmp_input/bmp_invalid.json @@ -0,0 +1,6 @@ +{ + "BMP": { + "table": { + } + } +} diff --git a/tests/config_test.py b/tests/config_test.py index 1054a52a33..e3e0dfb436 100644 --- a/tests/config_test.py +++ b/tests/config_test.py @@ -37,6 +37,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") @@ -392,6 +395,164 @@ def teardown_class(cls): dbconnector.load_namespace_config() +class TestBMPConfig(object): + @classmethod + def setup_class(cls): + print("SETUP") + os.environ['UTILITIES_UNIT_TESTING'] = "1" + import config.main as config + reload(config) + yield + print("TEARDOWN") + os.environ["UTILITIES_UNIT_TESTING"] = "0" + from .mock_tables import dbconnector + dbconnector.dedicated_dbs = {} + + def test_enable_bgp_neighbor_table( + self, + get_cmd_module, + setup_single_broadcom_asic): + (config, show) = get_cmd_module + jsonfile_config = os.path.join(mock_bmp_db_path, "bmp_invalid.json") + from .mock_tables import dbconnector + dbconnector.dedicated_dbs['CONFIG_DB'] = jsonfile_config + + runner = CliRunner() + db = Db() + obj = {'config_db': db.cfgdb} + + # bmp enable bgp-neighbor-table + with mock.patch('utilities_common.cli.run_command'): + result = runner.invoke(config.config.commands["bmp"], + ["enable", "bgp-neighbor-table"], obj=obj) + print(result.exit_code) + print(result.output) + assert result.exit_code == 0 + + def test_enable_disable_bgp_neighbor_table( + self, + get_cmd_module, + setup_single_broadcom_asic): + (config, show) = get_cmd_module + jsonfile_config = os.path.join(mock_bmp_db_path, "bmp.json") + from .mock_tables import dbconnector + dbconnector.dedicated_dbs['CONFIG_DB'] = jsonfile_config + + runner = CliRunner() + db = Db() + obj = {'config_db': db.cfgdb} + + # bmp enable bgp-neighbor-table first + with mock.patch('utilities_common.cli.run_command'): + result = runner.invoke(config.config.commands["bmp"], + ["enable", "bgp-neighbor-table"], obj=obj) + assert result.exit_code == 0 + + # bmp disable bgp-neighbor-table + with mock.patch('utilities_common.cli.run_command'): + result = runner.invoke(config.config.commands["bmp"], + ["disable", "bgp-neighbor-table"], obj=obj) + print(result.exit_code) + print(result.output) + assert result.exit_code == 0 + + def test_enable_bgp_rib_in_table( + self, + get_cmd_module, + setup_single_broadcom_asic): + (config, show) = get_cmd_module + jsonfile_config = os.path.join(mock_bmp_db_path, "bmp_invalid.json") + from .mock_tables import dbconnector + dbconnector.dedicated_dbs['CONFIG_DB'] = jsonfile_config + + runner = CliRunner() + db = Db() + obj = {'config_db': db.cfgdb} + + # bmp enable bgp-rib-in-table + with mock.patch('utilities_common.cli.run_command'): + result = runner.invoke(config.config.commands["bmp"], + ["enable", "bgp-rib-in-table"], obj=obj) + print(result.exit_code) + print(result.output) + assert result.exit_code == 0 + + def test_enable_disable_bgp_rib_in_table( + self, + get_cmd_module, + setup_single_broadcom_asic): + (config, show) = get_cmd_module + jsonfile_config = os.path.join(mock_bmp_db_path, "bmp.json") + from .mock_tables import dbconnector + dbconnector.dedicated_dbs['CONFIG_DB'] = jsonfile_config + + runner = CliRunner() + db = Db() + obj = {'config_db': db.cfgdb} + + # bmp enable bgp-rib-in-table first + with mock.patch('utilities_common.cli.run_command'): + result = runner.invoke(config.config.commands["bmp"], + ["enable", "bgp-rib-in-table"], obj=obj) + assert result.exit_code == 0 + + # bmp disable bgp-rib-in-table + with mock.patch('utilities_common.cli.run_command'): + result = runner.invoke(config.config.commands["bmp"], + ["disable", "bgp-rib-in-table"], obj=obj) + print(result.exit_code) + print(result.output) + assert result.exit_code == 0 + + def test_enable_bgp_rib_out_table( + self, + get_cmd_module, + setup_single_broadcom_asic): + (config, show) = get_cmd_module + jsonfile_config = os.path.join(mock_bmp_db_path, "bmp_invalid.json") + from .mock_tables import dbconnector + dbconnector.dedicated_dbs['CONFIG_DB'] = jsonfile_config + + runner = CliRunner() + db = Db() + obj = {'config_db': db.cfgdb} + + # bmp enable bgp-rib-out-table + with mock.patch('utilities_common.cli.run_command'): + result = runner.invoke(config.config.commands["bmp"], + ["enable", "bgp-rib-out-table"], obj=obj) + print(result.exit_code) + print(result.output) + assert result.exit_code == 0 + + def test_enable_disable_bgp_rib_out_table( + self, + get_cmd_module, + setup_single_broadcom_asic): + (config, show) = get_cmd_module + jsonfile_config = os.path.join(mock_bmp_db_path, "bmp.json") + from .mock_tables import dbconnector + dbconnector.dedicated_dbs['CONFIG_DB'] = jsonfile_config + + runner = CliRunner() + db = Db() + obj = {'config_db': db.cfgdb} + + # bmp enable bgp-rib-out-table first + with mock.patch('utilities_common.cli.run_command'): + result = runner.invoke(config.config.commands["bmp"], + ["enable", "bgp-rib-out-table"], obj=obj) + assert result.exit_code == 0 + + # bmp disable bgp-rib-out-table + with mock.patch('utilities_common.cli.run_command'): + result = runner.invoke(config.config.commands["bmp"], + ["disable", "bgp-rib-out-table"], obj=obj) + print(result.exit_code) + print(result.output) + assert result.exit_code == 0 + + class TestLoadMinigraph(object): @classmethod def setup_class(cls):