diff --git a/dockers/docker-dhcp-relay/cli-plugin-tests/mock_config.py b/dockers/docker-dhcp-relay/cli-plugin-tests/mock_config.py new file mode 100644 index 000000000000..3279457f4339 --- /dev/null +++ b/dockers/docker-dhcp-relay/cli-plugin-tests/mock_config.py @@ -0,0 +1,17 @@ +from unittest.mock import call + +TEST_DATA = [ + [ + "DHCPv6_Helpers", + { + "config_db": { + "DHCP_RELAY": { + "Vlan1000": { + "dhcpv6_servers": "fc02:2000::1,fc02:2000::2", + "dhcpv6_option|rfc6939_support": "true" + } + } + }, + }, + ], +] \ No newline at end of file diff --git a/dockers/docker-dhcp-relay/cli-plugin-tests/test_show_dhcpv6_helper.py b/dockers/docker-dhcp-relay/cli-plugin-tests/test_show_dhcpv6_helper.py index 0ea3552230e5..aab587ecaef6 100644 --- a/dockers/docker-dhcp-relay/cli-plugin-tests/test_show_dhcpv6_helper.py +++ b/dockers/docker-dhcp-relay/cli-plugin-tests/test_show_dhcpv6_helper.py @@ -1,18 +1,18 @@ import sys import os -from unittest import mock +import pytest sys.path.append('../cli/show/plugins/') import show_dhcpv6_helper as show from click.testing import CliRunner +from swsscommon import swsscommon +from mock_config import TEST_DATA +from parameterized import parameterized +from pyfakefs.fake_filesystem_unittest import patchfs try: - modules_path = os.path.join(os.path.dirname(__file__), "../../../src/sonic-utilities") - test_path = os.path.join(modules_path, "tests") - mock_table_path = os.path.join(test_path, "mock_tables") - sys.path.insert(0, modules_path) - sys.path.insert(0, test_path) - sys.path.insert(0, mock_table_path) - import dbconnector + sys.path.insert(0, '../../../src/sonic-host-services/tests/common') + from mock_configdb import MockConfigDb + swsscommon.ConfigDBConnector = MockConfigDb except KeyError: pass @@ -21,12 +21,20 @@ Vlan1000 fc02:2000::1 fc02:2000::2 -------- ------------ - """ + +DBCONFIG_PATH = '/var/run/redis/sonic-db/database_config.json' + class TestDhcpRelayHelper(object): - def test_show_dhcpv6_helper(self): + @parameterized.expand(TEST_DATA) + @patchfs + def test_show_dhcpv6_helper(self, 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"]) runner = CliRunner() - result = runner.invoke(show.dhcp_relay_helper.commands["ipv6"], []) - assert result.output == expected_table + table = MockConfigDb.get_table(self, "DHCP_RELAY") + result = show.get_data(table, "Vlan1000") + assert result == expected_table diff --git a/dockers/docker-dhcp-relay/cli/show/plugins/show_dhcpv6_helper.py b/dockers/docker-dhcp-relay/cli/show/plugins/show_dhcpv6_helper.py index 684b3625f659..f22907df294d 100644 --- a/dockers/docker-dhcp-relay/cli/show/plugins/show_dhcpv6_helper.py +++ b/dockers/docker-dhcp-relay/cli/show/plugins/show_dhcpv6_helper.py @@ -7,6 +7,7 @@ import utilities_common.cli as clicommon DHCP_RELAY = 'DHCP_RELAY' +config_db = ConfigDBConnector() @click.group(cls=clicommon.AliasedGroup, name="dhcp_relay_helper") def dhcp_relay_helper(): @@ -16,20 +17,25 @@ def dhcp_relay_helper(): @dhcp_relay_helper.command('ipv6') def get_dhcpv6_helper_address(): """Parse through DHCP_RELAY table for each interface in config_db.json and print dhcpv6 helpers in table format""" - config_db = ConfigDBConnector() if config_db is not None: config_db.connect() table_data = config_db.get_table(DHCP_RELAY) if table_data is not None: vlans = config_db.get_keys(DHCP_RELAY) for vlan in vlans: - vlan_data = table_data.get(vlan) - helpers_data = vlan_data.get('dhcpv6_servers') - if helpers_data is not None: - addr = {vlan:[]} - for ip in helpers_data.split(','): - addr[vlan].append(ip) - print(tabulate({'Interface':[vlan], vlan:addr.get(vlan)}, tablefmt='simple', stralign='right') + '\n') + output = get_data(table_data, vlan) + print(output) + + +def get_data(table_data, vlan): + vlan_data = table_data.get(vlan) + helpers_data = vlan_data.get('dhcpv6_servers') + if helpers_data is not None: + addr = {vlan:[]} + for ip in helpers_data.split(','): + addr[vlan].append(ip) + output = tabulate({'Interface':[vlan], vlan:addr.get(vlan)}, tablefmt='simple', stralign='right') + '\n' + return output def register(cli): cli.add_command(dhcp_relay_helper) diff --git a/sonic-slave-buster/Dockerfile.j2 b/sonic-slave-buster/Dockerfile.j2 index bbd68a31396e..f23c1aeb8c17 100644 --- a/sonic-slave-buster/Dockerfile.j2 +++ b/sonic-slave-buster/Dockerfile.j2 @@ -486,6 +486,10 @@ EXPOSE 22 RUN git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git /usr/share/depot_tools ENV PATH /usr/share/depot_tools:$PATH +# Install dependencies for dhcp relay test +RUN pip3 install parameterized==0.8.1 +RUN pip3 install pyfakefs + # Install docker engine 17.03.2~ce-0 inside docker and enable experimental feature RUN apt-get update RUN apt-get install -y \ diff --git a/sonic-slave-stretch/Dockerfile.j2 b/sonic-slave-stretch/Dockerfile.j2 index cc45afbc6f82..e077c69e01b3 100644 --- a/sonic-slave-stretch/Dockerfile.j2 +++ b/sonic-slave-stretch/Dockerfile.j2 @@ -291,6 +291,10 @@ RUN apt-get update && apt-get install -y \ python-lxml \ libexpat1-dev +# Install dependencies for dhcp relay test +RUN pip3 install parameterized==0.8.1 +RUN pip3 install pyfakefs + ## Config dpkg ## install the configuration file if it’s currently missing RUN sudo augtool --autosave "set /files/etc/dpkg/dpkg.cfg/force-confmiss"