Skip to content

Commit

Permalink
adding unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tahmed-dev committed Mar 12, 2021
1 parent f86d183 commit a5e4fb5
Show file tree
Hide file tree
Showing 7 changed files with 399 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/sonic-host-services/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ dist/
.coverage
coverage.xml
htmlcov/
/.pytest_cache/
Empty file.
2 changes: 1 addition & 1 deletion src/sonic-host-services/pytest.ini
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.
78 changes: 78 additions & 0 deletions src/sonic-host-services/tests/hostcfgd/hostcfgd_test.py
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)
32 changes: 32 additions & 0 deletions src/sonic-host-services/tests/hostcfgd/mock_configdb.py
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]
Loading

0 comments on commit a5e4fb5

Please sign in to comment.