Skip to content

Commit

Permalink
tests: refactor mock_{agent,prometheus}_response()
Browse files Browse the repository at this point in the history
  • Loading branch information
rezib committed Nov 13, 2024
1 parent 90b0f0e commit a6c0441
Showing 3 changed files with 18 additions and 37 deletions.
6 changes: 3 additions & 3 deletions slurmweb/tests/metrics/test_db.py
Original file line number Diff line number Diff line change
@@ -25,20 +25,20 @@ def setUp(self):

@mock.patch("slurmweb.metrics.db.requests.get")
def test_request(self, mock_requests_get):
mock_requests_get.return_value = mock_prometheus_response("nodes-hour")
_, mock_requests_get.return_value = mock_prometheus_response("nodes-hour")
self.db.request("nodes", "hour")

@mock.patch("slurmweb.metrics.db.requests.get")
def test_request_empty_result(self, mock_requests_get):
mock_requests_get.return_value = mock_prometheus_response("unknown-metric")
_, mock_requests_get.return_value = mock_prometheus_response("unknown-metric")
with self.assertRaisesRegex(
SlurmwebMetricsDBError, "^Empty result for query .*$"
):
self.db.request("nodes", "hour")

@mock.patch("slurmweb.metrics.db.requests.get")
def test_request_unknown_path(self, mock_requests_get):
mock_requests_get.return_value = mock_prometheus_response("unknown-path")
_, mock_requests_get.return_value = mock_prometheus_response("unknown-path")
with self.assertRaisesRegex(
SlurmwebMetricsDBError, "^Prometheus error for query .*$"
):
8 changes: 4 additions & 4 deletions slurmweb/tests/test_agent.py
Original file line number Diff line number Diff line change
@@ -803,7 +803,7 @@ def test_request_metrics_error(self):

@mock.patch("slurmweb.metrics.db.requests.get")
def test_request_metrics_nodes(self, mock_requests_get):
mock_requests_get.return_value = mock_prometheus_response("nodes-hour")
_, mock_requests_get.return_value = mock_prometheus_response("nodes-hour")
response = self.client.get(f"/v{get_version()}/metrics/nodes")
self.assertEqual(response.status_code, 200)
self.assertCountEqual(
@@ -813,7 +813,7 @@ def test_request_metrics_nodes(self, mock_requests_get):

@mock.patch("slurmweb.metrics.db.requests.get")
def test_request_metrics_cores(self, mock_requests_get):
mock_requests_get.return_value = mock_prometheus_response("cores-hour")
_, mock_requests_get.return_value = mock_prometheus_response("cores-hour")
response = self.client.get(f"/v{get_version()}/metrics/cores")
self.assertEqual(response.status_code, 200)
self.assertCountEqual(
@@ -823,7 +823,7 @@ def test_request_metrics_cores(self, mock_requests_get):

@mock.patch("slurmweb.metrics.db.requests.get")
def test_request_metrics_jobs(self, mock_requests_get):
mock_requests_get.return_value = mock_prometheus_response("jobs-hour")
_, mock_requests_get.return_value = mock_prometheus_response("jobs-hour")
response = self.client.get(f"/v{get_version()}/metrics/jobs")
self.assertEqual(response.status_code, 200)
self.assertCountEqual(
@@ -896,7 +896,7 @@ def test_request_metrics_jobs_denied(self):

@mock.patch("slurmweb.metrics.db.requests.get")
def test_request_metrics_unexpected(self, mock_requests_get):
mock_requests_get.return_value = mock_prometheus_response("unknown-metric")
_, mock_requests_get.return_value = mock_prometheus_response("unknown-metric")
response = self.client.get(f"/v{get_version()}/metrics/jobs")
self.assertEqual(response.status_code, 500)
self.assertRegex(response.json["description"], "^Empty result for query .*$")
41 changes: 11 additions & 30 deletions slurmweb/tests/utils.py
Original file line number Diff line number Diff line change
@@ -119,23 +119,23 @@ def fake_text_response():
return text, response


def mock_agent_response(asset_name, remove_key=None):
def mock_component_response(component, asset_name, remove_key=None):
"""Return mocked requests Response corresponding to the given agent asset."""
with open(ASSETS / "agent/status.json") as fh:
with open(ASSETS / component / "status.json") as fh:
requests_statuses = json.load(fh)

if asset_name not in requests_statuses:
warnings.warn(
f"Unable to find asset {asset_name} in agent requests status file"
f"Unable to find asset {asset_name} in {component} requests status file"
)
raise SlurmwebAssetUnavailable()

is_json = True
if requests_statuses[asset_name]["content-type"] == "application/json":
asset = load_json_asset(f"agent/{asset_name}.json")
asset = load_json_asset(f"{component}/{asset_name}.json")
else:
is_json = False
asset = load_asset(f"agent/{asset_name}.txt")
asset = load_asset(f"{component}/{asset_name}.txt")

# Remove specific key from asset, if JSON asset and key to remove is specified. This
# is useful to test some error case.
@@ -154,33 +154,14 @@ def mock_agent_response(asset_name, remove_key=None):
return asset, response


def mock_prometheus_response(asset_name):
"""Return mocked requests Response corresponding to the given Prometheus asset."""
with open(ASSETS / "prometheus/status.json") as fh:
requests_statuses = json.load(fh)
def mock_agent_response(asset_name, remove_key=None):
"""Return mocked requests Response corresponding to the given agent asset."""
return mock_component_response("agent", asset_name, remove_key)

if asset_name not in requests_statuses:
warnings.warn(
f"Unable to find asset {asset_name} in Prometheus requests status file"
)
raise SlurmwebAssetUnavailable()

is_json = True
if requests_statuses[asset_name]["content-type"] == "application/json":
asset = load_json_asset(f"prometheus/{asset_name}.json")
else:
is_json = False
asset = load_asset(f"prometheus/{asset_name}.txt")
response = mock.create_autospec(requests.Response)
response.url = "/mocked/query"
response.status_code = requests_statuses[asset_name]["status"]
response.headers = {"content-type": requests_statuses[asset_name]["content-type"]}
if is_json:
response.json = mock.Mock(return_value=asset)
else:
response.text = mock.PropertyMock(return_value=asset)

return response
def mock_prometheus_response(asset_name):
"""Return mocked requests Response corresponding to the given Prometheus asset."""
return mock_component_response("prometheus", asset_name)


class RemoveActionInPolicy:

0 comments on commit a6c0441

Please sign in to comment.