test dhcp acl (#8359)

This commit is contained in:
trzhang-msft 2021-08-09 11:20:26 -07:00 committed by GitHub
parent cf73e22d52
commit 62347850be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 221 additions and 4 deletions

View File

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

View File

@ -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)

View File

@ -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,
},
],
]

View File

@ -5,7 +5,7 @@ class MockConfigDb(object):
STATE_DB = None
CONFIG_DB = None
def __init__(self):
def __init__(self, **kwargs):
pass
@staticmethod

View File

@ -10,7 +10,7 @@ from swsscommon import swsscommon
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

View File

@ -7,7 +7,7 @@ from sonic_py_common.general import load_module_from_source
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