-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
caclmgrd: add test code to check dhcp acl update (#8509)
- add test code to check dhcp acl update - port #8359 (caclmgrd: add test code to check dhcp acl update) to 202012 branch
- Loading branch information
trzhang-msft
authored
Sep 15, 2021
1 parent
e486a2f
commit ce95f26
Showing
7 changed files
with
221 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
addopts = --cov=scripts --cov-report html --cov-report term --cov-report xml --ignore=tests/hostcfgd/test_vectors.py --ignore=tests/caclmgrd/test_dhcp_vectors.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,7 @@ | |
tests_require = [ | ||
'parameterized', | ||
'pytest', | ||
'pyfakefs', | ||
'sonic-py-common' | ||
], | ||
classifiers = [ | ||
|
Empty file.
54 changes: 54 additions & 0 deletions
54
src/sonic-host-services/tests/caclmgrd/caclmgrd_dhcp_test.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import os | ||
import sys | ||
|
||
from swsscommon 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.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
163
src/sonic-host-services/tests/caclmgrd/test_dhcp_vectors.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
], | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters