def setUp(self): swsscommon.swsscommon.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) caclmgrd_path = os.path.join(scripts_path, 'caclmgrd') self.caclmgrd = load_module_from_source('caclmgrd', caclmgrd_path)
from swsscommon import swsscommon from sonic_py_common.general import load_module_from_source from .mock_connector import MockConnector swsscommon.SonicV2Connector = MockConnector test_path = 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 procdockerstatsd_path = os.path.join(scripts_path, 'procdockerstatsd') procdockerstatsd = load_module_from_source('procdockerstatsd', procdockerstatsd_path) class TestProcDockerStatsDaemon(object): def test_convert_to_bytes(self): test_data = [('1B', 1), ('500B', 500), ('1KB', 1000), ('500KB', 500000), ('1MB', 1000000), ('500MB', 500000000), ('1MiB', 1048576), ('500MiB', 524288000), ('66.41MiB', 69635932), ('333.6MiB', 349804954), ('1GiB', 1073741824), ('500GiB', 536870912000), ('7.751GiB', 8322572878)] pdstatsd = procdockerstatsd.ProcDockerStats( procdockerstatsd.SYSLOG_IDENTIFIER) for test_input, expected_output in test_data:
from sonic_py_common.general import load_module_from_source from unittest import TestCase, mock from pyfakefs.fake_filesystem_unittest import patchfs from .test_dhcp_vectors import CACLMGRD_DHCP_TEST_VECTOR from tests.common.mock_configdb import MockConfigDb DBCONFIG_PATH = '/var/run/redis/sonic-db/database_config.json' swsscommon.swsscommon.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) caclmgrd_path = os.path.join(scripts_path, 'caclmgrd') caclmgrd = load_module_from_source('caclmgrd', caclmgrd_path) class TestCaclmgrdDhcp(TestCase): """ Test caclmgrd dhcp """ @parameterized.expand(CACLMGRD_DHCP_TEST_VECTOR) @patchfs def test_caclmgrd_dhcp(self, test_name, test_data, fs): if not os.path.exists(DBCONFIG_PATH): fs.create_file(DBCONFIG_PATH) # fake database_config.json MockConfigDb.set_config_db(test_data["config_db"]) with mock.patch("caclmgrd.subprocess") as mocked_subprocess:
from .test_vectors import HOSTCFGD_TEST_VECTOR, HOSTCFG_DAEMON_CFG_DB from tests.common.mock_configdb import MockConfigDb, MockDBConnector from pyfakefs.fake_filesystem_unittest import patchfs from deepdiff import DeepDiff from unittest.mock import call 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') hostcfgd = load_module_from_source('hostcfgd', hostcfgd_path) hostcfgd.ConfigDBConnector = MockConfigDb hostcfgd.DBConnector = MockDBConnector hostcfgd.Table = mock.Mock() class TestHostcfgd(TestCase): """ Test hostcfd daemon - feature """ def __verify_table(self, table, feature_state_table, expected_table): """ verify config db tables Compares Config DB table (FEATURE) with expected output table. Verifies that State DB table (FEATURE) is updated.
import os import sys from unittest.mock import MagicMock, patch import pytest from sonic_py_common.general import load_module_from_source from . import common_test load_module_from_source( "docker", os.path.join(os.path.dirname(os.path.realpath(__file__)), "mock_docker.py")) sys.path.append("ctrmgr") import ctrmgr_tools # ctr_image_names.json data for the test cases # str_ctr_image_names_json = '\ {\n\ "snmp" : "docker-snmp",\n\ "lldp" : "docker-lldp"\n\ }\n' # ctrmgr_tools test cases # NOTE: Ensure state-db entry is complete in PRE as we need to # overwrite any context left behind from last test run. # tools_test_data = { 0: {
import os from unittest.mock import MagicMock, patch import pytest from sonic_py_common.general import load_module_from_source from . import common_test load_module_from_source( "docker", os.path.join(os.path.dirname(os.path.realpath(__file__)), "mock_docker.py")) container = load_module_from_source( "container", os.path.join(os.path.dirname(os.path.realpath(__file__)), "../ctrmgr/container")) # container_start test cases # start_test_data = { 0: { common_test.DESCR: "container start for local", common_test.PRE: { common_test.CONFIG_DB_NO: { common_test.FEATURE_TABLE: { "snmp": { "set_owner": "local" } } }, common_test.STATE_DB_NO: {
BUILTINS = "builtins" else: BUILTINS = "__builtin__" from .mock_connector import MockConnector swsssdk.SonicV2Connector = MockConnector test_path = 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 determine_reboot_cause_path = os.path.join(scripts_path, 'determine-reboot-cause') determine_reboot_cause = load_module_from_source('determine_reboot_cause', determine_reboot_cause_path) PROC_CMDLINE_CONTENTS = """\ BOOT_IMAGE=/image-20191130.52/boot/vmlinuz-4.9.0-11-2-amd64 root=/dev/sda4 rw console=tty0 console=ttyS1,9600n8 quiet net.ifnames=0 biosdevname=0 loop=image-20191130.52/fs.squashfs loopfstype=squashfs apparmor=1 security=apparmor varlog_size=4096 usbcore.autosuspend=-1 module_blacklist=gpio_ich SONIC_BOOT_TYPE=warm""" EXPECTED_PARSE_WARMFAST_REBOOT_FROM_PROC_CMDLINE = "warm" PROC_CMDLINE_CONTENTS = """\ BOOT_IMAGE=/image-20191130.52/boot/vmlinuz-4.9.0-11-2-amd64 root=/dev/sda4 rw console=tty0 console=ttyS1,9600n8 quiet net.ifnames=0 biosdevname=0 loop=image-20191130.52/fs.squashfs loopfstype=squashfs apparmor=1 security=apparmor varlog_size=4096 usbcore.autosuspend=-1 module_blacklist=gpio_ich SONIC_BOOT_TYPE=warm""" REBOOT_CAUSE_CONTENTS = """\ User issued 'warm-reboot' command [User: admin, Time: Mon Nov 2 22:37:45 UTC 2020]""" GET_SONIC_VERSION_INFO = {'commit_id': 'e59ec8291', 'build_date': 'Mon Nov 2 06:00:14 UTC 2020', 'build_number': 75, 'kernel_version': '4.9.0-11-2-amd64', 'debian_version': '9.13', 'built_by': 'sonicbld@jenkins-slave-phx-2', 'asic_type': 'mellanox', 'build_version': '20191130.52'}