-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[hostcfgd]: Add Ability To Configure Feature During Run-time (#6700)
Features may be enabled/disabled for the same topology based on run-time configuration. This PR adds the ability to enable/disable feature based on config db data. signed-off-by: Tamer Ahmed <tamer.ahmed@microsoft.com>
- Loading branch information
1 parent
7f52abc
commit 51ab39f
Showing
9 changed files
with
494 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,5 +12,6 @@ dist/ | |
|
||
# Unit test coverage | ||
.coverage | ||
.pytest_cache/ | ||
coverage.xml | ||
htmlcov/ |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
[pytest] | ||
addopts = --cov=scripts --cov-report html --cov-report term --cov-report xml | ||
addopts = --cov=scripts --cov-report html --cov-report term --cov-report xml --ignore=tests/hostcfgd/test_vectors.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import importlib.machinery | ||
import importlib.util | ||
import os | ||
import sys | ||
import swsssdk | ||
|
||
from parameterized import parameterized | ||
from unittest import TestCase, mock | ||
from tests.hostcfgd.test_vectors import HOSTCFGD_TEST_VECTOR | ||
from tests.hostcfgd.mock_configdb import MockConfigDb | ||
|
||
|
||
swsssdk.ConfigDBConnector = MockConfigDb | ||
test_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) | ||
modules_path = os.path.dirname(test_path) | ||
scripts_path = os.path.join(modules_path, "scripts") | ||
sys.path.insert(0, modules_path) | ||
|
||
# Load the file under test | ||
hostcfgd_path = os.path.join(scripts_path, 'hostcfgd') | ||
loader = importlib.machinery.SourceFileLoader('hostcfgd', hostcfgd_path) | ||
spec = importlib.util.spec_from_loader(loader.name, loader) | ||
hostcfgd = importlib.util.module_from_spec(spec) | ||
loader.exec_module(hostcfgd) | ||
sys.modules['hostcfgd'] = hostcfgd | ||
|
||
|
||
class TestHostcfgd(TestCase): | ||
""" | ||
Test hostcfd daemon - feature | ||
""" | ||
def __verify_table(self, table, expected_table): | ||
""" | ||
verify config db tables | ||
Compares Config DB table (FEATURE) with expected output table | ||
Args: | ||
table(dict): Current Config Db table | ||
expected_table(dict): Expected Config Db table | ||
Returns: | ||
None | ||
""" | ||
is_equal = len(table) == len(expected_table) | ||
if is_equal: | ||
for key, fields in expected_table.items(): | ||
is_equal = is_equal and key in table and len(fields) == len(table[key]) | ||
if is_equal: | ||
for field, value in fields.items(): | ||
is_equal = is_equal and value == table[key][field] | ||
if not is_equal: | ||
break; | ||
else: | ||
break | ||
return is_equal | ||
|
||
@parameterized.expand(HOSTCFGD_TEST_VECTOR) | ||
def test_hostcfgd(self, test_name, test_data): | ||
""" | ||
Test hostcfd daemon initialization | ||
Args: | ||
test_name(str): test name | ||
test_data(dict): test data which contains initial Config Db tables, and expected results | ||
Returns: | ||
None | ||
""" | ||
MockConfigDb.set_config_db(test_data["config_db"]) | ||
with mock.patch("hostcfgd.subprocess") as mocked_subprocess: | ||
host_config_daemon = hostcfgd.HostConfigDaemon() | ||
host_config_daemon.update_all_feature_states() | ||
assert self.__verify_table( | ||
MockConfigDb.get_config_db()["FEATURE"], | ||
test_data["expected_config_db"]["FEATURE"] | ||
), "Test failed for test data: {0}".format(test_data) | ||
mocked_subprocess.check_call.assert_has_calls(test_data["expected_subprocess_calls"], any_order=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
class MockConfigDb(object): | ||
""" | ||
Mock Config DB which responds to data tables requests and store updates to the data table | ||
""" | ||
STATE_DB = None | ||
CONFIG_DB = None | ||
|
||
def __init__(self): | ||
pass | ||
|
||
@staticmethod | ||
def set_config_db(test_config_db): | ||
MockConfigDb.CONFIG_DB = test_config_db | ||
|
||
@staticmethod | ||
def get_config_db(): | ||
return MockConfigDb.CONFIG_DB | ||
|
||
def connect(self, wait_for_init=True, retry_on=True): | ||
pass | ||
|
||
def get(self, db_id, key, field): | ||
return MockConfigDb.CONFIG_DB[key][field] | ||
|
||
def get_entry(self, key, field): | ||
return MockConfigDb.CONFIG_DB[key][field] | ||
|
||
def set_entry(self, key, field, data): | ||
MockConfigDb.CONFIG_DB[key][field] = data | ||
|
||
def get_table(self, table_name): | ||
return MockConfigDb.CONFIG_DB[table_name] |
Oops, something went wrong.