sonic-buildimage/platform/mellanox/mlnx-platform-api/tests/test_fan_api.py
Vivek 342604a4bb
[mellanox] Integrate HW-MGMT Version 7.0030.2008 (#17170)
* [Mellanox] Don't populate arm64 Kconfig when integrating hw-mgmt

Signed-off-by: Vivek Reddy <vkarri@nvidia.com>

* Intgerate HW-MGMT 7.0030.2008 Changes

 ## Patch List
* 0285-UBUNTU-SAUCE-mlxbf-gige-Fix-intermittent-no-ip-issue.patch :
* 0286-pinctrl-Introduce-struct-pinfunction-and-PINCTRL_PIN.patch :
* 0287-pinctrl-mlxbf3-Add-pinctrl-driver-support.patch :
* 0288-UBUNTU-SAUCE-gpio-mmio-handle-ngpios-properly-in-bgp.patch :
* 0289-UBUNTU-SAUCE-gpio-mlxbf3-Add-gpio-driver-support.patch :
* 0291-mlxsw-core_hwmon-Align-modules-label-name-assignment.patch :
* 0292-mlxsw-i2c-Limit-single-transaction-buffer-size.patch :
* 0293-mlxsw-reg-Limit-MTBR-register-records-buffer-by-one-.patch :
* 0296-UBUNTU-SAUCE-mmc-sdhci-of-dwcmshc-Add-runtime-PM-ope.patch :
* 0298-UBUNTU-SAUCE-mlxbf-ptm-use-0444-instead-of-S_IRUGO.patch :
* 0299-UBUNTU-SAUCE-mlxbf-ptm-add-atx-debugfs-nodes.patch :
* 0300-UBUNTU-SAUCE-mlxbf-ptm-update-module-version.patch :
* 0301-UBUNTU-SAUCE-mlxbf-gige-Fix-kernel-panic-at-shutdown.patch :
* 0302-UBUNTU-SAUCE-mlxbf-bootctl-support-SMC-call-for-sett.patch :
* 0303-UBUNTU-SAUCE-Add-BF3-related-ACPI-config-and-Ring-de.patch :
* 0306-dt-bindings-trivial-devices-Add-infineon-xdpe1a2g7.patch :
* 0307-leds-mlxreg-Add-support-for-new-flavour-of-capabilit.patch :
* 0308-leds-mlxreg-Remove-code-for-amber-LED-colour.patch :
* 0308-platform_data-mlxreg-Add-capability-bit-and-mask-fie.patch :
* 0309-hwmon-mlxreg-fan-Add-support-for-new-flavour-of-capa.patch :
* 0310-hwmon-mlxreg-fan-Extend-number-of-supporetd-fans.patch :
* 0317-platform-mellanox-Introduce-support-for-switches-equ.patch :
* 0318-mellanox-Relocate-mlx-platform-driver.patch :
* 0319-UBUNTU-SAUCE-mlxbf-tmfifo-fix-potential-race.patch :
* 0320-UBUNTU-SAUCE-mlxbf-tmfifo-Drop-the-Rx-packet-if-no-m.patch :
* 0321-UBUNTU-SAUCE-mlxbf-tmfifo-Drop-jumbo-frames.patch :
* 0322-UBUNTU-SAUCE-mlxbf-tmfifo.c-Amend-previous-tmfifo-pa.patch :
* 0323-mlxbf_gige-add-set_link_ksettings-ethtool-callback.patch :
* 0324-mlxbf_gige-fix-white-space-in-mlxbf_gige_eth_ioctl.patch :
* 0325-UBUNTU-SAUCE-mlxbf-bootctl-Fix-kernel-panic-due-to-b.patch :
* 0326-platform-mellanox-mlxreg-hotplug-Add-support-for-new.patch :
* 0327-platform-mellanox-mlx-platform-Change-register-name.patch :
* 0328-platform-mellanox-mlx-platform-Add-support-for-new-X.patch :

* [Mellanox] Remove thermal zone related code and replace with new one

* Revert "Revert "[Mellanox] Align PSU temperature sysfs node name with hw-management change (#16820)" (#16956)"

This reverts commit c2edc6f9d5.

---------

Signed-off-by: Vivek Reddy <vkarri@nvidia.com>
Co-authored-by: Junchao-Mellanox <junchao@nvidia.com>
Co-authored-by: Junchao-Mellanox <57339448+Junchao-Mellanox@users.noreply.github.com>
2023-11-16 22:49:29 +08:00

158 lines
6.5 KiB
Python

#
# Copyright (c) 2020-2023 NVIDIA CORPORATION & AFFILIATES.
# Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import os
import pytest
import subprocess
import sys
from mock import MagicMock, patch
test_path = os.path.dirname(os.path.abspath(__file__))
modules_path = os.path.dirname(test_path)
sys.path.insert(0, modules_path)
from sonic_platform import utils
from sonic_platform.fan import Fan, PsuFan, FAN_DIR_VALUE_INTAKE, FAN_DIR_VALUE_EXHAUST
from sonic_platform.fan_drawer import RealDrawer, VirtualDrawer
from sonic_platform.psu import Psu
class TestFan:
def test_fan_drawer_basic(self):
# Real drawer
fan_drawer = RealDrawer(0)
assert fan_drawer.get_index() == 1
assert fan_drawer.get_name() == 'drawer1'
utils.read_int_from_file = MagicMock(return_value=1)
assert fan_drawer.get_presence() is True
utils.read_int_from_file = MagicMock(return_value=0)
assert fan_drawer.get_presence() is False
assert fan_drawer.get_position_in_parent() == 1
assert fan_drawer.is_replaceable() is True
fan_drawer.get_presence = MagicMock(return_value=False)
assert fan_drawer.get_direction() == Fan.FAN_DIRECTION_NOT_APPLICABLE
fan_drawer.get_presence = MagicMock(return_value=True)
assert fan_drawer.get_direction() == Fan.FAN_DIRECTION_EXHAUST
utils.read_int_from_file = MagicMock(return_value=1)
assert fan_drawer.get_direction() == Fan.FAN_DIRECTION_INTAKE
# Invalid fan dir value
utils.read_int_from_file = MagicMock(return_value=2)
assert fan_drawer.get_direction() == Fan.FAN_DIRECTION_NOT_APPLICABLE
utils.read_int_from_file = MagicMock(side_effect=ValueError(''))
assert fan_drawer.get_direction() == Fan.FAN_DIRECTION_NOT_APPLICABLE
utils.read_int_from_file = MagicMock(side_effect=IOError(''))
assert fan_drawer.get_direction() == Fan.FAN_DIRECTION_NOT_APPLICABLE
# Virtual drawer
fan_drawer = VirtualDrawer(0)
assert fan_drawer.get_name() == 'N/A'
assert fan_drawer.get_presence() is True
assert fan_drawer.is_replaceable() is False
def test_system_fan_basic(self):
fan_drawer = RealDrawer(0)
fan = Fan(2, fan_drawer, 1)
assert fan.get_position_in_parent() == 1
assert fan.is_replaceable() is False
assert fan.get_speed_tolerance() == 50
assert fan.get_name() == 'fan3'
mock_sysfs_content = {
fan.fan_speed_get_path: 50,
fan.fan_max_speed_path: 100,
fan.fan_status_path: 0,
fan.fan_speed_set_path: 153
}
def mock_read_int_from_file(file_path, default=0, raise_exception=False):
return mock_sysfs_content[file_path]
utils.read_int_from_file = mock_read_int_from_file
assert fan.get_speed() == 50
mock_sysfs_content[fan.fan_speed_get_path] = 101
assert fan.get_speed() == 100
mock_sysfs_content[fan.fan_max_speed_path] = 0
assert fan.get_speed() == 101
assert fan.get_status() is True
mock_sysfs_content[fan.fan_status_path] = 1
assert fan.get_status() is False
assert fan.get_target_speed() == 60
fan.fan_drawer.get_direction = MagicMock(return_value=Fan.FAN_DIRECTION_EXHAUST)
assert fan.get_direction() == Fan.FAN_DIRECTION_EXHAUST
fan.fan_drawer.get_presence = MagicMock(return_value=True)
assert fan.get_presence() is True
@patch('sonic_platform.utils.write_file')
def test_system_fan_set_speed(self, mock_write_file):
fan_drawer = RealDrawer(0)
fan = Fan(2, fan_drawer, 1)
fan.set_speed(60)
mock_write_file.assert_called_with(fan.fan_speed_set_path, 153, raise_exception=True)
@patch('sonic_platform.utils.read_int_from_file')
@patch('sonic_platform.psu.Psu.get_presence')
@patch('sonic_platform.psu.Psu.get_powergood_status')
@patch('os.path.exists')
def test_psu_fan_basic(self, mock_path_exists, mock_powergood, mock_presence, mock_read_int):
mock_path_exists.return_value = False
psu = Psu(0)
fan = PsuFan(0, 1, psu)
assert fan.get_direction() == Fan.FAN_DIRECTION_NOT_APPLICABLE
assert fan.get_status() is True
assert fan.get_presence() is False
mock_presence.return_value = True
assert fan.get_presence() is False
mock_powergood.return_value = True
assert fan.get_presence() is False
mock_path_exists.return_value = True
assert fan.get_presence() is True
mock_read_int.return_value = int(255 / 10 * 7)
assert fan.get_target_speed() == 60
mock_read_int.return_value = FAN_DIR_VALUE_INTAKE
assert fan.get_direction() == Fan.FAN_DIRECTION_INTAKE
mock_read_int.return_value = FAN_DIR_VALUE_EXHAUST
assert fan.get_direction() == Fan.FAN_DIRECTION_EXHAUST
mock_read_int.return_value = -1 # invalid value
assert fan.get_direction() == Fan.FAN_DIRECTION_NOT_APPLICABLE
def test_psu_fan_set_speed(self):
psu = Psu(0)
fan = PsuFan(0, 1, psu)
subprocess.check_call = MagicMock()
mock_file_content = {
fan.psu_i2c_bus_path: 'bus',
fan.psu_i2c_addr_path: 'addr',
fan.psu_i2c_command_path: 'command'
}
def mock_read_str_from_file(file_path, default='', raise_exception=False):
return mock_file_content[file_path]
utils.read_str_from_file = mock_read_str_from_file
fan.set_speed(60)
assert subprocess.check_call.call_count == 0
fan.get_presence = MagicMock(return_value=True)
assert fan.set_speed(60)
subprocess.check_call.assert_called_with(["i2cset", "-f", "-y", "bus", "addr", "command", hex(60), "wp"], universal_newlines=True)
subprocess.check_call = MagicMock(side_effect=subprocess.CalledProcessError('', ''))
assert not fan.set_speed(60)
subprocess.check_call = MagicMock()
utils.read_str_from_file = MagicMock(side_effect=RuntimeError(''))
assert not fan.set_speed(60)