-
Notifications
You must be signed in to change notification settings - Fork 1.8k
[dhcp-relay] Add dhcp_relay show cli #13614
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ee896e8
[dhcp-relay] Add dhcp_relay show cli
yaqiangz 7ab2620
Merge branch 'master' into master_dhcp_relay_show
yaqiangz 2729f1e
Restore test_dhcp_relay_column_output
yaqiangz 4c01273
Add header for show dhcp_relay
yaqiangz c2fd8ec
Fix empty entry
yaqiangz dee185f
Optimize
yaqiangz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 88 additions & 18 deletions
106
dockers/docker-dhcp-relay/cli-plugin-tests/test_show_dhcp_relay.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,28 +1,98 @@ | ||
| import os | ||
| import pytest | ||
| import sys | ||
| import traceback | ||
| import os | ||
| sys.path.append('../cli/show/plugins/') | ||
| import show_dhcp_relay as show | ||
| import show.vlan as vlan | ||
| from swsscommon import swsscommon | ||
| from mock_config import TEST_DATA | ||
| from parameterized import parameterized | ||
| from pyfakefs.fake_filesystem_unittest import patchfs | ||
| from unittest import mock | ||
|
|
||
| from click.testing import CliRunner | ||
| try: | ||
| sys.path.insert(0, '../../../src/sonic-host-services/tests/common') | ||
| from mock_configdb import MockConfigDb | ||
| swsscommon.ConfigDBConnector = MockConfigDb | ||
| except KeyError: | ||
| pass | ||
|
|
||
| import show.vlan as vlan | ||
| from utilities_common.db import Db | ||
| expected_ipv6_table_with_header = """\ | ||
| +-------------+----------------------+ | ||
| | Interface | DHCP Relay Address | | ||
| +=============+======================+ | ||
| | Vlan1000 | fc02:2000::1 | | ||
| | | fc02:2000::2 | | ||
| +-------------+----------------------+ | ||
| """ | ||
|
|
||
| expected_ipv4_table_with_header = """\ | ||
| +-------------+----------------------+ | ||
| | Interface | DHCP Relay Address | | ||
| +=============+======================+ | ||
| | Vlan1000 | 192.0.0.1 | | ||
| | | 192.0.0.2 | | ||
| +-------------+----------------------+ | ||
| """ | ||
|
|
||
| expected_ipv6_table_without_header = """\ | ||
| -------- ------------ | ||
yaqiangz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Vlan1000 fc02:2000::1 | ||
| fc02:2000::2 | ||
| -------- ------------ | ||
| """ | ||
|
|
||
| expected_ipv4_table_without_header = """\ | ||
| -------- --------- | ||
| Vlan1000 192.0.0.1 | ||
| 192.0.0.2 | ||
| -------- --------- | ||
| """ | ||
|
|
||
| DBCONFIG_PATH = '/var/run/redis/sonic-db/database_config.json' | ||
|
|
||
| IP_VER_TEST_PARAM_MAP = { | ||
| "ipv4": { | ||
| "entry": "dhcp_servers", | ||
| "table": "VLAN" | ||
| }, | ||
| "ipv6": { | ||
| "entry": "dhcpv6_servers", | ||
| "table": "DHCP_RELAY" | ||
| } | ||
| } | ||
|
|
||
| sys.path.insert(0, '../cli/show/plugins/') | ||
| import show_dhcp_relay | ||
|
|
||
| def test_plugin_registration(): | ||
| cli = mock.MagicMock() | ||
| show.register(cli) | ||
| assert 'DHCP Helper Address' in dict(vlan.VlanBrief.COLUMNS) | ||
|
|
||
| class TestVlanDhcpRelay(object): | ||
| def test_plugin_registration(self): | ||
| cli = mock.MagicMock() | ||
| show_dhcp_relay.register(cli) | ||
| assert 'DHCP Helper Address' in dict(vlan.VlanBrief.COLUMNS) | ||
|
|
||
| def test_dhcp_relay_column_output(self): | ||
| ctx = ( | ||
| ({'Vlan100': {'dhcp_servers': ['192.0.0.1', '192.168.0.2']}}, {}, {}), | ||
| (), | ||
| ) | ||
| assert show_dhcp_relay.get_dhcp_helper_address(ctx, 'Vlan100') == '192.0.0.1\n192.168.0.2' | ||
| def test_dhcp_relay_column_output(): | ||
| ctx = ( | ||
| ({'Vlan1001': {'dhcp_servers': ['192.0.0.1', '192.168.0.2']}}, {}, {}), | ||
| (), | ||
| ) | ||
| assert show.get_dhcp_helper_address(ctx, 'Vlan1001') == '192.0.0.1\n192.168.0.2' | ||
|
|
||
|
|
||
| @parameterized.expand(TEST_DATA) | ||
| @patchfs | ||
| def test_show_dhcp_relay(test_name, test_data, fs): | ||
| if not os.path.exists(DBCONFIG_PATH): | ||
| fs.create_file(DBCONFIG_PATH) | ||
| MockConfigDb.set_config_db(test_data["config_db"]) | ||
| config_db = MockConfigDb() | ||
| ip_version = "ipv4" if "ipv4" in test_name else "ipv6" | ||
| table = config_db.get_table(IP_VER_TEST_PARAM_MAP[ip_version]["table"]) | ||
| result = show.get_data(table, "Vlan1000", IP_VER_TEST_PARAM_MAP[ip_version]["entry"], "with_header" in test_name) | ||
| if test_name == "ipv4_with_header": | ||
| expected_output = expected_ipv4_table_with_header | ||
| elif test_name == "ipv4_without_header": | ||
| expected_output = expected_ipv4_table_without_header | ||
| elif test_name == "ipv6_with_header": | ||
| expected_output = expected_ipv6_table_with_header | ||
| elif test_name == "ipv6_without_header": | ||
| expected_output = expected_ipv6_table_without_header | ||
| assert result == expected_output | ||
41 changes: 0 additions & 41 deletions
41
dockers/docker-dhcp-relay/cli-plugin-tests/test_show_dhcpv6_helper.py
This file was deleted.
Oops, something went wrong.
40 changes: 32 additions & 8 deletions
40
dockers/docker-dhcp-relay/cli/clear/plugins/clear_dhcp6relay_counter.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,32 +1,56 @@ | ||
| import sys | ||
| import click | ||
| import importlib | ||
| dhcp6_relay = importlib.import_module('show.plugins.dhcp-relay') | ||
|
|
||
| import utilities_common.cli as clicommon | ||
|
|
||
|
|
||
| def clear_dhcp_relay_ipv6_counter(interface): | ||
| counter = dhcp6_relay.DHCPv6_Counter() | ||
| counter_intf = counter.get_interface() | ||
|
|
||
| if interface: | ||
| counter.clear_table(interface) | ||
| else: | ||
| for intf in counter_intf: | ||
| counter.clear_table(intf) | ||
|
|
||
|
|
||
| # sonic-clear dhcp6relay_counters | ||
| @click.group(cls=clicommon.AliasedGroup) | ||
| def dhcp6relay_clear(): | ||
| pass | ||
|
|
||
|
|
||
| @dhcp6relay_clear.command('dhcp6relay_counters') | ||
| @click.option('-i', '--interface', required=False) | ||
| def dhcp6relay_clear_counters(interface): | ||
| """ Clear dhcp6relay message counts """ | ||
| clear_dhcp_relay_ipv6_counter(interface) | ||
|
|
||
| counter = dhcp6_relay.DHCPv6_Counter() | ||
| counter_intf = counter.get_interface() | ||
|
|
||
| if interface: | ||
| counter.clear_table(interface) | ||
| else: | ||
| for intf in counter_intf: | ||
| counter.clear_table(intf) | ||
| @click.group(cls=clicommon.AliasedGroup, name="dhcp_relay") | ||
| def dhcp_relay(): | ||
| pass | ||
|
|
||
|
|
||
| @dhcp_relay.group(cls=clicommon.AliasedGroup, name="ipv6") | ||
| def dhcp_relay_ipv6(): | ||
| pass | ||
|
|
||
|
|
||
| @dhcp_relay_ipv6.command('counters') | ||
| @click.option('-i', '--interface', required=False) | ||
| def clear_dhcp_relay_ipv6_counters(interface): | ||
| """ Clear dhcp_relay ipv6 message counts """ | ||
| clear_dhcp_relay_ipv6_counter(interface) | ||
|
|
||
|
|
||
| def register(cli): | ||
| cli.add_command(dhcp6relay_clear_counters) | ||
| cli.add_command(dhcp_relay) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| dhcp6relay_clear_counters() | ||
| dhcp_relay() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.