4
4
5
5
from parameterized import parameterized
6
6
from sonic_py_common .general import load_module_from_source
7
- from ipaddress import IPv4Address
7
+ from ipaddress import IPv4Address , IPv4Network
8
8
from unittest import TestCase , mock
9
9
from pyfakefs .fake_filesystem_unittest import patchfs
10
10
11
- from .test_soc_rules_vectors import CACLMGRD_SOC_TEST_VECTOR
11
+ from .test_soc_rules_vectors import CACLMGRD_SOC_TEST_VECTOR , CACLMGRD_SOC_TEST_VECTOR_EMPTY
12
12
from tests .common .mock_configdb import MockConfigDb
13
13
from unittest .mock import MagicMock , patch
14
14
@@ -29,7 +29,7 @@ def setUp(self):
29
29
30
30
@parameterized .expand (CACLMGRD_SOC_TEST_VECTOR )
31
31
@patchfs
32
- @patch ('caclmgrd.get_ip_from_interface_table ' , MagicMock (return_value = " 10.10.10.10" ))
32
+ @patch ('caclmgrd.get_ipv4_networks_from_interface_table ' , MagicMock (return_value = [ IPv4Network ( ' 10.10.10.18/24' , strict = False ), IPv4Network ( '10.10.11.18/24' , strict = False )] ))
33
33
def test_caclmgrd_soc (self , test_name , test_data , fs ):
34
34
if not os .path .exists (DBCONFIG_PATH ):
35
35
fs .create_file (DBCONFIG_PATH ) # fake database_config.json
@@ -50,11 +50,60 @@ def test_caclmgrd_soc(self, test_name, test_data, fs):
50
50
caclmgrd_daemon .update_control_plane_nat_acls ('' , {}, MockConfigDb ())
51
51
mocked_subprocess .Popen .assert_has_calls (test_data ["expected_subprocess_calls" ], any_order = True )
52
52
53
- def test_get_ip_from_interface_table (self ):
53
+
54
+ @parameterized .expand (CACLMGRD_SOC_TEST_VECTOR_EMPTY )
55
+ @patchfs
56
+ @patch ('caclmgrd.get_ipv4_networks_from_interface_table' , MagicMock (return_value = []))
57
+ def test_caclmgrd_soc_no_ips (self , test_name , test_data , fs ):
58
+ if not os .path .exists (DBCONFIG_PATH ):
59
+ fs .create_file (DBCONFIG_PATH ) # fake database_config.json
60
+
61
+ MockConfigDb .set_config_db (test_data ["config_db" ])
62
+
63
+ with mock .patch ("caclmgrd.subprocess" ) as mocked_subprocess :
64
+ popen_mock = mock .Mock ()
65
+ popen_attrs = test_data ["popen_attributes" ]
66
+ popen_mock .configure_mock (** popen_attrs )
67
+ mocked_subprocess .Popen .return_value = popen_mock
68
+ mocked_subprocess .PIPE = - 1
69
+
70
+ call_rc = test_data ["call_rc" ]
71
+ mocked_subprocess .call .return_value = call_rc
72
+
73
+ caclmgrd_daemon = self .caclmgrd .ControlPlaneAclManager ("caclmgrd" )
74
+ caclmgrd_daemon .update_control_plane_nat_acls ('' , {}, MockConfigDb ())
75
+ mocked_subprocess .Popen .assert_has_calls (test_data ["expected_subprocess_calls" ], any_order = True )
76
+
77
+
78
+ @parameterized .expand (CACLMGRD_SOC_TEST_VECTOR_EMPTY )
79
+ @patchfs
80
+ @patch ('caclmgrd.get_ipv4_networks_from_interface_table' , MagicMock (return_value = ['10.10.10.10' ]))
81
+ def test_caclmgrd_soc_ip_string (self , test_name , test_data , fs ):
82
+ if not os .path .exists (DBCONFIG_PATH ):
83
+ fs .create_file (DBCONFIG_PATH ) # fake database_config.json
84
+
85
+ MockConfigDb .set_config_db (test_data ["config_db" ])
86
+
87
+ with mock .patch ("caclmgrd.subprocess" ) as mocked_subprocess :
88
+ popen_mock = mock .Mock ()
89
+ popen_attrs = test_data ["popen_attributes" ]
90
+ popen_mock .configure_mock (** popen_attrs )
91
+ mocked_subprocess .Popen .return_value = popen_mock
92
+ mocked_subprocess .PIPE = - 1
93
+
94
+ call_rc = test_data ["call_rc" ]
95
+ mocked_subprocess .call .return_value = call_rc
96
+
97
+ caclmgrd_daemon = self .caclmgrd .ControlPlaneAclManager ("caclmgrd" )
98
+ caclmgrd_daemon .update_control_plane_nat_acls ('' , {}, MockConfigDb ())
99
+ mocked_subprocess .Popen .assert_has_calls (test_data ["expected_subprocess_calls" ], any_order = True )
100
+
101
+
102
+ def test_get_ipv4_networks_from_interface_table (self ):
54
103
if not os .path .exists (DBCONFIG_PATH ):
55
104
fs .create_file (DBCONFIG_PATH ) # fake database_config.json
56
105
57
106
table = {("Vlan1000" ,"10.10.10.1/32" ): "val" }
58
- ip_addr = self .caclmgrd .get_ip_from_interface_table (table , "Vlan" )
107
+ ip_addr = self .caclmgrd .get_ipv4_networks_from_interface_table (table , "Vlan" )
59
108
60
- assert (ip_addr == IPv4Address ('10.10.10.1' ) )
109
+ assert (ip_addr == [ IPv4Network ('10.10.10.1/32' )] )
0 commit comments