|
1 |
| -import os |
| 1 | +import pytest |
2 | 2 | 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 |
4 | 11 | from unittest import mock
|
5 | 12 |
|
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' |
7 | 95 |
|
8 |
| -import show.vlan as vlan |
9 |
| -from utilities_common.db import Db |
10 | 96 |
|
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 |
13 | 116 |
|
14 | 117 |
|
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 |
20 | 135 |
|
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 |
27 | 139 |
|
28 | 140 |
|
| 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 |
0 commit comments