diff --git a/config/main.py b/config/main.py index f48c446adf..80bfc238de 100644 --- a/config/main.py +++ b/config/main.py @@ -3822,41 +3822,27 @@ def replace_community(db, current_community, new_community): def contact(db): pass - -def is_valid_email(email): - return bool(re.search(r"^[\w\.\+\-]+\@[\w]+\.[a-z]{2,3}$", email)) - - @contact.command('add') -@click.argument('contact', metavar='', required=True) -@click.argument('contact_email', metavar='', required=True) +@click.argument('contact', metavar='', required=True, nargs=-1) @clicommon.pass_db -def add_contact(db, contact, contact_email): - """ Add snmp contact name and email """ - snmp = db.cfgdb.get_table("SNMP") +def add_contact(db, contact): + """ Add snmp contact""" + config_db = ValidatedConfigDBConnector(db.cfgdb) + if isinstance(contact, tuple): + contact = " ".join(contact) + snmp = config_db.get_table("SNMP") try: if snmp['CONTACT']: - click.echo("Contact already exists. Use sudo config snmp contact modify instead") + click.echo("Contact already exists, Use sudo config snmp contact modify instead") sys.exit(1) - else: - db.cfgdb.set_entry('SNMP', 'CONTACT', {contact: contact_email}) # TODO: ERROR IN YANG MODEL. Contact name is not defined as key - click.echo("Contact name {} and contact email {} have been added to " - "configuration".format(contact, contact_email)) - try: - click.echo("Restarting SNMP service...") - clicommon.run_command(['systemctl', 'reset-failed', 'snmp.service'], display_cmd=False) - clicommon.run_command(['systemctl', 'restart', 'snmp.service'], display_cmd=False) - except SystemExit as e: - click.echo("Restart service snmp failed with error {}".format(e)) - raise click.Abort() except KeyError: if "CONTACT" not in snmp.keys(): - if not is_valid_email(contact_email): - click.echo("Contact email {} is not valid".format(contact_email)) - sys.exit(2) - db.cfgdb.set_entry('SNMP', 'CONTACT', {contact: contact_email}) - click.echo("Contact name {} and contact email {} have been added to " - "configuration".format(contact, contact_email)) + try: + config_db.set_entry('SNMP', 'CONTACT', {'Contact': contact}) + click.echo("SNMP Contact {} has been added to configuration".format(contact)) + except ValueError as e: + ctx = click.get_current_context() + ctx.fail("Failed to set SNMP contact. Error: {}".format(e)) try: click.echo("Restarting SNMP service...") clicommon.run_command(['systemctl', 'reset-failed', 'snmp.service'], display_cmd=False) @@ -3867,18 +3853,22 @@ def add_contact(db, contact, contact_email): @contact.command('del') -@click.argument('contact', metavar='', required=True) +@click.argument('contact', metavar='', required=True, nargs=-1) @clicommon.pass_db -def del_contact(db, contact): - """ Delete snmp contact name and email """ +def delete_contact(db, contact): + """ Delete snmp contact""" + config_db = ValidatedConfigDBConnector(db.cfgdb) + if isinstance(contact, tuple): + contact = " ".join(contact) snmp = db.cfgdb.get_table("SNMP") try: - if not contact in (list(snmp['CONTACT'].keys()))[0]: - click.echo("SNMP contact {} is not configured".format(contact)) - sys.exit(1) - else: - db.cfgdb.set_entry('SNMP', 'CONTACT', None) - click.echo("SNMP contact {} removed from configuration".format(contact)) + if contact == snmp['CONTACT']['Contact']: + try: + config_db.set_entry('SNMP', 'CONTACT', None) + click.echo("SNMP Contact {} removed from configuration".format(contact)) + except (ValueError, JsonPatchConflict) as e: + ctx = click.get_current_context() + ctx.fail("Failed to remove SNMP contact from configuration. Error: {}".format(e)) try: click.echo("Restarting SNMP service...") clicommon.run_command(['systemctl', 'reset-failed', 'snmp.service'], display_cmd=False) @@ -3886,48 +3876,36 @@ def del_contact(db, contact): except SystemExit as e: click.echo("Restart service snmp failed with error {}".format(e)) raise click.Abort() + else: + click.echo("SNMP Contact {} does not exist.".format(contact)) + sys.exit(1) except KeyError: if "CONTACT" not in snmp.keys(): - click.echo("Contact name {} is not configured".format(contact)) + click.echo("SNMP Contact {} is not configured".format(contact)) sys.exit(2) @contact.command('modify') -@click.argument('contact', metavar='', required=True) -@click.argument('contact_email', metavar='', required=True) +@click.argument('contact', metavar='', required=True, nargs=-1) @clicommon.pass_db -def modify_contact(db, contact, contact_email): +def modify_contact(db, contact): """ Modify snmp contact""" - snmp = db.cfgdb.get_table("SNMP") + config_db = ValidatedConfigDBConnector(db.cfgdb) + if isinstance(contact, tuple): + contact = " ".join(contact) + snmp = config_db.get_table("SNMP") try: - current_snmp_contact_name = (list(snmp['CONTACT'].keys()))[0] - if current_snmp_contact_name == contact: - current_snmp_contact_email = snmp['CONTACT'][contact] - else: - current_snmp_contact_email = '' - if contact == current_snmp_contact_name and contact_email == current_snmp_contact_email: - click.echo("SNMP contact {} {} already exists".format(contact, contact_email)) + snmp_contact = snmp['CONTACT']['Contact'] + if contact in snmp_contact: + click.echo("SNMP contact {} already exists".format(contact)) sys.exit(1) - elif contact == current_snmp_contact_name and contact_email != current_snmp_contact_email: - if not is_valid_email(contact_email): - click.echo("Contact email {} is not valid".format(contact_email)) - sys.exit(2) - db.cfgdb.mod_entry('SNMP', 'CONTACT', {contact: contact_email}) - click.echo("SNMP contact {} email updated to {}".format(contact, contact_email)) - try: - click.echo("Restarting SNMP service...") - clicommon.run_command(['systemctl', 'reset-failed', 'snmp.service'], display_cmd=False) - clicommon.run_command(['systemctl', 'restart', 'snmp.service'], display_cmd=False) - except SystemExit as e: - click.echo("Restart service snmp failed with error {}".format(e)) - raise click.Abort() else: - if not is_valid_email(contact_email): - click.echo("Contact email {} is not valid".format(contact_email)) - sys.exit(2) - db.cfgdb.set_entry('SNMP', 'CONTACT', None) - db.cfgdb.set_entry('SNMP', 'CONTACT', {contact: contact_email}) - click.echo("SNMP contact {} and contact email {} updated".format(contact, contact_email)) + try: + config_db.mod_entry('SNMP', 'CONTACT', {'Contact': contact}) + click.echo("SNMP contact {} modified in configuration".format(contact)) + except ValueError as e: + ctx = click.get_current_context() + ctx.fail("Failed to modify SNMP contact. Error: {}".format(e)) try: click.echo("Restarting SNMP service...") clicommon.run_command(['systemctl', 'reset-failed', 'snmp.service'], display_cmd=False) @@ -3936,9 +3914,8 @@ def modify_contact(db, contact, contact_email): click.echo("Restart service snmp failed with error {}".format(e)) raise click.Abort() except KeyError: - if "CONTACT" not in snmp.keys(): - click.echo("Contact name {} is not configured".format(contact)) - sys.exit(3) + click.echo("Cannot modify SNMP Contact. You must use 'config snmp contact add' command.'") + sys.exit(2) @snmp.group(cls=clicommon.AbbreviationGroup) diff --git a/doc/Command-Reference.md b/doc/Command-Reference.md index 689ca23b73..aa1c2b4ac9 100644 --- a/doc/Command-Reference.md +++ b/doc/Command-Reference.md @@ -9821,9 +9821,9 @@ This command displays the global SNMP configuration that includes the location, Emerald City - SNMP_CONTACT SNMP_CONTACT_EMAIL - -------------- -------------------- - joe joe@contoso.com + CONTACT + -------------- + joe joe@contoso.com Community String Community Type @@ -9876,9 +9876,9 @@ This command displays the SNMP contact setting. - Example: ``` admin@sonic:~$ show runningconfiguration snmp contact - Contact Contact Email - --------- --------------- - joe joe@contoso.com + Contact + --------- + joe joe@contoso.com ``` - Usage: @@ -9889,7 +9889,7 @@ This command displays the SNMP contact setting. - Example: ``` admin@sonic:~$ show runningconfiguration snmp contact --json - {'joe': 'joe@contoso.com'} + {'Contact': 'joe joe@contoso.com'} ``` **show runningconfiguration snmp community** @@ -9989,13 +9989,13 @@ This command is used to add, delete, or modify the SNMP contact. - Usage: ``` - config snmp contact add + config snmp contact add ``` - Example: ``` admin@sonic:~$ sudo config snmp contact add joe joe@contoso.com - Contact name joe and contact email joe@contoso.com have been added to configuration + Contact joe joe@contoso.com has been added to configuration Restarting SNMP service... ``` @@ -10006,20 +10006,20 @@ This command is used to add, delete, or modify the SNMP contact. - Example: ``` - admin@sonic:~$ sudo config snmp contact del joe - SNMP contact joe removed from configuration + admin@sonic:~$ sudo config snmp contact del joe joe@contoso.com + SNMP contact joe joe@contoso.com removed from configuration Restarting SNMP service... ``` - Usage: ``` - config snmp contact modify + config snmp contact modify ``` - Example: ``` admin@sonic:~$ sudo config snmp contact modify test test@contoso.com - SNMP contact test and contact email test@contoso.com updated + SNMP contact test test@contoso.com updated Restarting SNMP service... ``` diff --git a/scripts/db_migrator.py b/scripts/db_migrator.py index 9be3ce325b..810599fd1b 100755 --- a/scripts/db_migrator.py +++ b/scripts/db_migrator.py @@ -856,6 +856,20 @@ def migrate_aaa(self): if keys: self.configDB.delete(self.configDB.CONFIG_DB, authorization_key) + def migrate_config_db_snmp_contact(self): + """ + Migrate SNMP": { "CONTACT": { "joe": "joe@email.com"}} to + SNMP": { "CONTACT": { "Contact": "joe joe@email.com"}} + As per the changes to fix DB schema based on sonic-snmp.yang + """ + snmp_contact = self.configDB.get_entry('SNMP', 'CONTACT') + if 'Contact' not in snmp_contact: + contact_name = list(snmp_contact.keys())[0] + contact_email = snmp_contact[contact_name] + self.configDB.set_entry('SNMP', 'CONTACT', None) + snmp_contact = {'Contact': contact_name + ' ' + contact_email} + self.configDB.set_entry('SNMP', 'CONTACT', snmp_contact) + def version_unknown(self): """ version_unknown tracks all SONiC versions that doesn't have a version @@ -1231,6 +1245,10 @@ def version_202405_01(self): Version 202405_01. """ log.log_info('Handling version_202405_01') + + if self.configDB.keys(self.configDB.CONFIG_DB, "SNMP|CONTACT"): + self.migrate_config_db_snmp_contact() + self.set_version('version_202411_01') return 'version_202411_01' diff --git a/show/main.py b/show/main.py index 06114eb79f..9b48829c36 100755 --- a/show/main.py +++ b/show/main.py @@ -1589,7 +1589,7 @@ def community(db, json_output): def contact(db, json_output): """show SNMP running configuration contact""" snmp = db.cfgdb.get_table('SNMP') - snmp_header = ["Contact", "Contact Email"] + snmp_header = ["Contact"] snmp_body = [] if json_output: try: @@ -1601,9 +1601,8 @@ def contact(db, json_output): else: try: if snmp['CONTACT']: - snmp_contact = list(snmp['CONTACT'].keys()) - snmp_contact_email = [snmp['CONTACT'][snmp_contact[0]]] - snmp_body.append([snmp_contact[0], snmp_contact_email[0]]) + snmp_contact = [snmp['CONTACT']['Contact']] + snmp_body.append(snmp_contact) except KeyError: snmp['CONTACT'] = '' click.echo(tabulate(snmp_body, snmp_header)) @@ -1670,7 +1669,7 @@ def show_run_snmp(db, ctx): snmp_users = db.cfgdb.get_table('SNMP_USER') snmp_location_header = ["Location"] snmp_location_body = [] - snmp_contact_header = ["SNMP_CONTACT", "SNMP_CONTACT_EMAIL"] + snmp_contact_header = ["Contact"] snmp_contact_body = [] snmp_comm_header = ["Community String", "Community Type"] snmp_comm_body = [] @@ -1687,9 +1686,8 @@ def show_run_snmp(db, ctx): click.echo("\n") try: if snmp_contact_location_table['CONTACT']: - snmp_contact = list(snmp_contact_location_table['CONTACT'].keys()) - snmp_contact_email = [snmp_contact_location_table['CONTACT'][snmp_contact[0]]] - snmp_contact_body.append([snmp_contact[0], snmp_contact_email[0]]) + snmp_contact = [snmp_contact_location_table['CONTACT']['Contact']] + snmp_contact_body.append(snmp_contact) except KeyError: snmp_contact_location_table['CONTACT'] = '' click.echo(tabulate(snmp_contact_body, snmp_contact_header)) diff --git a/tests/config_snmp_test.py b/tests/config_snmp_test.py index 25c54d36ec..b999187e90 100644 --- a/tests/config_snmp_test.py +++ b/tests/config_snmp_test.py @@ -15,15 +15,15 @@ from utilities_common.db import Db tabular_data_show_run_snmp_contact_expected = """\ -Contact Contact Email\n--------- --------------------\ntestuser testuser@contoso.com +Contact\n--------- --------------------\ntestuser testuser@contoso.com """ json_data_show_run_snmp_contact_expected = """\ -{'testuser': 'testuser@contoso.com'} +{'Contact': 'testuser testuser@contoso.com'} """ config_snmp_contact_add_del_new_contact ="""\ -Contact name testuser and contact email testuser@contoso.com have been added to configuration +SNMP Contact testuser testuser@contoso.com has been added to configuration Restarting SNMP service... """ @@ -242,17 +242,17 @@ def test_config_snmp_contact_del_without_contact_redis(self): result = runner.invoke(config.config.commands["snmp"].commands["contact"].commands["del"], ["blah"], obj=db) print(result.exit_code) assert result.exit_code == 2 - assert 'Contact name blah is not configured' in result.output + assert 'SNMP Contact blah is not configured' in result.output assert db.cfgdb.get_entry("SNMP", "CONTACT") == {} def test_config_snmp_contact_modify_without_contact_redis(self): db = Db() runner = CliRunner() result = runner.invoke(config.config.commands["snmp"].commands["contact"].commands["modify"], - ["blah", "blah@contoso.com"], obj=db) + ["blah blah@contoso.com"], obj=db) print(result.exit_code) - assert result.exit_code == 3 - assert 'Contact name blah is not configured' in result.output + assert result.exit_code == 2 + assert "Cannot modify SNMP Contact. You must use 'config snmp contact add' command." in result.output assert db.cfgdb.get_entry("SNMP", "CONTACT") == {} def test_config_snmp_contact_add_del_new_contact(self): @@ -260,19 +260,19 @@ def test_config_snmp_contact_add_del_new_contact(self): runner = CliRunner() with mock.patch('utilities_common.cli.run_command') as mock_run_command: result = runner.invoke(config.config.commands["snmp"].commands["contact"].commands["add"], - ["testuser", "testuser@contoso.com"], obj=db) + ["testuser testuser@contoso.com"], obj=db) print(result.exit_code) print(result.output) assert result.exit_code == 0 assert result.output == config_snmp_contact_add_del_new_contact - assert db.cfgdb.get_entry("SNMP", "CONTACT") == {"testuser": "testuser@contoso.com"} + assert db.cfgdb.get_entry("SNMP", "CONTACT") == {"Contact": "testuser testuser@contoso.com"} result = runner.invoke(config.config.commands["snmp"].commands["contact"].commands["del"], - ["testuser"], obj=db) + ["testuser testuser@contoso.com"], obj=db) print(result.exit_code) print(result.output) assert result.exit_code == 0 - assert 'SNMP contact testuser removed from configuration' in result.output + assert 'SNMP Contact testuser testuser@contoso.com removed from configuration' in result.output assert db.cfgdb.get_entry("SNMP", "CONTACT") == {} # Add snmp contact tests @@ -281,29 +281,18 @@ def test_config_snmp_contact_add_with_existing_contact(self): runner = CliRunner() with mock.patch('utilities_common.cli.run_command') as mock_run_command: result = runner.invoke(config.config.commands["snmp"].commands["contact"].commands["add"], - ["testuser", "testuser@contoso.com"], obj=db) + ["testuser testuser@contoso.com"], obj=db) print(result.exit_code) print(result.output) assert result.exit_code == 0 assert result.output == config_snmp_contact_add_del_new_contact - assert db.cfgdb.get_entry("SNMP", "CONTACT") == {"testuser": "testuser@contoso.com"} + assert db.cfgdb.get_entry("SNMP", "CONTACT") == {"Contact": "testuser testuser@contoso.com"} result = runner.invoke(config.config.commands["snmp"].commands["contact"].commands["add"], - ["blah", "blah@contoso.com"], obj=db) + ["blah blah@contoso.com"], obj=db) print(result.exit_code) assert result.exit_code == 1 - assert 'Contact already exists. Use sudo config snmp contact modify instead' in result.output - - def test_config_snmp_contact_add_invalid_email(self): - db = Db() - runner = CliRunner() - with mock.patch('utilities_common.cli.run_command') as mock_run_command: - result = runner.invoke(config.config.commands["snmp"].commands["contact"].commands["add"], - ["testuser", "testusercontoso.com"], obj=db) - print(result.exit_code) - assert result.exit_code == 2 - assert "Contact email testusercontoso.com is not valid" in result.output - + assert 'Contact already exists, Use sudo config snmp contact modify instead' in result.output # Delete snmp contact tests def test_config_snmp_contact_del_new_contact_when_contact_exists(self): @@ -311,35 +300,35 @@ def test_config_snmp_contact_del_new_contact_when_contact_exists(self): runner = CliRunner() with mock.patch('utilities_common.cli.run_command') as mock_run_command: result = runner.invoke(config.config.commands["snmp"].commands["contact"].commands["add"], - ["testuser", "testuser@contoso.com"], obj=db) + ["testuser testuser@contoso.com"], obj=db) print(result.exit_code) print(result.output) assert result.exit_code == 0 assert result.output == config_snmp_contact_add_del_new_contact - assert db.cfgdb.get_entry("SNMP", "CONTACT") == {"testuser": "testuser@contoso.com"} + assert db.cfgdb.get_entry("SNMP", "CONTACT") == {"Contact": "testuser testuser@contoso.com"} result = runner.invoke(config.config.commands["snmp"].commands["contact"].commands["del"], ["blah"], obj=db) print(result.exit_code) assert result.exit_code == 1 - assert 'SNMP contact blah is not configured' in result.output + assert 'SNMP Contact blah does not exist.' in result.output def test_config_snmp_contact_del_with_existing_contact(self): db = Db() runner = CliRunner() with mock.patch('utilities_common.cli.run_command') as mock_run_command: result = runner.invoke(config.config.commands["snmp"].commands["contact"].commands["add"], - ["testuser", "testuser@contoso.com"], obj=db) + ["testuser testuser@contoso.com"], obj=db) print(result.exit_code) print(result.output) assert result.exit_code == 0 assert result.output == config_snmp_contact_add_del_new_contact - assert db.cfgdb.get_entry("SNMP", "CONTACT") == {"testuser": "testuser@contoso.com"} + assert db.cfgdb.get_entry("SNMP", "CONTACT") == {"Contact": "testuser testuser@contoso.com"} result = runner.invoke(config.config.commands["snmp"].commands["contact"].commands["del"], - ["testuser"], obj=db) + ["testuser testuser@contoso.com"], obj=db) print(result.exit_code) assert result.exit_code == 0 - assert 'SNMP contact testuser removed from configuration' in result.output + assert 'SNMP Contact testuser testuser@contoso.com removed from configuration' in result.output assert db.cfgdb.get_entry("SNMP", "CONTACT") == {} # Modify snmp contact tests @@ -348,75 +337,38 @@ def test_config_snmp_contact_modify_email_with_existing_contact(self): runner = CliRunner() with mock.patch('utilities_common.cli.run_command') as mock_run_command: result = runner.invoke(config.config.commands["snmp"].commands["contact"].commands["add"], - ["testuser", "testuser@contoso.com"], obj=db) + ["testuser testuser@contoso.com"], obj=db) print(result.exit_code) print(result.output) assert result.exit_code == 0 assert result.output == config_snmp_contact_add_del_new_contact - assert db.cfgdb.get_entry("SNMP", "CONTACT") == {"testuser": "testuser@contoso.com"} + assert db.cfgdb.get_entry("SNMP", "CONTACT") == {"Contact": "testuser testuser@contoso.com"} result = runner.invoke(config.config.commands["snmp"].commands["contact"].commands["modify"], - ["testuser", "testuser@test.com"], obj=db) + ["testuser testuser@test.com"], obj=db) print(result.exit_code) assert result.exit_code == 0 - assert 'SNMP contact testuser email updated to testuser@test.com' in result.output - assert db.cfgdb.get_entry("SNMP", "CONTACT") == {"testuser": "testuser@test.com"} + assert 'SNMP contact testuser testuser@test.com modified in configuration' in result.output + assert db.cfgdb.get_entry("SNMP", "CONTACT") == {"Contact": "testuser testuser@test.com"} def test_config_snmp_contact_modify_contact_and_email_with_existing_entry(self): db = Db() runner = CliRunner() with mock.patch('utilities_common.cli.run_command') as mock_run_command: result = runner.invoke(config.config.commands["snmp"].commands["contact"].commands["add"], - ["testuser", "testuser@contoso.com"], obj=db) + ["testuser testuser@contoso.com"], obj=db) print(result.exit_code) print(result.output) assert result.exit_code == 0 assert result.output == config_snmp_contact_add_del_new_contact - assert db.cfgdb.get_entry("SNMP", "CONTACT") == {"testuser": "testuser@contoso.com"} + assert db.cfgdb.get_entry("SNMP", "CONTACT") == {"Contact": "testuser testuser@contoso.com"} result = runner.invoke(config.config.commands["snmp"].commands["contact"].commands["modify"], - ["testuser", "testuser@contoso.com"], obj=db) + ["testuser testuser@contoso.com"], obj=db) print(result.exit_code) assert result.exit_code == 1 assert 'SNMP contact testuser testuser@contoso.com already exists' in result.output - def test_config_snmp_contact_modify_existing_contact_with_invalid_email(self): - db = Db() - runner = CliRunner() - with mock.patch('utilities_common.cli.run_command') as mock_run_command: - result = runner.invoke(config.config.commands["snmp"].commands["contact"].commands["add"], - ["testuser", "testuser@contoso.com"], obj=db) - print(result.exit_code) - print(result.output) - assert result.exit_code == 0 - assert result.output == config_snmp_contact_add_del_new_contact - assert db.cfgdb.get_entry("SNMP", "CONTACT") == {"testuser": "testuser@contoso.com"} - - result = runner.invoke(config.config.commands["snmp"].commands["contact"].commands["modify"], - ["testuser", "testuser@contosocom"], obj=db) - print(result.exit_code) - assert result.exit_code == 2 - assert 'Contact email testuser@contosocom is not valid' in result.output - - - def test_config_snmp_contact_modify_new_contact_with_invalid_email(self): - db = Db() - runner = CliRunner() - with mock.patch('utilities_common.cli.run_command') as mock_run_command: - result = runner.invoke(config.config.commands["snmp"].commands["contact"].commands["add"], - ["testuser", "testuser@contoso.com"], obj=db) - print(result.exit_code) - print(result.output) - assert result.exit_code == 0 - assert result.output == config_snmp_contact_add_del_new_contact - assert db.cfgdb.get_entry("SNMP", "CONTACT") == {"testuser": "testuser@contoso.com"} - - result = runner.invoke(config.config.commands["snmp"].commands["contact"].commands["modify"], - ["blah", "blah@contoso@com"], obj=db) - print(result.exit_code) - assert result.exit_code == 2 - assert 'Contact email blah@contoso@com is not valid' in result.output - # Add snmp location tests def test_config_snmp_location_add_exiting_location_with_same_location_already_existing(self): db = Db() @@ -861,12 +813,6 @@ def test_config_snmp_user_del_invalid_user(self): assert result.exit_code == 1 assert 'SNMP user test_nopriv_RO_2 is not configured' in result.output - @pytest.mark.parametrize("invalid_email", ['test@contoso', 'test.contoso.com', 'testcontoso@com', - '123_%contoso.com', 'mytest@contoso.comm']) - def test_is_valid_email(self, invalid_email): - output = config.is_valid_email(invalid_email) - assert output == False - @patch("validated_config_db_connector.device_info.is_yang_config_validation_enabled", mock.Mock(return_value=True)) @patch("config.validated_config_db_connector.ValidatedConfigDBConnector.validated_set_entry", mock.Mock(side_effect=ValueError)) def test_config_snmp_community_add_new_community_with_invalid_type_yang_validation(self): diff --git a/tests/db_migrator_input/config_db/snmp_contact_expected.json b/tests/db_migrator_input/config_db/snmp_contact_expected.json new file mode 100644 index 0000000000..279f003a12 --- /dev/null +++ b/tests/db_migrator_input/config_db/snmp_contact_expected.json @@ -0,0 +1,5 @@ +{ + "SNMP|CONTACT": { + "Contact": "test-name test-email@test.com" + } +} diff --git a/tests/db_migrator_input/config_db/snmp_contact_input.json b/tests/db_migrator_input/config_db/snmp_contact_input.json new file mode 100644 index 0000000000..f304623b89 --- /dev/null +++ b/tests/db_migrator_input/config_db/snmp_contact_input.json @@ -0,0 +1,8 @@ +{ + "SNMP|CONTACT": { + "test-name": "test-email@test.com" + }, + "VERSIONS|DATABASE": { + "VERSION": "version_202405_01" + } +} diff --git a/tests/db_migrator_test.py b/tests/db_migrator_test.py index cdf4251bd7..3f7dc4701d 100644 --- a/tests/db_migrator_test.py +++ b/tests/db_migrator_test.py @@ -1013,3 +1013,28 @@ def test_per_command_aaa(self, test_json): diff = DeepDiff(resulting_table, expected_table, ignore_order=True) assert not diff + + +class TestSnmpContactNameEmailMerge(object): + @classmethod + def setup_class(cls): + os.environ['UTILITIES_UNIT_TESTING'] = "2" + + @classmethod + def teardown_class(cls): + os.environ['UTILITIES_UNIT_TESTING'] = "0" + dbconnector.dedicated_dbs['CONFIG_DB'] = None + + def test_snmp_contact_name_email_merge_migrator(self): + dbconnector.dedicated_dbs['CONFIG_DB'] = os.path.join(mock_db_path, 'config_db', 'snmp_contact_input') + import db_migrator + dbmgtr = db_migrator.DBMigrator(None) + dbmgtr.migrate() + dbconnector.dedicated_dbs['CONFIG_DB'] = os.path.join(mock_db_path, 'config_db', 'snmp_contact_expected') + expected_db = Db() + + resulting_table = dbmgtr.configDB.get_table('SNMP') + expected_table = expected_db.cfgdb.get_table('SNMP') + + diff = DeepDiff(resulting_table, expected_table, ignore_order=True) + assert not diff diff --git a/tests/show_snmp_test.py b/tests/show_snmp_test.py index 38a49882ff..94a88ac52d 100644 --- a/tests/show_snmp_test.py +++ b/tests/show_snmp_test.py @@ -27,16 +27,16 @@ """ config_snmp_contact_add_del_new_contact ="""\ -Contact name testuser and contact email testuser@contoso.com have been added to configuration +SNMP Contact testuser testuser@contoso.com has been added to configuration Restarting SNMP service... """ tabular_data_show_run_snmp_contact_expected = """\ -Contact Contact Email\n--------- --------------------\ntestuser testuser@contoso.com +Contact\n-----------------------------\ntestuser testuser@contoso.com """ json_data_show_run_snmp_contact_expected = """\ -{'testuser': 'testuser@contoso.com'} +{'Contact': 'testuser testuser@contoso.com'} """ tabular_data_show_run_snmp_community_expected = """\ @@ -98,9 +98,9 @@ public -SNMP_CONTACT SNMP_CONTACT_EMAIL --------------- -------------------- -testuser testuser@contoso.com +Contact +----------------------------- +testuser testuser@contoso.com Community String Community Type @@ -214,12 +214,12 @@ def test_show_run_snmp_contact_tabular(self): runner = CliRunner() with mock.patch('utilities_common.cli.run_command') as mock_run_command: result = runner.invoke(config.config.commands["snmp"].commands["contact"].commands["add"], - ["testuser", "testuser@contoso.com"], obj=db) + ["testuser testuser@contoso.com"], obj=db) print(result.exit_code) print(result.output) assert result.exit_code == 0 assert result.output == config_snmp_contact_add_del_new_contact - assert db.cfgdb.get_entry("SNMP", "CONTACT") == {"testuser": "testuser@contoso.com"} + assert db.cfgdb.get_entry("SNMP", "CONTACT") == {"Contact": "testuser testuser@contoso.com"} result = runner.invoke(show.cli.commands["runningconfiguration"].commands["snmp"].commands["contact"], [], obj=db) @@ -233,12 +233,12 @@ def test_show_run_snmp_contact_json(self): runner = CliRunner() with mock.patch('utilities_common.cli.run_command') as mock_run_command: result = runner.invoke(config.config.commands["snmp"].commands["contact"].commands["add"], - ["testuser", "testuser@contoso.com"], obj=db) + ["testuser testuser@contoso.com"], obj=db) print(result.exit_code) print(result.output) assert result.exit_code == 0 assert result.output == config_snmp_contact_add_del_new_contact - assert db.cfgdb.get_entry("SNMP", "CONTACT") == {"testuser": "testuser@contoso.com"} + assert db.cfgdb.get_entry("SNMP", "CONTACT") == {"Contact": "testuser testuser@contoso.com"} result = runner.invoke(show.cli.commands["runningconfiguration"].commands["snmp"].commands["contact"], ["--json"], obj=db) @@ -436,12 +436,12 @@ def test_show_run_snmp_tabular(self): runner = CliRunner() with mock.patch('utilities_common.cli.run_command') as mock_run_command: result = runner.invoke(config.config.commands["snmp"].commands["contact"].commands["add"], - ["testuser", "testuser@contoso.com"], obj=db) + ["testuser testuser@contoso.com"], obj=db) print(result.exit_code) print(result.output) assert result.exit_code == 0 assert result.output == config_snmp_contact_add_del_new_contact - assert db.cfgdb.get_entry("SNMP", "CONTACT") == {"testuser": "testuser@contoso.com"} + assert db.cfgdb.get_entry("SNMP", "CONTACT") == {"Contact": "testuser testuser@contoso.com"} result = runner.invoke(config.config.commands["snmp"].commands["location"].commands["add"], ["public"], obj=db)