Skip to content

Commit

Permalink
Remove unneccessary mock
Browse files Browse the repository at this point in the history
  • Loading branch information
sharonsyh committed Dec 1, 2024
1 parent 3ab4d89 commit f032488
Showing 1 changed file with 5 additions and 22 deletions.
27 changes: 5 additions & 22 deletions tests/test_metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,7 @@ def mock_gauge():
yield gauge


@patch("prometheus_client.exposition.push_to_gateway", autospec=True)
def test_energy_histogram(
mock_push_to_gateway: MagicMock,
mock_get_cpus: MagicMock,
mock_zeus_monitor: MagicMock,
mock_histogram: MagicMock,
Expand All @@ -79,12 +77,10 @@ def test_energy_histogram(
and that the correct energy values are recorded.
Args:
mock_push_to_gateway (MagicMock): Mocked `push_to_gateway`
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 @@ -161,20 +157,15 @@ def test_energy_histogram(
assert energy in calls, f"Expected DRAM energy {energy} in {calls}"


@patch("prometheus_client.exposition.push_to_gateway")
def test_energy_cumulative_counter(
mock_push_to_gateway: MagicMock,
mock_get_cpus: MagicMock,
mock_zeus_monitor: 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.
"""
mock_push_to_gateway.return_value = None
cpu_indices = [0, 1]
gpu_indices = [0, 1, 2]
prometheus_url = "http://localhost:9091"
Expand All @@ -195,10 +186,8 @@ def test_energy_cumulative_counter(
counter.labels = MagicMock(return_value=counter)
counter.inc = MagicMock()

with patch("prometheus_client.exposition.push_to_gateway") as mock_push:
mock_push.return_value = None
cumulative_counter.begin_window("test_counter")
cumulative_counter.end_window("test_counter")
cumulative_counter.begin_window("test_counter")
cumulative_counter.end_window("test_counter")

# Assert GPU counters
if mock_zeus_monitor.return_value.end_window.return_value.gpu_energy:
Expand All @@ -223,23 +212,19 @@ def test_energy_cumulative_counter(
cumulative_counter.cpu_counters[cpu_index].inc.assert_called_with(energy)


@patch("prometheus_client.exposition.push_to_gateway")
@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.
"""
mock_push_to_gateway.return_value = None
gpu_indices = [0, 1, 2]
prometheus_url = "http://localhost:9091"

Expand All @@ -260,10 +245,8 @@ def test_power_gauge(
gauge.labels = MagicMock(return_value=gauge)
gauge.set = MagicMock()

with patch("prometheus_client.exposition.push_to_gateway") as mock_push:
mock_push.return_value = None
power_gauge.begin_window("test_power_window")
power_gauge.end_window("test_power_window")
power_gauge.begin_window("test_power_window")
power_gauge.end_window("test_power_window")

# Assert that the gauges were set with the correct power values
if mock_power_monitor.return_value.get_power.return_value:
Expand Down

0 comments on commit f032488

Please sign in to comment.