Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[dvs] Use DB namespace to construct DVSDatabase objects #1514

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
48 changes: 30 additions & 18 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ def random_string(size=4, chars=string.ascii_uppercase + string.digits):


class AsicDbValidator(DVSDatabase):
def __init__(self, db_id: int, connector: str):
DVSDatabase.__init__(self, db_id, connector)
def __init__(self):
DVSDatabase.__init__(self, "ASIC_DB")
self._wait_for_asic_db_to_initialize()
self._populate_default_asic_db_values()
self._generate_oid_to_interface_mapping()
Expand Down Expand Up @@ -133,8 +133,8 @@ def _populate_default_asic_db_values(self) -> None:
class ApplDbValidator(DVSDatabase):
NEIGH_TABLE = "NEIGH_TABLE"

def __init__(self, db_id: int, connector: str):
DVSDatabase.__init__(self, db_id, connector)
def __init__(self):
DVSDatabase.__init__(self, "APPL_DB")

def __del__(self):
# Make sure no neighbors on physical interfaces
Expand Down Expand Up @@ -208,12 +208,12 @@ def runcmd_output(self, cmd: str) -> str:


class DockerVirtualSwitch:
APPL_DB_ID = 0
ASIC_DB_ID = 1
COUNTERS_DB_ID = 2
CONFIG_DB_ID = 4
FLEX_COUNTER_DB_ID = 5
STATE_DB_ID = 6
APPL_DB_ID = "APPL_DB"
ASIC_DB_ID = "ASIC_DB"
COUNTERS_DB_ID = "COUNTERS_DB"
CONFIG_DB_ID = "CONFIG_DB"
FLEX_COUNTER_DB_ID = "FLEX_COUNTER_DB"
STATE_DB_ID = "STATE_DB"

# FIXME: Should be broken up into helper methods in a later PR.
def __init__(
Expand Down Expand Up @@ -361,6 +361,18 @@ def __init__(
self.redis_sock = os.path.join(self.mount, "redis.sock")
self.redis_chassis_sock = os.path.join(self.mount, "redis_chassis.sock")

# Initialize the database configuration
with open("/var/run/redis/sonic-db/database_config.json") as f:
db_conf = json.load(f)

db_conf["INSTANCES"]["redis"]["unix_socket_path"] = self.redis_sock
vs_db_conf_file = f"/tmp/database_config-{self.pid}.json"

with open(vs_db_conf_file, "w") as f:
json.dump(db_conf, f)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not create a vs_db_conf_file under tests directory, as source code, and initialize from it directly?


swsscommon.SonicDBConfig.initialize(vs_db_conf_file)

# DB wrappers are declared here, lazy-loaded in the tests
self.app_db = None
self.asic_db = None
Expand Down Expand Up @@ -438,10 +450,10 @@ def _polling_function():
wait_for_result(_polling_function, service_polling_config)

def init_asic_db_validator(self) -> None:
self.asicdb = AsicDbValidator(self.ASIC_DB_ID, self.redis_sock)
self.asicdb = AsicDbValidator()

def init_appl_db_validator(self) -> None:
self.appldb = ApplDbValidator(self.APPL_DB_ID, self.redis_sock)
self.appldb = ApplDbValidator()

def check_swss_ready(self, timeout: int = 300) -> None:
"""Verify that SWSS is ready to receive inputs.
Expand Down Expand Up @@ -1091,15 +1103,15 @@ def setReadOnlyAttr(self, obj, attr, val):
# that implementation. Save it for a follow-up PR.
def get_app_db(self) -> ApplDbValidator:
if not self.app_db:
self.app_db = DVSDatabase(self.APPL_DB_ID, self.redis_sock)
self.app_db = DVSDatabase(self.APPL_DB_ID)

return self.app_db

# FIXME: Now that AsicDbValidator is using DVSDatabase we should converge this with
# that implementation. Save it for a follow-up PR.
def get_asic_db(self) -> AsicDbValidator:
if not self.asic_db:
db = DVSDatabase(self.ASIC_DB_ID, self.redis_sock)
db = DVSDatabase(self.ASIC_DB_ID)
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
Expand All @@ -1112,25 +1124,25 @@ def get_asic_db(self) -> AsicDbValidator:

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

return self.counters_db

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

return self.config_db

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

return self.flex_db

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

return self.state_db

Expand Down
8 changes: 3 additions & 5 deletions tests/dvslib/dvs_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,13 @@ class DVSDatabase:

DEFAULT_POLLING_CONFIG = PollingConfig(polling_interval=0.01, timeout=5, strict=True)

def __init__(self, db_id: int, connector: str):
def __init__(self, db_name: str):
"""Initialize a DVSDatabase instance.

Args:
db_id: The integer ID used to identify the given database instance in redis.
connector: The I/O connection used to communicate with
redis (e.g. UNIX socket, TCP socket, etc.).
db_name: The name of the database to connect to.
"""
self.db_connection = swsscommon.DBConnector(db_id, connector, 0)
self.db_connection = swsscommon.DBConnector(db_name, 0)

def create_entry(self, table_name: str, key: str, entry: Dict[str, str]) -> None:
"""Add the mapping {`key` -> `entry`} to the specified table.
Expand Down