Skip to content

Commit 4c01273

Browse files
committed
Add header for show dhcp_relay
1 parent 2729f1e commit 4c01273

File tree

3 files changed

+77
-14
lines changed

3 files changed

+77
-14
lines changed

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

+33-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
TEST_DATA = [
22
[
3-
"ipv6",
3+
"ipv6_with_header",
44
{
55
"config_db": {
66
"DHCP_RELAY": {
@@ -16,7 +16,38 @@
1616
},
1717
],
1818
[
19-
"ipv4",
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+
"ipv4_without_header",
2051
{
2152
"config_db": {
2253
"VLAN": {

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

+32-5
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,32 @@
1717
except KeyError:
1818
pass
1919

20-
expected_ipv6_table = """\
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 = """\
2139
-------- ------------
2240
Vlan1000 fc02:2000::1
2341
fc02:2000::2
2442
-------- ------------
2543
"""
2644

27-
expected_ipv4_table = """\
45+
expected_ipv4_table_without_header = """\
2846
-------- ---------
2947
Vlan1000 192.0.0.1
3048
192.0.0.2
@@ -66,6 +84,15 @@ def test_show_dhcp_relay(test_name, test_data, fs):
6684
fs.create_file(DBCONFIG_PATH)
6785
MockConfigDb.set_config_db(test_data["config_db"])
6886
config_db = MockConfigDb()
69-
table = config_db.get_table(IP_VER_TEST_PARAM_MAP[test_name]["table"])
70-
result = show.get_data(table, "Vlan1000", IP_VER_TEST_PARAM_MAP[test_name]["entry"])
71-
assert result == (expected_ipv4_table if test_name == "ipv4" else expected_ipv6_table)
87+
ip_version = "ipv4" if "ipv4" in test_name else "ipv6"
88+
table = config_db.get_table(IP_VER_TEST_PARAM_MAP[ip_version]["table"])
89+
result = show.get_data(table, "Vlan1000", IP_VER_TEST_PARAM_MAP[ip_version]["entry"], "with_header" in test_name)
90+
if test_name == "ipv4_with_header":
91+
expected_output = expected_ipv4_table_with_header
92+
elif test_name == "ipv4_without_header":
93+
expected_output = expected_ipv4_table_without_header
94+
elif test_name == "ipv6_with_header":
95+
expected_output = expected_ipv6_table_with_header
96+
elif test_name == "ipv6_without_header":
97+
expected_output = expected_ipv6_table_without_header
98+
assert result == expected_output

dockers/docker-dhcp-relay/cli/show/plugins/show_dhcp_relay.py

+12-7
Original file line numberDiff line numberDiff line change
@@ -110,31 +110,36 @@ def dhcp_relay_helper():
110110
pass
111111

112112

113-
def get_dhcp_relay(table_name, entry_name):
113+
def get_dhcp_relay(table_name, entry_name, with_header):
114114
if config_db is not None:
115115
config_db.connect()
116116
table_data = config_db.get_table(table_name)
117117
if table_data is not None:
118118
vlans = config_db.get_keys(table_name)
119119
for vlan in vlans:
120-
output = get_data(table_data, vlan, entry_name)
120+
output = get_data(table_data, vlan, entry_name, with_header)
121121
print(output)
122122

123123

124124
@dhcp_relay_helper.command('ipv6')
125125
def get_dhcpv6_helper_address():
126126
"""Parse through DHCP_RELAY table for each interface in config_db.json and print dhcpv6 helpers in table format"""
127-
get_dhcp_relay(DHCP_RELAY, DHCPV6_SERVERS)
127+
get_dhcp_relay(DHCP_RELAY, DHCPV6_SERVERS, with_header=False)
128128

129129

130-
def get_data(table_data, vlan, entry_name):
130+
def get_data(table_data, vlan, entry_name, with_header=True):
131131
vlan_data = table_data.get(vlan)
132132
helpers_data = vlan_data.get(entry_name)
133133
if helpers_data is not None:
134134
addr = {vlan: []}
135135
for ip in helpers_data:
136136
addr[vlan].append(ip)
137-
output = tabulate({'Interface': [vlan], vlan: addr.get(vlan)}, tablefmt='simple', stralign='right') + '\n'
137+
138+
data = {'Interface': [vlan], 'DHCP Relay Address': ["\n".join(addr.get(vlan))]}
139+
if with_header:
140+
output = tabulate(data, tablefmt='grid', stralign='right', headers='keys') + '\n'
141+
else:
142+
output = tabulate(data, tablefmt='simple', stralign='right') + '\n'
138143
return output
139144

140145

@@ -156,12 +161,12 @@ def dhcp_relay_ipv4():
156161

157162
@dhcp_relay_ipv4.command("helper")
158163
def dhcp_relay_ipv4_destination():
159-
get_dhcp_relay(VLAN, DHCPV4_SERVERS)
164+
get_dhcp_relay(VLAN, DHCPV4_SERVERS, with_header=True)
160165

161166

162167
@dhcp_relay_ipv6.command("destination")
163168
def dhcp_relay_ipv6_destination():
164-
get_dhcp_relay(DHCP_RELAY, DHCPV6_SERVERS)
169+
get_dhcp_relay(DHCP_RELAY, DHCPV6_SERVERS, with_header=True)
165170

166171

167172
@dhcp_relay_ipv6.command("counters")

0 commit comments

Comments
 (0)