diff --git a/tests/v2/context.py b/tests/v2/context.py new file mode 100644 index 0000000..04fe739 --- /dev/null +++ b/tests/v2/context.py @@ -0,0 +1,6 @@ +import os +import sys + +sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))) + +from firecrest.v2.AsyncClient import AsyncFirecrest diff --git a/tests/v2/responses/systems.json b/tests/v2/responses/systems.json new file mode 100644 index 0000000..558003e --- /dev/null +++ b/tests/v2/responses/systems.json @@ -0,0 +1,115 @@ +{ + "status_code": 200, + "response": { + "systems": [ + { + "name": "cluster-api", + "host": "192.168.240.2", + "sshPort": 22, + "sshCertEmbeddedCmd": true, + "scheduler": { + "type": "slurm", + "version": "24.05.0", + "apiUrl": "http://192.168.240.2:6820", + "apiVersion": "0.0.40" + }, + "health": { + "lastChecked": "2024-10-04T14:39:29.092143Z", + "latency": 0.006885528564453125, + "healthy": true, + "message": null, + "nodes": { + "available": 1, + "total": 1 + } + }, + "probing": { + "interval": 60, + "timeout": 10, + "healthyLatency": 1.5, + "healthyLoad": 0.8, + "startupGracePeriod": 300 + }, + "fileSystems": [ + { + "path": "/home", + "dataType": "users", + "defaultWorkDir": true + }, + { + "path": "/store", + "dataType": "store", + "defaultWorkDir": false + }, + { + "path": "/archive", + "dataType": "archive", + "defaultWorkDir": false + } + ], + "datatransferJobsDirectives": [ + "#SBATCH --constraint=mc", + "#SBATCH --nodes=1", + "#SBATCH --time=0-00:15:00" + ], + "timeouts": { + "sshConnection": 5, + "sshLogin": 5, + "sshCommandExecution": 5 + } + }, + { + "name": "cluster-ssh", + "host": "192.168.240.2", + "sshPort": 22, + "sshCertEmbeddedCmd": true, + "scheduler": { + "type": "slurm", + "version": "24.05.0", + "apiUrl": null, + "apiVersion": null + }, + "health": { + "lastChecked": "2024-10-04T14:39:29.696364Z", + "latency": 0.6117508411407471, + "healthy": true, + "message": null, + "nodes": { + "available": 1, + "total": 1 + } + }, + "probing": { + "interval": 60, + "timeout": 10, + "healthyLatency": 1.5, + "healthyLoad": 0.8, + "startupGracePeriod": 300 + }, + "fileSystems": [ + { + "path": "/home", + "dataType": "users", + "defaultWorkDir": true + }, + { + "path": "/store", + "dataType": "store", + "defaultWorkDir": false + }, + { + "path": "/scratch", + "dataType": "scratch", + "defaultWorkDir": false + } + ], + "datatransferJobsDirectives": [], + "timeouts": { + "sshConnection": 5, + "sshLogin": 5, + "sshCommandExecution": 5 + } + } + ] + } +} diff --git a/tests/v2/test_status.py b/tests/v2/test_status.py new file mode 100644 index 0000000..f014e75 --- /dev/null +++ b/tests/v2/test_status.py @@ -0,0 +1,70 @@ +import json +import pytest +import re + +from context import AsyncFirecrest +from werkzeug.wrappers import Response +from werkzeug.wrappers import Request + + +def read_json_file(filename): + with open(filename) as fp: + data = json.load(fp) + + return data + + +@pytest.fixture +def valid_client(fc_server): + class ValidAuthorization: + def get_access_token(self): + return "VALID_TOKEN" + + return AsyncFirecrest( + firecrest_url=fc_server.url_for("/"), + authorization=ValidAuthorization() + ) + + +@pytest.fixture +def invalid_client(fc_server): + class InvalidAuthorization: + def get_access_token(self): + return "INVALID_TOKEN" + + return AsyncFirecrest( + firecrest_url=fc_server.url_for("/"), + authorization=InvalidAuthorization() + ) + + +@pytest.fixture +def fc_server(httpserver): + httpserver.expect_request( + re.compile("^/status/systems.*"), method="GET" + ).respond_with_handler(systems_handler) + return httpserver + + +def systems_handler(request: Request): + if request.headers["Authorization"] != "Bearer VALID_TOKEN": + return Response( + json.dumps({"message": "Bad token; invalid JSON"}), + status=401, + content_type="application/json", + ) + + data = read_json_file("responses/systems.json") + + ret = data["response"] + ret_status = data["status_code"] + + return Response(json.dumps(ret), + status=ret_status, + content_type="application/json") + + +@pytest.mark.asyncio +async def test_systems(valid_client): + data = read_json_file("responses/systems.json") + assert await valid_client.systems() == data["response"]