From e3ca5b75d66f5037828f1ee168ef0abe3e39ed3e Mon Sep 17 00:00:00 2001 From: Mohammad Torkashvand <mohammad.torkashvand@geant.org> Date: Wed, 9 Apr 2025 14:18:35 +0200 Subject: [PATCH] add rest version api --- gso/api/v1/__init__.py | 2 ++ gso/api/v1/version.py | 20 ++++++++++++++++++++ test/__init__.py | 3 --- test/api/test_version.py | 21 +++++++++++++++++++++ test/conftest.py | 13 ++++++++----- 5 files changed, 51 insertions(+), 8 deletions(-) create mode 100644 gso/api/v1/version.py create mode 100644 test/api/test_version.py diff --git a/gso/api/v1/__init__.py b/gso/api/v1/__init__.py index 8772f54ef..e2493364c 100644 --- a/gso/api/v1/__init__.py +++ b/gso/api/v1/__init__.py @@ -4,8 +4,10 @@ from fastapi import APIRouter from gso.api.v1.network import router as network_router from gso.api.v1.subscriptions import router as subscriptions_router +from gso.api.v1.version import router as version_router router = APIRouter() router.include_router(subscriptions_router) router.include_router(network_router) +router.include_router(version_router) diff --git a/gso/api/v1/version.py b/gso/api/v1/version.py new file mode 100644 index 000000000..96cc37f83 --- /dev/null +++ b/gso/api/v1/version.py @@ -0,0 +1,20 @@ +"""API for getting the version of the orchestrator.""" + +from fastapi import APIRouter, Depends +from orchestrator.graphql.resolvers.version import VERSIONS +from orchestrator.security import authorize +from starlette import status + +router = APIRouter(prefix="/version", tags=["Network"], dependencies=[Depends(authorize)]) + + +@router.get("/", status_code=status.HTTP_200_OK) +def version() -> dict[str, str]: + """Get the version of deployed services.""" + version_dict = {} + for item in VERSIONS: + if ":" in item: + key, value = item.split(":", 1) + version_dict[key.strip()] = value.strip() + + return version_dict diff --git a/test/__init__.py b/test/__init__.py index d9001c931..433e89b0f 100644 --- a/test/__init__.py +++ b/test/__init__.py @@ -1,4 +1,3 @@ -import os from uuid import uuid4 LSO_RESULT_SUCCESS = { @@ -20,5 +19,3 @@ LSO_RESULT_FAILURE = { } USER_CONFIRM_EMPTY_FORM = [{}] - -os.environ["TESTING"] = "true" diff --git a/test/api/test_version.py b/test/api/test_version.py new file mode 100644 index 000000000..ffcf7acb1 --- /dev/null +++ b/test/api/test_version.py @@ -0,0 +1,21 @@ +from importlib import metadata + + +def test_version_endpoint(test_client, monkeypatch): + """ + Test that the /version endpoint returns the correct versions: + """ + response = test_client.get("api/v1/version") + + assert response.status_code == 200 + + data = response.json() + + assert data == { + "GAP Ansible collection": "Unknown", + "GÉANT Service Orchestrator": metadata.version("geant-service-orchestrator"), + "GÉANT Service Orchestrator GUI": "Unknown", + "LSO": "Unknown", + "Moodi Ansible collection": "Unknown", + "orchestrator-core": "3.1.1", + } diff --git a/test/conftest.py b/test/conftest.py index 2c015bd1b..926cd183b 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -54,13 +54,16 @@ class UseJuniperSide(strEnum): def pytest_configure(config): """Set an environment variable before loading any test modules.""" - config_filename = "gso/oss-params-example.json" - os.environ["OSS_PARAMS_FILENAME"] = config_filename + # Set environment variables for the test session + os.environ["OSS_PARAMS_FILENAME"] = "gso/oss-params-example.json" + os.environ["TESTING"] = "true" + # Register finalizers to clean up after tests are done + def cleanup() -> None: + del os.environ["OSS_PARAMS_FILENAME"] + del os.environ["TESTING"] -def pytest_unconfigure(config): - """Clean up the environment variable after all tests.""" - os.environ.pop("OSS_PARAMS_FILENAME", None) + pytest.session_cleanup = cleanup class FakerProvider(BaseProvider): -- GitLab