Skip to content

Commit

Permalink
[chassisd][thermalctld] Import mock libraries based on environment va…
Browse files Browse the repository at this point in the history
…riable (#112)

Previously, chassisd and thermalctld assumed that the swsscommon library would not be installed in the unit testing environment. This is not a valid assumption, and would cause unit tests to fail if swsscommon was available in the unit test environement, because it would get imported, but there would be no Redis DB to communicate with.

This PR uses environment variables, which are set by the unit tests themselves, to determine whether to load the real or mock libraries. This solution is similar to what is done in sonic-utilities.
  • Loading branch information
jleveque authored Nov 14, 2020
1 parent d5c586e commit 9cab7ef
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 21 deletions.
18 changes: 8 additions & 10 deletions sonic-chassisd/scripts/chassisd
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,16 @@ try:

from sonic_py_common import daemon_base, logger
from sonic_py_common.task_base import ProcessTaskBase
except ImportError as e:
raise ImportError (str(e) + " - required module not found")

try:
from swsscommon import swsscommon
# If unit testing is occurring, mock swsscommon and module_base
if os.environ["CHASSISD_UNIT_TESTING"] == "1":
from tests import mock_swsscommon as swsscommon
from tests.mock_module_base import ModuleBase
else:
from swsscommon import swsscommon
from sonic_platform_base.module_base import ModuleBase
except ImportError as e:
from tests import mock_swsscommon as swsscommon

try:
from sonic_platform_base.module_base import ModuleBase
except ImportError as e:
from tests.mock_module_base import ModuleBase
raise ImportError (str(e) + " - required module not found")

#
# Constants ====================================================================
Expand Down
10 changes: 7 additions & 3 deletions sonic-chassisd/tests/test_chassisd.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import sys
from imp import load_source

from mock import Mock, MagicMock, patch
from sonic_py_common import daemon_base
Expand All @@ -17,10 +18,7 @@
scripts_path = os.path.join(modules_path, "scripts")
sys.path.insert(0, modules_path)

from imp import load_source

load_source('chassisd', scripts_path + '/chassisd')
from chassisd import *

CHASSIS_MODULE_INFO_NAME_FIELD = 'name'
CHASSIS_MODULE_INFO_DESC_FIELD = 'desc'
Expand All @@ -31,13 +29,19 @@
CHASSIS_INFO_CARD_NUM_FIELD = 'module_num'

def setup_function():
os.environ["CHASSISD_UNIT_TESTING"] = "1"

load_source('chassisd', scripts_path + '/chassisd')
from chassisd import *

ModuleUpdater.log_notice = MagicMock()
ModuleUpdater.log_warning = MagicMock()


def teardown_function():
ModuleUpdater.log_notice.reset()
ModuleUpdater.log_warning.reset()
os.environ["CHASSISD_UNIT_TESTING"] = "0"


def test_moduleupdater_check_valid_fields():
Expand Down
10 changes: 6 additions & 4 deletions sonic-thermalctld/scripts/thermalctld
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ try:

from sonic_py_common import daemon_base, logger
from sonic_py_common.task_base import ProcessTaskBase

# If unit testing is occurring, mock swsscommon
if os.environ["THERMALCTLD_UNIT_TESTING"] == "1":
from tests import mock_swsscommon as swsscommon
else:
from swsscommon import swsscommon
except ImportError as e:
raise ImportError(repr(e) + " - required module not found")

try:
from swsscommon import swsscommon
except ImportError as e:
from tests import mock_swsscommon as swsscommon

SYSLOG_IDENTIFIER = 'thermalctld'
NOT_AVAILABLE = 'N/A'
Expand Down
11 changes: 7 additions & 4 deletions sonic-thermalctld/tests/test_thermalctld.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import sys
from imp import load_source

from mock import Mock, MagicMock, patch
from sonic_py_common import daemon_base
Expand All @@ -16,14 +17,15 @@
scripts_path = os.path.join(modules_path, "scripts")
sys.path.insert(0, modules_path)

from imp import load_source

load_source('thermalctld', scripts_path + '/thermalctld')
from thermalctld import *

TEMPER_INFO_TABLE_NAME = 'TEMPERATURE_INFO'

def setup_function():
os.environ["THERMALCTLD_UNIT_TESTING"] = "1"

load_source('thermalctld', scripts_path + '/thermalctld')
from thermalctld import *

FanStatus.log_notice = MagicMock()
FanStatus.log_warning = MagicMock()
FanUpdater.log_notice = MagicMock()
Expand All @@ -43,6 +45,7 @@ def teardown_function():
TemperatureStatus.log_warning.reset()
TemperatureUpdater.log_warning.reset()
TemperatureUpdater.log_warning.reset()
os.environ["THERMALCTLD_UNIT_TESTING"] = "0"


def test_fanstatus_set_presence():
Expand Down

0 comments on commit 9cab7ef

Please sign in to comment.