Skip to content

Commit 177a6b1

Browse files
authored
[dvs] Add redis polling interface to dvs fixture (sonic-net#1228)
* [dvs] Add redis polling interface to dvs fixture - Remove standalone db fixtures - Refactor NAT and ACL tests to use new interface Signed-off-by: Danny Allen <[email protected]>
1 parent 841cd69 commit 177a6b1

File tree

4 files changed

+134
-205
lines changed

4 files changed

+134
-205
lines changed

tests/conftest.py

+56-4
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@
1313

1414
from datetime import datetime
1515
from swsscommon import swsscommon
16-
17-
pytest_plugins = [
18-
"dvslib.dvs_database"
19-
]
16+
from dvslib import dvs_database as dvs_db
2017

2118
def ensure_system(cmd):
2219
(rc, output) = commands.getstatusoutput(cmd)
@@ -149,6 +146,13 @@ def runcmd_output(self, cmd):
149146
return subprocess.check_output("ip netns exec %s %s" % (self.nsname, cmd), shell=True)
150147

151148
class DockerVirtualSwitch(object):
149+
APP_DB_ID = 0
150+
ASIC_DB_ID = 1
151+
COUNTERS_DB_ID = 2
152+
CONFIG_DB_ID = 4
153+
FLEX_COUNTER_DB_ID = 5
154+
STATE_DB_ID = 6
155+
152156
def __init__(self, name=None, imgname=None, keeptb=False, fakeplatform=None):
153157
self.basicd = ['redis-server',
154158
'rsyslogd']
@@ -231,6 +235,14 @@ def __init__(self, name=None, imgname=None, keeptb=False, fakeplatform=None):
231235
self.redis_sock = self.mount + '/' + "redis.sock"
232236
self.check_ctn_status_and_db_connect()
233237

238+
# DB wrappers are declared here, lazy-loaded in the tests
239+
self.app_db = None
240+
self.asic_db = None
241+
self.counters_db = None
242+
self.config_db = None
243+
self.flex_db = None
244+
self.state_db = None
245+
234246
def destroy(self):
235247
if self.appldb:
236248
del self.appldb
@@ -923,6 +935,46 @@ def verify_acl_port_binding(self, bind_ports):
923935
acl_table_groups = atbl.getKeys()
924936
assert len(acl_table_groups) == len(bind_ports)
925937

938+
def get_app_db(self):
939+
if not self.app_db:
940+
self.app_db = dvs_db.DVSDatabase(self.APP_DB_ID, self.redis_sock)
941+
942+
return self.app_db
943+
944+
def get_asic_db(self):
945+
if not self.asic_db:
946+
db = dvs_db.DVSDatabase(self.ASIC_DB_ID, self.redis_sock)
947+
db.default_acl_tables = self.asicdb.default_acl_tables
948+
db.default_acl_entries = self.asicdb.default_acl_entries
949+
db.port_name_map = self.asicdb.portnamemap
950+
self.asic_db = db
951+
952+
return self.asic_db
953+
954+
def get_counters_db(self):
955+
if not self.counters_db:
956+
self.counters_db = dvs_db.DVSDatabase(self.COUNTERS_DB_ID, self.redis_sock)
957+
958+
return self.counters_db
959+
960+
def get_config_db(self):
961+
if not self.config_db:
962+
self.config_db = dvs_db.DVSDatabase(self.CONFIG_DB_ID, self.redis_sock)
963+
964+
return self.config_db
965+
966+
def get_flex_db(self):
967+
if not self.flex_db:
968+
self.flex_db = dvs_db.DVSDatabase(self.FLEX_COUNTER_DB_ID, self.redis_sock)
969+
970+
return self.flex_db
971+
972+
def get_state_db(self):
973+
if not self.state_db:
974+
self.state_db = dvs_db.DVSDatabase(self.STATE_DB_ID, self.redis_sock)
975+
976+
return self.state_db
977+
926978
@pytest.yield_fixture(scope="module")
927979
def dvs(request):
928980
name = request.config.getoption("--dvsname")

tests/dvslib/dvs_database.py

-124
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,9 @@
66

77
import time
88
import collections
9-
import pytest
109

1110
from swsscommon import swsscommon
1211

13-
APP_DB_ID = 0
14-
ASIC_DB_ID = 1
15-
COUNTERS_DB_ID = 2
16-
CONFIG_DB_ID = 4
17-
FLEX_COUNTER_DB_ID = 5
18-
STATE_DB_ID = 6
19-
2012

2113
# PollingConfig provides parameters that are used to control polling behavior
2214
# when accessing redis:
@@ -46,10 +38,6 @@ def __init__(self, db_id, connector):
4638
instance in redis.
4739
connector (str): The I/O connection used to communicate with
4840
redis (e.g. unix socket, tcp socket, etc.).
49-
50-
NOTE: Currently it's most convenient to let the user specify the
51-
connector since it's set up in the dvs fixture. We may abstract
52-
this further in the future as we refactor dvs.
5341
"""
5442

5543
self.db_connection = swsscommon.DBConnector(db_id, connector, 0)
@@ -264,115 +252,3 @@ def _db_poll(polling_config, access_function):
264252
assert False
265253

266254
return None
267-
268-
269-
@pytest.fixture
270-
def app_db(dvs):
271-
"""
272-
Provides access to the SONiC APP DB.
273-
274-
Args:
275-
dvs (DockerVirtualSwitch): The dvs fixture, automatically injected
276-
by pytest.
277-
278-
Returns:
279-
DVSDatabase: An instance of APP DB
280-
"""
281-
282-
return DVSDatabase(APP_DB_ID, dvs.redis_sock)
283-
284-
285-
@pytest.fixture
286-
def asic_db(dvs):
287-
"""
288-
Provides access to the SONiC ASIC DB.
289-
290-
Args:
291-
dvs (DockerVirtualSwitch): The dvs fixture, automatically injected
292-
by pytest.
293-
294-
Attributes:
295-
default_acl_tables (List[str]): IDs for the ACL tables that are
296-
configured in SONiC by default
297-
default_acl_entries (List[str]): IDs for the ACL rules that are
298-
configured in SONiC by default
299-
port_name_map (Dict[str, str]): A mapping from interface names
300-
(e.g. Ethernet0) to port IDs
301-
302-
Returns:
303-
DVSDatabase: An instance of ASIC DB
304-
"""
305-
306-
db = DVSDatabase(ASIC_DB_ID, dvs.redis_sock) # pylint: disable=invalid-name
307-
308-
# NOTE: This is an ugly hack to emulate the current asic db behavior,
309-
# this will be refactored along with the dvs fixture.
310-
db.default_acl_tables = dvs.asicdb.default_acl_tables # pylint: disable=attribute-defined-outside-init
311-
db.default_acl_entries = dvs.asicdb.default_acl_entries # pylint: disable=attribute-defined-outside-init
312-
db.port_name_map = dvs.asicdb.portnamemap # pylint: disable=attribute-defined-outside-init
313-
314-
return db
315-
316-
317-
@pytest.fixture
318-
def counters_db(dvs):
319-
"""
320-
Provides access to the SONiC Counters DB.
321-
322-
Args:
323-
dvs (DockerVirtualSwitch): The dvs fixture, automatically injected
324-
by pytest.
325-
326-
Returns:
327-
DVSDatabase: An instance of Counters DB
328-
"""
329-
330-
return DVSDatabase(COUNTERS_DB_ID, dvs.redis_sock)
331-
332-
333-
@pytest.fixture
334-
def config_db(dvs):
335-
"""
336-
Provides access to the SONiC Config DB.
337-
338-
Args:
339-
dvs (DockerVirtualSwitch): The dvs fixture, automatically injected
340-
by pytest.
341-
342-
Returns:
343-
DVSDatabase: An instance of Config DB
344-
"""
345-
346-
return DVSDatabase(CONFIG_DB_ID, dvs.redis_sock)
347-
348-
349-
@pytest.fixture
350-
def flex_counter_db(dvs):
351-
"""
352-
Provides access to the SONiC Flex Counter DB.
353-
354-
Args:
355-
dvs (DockerVirtualSwitch): The dvs fixture, automatically injected
356-
by pytest.
357-
358-
Returns:
359-
DVSDatabase: An instance of Flex Counter DB
360-
"""
361-
362-
return DVSDatabase(FLEX_COUNTER_DB_ID, dvs.redis_sock)
363-
364-
365-
@pytest.fixture
366-
def state_db(dvs):
367-
"""
368-
Provides access to the SONiC State DB.
369-
370-
Args:
371-
dvs (DockerVirtualSwitch): The dvs fixture, automatically injected
372-
by pytest.
373-
374-
Returns:
375-
DVSDatabase: An instance of State DB
376-
"""
377-
378-
return DVSDatabase(STATE_DB_ID, dvs.redis_sock)

0 commit comments

Comments
 (0)