Skip to content

Commit

Permalink
Add mock config db
Browse files Browse the repository at this point in the history
  • Loading branch information
kellyyeh committed Sep 21, 2021
1 parent ca16c7a commit 9e6e1ed
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 20 deletions.
17 changes: 17 additions & 0 deletions dockers/docker-dhcp-relay/cli-plugin-tests/mock_config.py
Original file line number Diff line number Diff line change
@@ -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"
}
}
},
},
],
]
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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

22 changes: 14 additions & 8 deletions dockers/docker-dhcp-relay/cli/show/plugins/show_dhcpv6_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions sonic-slave-buster/Dockerfile.j2
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
4 changes: 4 additions & 0 deletions sonic-slave-stretch/Dockerfile.j2
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 9e6e1ed

Please sign in to comment.