Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit tests for api v2 #127

Merged
merged 1 commit into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions tests/v2/context.py
Original file line number Diff line number Diff line change
@@ -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
115 changes: 115 additions & 0 deletions tests/v2/responses/systems.json
Original file line number Diff line number Diff line change
@@ -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
}
}
]
}
}
70 changes: 70 additions & 0 deletions tests/v2/test_status.py
Original file line number Diff line number Diff line change
@@ -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"]