Skip to content

Commit

Permalink
test: Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jneo8 committed Nov 16, 2023
1 parent 33712c6 commit edffa93
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 1 deletion.
27 changes: 27 additions & 0 deletions tests/unit/test_collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,9 @@ def test_30_ipmi_dcmi_collector_not_installed(self):
ipmi_dcmi_collector.ipmi_dcmi = Mock()
ipmi_dcmi_collector.ipmi_dcmi.installed = False
ipmi_dcmi_collector.ipmi_dcmi.get_current_power.return_value = {}

ipmi_dcmi_collector.ipmi_tool = Mock()
ipmi_dcmi_collector.dmidecode = Mock()
payloads = ipmi_dcmi_collector.collect()

self.assertEqual(len(list(payloads)), 1)
Expand All @@ -303,10 +306,34 @@ def test_31_ipmi_dcmi_collector_installed_and_okay(self):
"""Test ipmi dcmi collector can fetch correct number of metrics."""
ipmi_dcmi_collector = IpmiDcmiCollector(Mock())
ipmi_dcmi_collector.ipmi_dcmi = Mock()
ipmi_dcmi_collector.ipmi_tool = Mock()
ipmi_dcmi_collector.dmidecode = Mock()

mock_dcmi_payload = {"current_power": 105}

ipmi_dcmi_collector.ipmi_dcmi.get_current_power.return_value = mock_dcmi_payload
ipmi_dcmi_collector.ipmi_tool.get_ps_redundancy.return_value = (True, True)
ipmi_dcmi_collector.dmidecode.get_power_capacities.return_value = [1000, 1000]

payloads = ipmi_dcmi_collector.collect()

available_metrics = [spec.name for spec in ipmi_dcmi_collector.specifications]
self.assertEqual(len(list(payloads)), len(available_metrics))
for payload in payloads:
self.assertIn(payload.name, available_metrics)

def test_32_ipmi_dcmi_collector_get_ps_redundancy_not_ok(self):
"""Test ipmi dcmi collector can fetch correct number of metrics."""
ipmi_dcmi_collector = IpmiDcmiCollector(Mock())
ipmi_dcmi_collector.ipmi_dcmi = Mock()
ipmi_dcmi_collector.ipmi_tool = Mock()
ipmi_dcmi_collector.dmidecode = Mock()

mock_dcmi_payload = {"current_power": 105}

ipmi_dcmi_collector.ipmi_dcmi.get_current_power.return_value = mock_dcmi_payload
ipmi_dcmi_collector.ipmi_tool.get_ps_redundancy.return_value = (False, True)
ipmi_dcmi_collector.dmidecode.get_power_capacities.return_value = [1000, 1000]

payloads = ipmi_dcmi_collector.collect()

Expand Down
27 changes: 27 additions & 0 deletions tests/unit/test_dmidecode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import unittest
from unittest.mock import patch

from prometheus_hardware_exporter.collectors.dmidecode import Dmidecode
from prometheus_hardware_exporter.utils import Command, Result

TYPE_39_OUTPUT = "tests/unit/test_resources/dmidecode/dmidecode_type_39_output.txt"


class TestDmidecode(unittest.TestCase):
"""Test the Dmidecode class."""

@patch.object(Command, "__call__")
def test_00_get_power_capacities_success(self, mock_call):
with open(TYPE_39_OUTPUT, "r") as content:
mock_call.return_value = Result(content.read(), None)
dmidecode = Dmidecode()
power_capacities = dmidecode.get_power_capacities()
self.assertEqual(power_capacities, [1400, 1400])

@patch.object(Command, "__call__")
def test_01_get_power_capacities_error(self, mock_call):
mock_call.return_value = Result("", True)

dmidecode = Dmidecode()
power_capacities = dmidecode.get_power_capacities()
self.assertEqual(power_capacities, [])
22 changes: 21 additions & 1 deletion tests/unit/test_ipmi_dcmi.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import unittest
from unittest.mock import patch

from prometheus_hardware_exporter.collectors.ipmi_dcmi import IpmiDcmi
from prometheus_hardware_exporter.collectors.ipmi_dcmi import IpmiDcmi, IpmiTool
from prometheus_hardware_exporter.utils import Command, Result

DCMI_SAMPLE_OUTPUT = "tests/unit/test_resources/ipmi/ipmi_dcmi_sample_output.txt"
IPMITOOL_SDR_PS_SAMPLE_OUTPUT = "tests/unit/test_resources/ipmi/ipmitool_sdr_ps_sample_output.txt"


class TestIpmiDcmi(unittest.TestCase):
Expand All @@ -31,3 +32,22 @@ def test_01_get_current_power_parse_failure(self, mock_call):
ipmi_dcmi = IpmiDcmi()
payload = ipmi_dcmi.get_current_power()
self.assertEqual(payload, {})


class TestIpmiTool(unittest.TestCase):
"""Test the IpmiTool class."""

@patch.object(Command, "__call__")
def test_00_get_ps_redundancy_success(self, mock_call):
with open(IPMITOOL_SDR_PS_SAMPLE_OUTPUT, "r") as content:
mock_call.return_value = Result(content.read(), None)
ipmitool = IpmiTool()
ps_redundancy = ipmitool.get_ps_redundancy()
self.assertEqual(ps_redundancy, (True, True))

@patch.object(Command, "__call__")
def test_01_get_ps_redundancy_error(self, mock_call):
mock_call.return_value = Result("", True)
ipmitool = IpmiTool()
ps_redundancy = ipmitool.get_ps_redundancy()
self.assertEqual(ps_redundancy, (False, False))
35 changes: 35 additions & 0 deletions tests/unit/test_resources/dmidecode/dmidecode_type_39_output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# dmidecode 3.3
Getting SMBIOS data from sysfs.
SMBIOS 3.3.0 present.

Handle 0x2700, DMI type 39, 22 bytes
System Power Supply
Location: Not Specified
Name: PWR SPLY,1400W,RDNT,LTON
Manufacturer: DELL
Serial Number: CNLOD0019M3A6D
Asset Tag: Not Specified
Model Part Number: 01CW9GA04
Revision: Not Specified
Max Power Capacity: 1400 W
Status: Present, Unknown
Type: Unknown
Input Voltage Range Switching: Unknown
Plugged: Yes
Hot Replaceable: Yes

Handle 0x2701, DMI type 39, 22 bytes
System Power Supply
Location: Not Specified
Name: PWR SPLY,1400W,RDNT,LTON
Manufacturer: DELL
Serial Number: CNLOD0019M369D
Asset Tag: Not Specified
Model Part Number: 01CW9GA04
Revision: Not Specified
Max Power Capacity: 1400 W
Status: Present, Unknown
Type: Unknown
Input Voltage Range Switching: Unknown
Plugged: Yes
Hot Replaceable: Yes
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
PS Redundancy,77h,ok,7.1,Fully Redundant
Status,85h,ok,10.1,Presence detected
Status,86h,ok,10.2,Presence detected

0 comments on commit edffa93

Please sign in to comment.