Skip to content
Merged
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
5 changes: 2 additions & 3 deletions sonic-thermalctld/scripts/thermalctld
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ from datetime import datetime

import sonic_platform
from sonic_py_common import daemon_base, logger
from sonic_py_common.task_base import ProcessTaskBase
from sonic_py_common.task_base import ThreadTaskBase
from swsscommon import swsscommon


Expand Down Expand Up @@ -743,8 +743,7 @@ class TemperatureUpdater(logger.Logger):
self.chassis_table._del(name)


class ThermalMonitor(ProcessTaskBase):

class ThermalMonitor(ThreadTaskBase):
def __init__(
self, chassis, initial_interval, update_interval, update_elapsed_threshold
):
Expand Down
64 changes: 32 additions & 32 deletions sonic-thermalctld/tests/test_thermalctld.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os
import sys
import multiprocessing
import threading
from imp import load_source # TODO: Replace with importlib once we no longer need to support Python 2

# TODO: Clean this up once we no longer need to support Python 2
Expand Down Expand Up @@ -131,7 +131,7 @@ class TestFanUpdater(object):
@mock.patch('thermalctld.update_entity_info', mock.MagicMock())
def test_refresh_fan_drawer_status_fan_drawer_get_name_not_impl(self):
# Test case where fan_drawer.get_name is not implemented
fan_updater = thermalctld.FanUpdater(MockChassis(), multiprocessing.Event())
fan_updater = thermalctld.FanUpdater(MockChassis(), threading.Event())
mock_fan_drawer = mock.MagicMock()
fan_updater._refresh_fan_drawer_status(mock_fan_drawer, 1)
assert thermalctld.update_entity_info.call_count == 0
Expand All @@ -145,7 +145,7 @@ def test_update_fan_with_exception(self):
fan.make_over_speed()
chassis.get_all_fans().append(fan)

fan_updater = thermalctld.FanUpdater(chassis, multiprocessing.Event())
fan_updater = thermalctld.FanUpdater(chassis, threading.Event())
fan_updater.update()
assert fan.get_status_led() == MockFan.STATUS_LED_COLOR_RED
assert fan_updater.log_warning.call_count == 1
Expand All @@ -162,15 +162,15 @@ def test_set_fan_led_exception(self):
mock_fan = MockFan()
mock_fan.set_status_led = mock.MagicMock(side_effect=NotImplementedError)

fan_updater = thermalctld.FanUpdater(MockChassis(), multiprocessing.Event())
fan_updater = thermalctld.FanUpdater(MockChassis(), threading.Event())
fan_updater._set_fan_led(mock_fan_drawer, mock_fan, 'Test Fan', fan_status)
assert fan_updater.log_warning.call_count == 1
fan_updater.log_warning.assert_called_with('Failed to set status LED for fan Test Fan, set_status_led not implemented')

def test_fan_absent(self):
chassis = MockChassis()
chassis.make_absent_fan()
fan_updater = thermalctld.FanUpdater(chassis, multiprocessing.Event())
fan_updater = thermalctld.FanUpdater(chassis, threading.Event())
fan_updater.update()
fan_list = chassis.get_all_fans()
assert fan_list[0].get_status_led() == MockFan.STATUS_LED_COLOR_RED
Expand All @@ -194,7 +194,7 @@ def test_fan_absent(self):
def test_fan_faulty(self):
chassis = MockChassis()
chassis.make_faulty_fan()
fan_updater = thermalctld.FanUpdater(chassis, multiprocessing.Event())
fan_updater = thermalctld.FanUpdater(chassis, threading.Event())
fan_updater.update()
fan_list = chassis.get_all_fans()
assert fan_list[0].get_status_led() == MockFan.STATUS_LED_COLOR_RED
Expand All @@ -218,7 +218,7 @@ def test_fan_faulty(self):
def test_fan_under_speed(self):
chassis = MockChassis()
chassis.make_under_speed_fan()
fan_updater = thermalctld.FanUpdater(chassis, multiprocessing.Event())
fan_updater = thermalctld.FanUpdater(chassis, threading.Event())
fan_updater.update()
fan_list = chassis.get_all_fans()
assert fan_list[0].get_status_led() == MockFan.STATUS_LED_COLOR_RED
Expand All @@ -234,7 +234,7 @@ def test_fan_under_speed(self):
def test_fan_over_speed(self):
chassis = MockChassis()
chassis.make_over_speed_fan()
fan_updater = thermalctld.FanUpdater(chassis, multiprocessing.Event())
fan_updater = thermalctld.FanUpdater(chassis, threading.Event())
fan_updater.update()
fan_list = chassis.get_all_fans()
assert fan_list[0].get_status_led() == MockFan.STATUS_LED_COLOR_RED
Expand All @@ -253,7 +253,7 @@ def test_update_psu_fans(self):
mock_fan = MockFan()
psu._fan_list.append(mock_fan)
chassis._psu_list.append(psu)
fan_updater = thermalctld.FanUpdater(chassis, multiprocessing.Event())
fan_updater = thermalctld.FanUpdater(chassis, threading.Event())
fan_updater.update()
assert fan_updater.log_warning.call_count == 0

Expand All @@ -274,7 +274,7 @@ def test_update_module_fans(self):
chassis.set_modular_chassis(True)
module._fan_list.append(mock_fan)
chassis._module_list.append(module)
fan_updater = thermalctld.FanUpdater(chassis, multiprocessing.Event())
fan_updater = thermalctld.FanUpdater(chassis, threading.Event())
fan_updater.update()
assert fan_updater.log_warning.call_count == 0

Expand Down Expand Up @@ -321,7 +321,7 @@ def test_insufficient_fan_number():
chassis = MockChassis()
chassis.make_absent_fan()
chassis.make_faulty_fan()
fan_updater = thermalctld.FanUpdater(chassis, multiprocessing.Event())
fan_updater = thermalctld.FanUpdater(chassis, threading.Event())
fan_updater.update()
assert fan_updater.log_warning.call_count == 3
expected_calls = [
Expand Down Expand Up @@ -405,7 +405,7 @@ class TestTemperatureUpdater(object):
"""
def test_deinit(self):
chassis = MockChassis()
temp_updater = thermalctld.TemperatureUpdater(chassis, multiprocessing.Event())
temp_updater = thermalctld.TemperatureUpdater(chassis, threading.Event())
temp_updater.temperature_status_dict = {'key1': 'value1', 'key2': 'value2'}
temp_updater.table = Table("STATE_DB", "xtable")
temp_updater.table._del = mock.MagicMock()
Expand All @@ -427,7 +427,7 @@ def test_deinit(self):

def test_deinit_exception(self):
chassis = MockChassis()
temp_updater = thermalctld.TemperatureUpdater(chassis, multiprocessing.Event())
temp_updater = thermalctld.TemperatureUpdater(chassis, threading.Event())
temp_updater.temperature_status_dict = {'key1': 'value1', 'key2': 'value2'}
temp_updater.table = Table("STATE_DB", "xtable")
temp_updater.table._del = mock.MagicMock()
Expand All @@ -451,7 +451,7 @@ def test_deinit_exception(self):
def test_over_temper(self):
chassis = MockChassis()
chassis.make_over_temper_thermal()
temperature_updater = thermalctld.TemperatureUpdater(chassis, multiprocessing.Event())
temperature_updater = thermalctld.TemperatureUpdater(chassis, threading.Event())
temperature_updater.update()
thermal_list = chassis.get_all_thermals()
assert temperature_updater.log_warning.call_count == 1
Expand All @@ -465,7 +465,7 @@ def test_over_temper(self):
def test_under_temper(self):
chassis = MockChassis()
chassis.make_under_temper_thermal()
temperature_updater = thermalctld.TemperatureUpdater(chassis, multiprocessing.Event())
temperature_updater = thermalctld.TemperatureUpdater(chassis, threading.Event())
temperature_updater.update()
thermal_list = chassis.get_all_thermals()
assert temperature_updater.log_warning.call_count == 1
Expand All @@ -482,7 +482,7 @@ def test_update_psu_thermals(self):
mock_thermal = MockThermal()
psu._thermal_list.append(mock_thermal)
chassis._psu_list.append(psu)
temperature_updater = thermalctld.TemperatureUpdater(chassis, multiprocessing.Event())
temperature_updater = thermalctld.TemperatureUpdater(chassis, threading.Event())
temperature_updater.update()
assert temperature_updater.log_warning.call_count == 0

Expand All @@ -502,7 +502,7 @@ def test_update_sfp_thermals(self):
mock_thermal = MockThermal()
sfp._thermal_list.append(mock_thermal)
chassis._sfp_list.append(sfp)
temperature_updater = thermalctld.TemperatureUpdater(chassis, multiprocessing.Event())
temperature_updater = thermalctld.TemperatureUpdater(chassis, threading.Event())
temperature_updater.update()
assert temperature_updater.log_warning.call_count == 0

Expand All @@ -523,7 +523,7 @@ def test_update_thermal_with_exception(self):
thermal.make_over_temper()
chassis.get_all_thermals().append(thermal)

temperature_updater = thermalctld.TemperatureUpdater(chassis, multiprocessing.Event())
temperature_updater = thermalctld.TemperatureUpdater(chassis, threading.Event())
temperature_updater.update()
assert temperature_updater.log_warning.call_count == 2

Expand All @@ -544,7 +544,7 @@ def test_update_module_thermals(self):
chassis = MockChassis()
chassis.make_module_thermal()
chassis.set_modular_chassis(True)
temperature_updater = thermalctld.TemperatureUpdater(chassis, multiprocessing.Event())
temperature_updater = thermalctld.TemperatureUpdater(chassis, threading.Event())
temperature_updater.update()
assert len(temperature_updater.all_thermals) == 3

Expand All @@ -559,21 +559,21 @@ def test_dpu_chassis_thermals():
# Modular chassis (Not a dpu chassis) No Change in TemperatureUpdater Behaviour
chassis.set_modular_chassis(True)
chassis.set_my_slot(1)
temperature_updater = thermalctld.TemperatureUpdater(chassis, multiprocessing.Event())
temperature_updater = thermalctld.TemperatureUpdater(chassis, threading.Event())
assert temperature_updater.chassis_table
# DPU chassis TemperatureUpdater without is_smartswitch False return - No update to CHASSIS_STATE_DB
chassis.set_modular_chassis(False)
chassis.set_dpu(True)
temperature_updater = thermalctld.TemperatureUpdater(chassis, multiprocessing.Event())
temperature_updater = thermalctld.TemperatureUpdater(chassis, threading.Event())
assert not temperature_updater.chassis_table
# DPU chassis TemperatureUpdater without get_dpu_id implmenetation- No update to CHASSIS_STATE_DB
chassis.set_smartswitch(True)
temperature_updater = thermalctld.TemperatureUpdater(chassis, multiprocessing.Event())
temperature_updater = thermalctld.TemperatureUpdater(chassis, threading.Event())
assert not temperature_updater.chassis_table
# DPU chassis TemperatureUpdater with get_dpu_id implemented - Update data to CHASSIS_STATE_DB
dpu_id = 1
chassis.set_dpu_id(dpu_id)
temperature_updater = thermalctld.TemperatureUpdater(chassis, multiprocessing.Event())
temperature_updater = thermalctld.TemperatureUpdater(chassis, threading.Event())
assert temperature_updater.chassis_table
# Table name in chassis state db = TEMPERATURE_INFO_0 for dpu_id 0
assert temperature_updater.chassis_table.table_name == f"{TEMPER_INFO_TABLE_NAME}_{dpu_id}"
Expand All @@ -588,7 +588,7 @@ def test_dpu_chassis_state_deinit():
chassis.set_modular_chassis(False)
chassis.set_dpu(True)
chassis.set_dpu_id(1)
temperature_updater = thermalctld.TemperatureUpdater(chassis, multiprocessing.Event())
temperature_updater = thermalctld.TemperatureUpdater(chassis, threading.Event())
assert temperature_updater.chassis_table
temperature_updater.table = Table("STATE_DB", "xtable")
temperature_updater.phy_entity_table = None
Expand All @@ -611,7 +611,7 @@ def test_updater_dpu_thermal_check_chassis_table():
chassis.set_dpu(True)
chassis.set_smartswitch(True)
chassis.set_dpu_id(1)
temperature_updater = thermalctld.TemperatureUpdater(chassis, multiprocessing.Event())
temperature_updater = thermalctld.TemperatureUpdater(chassis, threading.Event())
temperature_updater.update()
assert temperature_updater.chassis_table.get_size() == chassis.get_num_thermals()

Expand All @@ -628,17 +628,17 @@ def test_updater_thermal_check_modular_chassis():
chassis = MockChassis()
assert chassis.is_modular_chassis() == False

temperature_updater = thermalctld.TemperatureUpdater(chassis, multiprocessing.Event())
temperature_updater = thermalctld.TemperatureUpdater(chassis, threading.Event())
assert temperature_updater.chassis_table == None

chassis.set_modular_chassis(True)
chassis.set_my_slot(-1)
temperature_updater = thermalctld.TemperatureUpdater(chassis, multiprocessing.Event())
temperature_updater = thermalctld.TemperatureUpdater(chassis, threading.Event())
assert temperature_updater.chassis_table == None

my_slot = 1
chassis.set_my_slot(my_slot)
temperature_updater = thermalctld.TemperatureUpdater(chassis, multiprocessing.Event())
temperature_updater = thermalctld.TemperatureUpdater(chassis, threading.Event())
assert temperature_updater.chassis_table != None
assert temperature_updater.chassis_table.table_name == '{}_{}'.format(TEMPER_INFO_TABLE_NAME, str(my_slot))

Expand All @@ -651,7 +651,7 @@ def test_updater_thermal_check_chassis_table():

chassis.set_modular_chassis(True)
chassis.set_my_slot(1)
temperature_updater = thermalctld.TemperatureUpdater(chassis, multiprocessing.Event())
temperature_updater = thermalctld.TemperatureUpdater(chassis, threading.Event())

temperature_updater.update()
assert temperature_updater.chassis_table.get_size() == chassis.get_num_thermals()
Expand All @@ -670,7 +670,7 @@ def test_updater_thermal_check_min_max():

chassis.set_modular_chassis(True)
chassis.set_my_slot(1)
temperature_updater = thermalctld.TemperatureUpdater(chassis, multiprocessing.Event())
temperature_updater = thermalctld.TemperatureUpdater(chassis, threading.Event())

temperature_updater.update()
slot_dict = temperature_updater.chassis_table.get(thermal.get_name())
Expand Down Expand Up @@ -813,7 +813,7 @@ def test_get_chassis_exception(self):

# ThermalControlDaemon should raise SystemExit with CHASSIS_GET_ERROR code when chassis initialization fails
with pytest.raises(SystemExit) as exc_info:
daemon_thermalctld = thermalctld.ThermalControlDaemon()
daemon_thermalctld = thermalctld.ThermalControlDaemon(5, 60, 30)

# Verify it exits with the correct error code
assert exc_info.value.code == thermalctld.CHASSIS_GET_ERROR
Expand All @@ -836,7 +836,7 @@ def test_get_chassis_success(self):
mock_platform_instance.get_chassis.return_value = mock_chassis
mock_platform_class.return_value = mock_platform_instance

daemon_thermalctld = thermalctld.ThermalControlDaemon()
daemon_thermalctld = thermalctld.ThermalControlDaemon(5, 60, 30)

# Verify chassis was set correctly
assert daemon_thermalctld.chassis is mock_chassis
Expand Down
Loading