Skip to content

Commit

Permalink
fix UT
Browse files Browse the repository at this point in the history
  • Loading branch information
isabelmsft committed Nov 1, 2022
1 parent 0bc9fa7 commit 3ce3dbc
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 12 deletions.
5 changes: 2 additions & 3 deletions config/validated_config_db_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ def apply_patch(self, gcu_patch, table):

try:
GenericUpdater().apply_patch(patch=gcu_patch, config_format=config_format, verbose=False, dry_run=False, ignore_non_yang_tables=False, ignore_paths=None)
return True
except EmptyTableError:
self.validated_delete_table(table)

Expand All @@ -114,7 +113,7 @@ def validated_mod_entry(self, table, key, value):
op = "remove"

gcu_patch = self.create_gcu_patch(op, table, key, value, mod_entry=True)
return self.apply_patch(gcu_patch, table)
self.apply_patch(gcu_patch, table)

def validated_set_entry(self, table, key, value):
if value is not None:
Expand All @@ -123,4 +122,4 @@ def validated_set_entry(self, table, key, value):
op = "remove"

gcu_patch = self.create_gcu_patch(op, table, key, value)
return self.apply_patch(gcu_patch, table)
self.apply_patch(gcu_patch, table)
18 changes: 18 additions & 0 deletions tests/aaa_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@

from click.testing import CliRunner
from utilities_common.db import Db
from jsonpatch import JsonPatchConflict
from unittest import mock
from mock import patch

import config.main as config
import config.validated_config_db_connector as validated_config_db_connector
import show.main as show

test_path = os.path.dirname(os.path.abspath(__file__))
Expand Down Expand Up @@ -280,3 +284,17 @@ def test_config_aaa_tacacs(self, get_cmd_module):
assert result.exit_code == 0
assert result.output == show_aaa_disable_accounting_output

@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=JsonPatchConflict))
def test_config_aaa_tacacs_delete_yang_validation(self):
config.ADHOC_VALIDATION = True
runner = CliRunner()
db = Db()
obj = {'db':db.cfgdb}

result = runner.invoke(config.config.commands["tacacs"].commands["delete"], ["10.10.10.10"], obj=obj)
print(result.exit_code)
print(result.output)
assert result.exit_code != 0
assert "Invalid ip address. Error:" in result.output

33 changes: 24 additions & 9 deletions tests/validated_config_db_connector_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,33 +33,48 @@ def test_validated_set_entry_empty_table(self):
mock_generic_updater = mock.Mock()
mock_generic_updater.apply_patch = mock.Mock(side_effect=EmptyTableError)
with mock.patch('validated_config_db_connector.GenericUpdater', return_value=mock_generic_updater):
remove_entry_success = validated_config_db_connector.ValidatedConfigDBConnector.validated_set_entry(mock.Mock(), SAMPLE_TABLE, SAMPLE_KEY, SAMPLE_VALUE_EMPTY)
assert remove_entry_success
try:
remove_entry_success = validated_config_db_connector.ValidatedConfigDBConnector.validated_set_entry(mock.Mock(), SAMPLE_TABLE, SAMPLE_KEY, SAMPLE_VALUE_EMPTY)
except Exception as ex:
assert False, "Exception {} thrown unexpectedly".format(ex)

def test_validated_mod_entry(self):
mock_generic_updater = mock.Mock()
with mock.patch('validated_config_db_connector.GenericUpdater', return_value=mock_generic_updater):
successful_application = validated_config_db_connector.ValidatedConfigDBConnector.validated_mod_entry(mock.Mock(), SAMPLE_TABLE, SAMPLE_KEY, SAMPLE_VALUE_DICT)
assert successful_application
try:
successful_application = validated_config_db_connector.ValidatedConfigDBConnector.validated_mod_entry(mock.Mock(), SAMPLE_TABLE, SAMPLE_KEY, SAMPLE_VALUE_DICT)
except Exception as ex:
assert False, "Exception {} thrown unexpectedly".format(ex)

def test_validated_delete_table_invalid_delete(self):
mock_generic_updater = mock.Mock()
mock_generic_updater.apply_patch = mock.Mock(side_effect=ValueError)
with mock.patch('validated_config_db_connector.GenericUpdater', return_value=mock_generic_updater):
delete_table_success = validated_config_db_connector.ValidatedConfigDBConnector.validated_delete_table(mock.Mock(), SAMPLE_TABLE)
assert not delete_table_success
try:
delete_table_success = validated_config_db_connector.ValidatedConfigDBConnector.validated_delete_table(mock.Mock(), SAMPLE_TABLE)
except Exception as ex:
assert False, "Exception {} thrown unexpectedly".format(ex)

def test_create_gcu_patch(self):
def test_create_gcu_patch_set_entry(self):
expected_gcu_patch = jsonpatch.JsonPatch([{"op": "add", "path": "/PORTCHANNEL", "value": {}}, {"op": "add", "path": "/PORTCHANNEL/PortChannel01", "value": {}}, {"op": "add", "path": "/PORTCHANNEL/PortChannel01", "value": "test"}])
with mock.patch('validated_config_db_connector.ConfigDBConnector.get_table', return_value=False):
with mock.patch('validated_config_db_connector.ConfigDBConnector.get_entry', return_value=False):
created_gcu_patch = validated_config_db_connector.ValidatedConfigDBConnector.create_gcu_patch(ValidatedConfigDBConnector(ConfigDBConnector()), "add", "PORTCHANNEL", "PortChannel01", SAMPLE_VALUE)
assert expected_gcu_patch == created_gcu_patch

def test_create_gcu_patch_mod_entry(self):
expected_gcu_patch = jsonpatch.JsonPatch([{"op": "add", "path": "/PORTCHANNEL/PortChannel01/test_key", "value": "test_value"}])
with mock.patch('validated_config_db_connector.ConfigDBConnector.get_table', return_value=True):
with mock.patch('validated_config_db_connector.ConfigDBConnector.get_entry', return_value=True):
created_gcu_patch = validated_config_db_connector.ValidatedConfigDBConnector.create_gcu_patch(ValidatedConfigDBConnector(ConfigDBConnector()), "add", "PORTCHANNEL", "PortChannel01", {"test_key": "test_value"}, mod_entry=True)
assert expected_gcu_patch == created_gcu_patch

def test_apply_patch(self):
mock_generic_updater = mock.Mock()
mock_generic_updater.apply_patch = mock.Mock(side_effect=EmptyTableError)
with mock.patch('validated_config_db_connector.GenericUpdater', return_value=mock_generic_updater):
with mock.patch('validated_config_db_connector.ValidatedConfigDBConnector.validated_delete_table', return_value=True):
apply_patch_success = validated_config_db_connector.ValidatedConfigDBConnector.apply_patch(mock.Mock(), SAMPLE_PATCH, SAMPLE_TABLE)
assert not apply_patch_success
try:
validated_config_db_connector.ValidatedConfigDBConnector.apply_patch(mock.Mock(), SAMPLE_PATCH, SAMPLE_TABLE)
except Exception as ex:
assert False, "Exception {} thrown unexpectedly".format(ex)

0 comments on commit 3ce3dbc

Please sign in to comment.