Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/sonic-host-services/pytest.ini
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[pytest]
addopts = --cov=scripts --cov-report html --cov-report term --cov-report xml --ignore=tests/hostcfgd/test_vectors.py --ignore=tests/hostcfgd/test_radius_vectors.py
addopts = --cov=scripts --cov-report html --cov-report term --cov-report xml --ignore=tests/hostcfgd/test_vectors.py --ignore=tests/hostcfgd/test_radius_vectors.py --ignore=tests/caclmgrd/test_dhcp_vectors.py
Empty file.
54 changes: 54 additions & 0 deletions src/sonic-host-services/tests/caclmgrd/caclmgrd_dhcp_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import os
import sys
import swsscommon

from parameterized import parameterized
from sonic_py_common.general import load_module_from_source
from unittest import TestCase, mock
from pyfakefs.fake_filesystem_unittest import patchfs

from .test_dhcp_vectors import CACLMGRD_DHCP_TEST_VECTOR
from tests.common.mock_configdb import MockConfigDb


DBCONFIG_PATH = '/var/run/redis/sonic-db/database_config.json'


swsscommon.swsscommon.ConfigDBConnector = MockConfigDb
test_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
modules_path = os.path.dirname(test_path)
scripts_path = os.path.join(modules_path, "scripts")
sys.path.insert(0, modules_path)
caclmgrd_path = os.path.join(scripts_path, 'caclmgrd')
caclmgrd = load_module_from_source('caclmgrd', caclmgrd_path)


class TestCaclmgrdDhcp(TestCase):
"""
Test caclmgrd dhcp
"""
@parameterized.expand(CACLMGRD_DHCP_TEST_VECTOR)
@patchfs
def test_caclmgrd_dhcp(self, test_name, test_data, fs):
if not os.path.exists(DBCONFIG_PATH):
fs.create_file(DBCONFIG_PATH) # fake database_config.json

MockConfigDb.set_config_db(test_data["config_db"])

with mock.patch("caclmgrd.subprocess") as mocked_subprocess:
popen_mock = mock.Mock()
popen_attrs = test_data["popen_attributes"]
popen_mock.configure_mock(**popen_attrs)
mocked_subprocess.Popen.return_value = popen_mock

call_mock = mock.Mock()
call_rc = test_data["call_rc"]
mocked_subprocess.call.return_value = call_rc

caclmgrd_daemon = caclmgrd.ControlPlaneAclManager("caclmgrd")
mux_update = test_data["mux_update"]

for key,data in mux_update:
caclmgrd_daemon.update_dhcp_acl(key, '', data)

mocked_subprocess.call.assert_has_calls(test_data["expected_subprocess_calls"], any_order=False)
163 changes: 163 additions & 0 deletions src/sonic-host-services/tests/caclmgrd/test_dhcp_vectors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
from unittest.mock import call

"""
caclmgrd dhcp test vector
"""
CACLMGRD_DHCP_TEST_VECTOR = [
[
"Active_Present",
{
"config_db": {
"DEVICE_METADATA": {
"localhost": {
"subtype": "DualToR",
"type": "ToRRouter",
}
},
},
"mux_update": [
("Ethernet4", {"state": "active"}),
("Ethernet8", {"state": "active"}),
],
"expected_subprocess_calls": [
call("iptables --check DHCP -m physdev --physdev-in Ethernet4 -j DROP", shell=True),
call("iptables --delete DHCP -m physdev --physdev-in Ethernet4 -j DROP", shell=True),
call("iptables --check DHCP -m physdev --physdev-in Ethernet8 -j DROP", shell=True),
call("iptables --delete DHCP -m physdev --physdev-in Ethernet8 -j DROP", shell=True),
],
"popen_attributes": {
'communicate.return_value': ('output', 'error'),
},
"call_rc": 0,
},
],
[
"Active_Absent",
{
"config_db": {
"DEVICE_METADATA": {
"localhost": {
"subtype": "DualToR",
"type": "ToRRouter",
}
},
},
"mux_update": [
("Ethernet4", {"state": "active"}),
("Ethernet8", {"state": "active"}),
],
"expected_subprocess_calls": [
call("iptables --check DHCP -m physdev --physdev-in Ethernet4 -j DROP", shell=True),
call("iptables --check DHCP -m physdev --physdev-in Ethernet8 -j DROP", shell=True),
],
"popen_attributes": {
'communicate.return_value': ('output', 'error'),
},
"call_rc": 1,
},
],
[
"Standby_Present",
{
"config_db": {
"DEVICE_METADATA": {
"localhost": {
"subtype": "DualToR",
"type": "ToRRouter",
}
},
},
"mux_update": [
("Ethernet4", {"state": "standby"}),
("Ethernet8", {"state": "standby"}),
],
"expected_subprocess_calls": [
call("iptables --check DHCP -m physdev --physdev-in Ethernet4 -j DROP", shell=True),
call("iptables --check DHCP -m physdev --physdev-in Ethernet8 -j DROP", shell=True),
],
"popen_attributes": {
'communicate.return_value': ('output', 'error'),
},
"call_rc": 0,
},
],
[
"Standby_Absent",
{
"config_db": {
"DEVICE_METADATA": {
"localhost": {
"subtype": "DualToR",
"type": "ToRRouter",
}
},
},
"mux_update": [
("Ethernet4", {"state": "standby"}),
("Ethernet8", {"state": "standby"}),
],
"expected_subprocess_calls": [
call("iptables --check DHCP -m physdev --physdev-in Ethernet4 -j DROP", shell=True),
call("iptables --insert DHCP -m physdev --physdev-in Ethernet4 -j DROP", shell=True),
call("iptables --check DHCP -m physdev --physdev-in Ethernet8 -j DROP", shell=True),
call("iptables --insert DHCP -m physdev --physdev-in Ethernet8 -j DROP", shell=True),
],
"popen_attributes": {
'communicate.return_value': ('output', 'error'),
},
"call_rc": 1,
},
],
[
"Unknown_Present",
{
"config_db": {
"DEVICE_METADATA": {
"localhost": {
"subtype": "DualToR",
"type": "ToRRouter",
}
},
},
"mux_update": [
("Ethernet4", {"state": "unknown"}),
("Ethernet8", {"state": "unknown"}),
],
"expected_subprocess_calls": [
call("iptables --check DHCP -m physdev --physdev-in Ethernet4 -j DROP", shell=True),
call("iptables --delete DHCP -m physdev --physdev-in Ethernet4 -j DROP", shell=True),
call("iptables --check DHCP -m physdev --physdev-in Ethernet8 -j DROP", shell=True),
call("iptables --delete DHCP -m physdev --physdev-in Ethernet8 -j DROP", shell=True),
],
"popen_attributes": {
'communicate.return_value': ('output', 'error'),
},
"call_rc": 0,
},
],
[
"Uknown_Absent",
{
"config_db": {
"DEVICE_METADATA": {
"localhost": {
"subtype": "DualToR",
"type": "ToRRouter",
}
},
},
"mux_update": [
("Ethernet4", {"state": "unknown"}),
("Ethernet8", {"state": "unknown"}),
],
"expected_subprocess_calls": [
call("iptables --check DHCP -m physdev --physdev-in Ethernet4 -j DROP", shell=True),
call("iptables --check DHCP -m physdev --physdev-in Ethernet8 -j DROP", shell=True),
],
"popen_attributes": {
'communicate.return_value': ('output', 'error'),
},
"call_rc": 1,
},
],
]
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class MockConfigDb(object):
STATE_DB = None
CONFIG_DB = None

def __init__(self):
def __init__(self, **kwargs):
pass

@staticmethod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from parameterized import parameterized
from unittest import TestCase, mock
from tests.hostcfgd.test_radius_vectors import HOSTCFGD_TEST_RADIUS_VECTOR
from tests.hostcfgd.mock_configdb import MockConfigDb
from tests.common.mock_configdb import MockConfigDb


swsscommon.ConfigDBConnector = MockConfigDb
Expand Down
2 changes: 1 addition & 1 deletion src/sonic-host-services/tests/hostcfgd/hostcfgd_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from unittest import TestCase, mock

from .test_vectors import HOSTCFGD_TEST_VECTOR
from .mock_configdb import MockConfigDb
from tests.common.mock_configdb import MockConfigDb

from pyfakefs.fake_filesystem_unittest import patchfs

Expand Down