Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions tests/hostcfgd/hostcfgd_fips_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from parameterized import parameterized
from unittest import TestCase, mock
from tests.common.mock_configdb import MockConfigDb, MockDBConnector
from tests.common.mock_restart_waiter import MockRestartWaiter
from tests.common.mock_bootloader import MockBootloader
from sonic_py_common.general import getstatusoutput_noshell

Expand All @@ -31,6 +32,7 @@
hostcfgd.ConfigDBConnector = MockConfigDb
hostcfgd.DBConnector = MockDBConnector
hostcfgd.Table = mock.Mock()
swsscommon.RestartWaiter = MockRestartWaiter
running_services = [{"unit":"ssh.service","load":"loaded","active":"active","sub":"running","description":"OpenBSD Secure Shell server"},
{"unit":"restapi.service","load":"loaded","active":"active","sub":"running","description":"SONiC Restful API Service"}]

Expand All @@ -43,6 +45,7 @@ def run_diff(self, file1, file2):
return output

def setUp(self):
os.environ["HOSTCFGD_UNIT_TESTING"] = "2"
self._workPath =os.path.join('/tmp/test_fips/', self._testMethodName)
self.test_data = {'DEVICE_METADATA':{},'FIPS': {}}
self.test_data['DEVICE_METADATA'] = {'localhost': {'hostname': 'fips'}}
Expand All @@ -58,6 +61,7 @@ def setUp(self):

def tearDown(self):
shutil.rmtree(self._workPath, ignore_errors=True)
os.environ["HOSTCFGD_UNIT_TESTING"] = ""

def assert_fips_runtime_config(self, result='1'):
with open(hostcfgd.OPENSSL_FIPS_CONFIG_FILE) as f:
Expand Down
4 changes: 4 additions & 0 deletions tests/hostcfgd/hostcfgd_logging_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from tests.hostcfgd.test_logging_vectors \
import HOSTCFGD_TEST_LOGGING_VECTOR as logging_test_data
from tests.common.mock_configdb import MockConfigDb, MockDBConnector
from tests.common.mock_restart_waiter import MockRestartWaiter
from unittest import TestCase, mock

test_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
Expand All @@ -32,6 +33,7 @@
hostcfgd.ConfigDBConnector = MockConfigDb
hostcfgd.DBConnector = MockDBConnector
hostcfgd.Table = mock.Mock()
swsscommon.RestartWaiter = MockRestartWaiter
hostcfgd.run_cmd = mock.Mock()


Expand All @@ -45,6 +47,7 @@ def __init__(self, *args, **kwargs):
self.host_config_daemon = None

def setUp(self):
os.environ["HOSTCFGD_UNIT_TESTING"] = "2"
MockConfigDb.set_config_db(logging_test_data['initial'])
self.host_config_daemon = hostcfgd.HostConfigDaemon()

Expand All @@ -61,6 +64,7 @@ def setUp(self):
def tearDown(self):
self.host_config_daemon = None
MockConfigDb.set_config_db({})
os.environ["HOSTCFGD_UNIT_TESTING"] = ""

def update_config(self, config_name):
MockConfigDb.mod_config_db(logging_test_data[config_name])
Expand Down
4 changes: 4 additions & 0 deletions tests/hostcfgd/hostcfgd_rsyslog_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from tests.hostcfgd.test_rsyslog_vectors \
import HOSTCFGD_TEST_RSYSLOG_VECTOR as rsyslog_test_data
from tests.common.mock_configdb import MockConfigDb, MockDBConnector
from tests.common.mock_restart_waiter import MockRestartWaiter
from unittest import TestCase, mock

test_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
Expand All @@ -32,6 +33,7 @@
hostcfgd.ConfigDBConnector = MockConfigDb
hostcfgd.DBConnector = MockDBConnector
hostcfgd.Table = mock.Mock()
swsscommon.RestartWaiter = MockRestartWaiter
hostcfgd.run_cmd = mock.Mock()


Expand All @@ -45,6 +47,7 @@ def __init__(self, *args, **kwargs):
self.host_config_daemon = None

def setUp(self):
os.environ["HOSTCFGD_UNIT_TESTING"] = "2"
MockConfigDb.set_config_db(rsyslog_test_data['initial'])
self.host_config_daemon = hostcfgd.HostConfigDaemon()

Expand All @@ -63,6 +66,7 @@ def setUp(self):
def tearDown(self):
self.host_config_daemon = None
MockConfigDb.set_config_db({})
os.environ["HOSTCFGD_UNIT_TESTING"] = ""

def update_config(self, config_name):
MockConfigDb.mod_config_db(rsyslog_test_data[config_name])
Expand Down
27 changes: 25 additions & 2 deletions tests/hostcfgd/hostcfgd_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

from .test_vectors import HOSTCFG_DAEMON_INIT_CFG_DB, HOSTCFG_DAEMON_CFG_DB
from tests.common.mock_configdb import MockConfigDb, MockDBConnector
from tests.common.mock_restart_waiter import MockRestartWaiter
from pyfakefs.fake_filesystem_unittest import patchfs
from deepdiff import DeepDiff
from unittest.mock import call
Expand All @@ -28,6 +29,7 @@
hostcfgd.ConfigDBConnector = MockConfigDb
hostcfgd.DBConnector = MockDBConnector
hostcfgd.Table = mock.Mock()
swsscommon.RestartWaiter = MockRestartWaiter


class TesNtpCfgd(TestCase):
Expand Down Expand Up @@ -165,15 +167,27 @@ def test_serial_console_is_caching_config(self):
class TestHostcfgdDaemon(TestCase):

def setUp(self):
os.environ["HOSTCFGD_UNIT_TESTING"] = "2"
self.get_dev_meta = mock.patch(
'sonic_py_common.device_info.get_device_runtime_metadata',
return_value={'DEVICE_RUNTIME_METADATA': {}})
self.get_dev_meta.start()
MockConfigDb.set_config_db(HOSTCFG_DAEMON_CFG_DB)
self.original_cmdline = None

def tearDown(self):
MockConfigDb.CONFIG_DB = {}
self.get_dev_meta.stop()
os.environ["HOSTCFGD_UNIT_TESTING"] = ""

# Restore cmdline if it was modified
if self.original_cmdline is not None:
modules_path = os.path.join(os.path.dirname(__file__), "../..")
tests_path = os.path.join(modules_path, "tests")
cmdline_path = os.path.join(tests_path, "proc", "cmdline")
with open(cmdline_path, 'w') as f:
f.write(self.original_cmdline)
self.original_cmdline = None

def test_loopback_events(self):
MockConfigDb.set_config_db(HOSTCFG_DAEMON_CFG_DB)
Expand Down Expand Up @@ -243,7 +257,16 @@ def test_kdump_load(self):
mocked_subprocess.check_call.assert_has_calls(expected, any_order=True)

def test_kdump_event_with_proc_cmdline(self):
os.environ["HOSTCFGD_UNIT_TESTING"] = "2"
modules_path = os.path.join(os.path.dirname(__file__), "../..")
tests_path = os.path.join(modules_path, "tests")
cmdline_path = os.path.join(tests_path, "proc", "cmdline")

with open(cmdline_path, 'r') as f:
self.original_cmdline = f.read()

with open(cmdline_path, 'w') as f:
f.write(self.original_cmdline.rstrip() + ' crashkernel=8G-:1G\n')

MockConfigDb.set_config_db(HOSTCFG_DAEMON_CFG_DB)
daemon = hostcfgd.HostConfigDaemon()
default=daemon.kdumpCfg.kdump_defaults
Expand All @@ -268,7 +291,6 @@ def test_kdump_event_with_proc_cmdline(self):
call(['sonic-kdump-config', '--ssh_path', '/a/b/c']) # Covering ssh_path
]
mocked_subprocess.check_call.assert_has_calls(expected, any_order=True)
os.environ["HOSTCFGD_UNIT_TESTING"] = ""

def test_devicemeta_event(self):
"""
Expand Down Expand Up @@ -821,6 +843,7 @@ def test_get_memory_statistics_pid_exception(self, mock_process):

def test_memory_statistics_handler_exception(self):
"""Test exception handling in memory_statistics_handler"""
MockConfigDb.set_config_db(HOSTCFG_DAEMON_CFG_DB)
daemon = hostcfgd.HostConfigDaemon()
with mock.patch.object(daemon.memorystatisticscfg, 'memory_statistics_update',
side_effect=Exception("Handler error")):
Expand Down
2 changes: 1 addition & 1 deletion tests/proc/cmdline
Original file line number Diff line number Diff line change
@@ -1 +1 @@
BOOT_IMAGE=/image-202405.0-dirty-20250403.162657/boot/vmlinuz-6.1.0-22-2-amd64 root=UUID=bba2ec4f-9141-4069-a41e-230ecac4a5fb rw console=tty0 console=ttyS0,115200n8 quiet processor.max_cstate=1 amd_idle.max_cstate=0 sonic_fips=1 net.ifnames=0 biosdevname=0 loop=image-202405.0-dirty-20250403.162657/fs.squashfs loopfstype=squashfs systemd.unified_cgroup_hierarchy=0 apparmor=1 security=apparmor varlog_size=4096 usbcore.autosuspend=-1 amd_iommu=off pci=resource_alignment=48@00:03.1 crashkernel=8G-:1G
BOOT_IMAGE=/image-202405.0-dirty-20250403.162657/boot/vmlinuz-6.1.0-22-2-amd64 root=UUID=bba2ec4f-9141-4069-a41e-230ecac4a5fb rw console=tty0 console=ttyS0,115200n8 quiet processor.max_cstate=1 amd_idle.max_cstate=0 sonic_fips=1 net.ifnames=0 biosdevname=0 loop=image-202405.0-dirty-20250403.162657/fs.squashfs loopfstype=squashfs systemd.unified_cgroup_hierarchy=0 apparmor=1 security=apparmor varlog_size=4096 usbcore.autosuspend=-1 amd_iommu=off pci=resource_alignment=48@00:03.1
Loading