diff --git a/app/core/utils.py b/app/core/utils.py index a0f33c8..5016b84 100644 --- a/app/core/utils.py +++ b/app/core/utils.py @@ -1,6 +1,7 @@ import logging import subprocess import xml.etree.ElementTree as ET +from http import HTTPStatus import docker import requests @@ -210,7 +211,7 @@ def check_dns_exists(): url = config.dns_exists_url() try: response = requests.get(url) - if response.status_code == 200 and response.content: + if response.status_code == HTTPStatus.OK and response.content: json_response = response.json() if "domain" in json_response: cached_domain = json_response["domain"] diff --git a/app/routes.py b/app/routes.py index 05781b2..9731b02 100644 --- a/app/routes.py +++ b/app/routes.py @@ -1,6 +1,7 @@ import json import logging from enum import Enum +from http import HTTPStatus import docker from fastapi import APIRouter, HTTPException, Request @@ -58,7 +59,7 @@ async def registries_all(): "/registries/", response_model=schemas.RegistryResponse, responses={ - 400: { + HTTPStatus.BAD_REQUEST: { "model": schemas.ErrorResponse, "description": "Failed to add registry.", } @@ -71,13 +72,13 @@ async def add_registry(body: schemas.RegistryInput): return response else: raise HTTPException( - status_code=400, + status_code=HTTPStatus.BAD_REQUEST, detail={"message": f"Registry {body.url} already exists!"}, ) except Exception as error: logger.error(error) raise HTTPException( - status_code=400, + status_code=HTTPStatus.BAD_REQUEST, detail={"message": f"Failed to add registry {error}"}, ) from error @@ -86,7 +87,7 @@ async def add_registry(body: schemas.RegistryInput): "/registries/", response_model=schemas.SuccessResponse, responses={ - 400: { + HTTPStatus.BAD_REQUEST: { "model": schemas.ErrorResponse, "description": "Registry not found.", } @@ -97,7 +98,7 @@ async def remove_registry(url: str): response = services.delete_registry(url) if response is None: raise HTTPException( - status_code=400, + status_code=HTTPStatus.BAD_REQUEST, detail={ "message": f"Cannot to remove registry {url} because it does not exists!" }, @@ -105,7 +106,7 @@ async def remove_registry(url: str): except Exception as error: logger.error(error) raise HTTPException( - status_code=400, + status_code=HTTPStatus.BAD_REQUEST, detail={"message": f"Failed to remove registry {url}"}, ) from error return {"message": f"Registry {url} removed successfully."} @@ -120,7 +121,7 @@ async def services_all(): "/services/", response_model=schemas.ServiceResponse, responses={ - 400: { + HTTPStatus.BAD_REQUEST: { "model": schemas.ErrorResponse, "description": "Failed to stop container or service not found.", } @@ -134,13 +135,13 @@ async def add_service(body: schemas.ServiceInput): return response else: raise HTTPException( - status_code=400, + status_code=HTTPStatus.BAD_REQUEST, detail={"message": f"Service {service['id']} already exists!"}, ) except Exception as error: logger.error(error) raise HTTPException( - status_code=400, + status_code=HTTPStatus.BAD_REQUEST, detail={"message": f"Failed to add service {error}"}, ) from error @@ -149,13 +150,18 @@ async def add_service(body: schemas.ServiceInput): "/services/{service_id}", response_model=schemas.ServiceResponse, responses={ - 400: {"model": schemas.ErrorResponse, "description": "Service not found."} + HTTPStatus.BAD_REQUEST: { + "model": schemas.ErrorResponse, + "description": "Service not found.", + } }, ) async def service_by_id(service_id: str): service_object = services.get_service_by_id(service_id) if service_object is None: - raise HTTPException(status_code=400, detail={"message": "Service not found."}) + raise HTTPException( + status_code=HTTPStatus.BAD_REQUEST, detail={"message": "Service not found."} + ) return service_object @@ -171,7 +177,7 @@ async def services_by_interface(interface_id: str): "/download-service/{service_id}", response_model=schemas.SuccessResponse, responses={ - 400: { + HTTPStatus.BAD_REQUEST: { "model": schemas.ErrorResponse, "description": "Failed to download image or service not found.", } @@ -181,7 +187,7 @@ async def download_service(service_id: str): service_object = services.get_service_by_id(service_id) if service_object is None: raise HTTPException( - status_code=400, + status_code=HTTPStatus.BAD_REQUEST, detail={"message": "Service not found."}, ) @@ -191,7 +197,7 @@ async def download_service(service_id: str): except Exception as error: logger.error(error) raise HTTPException( - status_code=400, + status_code=HTTPStatus.BAD_REQUEST, detail={"message": f"Failed to download image {error}"}, ) from error return {"message": "Download image successfully."} @@ -202,7 +208,7 @@ async def download_service_stream(service_id: str): service_object = services.get_service_by_id(service_id) if service_object is None: raise HTTPException( - status_code=400, + status_code=HTTPStatus.BAD_REQUEST, detail={"message": "Service not found."}, ) @@ -251,7 +257,7 @@ async def download_service_stream_sse(service_id: str, request: Request): service_object = services.get_service_by_id(service_id) if service_object is None: raise HTTPException( - status_code=400, + status_code=HTTPStatus.BAD_REQUEST, detail={"message": "Service not found."}, ) event_generator = generator(service_object, request) @@ -262,7 +268,7 @@ async def download_service_stream_sse(service_id: str, request: Request): "/run-service/", response_model=schemas.SuccessResponse, responses={ - 400: { + HTTPStatus.BAD_REQUEST: { "model": schemas.ErrorResponse, "description": "Failed to start container or service not found.", } @@ -277,14 +283,14 @@ async def run_service(body: schemas.RunServiceInput): if service_object is None: raise HTTPException( - status_code=400, + status_code=HTTPStatus.BAD_REQUEST, detail={"message": "Service not found."}, ) port = services.run_container_with_retries(service_object) if port is None: raise HTTPException( - status_code=400, + status_code=HTTPStatus.BAD_REQUEST, detail={ "message": f"Failed to create container for {service_object['id']}" }, @@ -299,7 +305,7 @@ async def run_service(body: schemas.RunServiceInput): "/stop-service/{service_id}", response_model=schemas.SuccessResponse, responses={ - 400: { + HTTPStatus.BAD_REQUEST: { "model": schemas.ErrorResponse, "description": "Failed to stop container or service not found.", } @@ -309,7 +315,7 @@ async def stop_service(service_id: str): service_object = services.get_service_by_id(service_id) if service_object is None: raise HTTPException( - status_code=400, + status_code=HTTPStatus.BAD_REQUEST, detail={"message": "Service not found."}, ) @@ -322,7 +328,7 @@ async def stop_service(service_id: str): return {"message": f"Container {service_object['id']} not found."} logger.error(error) raise HTTPException( - status_code=400, + status_code=HTTPStatus.BAD_REQUEST, detail={"message": f"Failed to stop container {error}"}, ) from error return {"message": f"Service {service_object['id']} successfully."} @@ -332,7 +338,7 @@ async def stop_service(service_id: str): "/restart-service/{service_id}", response_model=schemas.SuccessResponse, responses={ - 400: { + HTTPStatus.BAD_REQUEST: { "model": schemas.ErrorResponse, "description": "Failed to restart container or service not found.", } @@ -342,7 +348,7 @@ async def restart_service(service_id: str): service_object = services.get_service_by_id(service_id) if service_object is None: raise HTTPException( - status_code=400, + status_code=HTTPStatus.BAD_REQUEST, detail={"message": "Service not found."}, ) @@ -359,7 +365,7 @@ async def restart_service(service_id: str): return {"message": f"Container {service_object['id']} not found."} logger.error(error) raise HTTPException( - status_code=400, + status_code=HTTPStatus.BAD_REQUEST, detail={"message": f"Failed to restart container {error}"}, ) from error return {"message": f"Service {service_object['id']} restarted successfully."} @@ -369,7 +375,7 @@ async def restart_service(service_id: str): "/stop-all-services/", response_model=schemas.SuccessResponse, responses={ - 400: { + HTTPStatus.BAD_REQUEST: { "model": schemas.ErrorResponse, "description": "Failed to stop container or service not found.", } @@ -380,7 +386,7 @@ async def stop_all_services(): services.stop_all_running_services() except Exception as error: raise HTTPException( - status_code=400, + status_code=HTTPStatus.BAD_REQUEST, detail={"message": f"Failed to stop container {error}"}, ) from error return {"message": "All services stopped successfully."} @@ -391,7 +397,7 @@ async def remove_service(service_id): service_object = services.get_service_by_id(service_id) if service_object is None: raise HTTPException( - status_code=400, + status_code=HTTPStatus.BAD_REQUEST, detail={"message": "Service not found."}, ) @@ -403,7 +409,7 @@ async def remove_service(service_id): logger.warning(error) return {"message": f"Image {service_object['dockerImage']} not found."} raise HTTPException( - status_code=400, + status_code=HTTPStatus.BAD_REQUEST, detail={"message": f"Failed to remove image {error}"}, ) from error return {"message": f"Service {service_object['id']} removed successfully."} @@ -417,7 +423,7 @@ async def remove_volume(volume_name): except Exception as error: logger.error(error) raise HTTPException( - status_code=400, + status_code=HTTPStatus.BAD_REQUEST, detail={"message": f"Failed to remove image {error}"}, ) from error return {"message": f"Volume {volume_name} removed successfully."} @@ -430,7 +436,7 @@ async def system_prune(): except Exception as error: logger.error(error) raise HTTPException( - status_code=400, + status_code=HTTPStatus.BAD_REQUEST, detail={"message": f"Failed to prune docker {error}"}, ) from error return {"message": "System pruned successfully."} @@ -440,7 +446,7 @@ async def system_prune(): "/stats/{service_id}", response_model=schemas.ContainerStatsResponse, responses={ - 400: { + HTTPStatus.BAD_REQUEST: { "model": schemas.ErrorResponse, "description": "Failed to get stats or service not found.", } @@ -450,7 +456,7 @@ async def stats_by_service(service_id: str): service_object = services.get_service_by_id(service_id) if service_object is None: raise HTTPException( - status_code=400, + status_code=HTTPStatus.BAD_REQUEST, detail={"message": "Service not found."}, ) @@ -461,7 +467,7 @@ async def stats_by_service(service_id: str): except Exception as error: logger.error(error) raise HTTPException( - status_code=400, + status_code=HTTPStatus.BAD_REQUEST, detail={"message": f"Failed to remove image {error}"}, ) from error diff --git a/resources/mocks/generic/check.py b/resources/mocks/generic/check.py index c710b9d..18ef3bb 100644 --- a/resources/mocks/generic/check.py +++ b/resources/mocks/generic/check.py @@ -1,3 +1,5 @@ +from http import HTTPStatus + import requests response = requests.post( @@ -8,6 +10,6 @@ }, stream=True, ) -if response.status_code == 200: +if response.status_code == HTTPStatus.OK: for chunk in response.iter_content(chunk_size=1024): print(chunk) diff --git a/tests/test_registry.py b/tests/test_registry.py index b05234b..328b6f1 100644 --- a/tests/test_registry.py +++ b/tests/test_registry.py @@ -1,4 +1,5 @@ import os +from http import HTTPStatus import pytest from fastapi.testclient import TestClient @@ -29,11 +30,11 @@ def multiple_registries_client(): class TestController: def test_get_registries(self, client) -> None: response = client.get("/v1/registries/") - assert response.status_code == 200 + assert response.status_code == HTTPStatus.OK def test_add_registry(self, client) -> None: response = client.get("/v1/services/") - assert response.status_code == 200 + assert response.status_code == HTTPStatus.OK number_of_services = len(response.json()) response = client.post( @@ -42,21 +43,21 @@ def test_add_registry(self, client) -> None: "url": "https://raw.githubusercontent.com/premAI-io/prem-daemon/main/resources/mocks/manifests.json" }, ) - assert response.status_code == 200 + assert response.status_code == HTTPStatus.OK response = client.get("/v1/services/") - assert response.status_code == 200 + assert response.status_code == HTTPStatus.OK services = response.json() assert len(services) > number_of_services assert len(services) == len({service["id"] for service in services}) def test_delete_registry(self, client) -> None: response = client.get("/v1/services/") - assert response.status_code == 200 + assert response.status_code == HTTPStatus.OK number_of_services = len(response.json()) response = client.get("/v1/registries/") - assert response.status_code == 200 + assert response.status_code == HTTPStatus.OK number_of_registries = len(response.json()) response = client.delete( @@ -65,14 +66,14 @@ def test_delete_registry(self, client) -> None: "url": "https://raw.githubusercontent.com/premAI-io/prem-daemon/main/resources/mocks/manifests.json" }, ) - assert response.status_code == 200 + assert response.status_code == HTTPStatus.OK response = client.get("/v1/services/") - assert response.status_code == 200 + assert response.status_code == HTTPStatus.OK assert len(response.json()) < number_of_services response = client.get("/v1/registries/") - assert response.status_code == 200 + assert response.status_code == HTTPStatus.OK assert len(response.json()) < number_of_registries response = client.post( @@ -81,11 +82,11 @@ def test_delete_registry(self, client) -> None: "url": "https://raw.githubusercontent.com/premAI-io/prem-daemon/main/resources/mocks/manifests.json" }, ) - assert response.status_code == 200 + assert response.status_code == HTTPStatus.OK def test_add_custom_service(self, client) -> None: response = client.get("/v1/services/") - assert response.status_code == 200 + assert response.status_code == HTTPStatus.OK number_of_services = len(response.json()) response = client.post( @@ -105,28 +106,28 @@ def test_add_custom_service(self, client) -> None: "defaultExternalPort": 10111, }, ) - assert response.status_code == 200 + assert response.status_code == HTTPStatus.OK response = client.get("/v1/services/") - assert response.status_code == 200 + assert response.status_code == HTTPStatus.OK assert len(response.json()) == number_of_services + 1 def test_add_existing_service(self, client) -> None: response = client.get("/v1/services/") - assert response.status_code == 200 + assert response.status_code == HTTPStatus.OK number_of_services = len(response.json()) response = client.post( "/v1/services/", json=response.json()[-1], ) - assert response.status_code != 200 + assert response.status_code != HTTPStatus.OK response = client.get("/v1/services/") - assert response.status_code == 200 + assert response.status_code == HTTPStatus.OK assert len(response.json()) == number_of_services def test_multiple_registries(self, multiple_registries_client): response = multiple_registries_client.get("/v1/registries/") - assert response.status_code == 200 + assert response.status_code == HTTPStatus.OK assert len(response.json()) == 2 diff --git a/tests/test_services.py b/tests/test_services.py index a3550e6..062c92c 100644 --- a/tests/test_services.py +++ b/tests/test_services.py @@ -1,3 +1,5 @@ +from http import HTTPStatus + from fastapi.testclient import TestClient from main import get_application @@ -11,89 +13,89 @@ def setup_method(self): def test_interfaces(self) -> None: response = self.client.get("/v1/interfaces/") - assert response.status_code == 200 + assert response.status_code == HTTPStatus.OK def test_services(self) -> None: response = self.client.get("/v1/services/") - assert response.status_code == 200 + assert response.status_code == HTTPStatus.OK def test_service_by_id(self) -> None: response = self.client.get("/v1/services/vicuna-7b-q4") - assert response.status_code == 200 + assert response.status_code == HTTPStatus.OK def test_services_by_interface(self) -> None: response = self.client.get("/v1/services-by-interface/chat") - assert response.status_code == 200 + assert response.status_code == HTTPStatus.OK def test_download_remove_service(self) -> None: response = self.client.get("/v1/download-service/redis-vector-db") - assert response.status_code == 200 + assert response.status_code == HTTPStatus.OK response = self.client.get("/v1/download-service-stream/redis-vector-db") - assert response.status_code == 200 + assert response.status_code == HTTPStatus.OK response = self.client.get("/v1/remove-service/redis-vector-db") - assert response.status_code == 200 + assert response.status_code == HTTPStatus.OK def test_run_stop_service(self) -> None: response = self.client.get("/v1/download-service/redis-vector-db") - assert response.status_code == 200 + assert response.status_code == HTTPStatus.OK response = self.client.post( "/v1/run-service/", json={"id": "redis-vector-db"}, ) - assert response.status_code == 200 + assert response.status_code == HTTPStatus.OK response = self.client.get("/v1/services/redis-vector-db") - assert response.status_code == 200 + assert response.status_code == HTTPStatus.OK response = self.client.get("/v1/stop-service/redis-vector-db") - assert response.status_code == 200 + assert response.status_code == HTTPStatus.OK response = self.client.post( "/v1/run-service/", json={"id": "redis-vector-db"}, ) - assert response.status_code == 200 + assert response.status_code == HTTPStatus.OK response = self.client.get("/v1/stop-service/redis-vector-db") - assert response.status_code == 200 + assert response.status_code == HTTPStatus.OK response = self.client.get("/v1/remove-volume/prem-redis-vector-db-data") - assert response.status_code == 200 + assert response.status_code == HTTPStatus.OK def test_run_restart_service(self) -> None: response = self.client.get("/v1/download-service/redis-vector-db") - assert response.status_code == 200 + assert response.status_code == HTTPStatus.OK response = self.client.post( "/v1/run-service/", json={"id": "redis-vector-db"}, ) - assert response.status_code == 200 + assert response.status_code == HTTPStatus.OK response = self.client.get("/v1/restart-service/redis-vector-db") - assert response.status_code == 200 + assert response.status_code == HTTPStatus.OK response = self.client.get("/v1/stop-service/redis-vector-db") - assert response.status_code == 200 + assert response.status_code == HTTPStatus.OK response = self.client.get("/v1/remove-volume/prem-redis-vector-db-data") - assert response.status_code == 200 + assert response.status_code == HTTPStatus.OK def test_run_stop_all_services(self) -> None: response = self.client.get("/v1/download-service/redis-vector-db") - assert response.status_code == 200 + assert response.status_code == HTTPStatus.OK response = self.client.post( "/v1/run-service/", json={"id": "redis-vector-db"}, ) - assert response.status_code == 200 + assert response.status_code == HTTPStatus.OK response = self.client.get("/v1/services/redis-vector-db") - assert response.status_code == 200 + assert response.status_code == HTTPStatus.OK response = self.client.get("/v1/stop-all-services/") - assert response.status_code == 200 + assert response.status_code == HTTPStatus.OK diff --git a/tests/test_settings.py b/tests/test_settings.py index f2f4987..a80062b 100644 --- a/tests/test_settings.py +++ b/tests/test_settings.py @@ -1,3 +1,5 @@ +from http import HTTPStatus + from fastapi.testclient import TestClient from main import get_application @@ -13,40 +15,40 @@ def test_stats(self) -> None: response = self.client.get("/v1/stop-service/redis-vector-db") response = self.client.get("/v1/download-service/redis-vector-db") - assert response.status_code == 200 + assert response.status_code == HTTPStatus.OK response = self.client.post( "/v1/run-service/", json={"id": "redis-vector-db"}, ) - assert response.status_code == 200 + assert response.status_code == HTTPStatus.OK response = self.client.get("/v1/stats/") - assert response.status_code == 200 + assert response.status_code == HTTPStatus.OK response = self.client.get("/v1/stats-all/") - assert response.status_code == 200 + assert response.status_code == HTTPStatus.OK response = self.client.get("/v1/gpu-stats-all/") - assert response.status_code == 200 + assert response.status_code == HTTPStatus.OK response = self.client.get("/v1/stop-service/redis-vector-db") - assert response.status_code == 200 + assert response.status_code == HTTPStatus.OK def test_stats_by_service(self) -> None: response = self.client.get("/v1/stop-service/redis-vector-db") response = self.client.get("/v1/download-service/redis-vector-db") - assert response.status_code == 200 + assert response.status_code == HTTPStatus.OK response = self.client.post( "/v1/run-service/", json={"id": "redis-vector-db"}, ) - assert response.status_code == 200 + assert response.status_code == HTTPStatus.OK response = self.client.get("/v1/stats/redis-vector-db") - assert response.status_code == 200 + assert response.status_code == HTTPStatus.OK response = self.client.get("/v1/stop-service/redis-vector-db") - assert response.status_code == 200 + assert response.status_code == HTTPStatus.OK