Skip to content

Commit 0c455c1

Browse files
committed
Fix test failures: MockConfigDb init, caclmgrd mock targets, and xdist compat
- Change MockConfigDb.CONFIG_DB default from None to {} to prevent TypeError in setUp methods that assign to CONFIG_DB[table] - Fix caclmgrd test files to use correct swsscommon import: change "import swsscommon" to "from swsscommon import swsscommon" so swsscommon.ConfigDBConnector correctly resolves to the submodule - Add missing ConfigDBConnector mock in generate_internal_docker_ip and namespace_docker_ip tests - Add tests/caclmgrd/conftest.py with autouse fixture that initializes DEVICE_METADATA and FEATURE tables in MockConfigDb.CONFIG_DB, required by ControlPlaneAclManager.__init__ Fixes 18 of 24 test failures (remaining 6 are pre-existing SWIG type mismatches in ldap/radius/tacacs tests). Signed-off-by: Tamer Ahmed <tamerahmed@microsoft.com>
1 parent 862cbd8 commit 0c455c1

10 files changed

+33
-12
lines changed

tests/caclmgrd/caclmgrd_bgp_loopback1_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import os
22
import sys
3-
import swsscommon
3+
from swsscommon import swsscommon
44

55
from parameterized import parameterized
66
from sonic_py_common.general import load_module_from_source
@@ -18,7 +18,7 @@ class TestCaclmgrdLoopback1Drop(TestCase):
1818
Test caclmgrd soc
1919
"""
2020
def setUp(self):
21-
swsscommon.swsscommon.ConfigDBConnector = MockConfigDb
21+
swsscommon.ConfigDBConnector = MockConfigDb
2222
test_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
2323
modules_path = os.path.dirname(test_path)
2424
scripts_path = os.path.join(modules_path, "scripts")

tests/caclmgrd/caclmgrd_dhcp_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import os
22
import sys
3-
import swsscommon
3+
from swsscommon import swsscommon
44

55
from parameterized import parameterized
66
from sonic_py_common.general import load_module_from_source

tests/caclmgrd/caclmgrd_feature_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import os
22
import sys
3-
import swsscommon
3+
from swsscommon import swsscommon
44

55
from parameterized import parameterized
66
from sonic_py_common.general import load_module_from_source
@@ -18,7 +18,7 @@ class TestFeature(TestCase):
1818
Test caclmgrd feature present
1919
"""
2020
def setUp(self):
21-
swsscommon.swsscommon.ConfigDBConnector = MockConfigDb
21+
swsscommon.ConfigDBConnector = MockConfigDb
2222
test_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
2323
modules_path = os.path.dirname(test_path)
2424
scripts_path = os.path.join(modules_path, "scripts")

tests/caclmgrd/caclmgrd_generate_allow_internal_docker_ip_traffic_test.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
import sys
33

44
from parameterized import parameterized
5+
from swsscommon import swsscommon
56
from sonic_py_common.general import load_module_from_source
67
from unittest import TestCase, mock
8+
from tests.common.mock_configdb import MockConfigDb
79
from pyfakefs.fake_filesystem_unittest import patchfs
810

911
from .test_internal_docker_ip_traffic_vectors import CACLMGRD_INTERNAL_DOCKER_IP_TEST_VECTOR
@@ -20,6 +22,7 @@ def setUp(self):
2022
sys.path.insert(0, modules_path)
2123
caclmgrd_path = os.path.join(scripts_path, 'caclmgrd')
2224
self.caclmgrd = load_module_from_source('caclmgrd', caclmgrd_path)
25+
swsscommon.ConfigDBConnector = MockConfigDb
2326
self.maxDiff = None
2427

2528
@parameterized.expand(CACLMGRD_INTERNAL_DOCKER_IP_TEST_VECTOR)

tests/caclmgrd/caclmgrd_namespace_docker_ip_test.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import os
22
import sys
33

4+
from swsscommon import swsscommon
45
from sonic_py_common.general import load_module_from_source
56
from unittest import TestCase, mock
7+
from tests.common.mock_configdb import MockConfigDb
68

79
class TestCaclmgrdNamespaceDockerIP(TestCase):
810
"""
@@ -15,6 +17,7 @@ def setUp(self):
1517
sys.path.insert(0, modules_path)
1618
caclmgrd_path = os.path.join(scripts_path, 'caclmgrd')
1719
self.caclmgrd = load_module_from_source('caclmgrd', caclmgrd_path)
20+
swsscommon.ConfigDBConnector = MockConfigDb
1821
self.maxDiff = None
1922

2023
def test_caclmgrd_namespace_docker_ip(self):

tests/caclmgrd/caclmgrd_scale_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import os
22
import sys
3-
import swsscommon
3+
from swsscommon import swsscommon
44

55
from parameterized import parameterized
66
from sonic_py_common.general import load_module_from_source
@@ -18,7 +18,7 @@ class TestCaclmgrdScale(TestCase):
1818
Test caclmgrd with scale cacl rules
1919
"""
2020
def setUp(self):
21-
swsscommon.swsscommon.ConfigDBConnector = MockConfigDb
21+
swsscommon.ConfigDBConnector = MockConfigDb
2222
test_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
2323
modules_path = os.path.dirname(test_path)
2424
scripts_path = os.path.join(modules_path, "scripts")

tests/caclmgrd/caclmgrd_soc_rules_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import os
22
import sys
3-
import swsscommon
3+
from swsscommon import swsscommon
44

55
from parameterized import parameterized
66
from sonic_py_common.general import load_module_from_source
@@ -19,7 +19,7 @@ class TestCaclmgrdSoc(TestCase):
1919
Test caclmgrd soc
2020
"""
2121
def setUp(self):
22-
swsscommon.swsscommon.ConfigDBConnector = MockConfigDb
22+
swsscommon.ConfigDBConnector = MockConfigDb
2323
test_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
2424
modules_path = os.path.dirname(test_path)
2525
scripts_path = os.path.join(modules_path, "scripts")

tests/caclmgrd/caclmgrd_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import os
22
import sys
3-
import swsscommon
3+
from swsscommon import swsscommon
44
from unittest.mock import call, patch, MagicMock
55
from unittest import TestCase, mock
66
from tests.common.mock_configdb import MockConfigDb
@@ -14,7 +14,7 @@
1414

1515
class TestCaclmgrd(TestCase):
1616
def setUp(self):
17-
swsscommon.swsscommon.ConfigDBConnector = MockConfigDb
17+
swsscommon.ConfigDBConnector = MockConfigDb
1818
test_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
1919
modules_path = os.path.dirname(test_path)
2020
scripts_path = os.path.join(modules_path, "scripts")

tests/caclmgrd/conftest.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import pytest
2+
from tests.common.mock_configdb import MockConfigDb
3+
4+
5+
@pytest.fixture(autouse=True)
6+
def reset_config_db():
7+
"""Ensure MockConfigDb.CONFIG_DB has minimal required data for caclmgrd."""
8+
if not MockConfigDb.CONFIG_DB.get('DEVICE_METADATA'):
9+
MockConfigDb.CONFIG_DB['DEVICE_METADATA'] = {
10+
'localhost': {}
11+
}
12+
if not MockConfigDb.CONFIG_DB.get('FEATURE'):
13+
MockConfigDb.CONFIG_DB['FEATURE'] = {}
14+
yield
15+
MockConfigDb.CONFIG_DB = {}

tests/common/mock_configdb.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ class MockConfigDb(object):
33
Mock Config DB which responds to data tables requests and store updates to the data table
44
"""
55
STATE_DB = None
6-
CONFIG_DB = None
6+
CONFIG_DB = {}
77
event_queue = []
88

99
def __init__(self, **kwargs):

0 commit comments

Comments
 (0)