-
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.
- Loading branch information
1 parent
f86d183
commit a5e4fb5
Showing
7 changed files
with
399 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,3 +14,4 @@ dist/ | |
.coverage | ||
coverage.xml | ||
htmlcov/ | ||
/.pytest_cache/ |
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 |
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 hostcfgdTestVector | ||
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 | ||
""" | ||
isEqual = len(table) == len(expected_table) | ||
if isEqual: | ||
for key, fields in expected_table.items(): | ||
isEqual = isEqual and key in table | ||
if isEqual: | ||
for field, value in fields.items(): | ||
isEqual = isEqual and value == table[key][field] | ||
if not isEqual: | ||
break; | ||
else: | ||
break | ||
return isEqual | ||
|
||
@parameterized.expand(hostcfgdTestVector) | ||
def test_hostcfgd(self, name, testData): | ||
""" | ||
Test hostcfd daemon initialization | ||
Args: | ||
name(str): test name | ||
test_data(dict): test data which contains initial Config Db tables, and expected results | ||
Returns: | ||
None | ||
""" | ||
MockConfigDb.set_config_db(testData["config_db"]) | ||
with mock.patch("hostcfgd.subprocess") as mockSubprocess: | ||
hostConfigDaemon = hostcfgd.HostConfigDaemon() | ||
hostConfigDaemon.update_all_feature_states() | ||
assert self.__verify_table( | ||
MockConfigDb.get_config_db()["FEATURE"], | ||
testData["expected_config_db"]["FEATURE"] | ||
), "Test failed for test data: {0}".format(testData) | ||
mockSubprocess.check_call.assert_has_calls(testData["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.