Skip to content

Commit

Permalink
Add the mock_push_to_gateway Patch to each test
Browse files Browse the repository at this point in the history
  • Loading branch information
sharonsyh committed Nov 30, 2024
1 parent b62ed5d commit 0dbb236
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions tests/test_metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,25 @@ def mock_gauge():
yield gauge


@patch("prometheus_client.exposition.push_to_gateway", autospec=True)
def test_energy_histogram(
mock_get_cpus: MagicMock, mock_zeus_monitor: MagicMock, mock_histogram: MagicMock
mock_push_to_gateway: MagicMock,
mock_get_cpus: MagicMock,
mock_zeus_monitor: MagicMock,
mock_histogram: MagicMock,
) -> None:
"""Test EnergyHistogram class.
Validates that GPU, CPU, and DRAM histograms are properly initialized,
and that the correct energy values are recorded.
Args:
mock_push_to_gateway (MagicMock): Mocked `push_to_gateway` function for Prometheus.
mock_get_cpus (MagicMock): Mocked `get_cpus` fixture.
mock_zeus_monitor (MagicMock): Mocked ZeusMonitor fixture.
mock_histogram (MagicMock): Mocked Prometheus Histogram fixture.
"""
mock_push_to_gateway.return_value = None
cpu_indices = [0, 1]
gpu_indices = [0, 1, 2]
prometheus_url = "http://localhost:9091"
Expand Down Expand Up @@ -150,12 +156,16 @@ def test_energy_histogram(
assert energy in calls, f"Expected DRAM energy {energy} in {calls}"


@patch("prometheus_client.exposition.push_to_gateway", autospec=True)
def test_energy_cumulative_counter(
mock_get_cpus: MagicMock, mock_zeus_monitor: MagicMock
mock_push_to_gateway: MagicMock,
mock_get_cpus: MagicMock,
mock_zeus_monitor: MagicMock,
) -> None:
"""Test EnergyCumulativeCounter with mocked ZeusMonitor.
Args:
mock_push_to_gateway (MagicMock): Mocked `push_to_gateway` function for Prometheus.
mock_get_cpus (MagicMock): Mocked `get_cpus` fixture.
mock_zeus_monitor (MagicMock): Mocked ZeusMonitor fixture.
"""
Expand Down Expand Up @@ -205,15 +215,18 @@ def test_energy_cumulative_counter(
cumulative_counter.cpu_counters[cpu_index].inc.assert_called_with(energy)


@patch("prometheus_client.exposition.push_to_gateway", autospec=True)
@patch("zeus.device.gpu.get_gpus")
def test_power_gauge(
mock_push_to_gateway: MagicMock,
mock_get_gpus: MagicMock,
mock_power_monitor: MagicMock,
mock_gauge: MagicMock,
) -> None:
"""Test PowerGauge with mocked PowerMonitor and Prometheus Gauges.
Args:
mock_push_to_gateway (MagicMock): Mocked `push_to_gateway` function for Prometheus.
mock_get_gpus (MagicMock): Mocked `get_gpus` function to simulate available GPUs.
mock_power_monitor (MagicMock): Mocked PowerMonitor to simulate GPU power data.
mock_gauge (MagicMock): Mocked Prometheus Gauge creation.
Expand Down

0 comments on commit 0dbb236

Please sign in to comment.