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

feat(api): implement post/fleets route #15

Open
wants to merge 3 commits into
base: feat/spotfleet-mgmt-ui
Choose a base branch
from
Open
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
Empty file.
10 changes: 10 additions & 0 deletions DeadlineStack/spotfleet-mgmt-ui/server/internals/fleets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from services.backup import Backup
from services.schema_validator import SchemaValidator


class FleetsInternal:
def create_fleet(deadline_config_path: str, output_backup_path: str, config_schema_path: str = "schemas/fleet_config.json"):
data = SchemaValidator().validate(deadline_config_path, config_schema_path)
backup_result = Backup().create(deadline_config_path, output_backup_path)

return data, backup_result
4 changes: 3 additions & 1 deletion DeadlineStack/spotfleet-mgmt-ui/server/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
uvicorn==0.27.0.post1
fastapi==0.109.2
pytest==8.0.2
boto3
boto3
httpx
jsonschema
22 changes: 19 additions & 3 deletions DeadlineStack/spotfleet-mgmt-ui/server/routes/fleets.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from fastapi import APIRouter
from fastapi import APIRouter, HTTPException
from services.backup import Backup, BackupException
from services.schema_validator import SchemaValidator, SchemaValidationException
from internals.fleets import FleetsInternal

router = APIRouter(
prefix="/fleets",
Expand All @@ -18,8 +21,21 @@ async def get_fleet(fleet_id: str):


@router.post("/", status_code=200)
async def create_fleet():
return {"body": "new"}
async def create_fleet(deadline_config_path: str, output_backup_path: str, config_schema_path: str = "schemas/fleet_config.json"):
try:
data, backup_result = FleetsInternal.create_fleet(
deadline_config_path, output_backup_path, config_schema_path)

return {"status": "ok", "backup_path": backup_result, "fleet_data": data}
except SchemaValidationException as e:
raise HTTPException(
status_code=422, detail=str(e))
except BackupException as e:
raise HTTPException(
status_code=422, detail=str(e))
except Exception as e:
raise HTTPException(
status_code=500, detail=f"Error processing the request: {str(e)}")


@router.put("/{fleet_id}", status_code=200)
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import sys
from pathlib import Path
sys.path.append(str(Path(__file__).resolve().parents[2]))
from fastapi.testclient import TestClient
from ...main import app, API_PREFIX

client = TestClient(app)

deadline_config_path = "server/schemas/config_exemple.json"
deadline_invalid_config_path = "server/schemas/invalid_config_exemple.json"
output_backup_path = "server/schemas/backup.json"
fleet_config_path = "server/schemas/fleet_config.json"


def test_post_fleets_valid_data():
response = client.post(
f"{API_PREFIX}/fleets",
params={
"deadline_config_path": deadline_config_path,
"output_backup_path": output_backup_path,
"config_schema_path": fleet_config_path,
},
)
assert response.status_code == 200
assert "status" in response.json()
assert response.json()["status"] == "ok"


def test_post_fleets_invalid_schema():
response = client.post(
f"{API_PREFIX}/fleets",
params={
"deadline_config_path": deadline_invalid_config_path,
"output_backup_path": output_backup_path,
"config_schema_path": fleet_config_path,
},
)
assert response.status_code == 422
assert "detail" in response.json()
assert "Validation error" in response.json()["detail"]


def test_post_fleets_invalid_input():
response = client.post(
f"{API_PREFIX}/fleets",
params={
"deadline_config_path": "invalid_path",
"output_backup_path": output_backup_path,
"config_schema_path": fleet_config_path,
},
)
assert response.status_code == 422
assert "detail" in response.json()
assert "File not found" in response.json()["detail"]


def test_post_fleets_invalid_output():
response = client.post(
f"{API_PREFIX}/fleets",
params={
"deadline_config_path": deadline_config_path,
"output_backup_path": deadline_config_path,
"config_schema_path": fleet_config_path,
},
)
assert response.status_code == 422
assert "detail" in response.json()
assert "Failed to create backup:" in response.json()["detail"]
6 changes: 5 additions & 1 deletion DeadlineStack/spotfleet-mgmt-ui/server/services/backup.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import shutil


class BackupException(Exception):
pass


class Backup:

def create(self, original_path: str, output_path: str):
try:
shutil.copy(original_path, output_path)
return output_path
except Exception as e:
return False
raise BackupException(f"Failed to create backup: {str(e)}")
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from ..backup import Backup
from ..backup import Backup, BackupException
import pytest
import os


Expand All @@ -19,24 +20,28 @@ def delete_files(self):

def test_invalid_original_path(self):
# Test case: Attempt to create a backup with an unknown original path (file does not exist).
assert self._backup.create('unknow.txt', self._output_path) == False
with pytest.raises(BackupException, match="Failed to create backup:"):
self._backup.create('unknow.txt', self._output_path)

def test_missing_original_path(self):
# Test case: Attempt to create a backup with a missing original path.
assert self._backup.create('', self._output_path) == False
with pytest.raises(BackupException, match="Failed to create backup:"):
assert self._backup.create('', self._output_path) is None

def test_missing_output_path(self):
# Test case: Attempt to create a backup with a missing output path.
self.create_original_file()
assert self._backup.create(self._original_path, '') == False
self.delete_files()
with pytest.raises(BackupException, match="Failed to create backup:"):
self.create_original_file()
assert self._backup.create(self._original_path, '') is None
self.delete_files()

def test_same_path(self):
# Test case: Attempt to create a backup with the same original and output paths.
self.create_original_file()
assert self._backup.create(
self._original_path, self._original_path) == False
self.delete_files()
with pytest.raises(BackupException, match="Failed to create backup:"):
self.create_original_file()
assert self._backup.create(
self._original_path, self._original_path) is None
self.delete_files()

def test_create_success(self):
# Test case: Successfully create a backup and verify content equality.
Expand Down