Skip to content

Commit e83a858

Browse files
authored
Merge pull request #4 from kamelnetworks/acl-ip2me-test
*[caclmgrd]: Tests for IP2ME rules generation
2 parents 709046b + f5a2e50 commit e83a858

File tree

3 files changed

+173
-1
lines changed

3 files changed

+173
-1
lines changed

tests/caclmgrd/caclmgrd_ip2me_test.py

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import os
2+
import sys
3+
4+
from swsscommon import swsscommon
5+
from parameterized import parameterized
6+
from sonic_py_common.general import load_module_from_source
7+
from unittest import TestCase, mock
8+
from pyfakefs.fake_filesystem_unittest import patchfs
9+
10+
from .test_ip2me_vectors import CACLMGRD_IP2ME_TEST_VECTOR
11+
from tests.common.mock_configdb import MockConfigDb
12+
13+
14+
DBCONFIG_PATH = '/var/run/redis/sonic-db/database_config.json'
15+
16+
17+
class TestCaclmgrdIP2Me(TestCase):
18+
"""
19+
Test caclmgrd IP2Me
20+
"""
21+
def setUp(self):
22+
swsscommon.ConfigDBConnector = MockConfigDb
23+
test_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
24+
modules_path = os.path.dirname(test_path)
25+
scripts_path = os.path.join(modules_path, "scripts")
26+
sys.path.insert(0, modules_path)
27+
caclmgrd_path = os.path.join(scripts_path, 'caclmgrd')
28+
self.caclmgrd = load_module_from_source('caclmgrd', caclmgrd_path)
29+
self.maxDiff = None
30+
31+
@parameterized.expand(CACLMGRD_IP2ME_TEST_VECTOR)
32+
@patchfs
33+
def test_caclmgrd_ip2me(self, test_name, test_data, fs):
34+
if not os.path.exists(DBCONFIG_PATH):
35+
fs.create_file(DBCONFIG_PATH) # fake database_config.json
36+
37+
MockConfigDb.set_config_db(test_data["config_db"])
38+
self.caclmgrd.ControlPlaneAclManager.get_namespace_mgmt_ip = mock.MagicMock()
39+
self.caclmgrd.ControlPlaneAclManager.get_namespace_mgmt_ipv6 = mock.MagicMock()
40+
caclmgrd_daemon = self.caclmgrd.ControlPlaneAclManager("caclmgrd")
41+
ret = caclmgrd_daemon.generate_block_ip2me_traffic_iptables_commands('')
42+
self.assertListEqual(test_data["return"], ret)

tests/caclmgrd/test_ip2me_vectors.py

+127
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
from unittest.mock import call
2+
3+
"""
4+
caclmgrd ip2me block test vector
5+
"""
6+
CACLMGRD_IP2ME_TEST_VECTOR = [
7+
[
8+
"Only MGMT interface - default rules",
9+
{
10+
"config_db": {
11+
"MGMT_INTERFACE": {
12+
"eth0|172.18.0.100/24": {
13+
"gwaddr": "172.18.0.1"
14+
}
15+
},
16+
"LOOPBACK_INTERFACE": {},
17+
"VLAN_INTERFACE": {},
18+
"PORTCHANNEL_INTERFACE": {},
19+
"INTERFACE": {},
20+
"DEVICE_METADATA": {
21+
"localhost": {
22+
}
23+
},
24+
"FEATURE": {},
25+
},
26+
"return": [
27+
"iptables -A INPUT -d 172.18.0.0/32 -j DROP"
28+
],
29+
},
30+
],
31+
[
32+
"Layer-3 loopback interfaces - block access",
33+
{
34+
"config_db": {
35+
"LOOPBACK_INTERFACE": {
36+
"Loopback0|10.10.10.10/32": {},
37+
},
38+
"VLAN_INTERFACE": {},
39+
"PORTCHANNEL_INTERFACE": {
40+
"PortChannel0001|10.10.11.10/32": {},
41+
},
42+
"INTERFACE": {
43+
"Ethernet0|10.10.12.10/32": {}
44+
},
45+
"MGMT_INTERFACE": {
46+
"eth0|172.18.0.100/24": {
47+
"gwaddr": "172.18.0.1"
48+
}
49+
},
50+
"DEVICE_METADATA": {
51+
"localhost": {
52+
}
53+
},
54+
"FEATURE": {},
55+
},
56+
"return": [
57+
"iptables -A INPUT -d 10.10.10.10/32 -j DROP",
58+
"iptables -A INPUT -d 172.18.0.0/32 -j DROP",
59+
"iptables -A INPUT -d 10.10.11.10/32 -j DROP",
60+
"iptables -A INPUT -d 10.10.12.10/32 -j DROP",
61+
],
62+
},
63+
],
64+
[
65+
"One VLAN interface, /24, we are .1",
66+
{
67+
"config_db": {
68+
"MGMT_INTERFACE": {
69+
"eth0|172.18.0.100/24": {
70+
"gwaddr": "172.18.0.1"
71+
}
72+
},
73+
"LOOPBACK_INTERFACE": {},
74+
"VLAN_INTERFACE": {
75+
"Vlan110|10.10.11.1/24": {},
76+
},
77+
"PORTCHANNEL_INTERFACE": {},
78+
"INTERFACE": {},
79+
"DEVICE_METADATA": {
80+
"localhost": {
81+
}
82+
},
83+
"FEATURE": {},
84+
},
85+
"return": [
86+
"iptables -A INPUT -d 172.18.0.0/32 -j DROP",
87+
"iptables -A INPUT -d 10.10.11.1/32 -j DROP",
88+
],
89+
},
90+
],
91+
[
92+
"One interface of each type, IPv6, /64 - block all interfaces but MGMT",
93+
{
94+
"config_db": {
95+
"LOOPBACK_INTERFACE": {
96+
"Loopback0|2001:db8:10::/64": {},
97+
},
98+
"VLAN_INTERFACE": {
99+
"Vlan110|2001:db8:11::/64": {},
100+
},
101+
"PORTCHANNEL_INTERFACE": {
102+
"PortChannel0001|2001:db8:12::/64": {},
103+
},
104+
"INTERFACE": {
105+
"Ethernet0|2001:db8:13::/64": {}
106+
},
107+
"MGMT_INTERFACE": {
108+
"eth0|2001:db8:200::200/64": {
109+
"gwaddr": "2001:db8:200::100"
110+
}
111+
},
112+
"DEVICE_METADATA": {
113+
"localhost": {
114+
}
115+
},
116+
"FEATURE": {},
117+
},
118+
"return": [
119+
"ip6tables -A INPUT -d 2001:db8:10::/128 -j DROP",
120+
"ip6tables -A INPUT -d 2001:db8:200::/128 -j DROP",
121+
"ip6tables -A INPUT -d 2001:db8:11::1/128 -j DROP",
122+
"ip6tables -A INPUT -d 2001:db8:12::/128 -j DROP",
123+
"ip6tables -A INPUT -d 2001:db8:13::/128 -j DROP"
124+
],
125+
},
126+
]
127+
]

tests/common/mock_configdb.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ def set_entry(self, key, field, data):
4343
MockConfigDb.CONFIG_DB[key][field] = data
4444

4545
def get_table(self, table_name):
46-
return MockConfigDb.CONFIG_DB[table_name]
46+
data = {}
47+
for k, v in MockConfigDb.CONFIG_DB[table_name].items():
48+
data[self.deserialize_key(k)] = v
49+
return data
4750

4851
def subscribe(self, table_name, callback):
4952
self.handlers[table_name] = callback

0 commit comments

Comments
 (0)