86 lines
2.8 KiB
Python
86 lines
2.8 KiB
Python
"""Tests for IPMI client."""
|
|
import pytest
|
|
from unittest.mock import Mock, patch
|
|
from backend.ipmi_client import IPMIClient, TemperatureReading
|
|
|
|
|
|
def test_fan_mapping():
|
|
"""Test fan ID mapping."""
|
|
assert IPMIClient.FAN_MAPPING["0x00"] == 1
|
|
assert IPMIClient.FAN_MAPPING["0x06"] == 7
|
|
|
|
|
|
def test_hex_to_percent_conversion():
|
|
"""Test hex to percent conversion mapping."""
|
|
assert IPMIClient.HEX_TO_PERCENT["0x00"] == 0
|
|
assert IPMIClient.HEX_TO_PERCENT["0x64"] == 100
|
|
assert IPMIClient.PERCENT_TO_HEX[50] == "0x32"
|
|
assert IPMIClient.PERCENT_TO_HEX[100] == "0x64"
|
|
|
|
|
|
def test_determine_temp_location():
|
|
"""Test temperature location detection."""
|
|
client = IPMIClient("192.168.1.1", "user", "pass")
|
|
|
|
assert client._determine_temp_location("CPU1 Temp") == "cpu1"
|
|
assert client._determine_temp_location("CPU2 Temp") == "cpu2"
|
|
assert client._determine_temp_location("Processor 1") == "cpu1"
|
|
assert client._determine_temp_location("Inlet Temp") == "inlet"
|
|
assert client._determine_temp_location("Exhaust Temp") == "exhaust"
|
|
assert client._determine_temp_location("DIMM 1") == "memory"
|
|
assert client._determine_temp_location("Unknown Sensor") == "other"
|
|
|
|
|
|
def test_determine_sensor_type():
|
|
"""Test sensor type detection."""
|
|
client = IPMIClient("192.168.1.1", "user", "pass")
|
|
|
|
assert client._determine_sensor_type("CPU Temp") == "temperature"
|
|
assert client._determine_sensor_type("Fan 1 RPM") == "fan"
|
|
assert client._determine_sensor_type("12V") == "voltage"
|
|
assert client._determine_sensor_type("Power Supply") == "power"
|
|
|
|
|
|
def test_parse_sensor_value():
|
|
"""Test sensor value parsing."""
|
|
client = IPMIClient("192.168.1.1", "user", "pass")
|
|
|
|
value, unit = client._parse_sensor_value("45 degrees C")
|
|
assert value == 45.0
|
|
assert unit == "°C"
|
|
|
|
value, unit = client._parse_sensor_value("4200 RPM")
|
|
assert value == 4200.0
|
|
assert unit == "RPM"
|
|
|
|
value, unit = client._parse_sensor_value("12.05 Volts")
|
|
assert value == 12.05
|
|
assert unit == "V"
|
|
|
|
value, unit = client._parse_sensor_value("250 Watts")
|
|
assert value == 250.0
|
|
assert unit == "W"
|
|
|
|
|
|
@patch('backend.ipmi_client.subprocess.run')
|
|
def test_test_connection(mock_run):
|
|
"""Test connection test method."""
|
|
mock_run.return_value = Mock(returncode=0, stdout="", stderr="")
|
|
|
|
client = IPMIClient("192.168.1.1", "user", "pass")
|
|
result = client.test_connection()
|
|
|
|
assert result is True
|
|
mock_run.assert_called_once()
|
|
|
|
|
|
@patch('backend.ipmi_client.subprocess.run')
|
|
def test_test_connection_failure(mock_run):
|
|
"""Test connection test with failure."""
|
|
mock_run.return_value = Mock(returncode=1, stdout="", stderr="Error")
|
|
|
|
client = IPMIClient("192.168.1.1", "user", "pass")
|
|
result = client.test_connection()
|
|
|
|
assert result is False
|