Skip to content

Commit 9d2ec1d

Browse files
yaqiangzmssonicbld
authored andcommitted
[dhcp-relay] Add dhcp_relay show cli (#13614)
Why I did it Currently the show and clear cli of dhcp_relayis may cause confusion. How I did it Add doc for it: [doc] Add docs for dhcp_relay show/clear cli sonic-utilities#2649 Add dhcp_relay config cli and test cases. show dhcp_relay ipv4 helper show dhcp_relay ipv6 destination show dhcp_relay ipv6 counters sonic-clear dhcp_relay ipv6 counters How to verify it Unit test all passed
1 parent 850a044 commit 9d2ec1d

File tree

5 files changed

+397
-98
lines changed

5 files changed

+397
-98
lines changed

dockers/docker-dhcp-relay/cli-plugin-tests/mock_config.py

+119-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
TEST_DATA = [
1+
COMMON_TEST_DATA = [
22
[
3-
"DHCPv6_Helpers",
3+
"ipv6_with_header",
44
{
55
"config_db": {
66
"DHCP_RELAY": {
@@ -12,7 +12,123 @@
1212
"dhcpv6_option|rfc6939_support": "true"
1313
}
1414
}
15-
},
15+
}
1616
},
1717
],
18+
[
19+
"ipv6_without_header",
20+
{
21+
"config_db": {
22+
"DHCP_RELAY": {
23+
"Vlan1000": {
24+
"dhcpv6_servers": [
25+
"fc02:2000::1",
26+
"fc02:2000::2"
27+
],
28+
"dhcpv6_option|rfc6939_support": "true"
29+
}
30+
}
31+
}
32+
},
33+
],
34+
[
35+
"ipv4_with_header",
36+
{
37+
"config_db": {
38+
"VLAN": {
39+
"Vlan1000": {
40+
"dhcp_servers": [
41+
"192.0.0.1",
42+
"192.0.0.2"
43+
]
44+
}
45+
}
46+
}
47+
}
48+
]
49+
]
50+
51+
NEW_ADDED_TEST_DATA = [
52+
[
53+
"ipv6",
54+
{
55+
"config_db": {
56+
"DHCP_RELAY": {
57+
"Vlan1000": {
58+
"dhcpv6_servers": [
59+
"fc02:2000::1",
60+
"fc02:2000::2"
61+
],
62+
"dhcpv6_option|rfc6939_support": "true"
63+
}
64+
}
65+
}
66+
},
67+
],
68+
[
69+
"ipv4",
70+
{
71+
"config_db": {
72+
"VLAN": {
73+
"Vlan1000": {
74+
"dhcp_servers": [
75+
"192.0.0.1",
76+
"192.0.0.2"
77+
]
78+
},
79+
"Vlan1001": {
80+
"vlanid": "1001"
81+
}
82+
}
83+
}
84+
}
85+
]
86+
]
87+
88+
MULTI_TEST_DATA = [
89+
[
90+
"ipv6",
91+
{
92+
"config_db": {
93+
"DHCP_RELAY": {
94+
"Vlan1000": {
95+
"dhcpv6_servers": [
96+
"fc02:2000::1",
97+
"fc02:2000::2"
98+
],
99+
"dhcpv6_option|rfc6939_support": "true"
100+
},
101+
"Vlan1001": {
102+
"dhcpv6_servers": [
103+
"fc02:2000::3",
104+
"fc02:2000::4"
105+
],
106+
"dhcpv6_option|rfc6939_support": "true"
107+
}
108+
}
109+
}
110+
},
111+
],
112+
[
113+
"ipv4",
114+
{
115+
"config_db": {
116+
"VLAN": {
117+
"Vlan1000": {
118+
"dhcp_servers": [
119+
"192.0.0.1",
120+
"192.0.0.2"
121+
]
122+
},
123+
"Vlan1001": {
124+
"vlanid": "1001",
125+
"dhcp_servers": [
126+
"192.0.0.3",
127+
"192.0.0.4"
128+
]
129+
}
130+
}
131+
}
132+
}
133+
]
18134
]
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,155 @@
1-
import os
1+
import pytest
22
import sys
3-
import traceback
3+
import os
4+
sys.path.append('../cli/show/plugins/')
5+
import show_dhcp_relay as show
6+
import show.vlan as vlan
7+
from swsscommon import swsscommon
8+
from mock_config import COMMON_TEST_DATA, NEW_ADDED_TEST_DATA, MULTI_TEST_DATA
9+
from parameterized import parameterized
10+
from pyfakefs.fake_filesystem_unittest import patchfs
411
from unittest import mock
512

6-
from click.testing import CliRunner
13+
try:
14+
sys.path.insert(0, '../../../src/sonic-host-services/tests/common')
15+
from mock_configdb import MockConfigDb
16+
swsscommon.ConfigDBConnector = MockConfigDb
17+
except KeyError:
18+
pass
19+
20+
expected_ipv6_table_with_header = """\
21+
+-------------+----------------------+
22+
| Interface | DHCP Relay Address |
23+
+=============+======================+
24+
| Vlan1000 | fc02:2000::1 |
25+
| | fc02:2000::2 |
26+
+-------------+----------------------+
27+
"""
28+
29+
expected_ipv4_table_with_header = """\
30+
+-------------+----------------------+
31+
| Interface | DHCP Relay Address |
32+
+=============+======================+
33+
| Vlan1000 | 192.0.0.1 |
34+
| | 192.0.0.2 |
35+
+-------------+----------------------+
36+
"""
37+
38+
expected_ipv6_table_without_header = """\
39+
-------- ------------
40+
Vlan1000 fc02:2000::1
41+
fc02:2000::2
42+
-------- ------------
43+
"""
44+
45+
expected_ipv6_table_multi_with_header = """\
46+
+-------------+----------------------+
47+
| Interface | DHCP Relay Address |
48+
+=============+======================+
49+
| Vlan1000 | fc02:2000::1 |
50+
| | fc02:2000::2 |
51+
+-------------+----------------------+
52+
| Vlan1001 | fc02:2000::3 |
53+
| | fc02:2000::4 |
54+
+-------------+----------------------+
55+
"""
56+
57+
expected_ipv4_table_multi_with_header = """\
58+
+-------------+----------------------+
59+
| Interface | DHCP Relay Address |
60+
+=============+======================+
61+
| Vlan1000 | 192.0.0.1 |
62+
| | 192.0.0.2 |
63+
+-------------+----------------------+
64+
| Vlan1001 | 192.0.0.3 |
65+
| | 192.0.0.4 |
66+
+-------------+----------------------+
67+
"""
68+
69+
DBCONFIG_PATH = '/var/run/redis/sonic-db/database_config.json'
70+
71+
IP_VER_TEST_PARAM_MAP = {
72+
"ipv4": {
73+
"entry": "dhcp_servers",
74+
"table": "VLAN"
75+
},
76+
"ipv6": {
77+
"entry": "dhcpv6_servers",
78+
"table": "DHCP_RELAY"
79+
}
80+
}
81+
82+
83+
def test_plugin_registration():
84+
cli = mock.MagicMock()
85+
show.register(cli)
86+
assert 'DHCP Helper Address' in dict(vlan.VlanBrief.COLUMNS)
87+
88+
89+
def test_dhcp_relay_column_output():
90+
ctx = (
91+
({'Vlan1001': {'dhcp_servers': ['192.0.0.1', '192.168.0.2']}}, {}, {}),
92+
(),
93+
)
94+
assert show.get_dhcp_helper_address(ctx, 'Vlan1001') == '192.0.0.1\n192.168.0.2'
795

8-
import show.vlan as vlan
9-
from utilities_common.db import Db
1096

11-
sys.path.insert(0, '../cli/show/plugins/')
12-
import show_dhcp_relay
97+
@parameterized.expand(COMMON_TEST_DATA)
98+
@patchfs
99+
def test_show_dhcp_relay(test_name, test_data, fs):
100+
if not os.path.exists(DBCONFIG_PATH):
101+
fs.create_file(DBCONFIG_PATH)
102+
MockConfigDb.set_config_db(test_data["config_db"])
103+
config_db = MockConfigDb()
104+
ip_version = "ipv4" if "ipv4" in test_name else "ipv6"
105+
table = config_db.get_table(IP_VER_TEST_PARAM_MAP[ip_version]["table"])
106+
if test_name == "ipv4_with_header":
107+
result = show.get_dhcp_relay_data_with_header(table, IP_VER_TEST_PARAM_MAP[ip_version]["entry"])
108+
expected_output = expected_ipv4_table_with_header
109+
elif test_name == "ipv6_with_header":
110+
result = show.get_dhcp_relay_data_with_header(table, IP_VER_TEST_PARAM_MAP[ip_version]["entry"])
111+
expected_output = expected_ipv6_table_with_header
112+
elif test_name == "ipv6_without_header":
113+
result = show.get_data(table, "Vlan1000")
114+
expected_output = expected_ipv6_table_without_header
115+
assert result == expected_output
13116

14117

15-
class TestVlanDhcpRelay(object):
16-
def test_plugin_registration(self):
17-
cli = mock.MagicMock()
18-
show_dhcp_relay.register(cli)
19-
assert 'DHCP Helper Address' in dict(vlan.VlanBrief.COLUMNS)
118+
@parameterized.expand(NEW_ADDED_TEST_DATA)
119+
@patchfs
120+
def test_show_new_added_dhcp_relay(test_name, test_data, fs):
121+
if not os.path.exists(DBCONFIG_PATH):
122+
fs.create_file(DBCONFIG_PATH)
123+
MockConfigDb.set_config_db(test_data["config_db"])
124+
config_db = MockConfigDb()
125+
ip_version = test_name
126+
table = config_db.get_table(IP_VER_TEST_PARAM_MAP[ip_version]["table"])
127+
if ip_version == "ipv4":
128+
result = show.get_dhcp_relay_data_with_header(table, IP_VER_TEST_PARAM_MAP[ip_version]["entry"])
129+
expected_output = expected_ipv4_table_with_header
130+
assert result == expected_output
131+
else:
132+
result = show.get_dhcp_relay_data_with_header(table, IP_VER_TEST_PARAM_MAP[ip_version]["entry"])
133+
expected_output = expected_ipv6_table_with_header
134+
assert result == expected_output
20135

21-
def test_dhcp_relay_column_output(self):
22-
ctx = (
23-
({'Vlan100': {'dhcp_servers': ['192.0.0.1', '192.168.0.2']}}, {}, {}),
24-
(),
25-
)
26-
assert show_dhcp_relay.get_dhcp_helper_address(ctx, 'Vlan100') == '192.0.0.1\n192.168.0.2'
136+
result = show.get_data(table, "Vlan1001")
137+
expected_output = ""
138+
assert result == expected_output
27139

28140

141+
@parameterized.expand(MULTI_TEST_DATA)
142+
@patchfs
143+
def test_show_multi_dhcp_relay(test_name, test_data, fs):
144+
if not os.path.exists(DBCONFIG_PATH):
145+
fs.create_file(DBCONFIG_PATH)
146+
MockConfigDb.set_config_db(test_data["config_db"])
147+
config_db = MockConfigDb()
148+
ip_version = test_name
149+
table = config_db.get_table(IP_VER_TEST_PARAM_MAP[ip_version]["table"])
150+
result = show.get_dhcp_relay_data_with_header(table, IP_VER_TEST_PARAM_MAP[ip_version]["entry"])
151+
if ip_version == "ipv4":
152+
expected_output = expected_ipv4_table_multi_with_header
153+
else:
154+
expected_output = expected_ipv6_table_multi_with_header
155+
assert result == expected_output

dockers/docker-dhcp-relay/cli-plugin-tests/test_show_dhcpv6_helper.py

-41
This file was deleted.

0 commit comments

Comments
 (0)