Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[dhcp_relay] Fix dhcp_relay counter display issue #3054

Merged
merged 1 commit into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions config/vlan.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,10 @@ def is_dhcpv6_relay_config_exist(db, vlan_name):
return True


def delete_state_db_entry(entry_name):
state_db = SonicV2Connector()
state_db.connect(state_db.STATE_DB)
exists = state_db.exists(state_db.STATE_DB, 'DHCPv6_COUNTER_TABLE|{}'.format(entry_name))
def delete_db_entry(entry_name, db_connector, db_name):
exists = db_connector.exists(db_name, entry_name)
if exists:
state_db.delete(state_db.STATE_DB, 'DHCPv6_COUNTER_TABLE|{}'.format(entry_name))
db_connector.delete(db_name, entry_name)


@vlan.command('del')
Expand Down Expand Up @@ -125,7 +123,8 @@ def del_vlan(db, vid, no_restart_dhcp_relay):
# We need to restart dhcp_relay service after dhcpv6_relay config change
if is_dhcp_relay_running():
dhcp_relay_util.handle_restart_dhcp_relay_service()
delete_state_db_entry(vlan)
delete_db_entry("DHCPv6_COUNTER_TABLE|{}".format(vlan), db.db, db.db.STATE_DB)
delete_db_entry("DHCP_COUNTER_TABLE|{}".format(vlan), db.db, db.db.STATE_DB)

vlans = db.cfgdb.get_keys('VLAN')
if not vlans:
Expand Down Expand Up @@ -279,6 +278,8 @@ def del_vlan_member(db, vid, port):

try:
config_db.set_entry('VLAN_MEMBER', (vlan, port), None)
delete_db_entry("DHCPv6_COUNTER_TABLE|{}".format(port), db.db, db.db.STATE_DB)
delete_db_entry("DHCP_COUNTER_TABLE|{}".format(port), db.db, db.db.STATE_DB)
except JsonPatchConflict:
ctx.fail("{} invalid or does not exist, or {} is not a member of {}".format(vlan, port, vlan))

29 changes: 20 additions & 9 deletions tests/vlan_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,20 +383,31 @@ def test_config_vlan_del_vlan(self, mock_restart_dhcp_relay_service):
assert result.exit_code != 0
assert "Error: VLAN ID 1000 can not be removed. First remove all members assigned to this VLAN." in result.output

vlan_member = db.cfgdb.get_table('VLAN_MEMBER')
keys = [ (k, v) for k, v in vlan_member if k == 'Vlan{}'.format(1000) ]
for k,v in keys:
result = runner.invoke(config.config.commands["vlan"].commands["member"].commands["del"], ["1000", v], obj=db)
print(result.exit_code)
print(result.output)
assert result.exit_code == 0
with mock.patch("config.vlan.delete_db_entry") as delete_db_entry:
vlan_member = db.cfgdb.get_table('VLAN_MEMBER')
keys = [ (k, v) for k, v in vlan_member if k == 'Vlan{}'.format(1000) ]
for k,v in keys:
result = runner.invoke(config.config.commands["vlan"].commands["member"].commands["del"], ["1000", v], obj=db)
print(result.exit_code)
print(result.output)
assert result.exit_code == 0

with mock.patch("config.vlan.delete_state_db_entry") as delete_state_db_entry:
result = runner.invoke(config.config.commands["vlan"].commands["del"], ["1000"], obj=db)
print(result.exit_code)
print(result.output)
assert result.exit_code == 0
delete_state_db_entry.assert_called_once_with("Vlan1000")
delete_db_entry.assert_has_calls([
mock.call("DHCPv6_COUNTER_TABLE|Vlan1000", mock.ANY, db.db.STATE_DB),
mock.call("DHCPv6_COUNTER_TABLE|Ethernet4", mock.ANY, db.db.STATE_DB),
mock.call("DHCPv6_COUNTER_TABLE|Ethernet8", mock.ANY, db.db.STATE_DB),
mock.call("DHCPv6_COUNTER_TABLE|Ethernet12", mock.ANY, db.db.STATE_DB),
mock.call("DHCPv6_COUNTER_TABLE|Ethernet16", mock.ANY, db.db.STATE_DB),
mock.call("DHCP_COUNTER_TABLE|Vlan1000", mock.ANY, db.db.STATE_DB),
mock.call("DHCP_COUNTER_TABLE|Ethernet4", mock.ANY, db.db.STATE_DB),
mock.call("DHCP_COUNTER_TABLE|Ethernet8", mock.ANY, db.db.STATE_DB),
mock.call("DHCP_COUNTER_TABLE|Ethernet12", mock.ANY, db.db.STATE_DB),
mock.call("DHCP_COUNTER_TABLE|Ethernet16", mock.ANY, db.db.STATE_DB)
], any_order=True)

# show output
result = runner.invoke(show.cli.commands["vlan"].commands["brief"], [], obj=db)
Expand Down
Loading