Skip to content

Commit a5e4fb5

Browse files
committed
adding unit tests
1 parent f86d183 commit a5e4fb5

File tree

7 files changed

+399
-1
lines changed

7 files changed

+399
-1
lines changed

src/sonic-host-services/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ dist/
1414
.coverage
1515
coverage.xml
1616
htmlcov/
17+
/.pytest_cache/

src/sonic-host-services/__init__.py

Whitespace-only changes.

src/sonic-host-services/pytest.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
[pytest]
2-
addopts = --cov=scripts --cov-report html --cov-report term --cov-report xml
2+
addopts = --cov=scripts --cov-report html --cov-report term --cov-report xml --ignore=tests/hostcfgd/test_vectors.py

src/sonic-host-services/tests/hostcfgd/__init__.py

Whitespace-only changes.
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import importlib.machinery
2+
import importlib.util
3+
import os
4+
import sys
5+
import swsssdk
6+
7+
from parameterized import parameterized
8+
from unittest import TestCase, mock
9+
from tests.hostcfgd.test_vectors import hostcfgdTestVector
10+
from tests.hostcfgd.mock_configdb import MockConfigDb
11+
12+
13+
swsssdk.ConfigDBConnector = MockConfigDb
14+
test_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
15+
modules_path = os.path.dirname(test_path)
16+
scripts_path = os.path.join(modules_path, "scripts")
17+
sys.path.insert(0, modules_path)
18+
19+
# Load the file under test
20+
hostcfgd_path = os.path.join(scripts_path, 'hostcfgd')
21+
loader = importlib.machinery.SourceFileLoader('hostcfgd', hostcfgd_path)
22+
spec = importlib.util.spec_from_loader(loader.name, loader)
23+
hostcfgd = importlib.util.module_from_spec(spec)
24+
loader.exec_module(hostcfgd)
25+
sys.modules['hostcfgd'] = hostcfgd
26+
27+
28+
class TestHostcfgd(TestCase):
29+
"""
30+
Test hostcfd daemon - feature
31+
"""
32+
def __verify_table(self, table, expected_table):
33+
"""
34+
verify config db tables
35+
36+
Compares Config DB table (FEATURE) with expected output table
37+
38+
Args:
39+
table(dict): Current Config Db table
40+
expected_table(dict): Expected Config Db table
41+
42+
Returns:
43+
None
44+
"""
45+
isEqual = len(table) == len(expected_table)
46+
if isEqual:
47+
for key, fields in expected_table.items():
48+
isEqual = isEqual and key in table
49+
if isEqual:
50+
for field, value in fields.items():
51+
isEqual = isEqual and value == table[key][field]
52+
if not isEqual:
53+
break;
54+
else:
55+
break
56+
return isEqual
57+
58+
@parameterized.expand(hostcfgdTestVector)
59+
def test_hostcfgd(self, name, testData):
60+
"""
61+
Test hostcfd daemon initialization
62+
63+
Args:
64+
name(str): test name
65+
test_data(dict): test data which contains initial Config Db tables, and expected results
66+
67+
Returns:
68+
None
69+
"""
70+
MockConfigDb.set_config_db(testData["config_db"])
71+
with mock.patch("hostcfgd.subprocess") as mockSubprocess:
72+
hostConfigDaemon = hostcfgd.HostConfigDaemon()
73+
hostConfigDaemon.update_all_feature_states()
74+
assert self.__verify_table(
75+
MockConfigDb.get_config_db()["FEATURE"],
76+
testData["expected_config_db"]["FEATURE"]
77+
), "Test failed for test data: {0}".format(testData)
78+
mockSubprocess.check_call.assert_has_calls(testData["expected_subprocess_calls"], any_order=True)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class MockConfigDb(object):
2+
"""
3+
Mock Config DB which responds to data tables requests and store updates to the data table
4+
"""
5+
STATE_DB = None
6+
CONFIG_DB = None
7+
8+
def __init__(self):
9+
pass
10+
11+
@staticmethod
12+
def set_config_db(test_config_db):
13+
MockConfigDb.CONFIG_DB = test_config_db
14+
15+
@staticmethod
16+
def get_config_db():
17+
return MockConfigDb.CONFIG_DB
18+
19+
def connect(self, wait_for_init=True, retry_on=True):
20+
pass
21+
22+
def get(self, db_id, key, field):
23+
return MockConfigDb.CONFIG_DB[key][field]
24+
25+
def get_entry(self, key, field):
26+
return MockConfigDb.CONFIG_DB[key][field]
27+
28+
def set_entry(self, key, field, data):
29+
MockConfigDb.CONFIG_DB[key][field] = data
30+
31+
def get_table(self, table_name):
32+
return MockConfigDb.CONFIG_DB[table_name]

0 commit comments

Comments
 (0)