Skip to content

Commit f4f5e8f

Browse files
BregaladTarantmikuska
authored andcommitted
Fixed get_diagnostics (error handling, fixed endpoint construction). Added new unit tests.
1 parent 0822b26 commit f4f5e8f

File tree

2 files changed

+58
-24
lines changed

2 files changed

+58
-24
lines changed

tests/test_client_library.py

+39
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
from virl2_client.virl2_client import (
3434
ClientConfig,
3535
ClientLibrary,
36+
DiagnosticCategory,
3637
InitializationError,
3738
Version,
3839
)
@@ -739,3 +740,41 @@ def test_convergence_parametrization(client_library_server_current, mocked_sessi
739740
with pytest.raises(RuntimeError) as err:
740741
lab.wait_until_lab_converged(max_iterations=1)
741742
assert ("has not converged, maximum tries %s exceeded" % 1) in err.value.args[0]
743+
744+
745+
@pytest.mark.parametrize(
746+
"categories, expected_paths",
747+
[
748+
(None, [diag.value for diag in DiagnosticCategory]),
749+
([DiagnosticCategory.COMPUTES], [DiagnosticCategory.COMPUTES.value]),
750+
(
751+
[DiagnosticCategory.LABS, DiagnosticCategory.SERVICES],
752+
[DiagnosticCategory.LABS.value, DiagnosticCategory.SERVICES.value],
753+
),
754+
],
755+
)
756+
def test_get_diagnostics_paths(client_library, categories, expected_paths):
757+
with respx.mock(base_url="https://0.0.0.0/api/v0/") as respx_mock:
758+
for path in expected_paths:
759+
respx_mock.get(f"diagnostics/{path}").mock(
760+
return_value=httpx.Response(200, json={"data": "sample"})
761+
)
762+
diagnostics_data = client_library.get_diagnostics(categories=categories)
763+
for path in expected_paths:
764+
assert path in diagnostics_data
765+
assert diagnostics_data[path] == {"data": "sample"}
766+
767+
768+
def test_get_diagnostics_error_handling(client_library):
769+
with respx.mock(base_url="https://0.0.0.0/api/v0/") as respx_mock:
770+
for diag_type in DiagnosticCategory:
771+
respx_mock.get(f"diagnostics/{diag_type.value}").mock(
772+
return_value=httpx.Response(404)
773+
)
774+
775+
diagnostics_data = client_library.get_diagnostics()
776+
777+
for diag_type in DiagnosticCategory:
778+
assert diagnostics_data[diag_type.value] == {
779+
"error": f"Failed to fetch {diag_type.value} diagnostics"
780+
}

virl2_client/virl2_client.py

+19-24
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ def make_client(self) -> ClientLibrary:
163163
return client
164164

165165

166-
class Diagnostics(Enum):
166+
class DiagnosticCategory(Enum):
167167
COMPUTES = "computes"
168168
LABS = "labs"
169169
LAB_EVENTS = "lab_events"
@@ -207,7 +207,7 @@ class ClientLibrary:
207207
"labs": "labs",
208208
"lab": "labs/{lab_id}",
209209
"lab_topology": "labs/{lab_id}/topology",
210-
"diagnostics": "diagnostics",
210+
"diagnostics": "diagnostics/{category}",
211211
"system_health": "system_health",
212212
"system_stats": "system_stats",
213213
"populate_lab_tiles": "populate_lab_tiles",
@@ -639,16 +639,11 @@ def all_labs(self, show_all: bool = False) -> list[Lab]:
639639
:param show_all: Whether to get only labs owned by the admin or all user labs.
640640
:returns: A list of Lab objects.
641641
"""
642-
url = {"url": self._url_for("labs")}
643-
if show_all:
644-
url["params"] = {"show_all": True}
645-
lab_ids = self._session.get(**url).json()
646-
642+
lab_ids = self.get_lab_list(show_all=show_all)
647643
result = []
648644
for lab_id in lab_ids:
649645
lab = self.join_existing_lab(lab_id)
650646
result.append(lab)
651-
652647
return result
653648

654649
@locked
@@ -823,30 +818,30 @@ def join_existing_lab(self, lab_id: str, sync_lab: bool = True) -> Lab:
823818
self._labs[lab_id] = lab
824819
return lab
825820

826-
def get_diagnostics(self, types: list[Diagnostics] | None = None) -> dict:
821+
def get_diagnostics(
822+
self, categories: list[DiagnosticCategory] | None = None
823+
) -> dict:
827824
"""
828825
Return selected diagnostic data as a JSON object.
829826
830-
:param types: List of diagnostic types to fetch. If None, fetch all.
827+
:param categories: List of diagnostic categories to fetch. If None, fetch all.
831828
:returns: The diagnostic data.
832829
"""
833-
if types is None:
834-
types = list(Diagnostics)
830+
if categories is None:
831+
categories = list(DiagnosticCategory)
835832

836833
diagnostics_data = {}
837-
for diag_type in types:
838-
# Construct the endpoint path using given diagnostics value
839-
endpoint = f"diagnostics/{diag_type.value}"
840-
url = self._url_for(endpoint)
841-
response = self._session.get(url)
842-
843-
if response.status_code == 200:
844-
diagnostics_data[diag_type.value] = response.json()
845-
else:
846-
diagnostics_data[diag_type.value] = {
847-
"error": f"Failed to fetch {diag_type.value} diagnostics"
834+
for category in categories:
835+
value = category.value
836+
url = self._url_for("diagnostics", category=value)
837+
try:
838+
response = self._session.get(url)
839+
response.raise_for_status()
840+
diagnostics_data[value] = response.json()
841+
except httpx.HTTPStatusError:
842+
diagnostics_data[value] = {
843+
"error": f"Failed to fetch {value} diagnostics"
848844
}
849-
850845
return diagnostics_data
851846

852847
def get_system_health(self) -> dict:

0 commit comments

Comments
 (0)