Skip to content

Commit

Permalink
[dvs] Add redis polling interface to dvs fixture (#1228)
Browse files Browse the repository at this point in the history
* [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]>
  • Loading branch information
daall authored Mar 26, 2020
1 parent 841cd69 commit 177a6b1
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 205 deletions.
60 changes: 56 additions & 4 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@

from datetime import datetime
from swsscommon import swsscommon

pytest_plugins = [
"dvslib.dvs_database"
]
from dvslib import dvs_database as dvs_db

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

class DockerVirtualSwitch(object):
APP_DB_ID = 0
ASIC_DB_ID = 1
COUNTERS_DB_ID = 2
CONFIG_DB_ID = 4
FLEX_COUNTER_DB_ID = 5
STATE_DB_ID = 6

def __init__(self, name=None, imgname=None, keeptb=False, fakeplatform=None):
self.basicd = ['redis-server',
'rsyslogd']
Expand Down Expand Up @@ -231,6 +235,14 @@ def __init__(self, name=None, imgname=None, keeptb=False, fakeplatform=None):
self.redis_sock = self.mount + '/' + "redis.sock"
self.check_ctn_status_and_db_connect()

# DB wrappers are declared here, lazy-loaded in the tests
self.app_db = None
self.asic_db = None
self.counters_db = None
self.config_db = None
self.flex_db = None
self.state_db = None

def destroy(self):
if self.appldb:
del self.appldb
Expand Down Expand Up @@ -923,6 +935,46 @@ def verify_acl_port_binding(self, bind_ports):
acl_table_groups = atbl.getKeys()
assert len(acl_table_groups) == len(bind_ports)

def get_app_db(self):
if not self.app_db:
self.app_db = dvs_db.DVSDatabase(self.APP_DB_ID, self.redis_sock)

return self.app_db

def get_asic_db(self):
if not self.asic_db:
db = dvs_db.DVSDatabase(self.ASIC_DB_ID, self.redis_sock)
db.default_acl_tables = self.asicdb.default_acl_tables
db.default_acl_entries = self.asicdb.default_acl_entries
db.port_name_map = self.asicdb.portnamemap
self.asic_db = db

return self.asic_db

def get_counters_db(self):
if not self.counters_db:
self.counters_db = dvs_db.DVSDatabase(self.COUNTERS_DB_ID, self.redis_sock)

return self.counters_db

def get_config_db(self):
if not self.config_db:
self.config_db = dvs_db.DVSDatabase(self.CONFIG_DB_ID, self.redis_sock)

return self.config_db

def get_flex_db(self):
if not self.flex_db:
self.flex_db = dvs_db.DVSDatabase(self.FLEX_COUNTER_DB_ID, self.redis_sock)

return self.flex_db

def get_state_db(self):
if not self.state_db:
self.state_db = dvs_db.DVSDatabase(self.STATE_DB_ID, self.redis_sock)

return self.state_db

@pytest.yield_fixture(scope="module")
def dvs(request):
name = request.config.getoption("--dvsname")
Expand Down
124 changes: 0 additions & 124 deletions tests/dvslib/dvs_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,9 @@

import time
import collections
import pytest

from swsscommon import swsscommon

APP_DB_ID = 0
ASIC_DB_ID = 1
COUNTERS_DB_ID = 2
CONFIG_DB_ID = 4
FLEX_COUNTER_DB_ID = 5
STATE_DB_ID = 6


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

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

return None


@pytest.fixture
def app_db(dvs):
"""
Provides access to the SONiC APP DB.
Args:
dvs (DockerVirtualSwitch): The dvs fixture, automatically injected
by pytest.
Returns:
DVSDatabase: An instance of APP DB
"""

return DVSDatabase(APP_DB_ID, dvs.redis_sock)


@pytest.fixture
def asic_db(dvs):
"""
Provides access to the SONiC ASIC DB.
Args:
dvs (DockerVirtualSwitch): The dvs fixture, automatically injected
by pytest.
Attributes:
default_acl_tables (List[str]): IDs for the ACL tables that are
configured in SONiC by default
default_acl_entries (List[str]): IDs for the ACL rules that are
configured in SONiC by default
port_name_map (Dict[str, str]): A mapping from interface names
(e.g. Ethernet0) to port IDs
Returns:
DVSDatabase: An instance of ASIC DB
"""

db = DVSDatabase(ASIC_DB_ID, dvs.redis_sock) # pylint: disable=invalid-name

# NOTE: This is an ugly hack to emulate the current asic db behavior,
# this will be refactored along with the dvs fixture.
db.default_acl_tables = dvs.asicdb.default_acl_tables # pylint: disable=attribute-defined-outside-init
db.default_acl_entries = dvs.asicdb.default_acl_entries # pylint: disable=attribute-defined-outside-init
db.port_name_map = dvs.asicdb.portnamemap # pylint: disable=attribute-defined-outside-init

return db


@pytest.fixture
def counters_db(dvs):
"""
Provides access to the SONiC Counters DB.
Args:
dvs (DockerVirtualSwitch): The dvs fixture, automatically injected
by pytest.
Returns:
DVSDatabase: An instance of Counters DB
"""

return DVSDatabase(COUNTERS_DB_ID, dvs.redis_sock)


@pytest.fixture
def config_db(dvs):
"""
Provides access to the SONiC Config DB.
Args:
dvs (DockerVirtualSwitch): The dvs fixture, automatically injected
by pytest.
Returns:
DVSDatabase: An instance of Config DB
"""

return DVSDatabase(CONFIG_DB_ID, dvs.redis_sock)


@pytest.fixture
def flex_counter_db(dvs):
"""
Provides access to the SONiC Flex Counter DB.
Args:
dvs (DockerVirtualSwitch): The dvs fixture, automatically injected
by pytest.
Returns:
DVSDatabase: An instance of Flex Counter DB
"""

return DVSDatabase(FLEX_COUNTER_DB_ID, dvs.redis_sock)


@pytest.fixture
def state_db(dvs):
"""
Provides access to the SONiC State DB.
Args:
dvs (DockerVirtualSwitch): The dvs fixture, automatically injected
by pytest.
Returns:
DVSDatabase: An instance of State DB
"""

return DVSDatabase(STATE_DB_ID, dvs.redis_sock)
Loading

0 comments on commit 177a6b1

Please sign in to comment.