Skip to content

Commit

Permalink
fix portchannel unit tests, update decorator name
Browse files Browse the repository at this point in the history
  • Loading branch information
Ubuntu committed Jul 9, 2022
1 parent 3c69396 commit a40c351
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 25 deletions.
8 changes: 4 additions & 4 deletions config/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import utilities_common.cli as clicommon
from utilities_common.helper import get_port_pbh_binding, get_port_acl_binding
from utilities_common.general import load_db_config, load_module_from_source
from .validated_config_db_connector import validate
from .validated_config_db_connector import ValidatedConfigDBConnector, validated_set_entry
import utilities_common.multi_asic as multi_asic_util

from .utils import log
Expand Down Expand Up @@ -1895,8 +1895,8 @@ def portchannel(db, ctx, namespace):
def add_portchannel(ctx, portchannel_name, min_links, fallback):
"""Add port channel"""

db = validate(ctx.obj['db'])

db = ValidatedConfigDBConnector(ctx.obj['db'])
if is_portchannel_present_in_db(db, portchannel_name):
ctx.fail("{} already exists!".format(portchannel_name))

Expand All @@ -1918,7 +1918,7 @@ def add_portchannel(ctx, portchannel_name, min_links, fallback):
def remove_portchannel(ctx, portchannel_name):
"""Remove port channel"""

db = validate(ctx.obj['db'])
db = ValidatedConfigDBConnector(ctx.obj['db'])

if len([(k, v) for k, v in db.get_table('PORTCHANNEL_MEMBER') if k == portchannel_name]) != 0:
click.echo("Error: Portchannel {} contains members. Remove members before deleting Portchannel!".format(portchannel_name))
Expand Down
39 changes: 19 additions & 20 deletions config/validated_config_db_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,24 @@

from generic_config_updater.generic_updater import GenericUpdater, ConfigFormat

def validate(config_db_connector):

def validated_set_entry(table, key, value):
if value:
op = "add"
else:
op = "remove"
path = "/{}/{}".format(table, key)
gcu_json_input = []
gcu_json = {"op": "{}".format(op),
"path": "{}".format(path)}
if value:
gcu_json["value"] = value

gcu_json_input.append(gcu_json)
gcu_patch = jsonpatch.JsonPatch(gcu_json_input)
format = ConfigFormat.CONFIGDB.name
config_format = ConfigFormat[format.upper()]
GenericUpdater().apply_patch(patch=gcu_patch, config_format=config_format, verbose=False, dry_run=False, ignore_non_yang_tables=False, ignore_paths=None)

def ValidatedConfigDBConnector(config_db_connector):
config_db_connector.set_entry = validated_set_entry
return config_db_connector

def validated_set_entry(table, key, value):
if value:
op = "add"
else:
op = "remove"
path = "/{}/{}".format(table, key)
gcu_json_input = []
gcu_json = {"op": "{}".format(op),
"path": "{}".format(path)}
if value:
gcu_json["value"] = value

gcu_json_input.append(gcu_json)
gcu_patch = jsonpatch.JsonPatch(gcu_json_input)
format = ConfigFormat.CONFIGDB.name
config_format = ConfigFormat[format.upper()]
GenericUpdater().apply_patch(patch=gcu_patch, config_format=config_format, verbose=False, dry_run=False, ignore_non_yang_tables=False, ignore_paths=None)
1 change: 1 addition & 0 deletions generic_config_updater/generic_updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ def apply(self, patch):

# Generate list of changes to apply
self.logger.log_notice("Sorting patch updates.")
print('hi')
changes = self.patchsorter.sort(patch)
changes_len = len(changes)
self.logger.log_notice(f"The patch was sorted into {changes_len} " \
Expand Down
1 change: 1 addition & 0 deletions tests/config_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,7 @@ def test_apply_patch__all_optional_params_non_default__non_default_values_used(s
"--ignore-path", "",
"--verbose"],
catch_exceptions=False)
print(result.output)

# Assert
self.assertEqual(expected_exit_code, result.exit_code)
Expand Down
9 changes: 8 additions & 1 deletion tests/portchannel_test.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
import os
import traceback
import mock

from click.testing import CliRunner
from jsonpatch import JsonPatchConflict

import config.main as config
import show.main as show
from utilities_common.db import Db
from mock import patch

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


@patch("config.main.is_portchannel_present_in_db", mock.Mock(return_value=False))
@patch("config.validated_config_db_connector.validated_set_entry", mock.Mock(side_effect=ValueError))
def test_add_portchannel_with_invalid_name(self):
runner = CliRunner()
db = Db()
Expand All @@ -25,6 +30,7 @@ def test_add_portchannel_with_invalid_name(self):
assert result.exit_code != 0
assert "Error: PortChan005 is invalid!, name should have prefix 'PortChannel' and suffix '<0-9999>'" in result.output

@patch("config.validated_config_db_connector.validated_set_entry", mock.Mock(side_effect=JsonPatchConflict))
def test_delete_portchannel_with_invalid_name(self):
runner = CliRunner()
db = Db()
Expand All @@ -49,6 +55,7 @@ def test_add_existing_portchannel_again(self):
assert result.exit_code != 0
assert "Error: PortChannel0001 already exists!" in result.output

@patch("config.validated_config_db_connector.validated_set_entry", mock.Mock(side_effect=JsonPatchConflict))
def test_delete_non_existing_portchannel(self):
runner = CliRunner()
db = Db()
Expand Down

0 comments on commit a40c351

Please sign in to comment.