From a63de4719bdd0b074427b99168723b39e7261101 Mon Sep 17 00:00:00 2001 From: Amit Kumar Date: Fri, 5 Jan 2024 15:53:58 +0000 Subject: [PATCH 1/8] delete or stop server --- jhub_apps/hub_client/hub_client.py | 7 ++++--- jhub_apps/service/service.py | 8 +++++++- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/jhub_apps/hub_client/hub_client.py b/jhub_apps/hub_client/hub_client.py index 56b3de8b..336600e7 100644 --- a/jhub_apps/hub_client/hub_client.py +++ b/jhub_apps/hub_client/hub_client.py @@ -53,7 +53,7 @@ def create_server(self, username, servername, edit=False, user_options=None): servername = f"{servername}-{uuid.uuid4().hex[:7]}" if server: if edit: - self.delete_server(username, server["name"]) + self.delete_server(username, server["name"], remove=True) else: raise ValueError(f"Server: {servername} already exists") url = f"/users/{username}/servers/{servername}" @@ -63,9 +63,10 @@ def create_server(self, username, servername, edit=False, user_options=None): r.raise_for_status() return r.status_code, servername - def delete_server(self, username, server_name): + def delete_server(self, username, server_name, remove=False): url = f"/users/{username}/servers/{server_name}" - params = {"remove": True} + # This will remove it from the database, otherwise it will just stop the server + params = {"remove": remove} r = requests.delete(API_URL + url, headers=self._headers(), json=params) r.raise_for_status() return r.status_code diff --git a/jhub_apps/service/service.py b/jhub_apps/service/service.py index f08626f6..dd7143d0 100644 --- a/jhub_apps/service/service.py +++ b/jhub_apps/service/service.py @@ -142,11 +142,17 @@ async def update_server( @router.delete("/server/{server_name}") -async def delete_server(user: User = Depends(get_current_user), server_name=None): +async def delete_server( + user: User = Depends(get_current_user), + server_name=None, + remove=False, +): + """Delete or stop server. Delete if remove is True otherwise stop the server""" hub_client = HubClient() return hub_client.delete_server( user.name, server_name=server_name, + remove=remove ) From 8ff7780f7caa39385d790e074adad9028012aded Mon Sep 17 00:00:00 2001 From: Amit Kumar Date: Fri, 5 Jan 2024 15:59:55 +0000 Subject: [PATCH 2/8] add start server endpoint --- jhub_apps/hub_client/hub_client.py | 10 ++++++++++ jhub_apps/service/service.py | 12 ++++++++++++ 2 files changed, 22 insertions(+) diff --git a/jhub_apps/hub_client/hub_client.py b/jhub_apps/hub_client/hub_client.py index 336600e7..2e26b7e6 100644 --- a/jhub_apps/hub_client/hub_client.py +++ b/jhub_apps/hub_client/hub_client.py @@ -46,6 +46,16 @@ def normalize_server_name(self, servername): text = text.replace(" ", "-") return text + def start_server(self, username, servername): + server = self.get_server(username, servername) + if not server: + return None + url = f"/users/{username}/servers/{servername}" + data = {"name": servername} + r = requests.post(API_URL + url, headers=self._headers(), json=data) + r.raise_for_status() + return r.status_code, servername + def create_server(self, username, servername, edit=False, user_options=None): server = self.get_server(username, servername) if not edit: diff --git a/jhub_apps/service/service.py b/jhub_apps/service/service.py index dd7143d0..eed19005 100644 --- a/jhub_apps/service/service.py +++ b/jhub_apps/service/service.py @@ -121,6 +121,18 @@ async def create_server( ) +@router.post("/server/{server_name}") +async def start_server( + server_name=None, + user: User = Depends(get_current_user), +): + hub_client = HubClient() + return hub_client.start_server( + username=user.name, + servername=server_name, + ) + + @router.put("/server/{server_name}") async def update_server( server: ServerCreation = Depends(Checker(ServerCreation)), From 9b9a84ada3d2ac52541cab1e8095956e3cc4b745 Mon Sep 17 00:00:00 2001 From: Amit Kumar Date: Fri, 5 Jan 2024 16:27:16 +0000 Subject: [PATCH 3/8] fix delete server endpoint test --- jhub_apps/tests/test_api.py | 1 + 1 file changed, 1 insertion(+) diff --git a/jhub_apps/tests/test_api.py b/jhub_apps/tests/test_api.py index 9e82306d..027271dc 100644 --- a/jhub_apps/tests/test_api.py +++ b/jhub_apps/tests/test_api.py @@ -65,6 +65,7 @@ def test_api_delete_server(delete_server, client): delete_server.assert_called_once_with( MOCK_USER.name, server_name="panel-app", + remove=False ) assert response.json() == create_server_response From 97e4f7f5aab3fba50848990ce0bcbcd1a5c90649 Mon Sep 17 00:00:00 2001 From: Amit Kumar Date: Fri, 5 Jan 2024 16:49:14 +0000 Subject: [PATCH 4/8] add/update tests for stop/delete server --- jhub_apps/service/service.py | 2 +- jhub_apps/tests/test_api.py | 12 +++++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/jhub_apps/service/service.py b/jhub_apps/service/service.py index eed19005..caa40d2a 100644 --- a/jhub_apps/service/service.py +++ b/jhub_apps/service/service.py @@ -157,7 +157,7 @@ async def update_server( async def delete_server( user: User = Depends(get_current_user), server_name=None, - remove=False, + remove: bool = False, ): """Delete or stop server. Delete if remove is True otherwise stop the server""" hub_client = HubClient() diff --git a/jhub_apps/tests/test_api.py b/jhub_apps/tests/test_api.py index 027271dc..88f94a92 100644 --- a/jhub_apps/tests/test_api.py +++ b/jhub_apps/tests/test_api.py @@ -3,6 +3,8 @@ import json from unittest.mock import patch +import pytest + from jhub_apps.hub_client.hub_client import HubClient from jhub_apps.spawner.types import FRAMEWORKS from jhub_apps.tests.constants import MOCK_USER @@ -57,15 +59,19 @@ def test_api_create_server(create_server, client): assert response.json() == create_server_response +@pytest.mark.parametrize("name,remove", [ + ('delete', True,), + ('stop', False,), +]) @patch.object(HubClient, "delete_server") -def test_api_delete_server(delete_server, client): +def test_api_delete_server(delete_server, name, remove, client): create_server_response = {"user": "aktech"} delete_server.return_value = create_server_response - response = client.delete("/server/panel-app") + response = client.delete("/server/panel-app", params={"remove": remove}) delete_server.assert_called_once_with( MOCK_USER.name, server_name="panel-app", - remove=False + remove=remove ) assert response.json() == create_server_response From 8e26a5b90c1b9964657a8b9441fddf3b3c8ca3b5 Mon Sep 17 00:00:00 2001 From: Amit Kumar Date: Fri, 5 Jan 2024 16:51:34 +0000 Subject: [PATCH 5/8] add test for start server --- jhub_apps/tests/test_api.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/jhub_apps/tests/test_api.py b/jhub_apps/tests/test_api.py index 88f94a92..c47bad06 100644 --- a/jhub_apps/tests/test_api.py +++ b/jhub_apps/tests/test_api.py @@ -59,6 +59,21 @@ def test_api_create_server(create_server, client): assert response.json() == create_server_response +@patch.object(HubClient, "start_server") +def test_api_start_server(create_server, client): + start_server_response = {"user": "aktech"} + create_server.return_value = start_server_response + server_name = "server-name" + response = client.post( + f"/server/{server_name}", + ) + create_server.assert_called_once_with( + username=MOCK_USER.name, + servername=server_name, + ) + assert response.json() == start_server_response + + @pytest.mark.parametrize("name,remove", [ ('delete', True,), ('stop', False,), From af15e59a8e37ffb67d89cb85c3a3aaab81ca9c3f Mon Sep 17 00:00:00 2001 From: Amit Kumar Date: Fri, 5 Jan 2024 16:53:23 +0000 Subject: [PATCH 6/8] return 404 if no server found --- jhub_apps/service/service.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/jhub_apps/service/service.py b/jhub_apps/service/service.py index caa40d2a..3f38f535 100644 --- a/jhub_apps/service/service.py +++ b/jhub_apps/service/service.py @@ -127,10 +127,13 @@ async def start_server( user: User = Depends(get_current_user), ): hub_client = HubClient() - return hub_client.start_server( + response = hub_client.start_server( username=user.name, servername=server_name, ) + if response is None: + return status.HTTP_404_NOT_FOUND + return response @router.put("/server/{server_name}") From e6315640115594a2408f7c8f37fc75222335be47 Mon Sep 17 00:00:00 2001 From: Amit Kumar Date: Fri, 5 Jan 2024 17:01:06 +0000 Subject: [PATCH 7/8] add test for server not found --- jhub_apps/service/service.py | 10 ++++++++-- jhub_apps/tests/test_api.py | 17 +++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/jhub_apps/service/service.py b/jhub_apps/service/service.py index 3f38f535..8fd06439 100644 --- a/jhub_apps/service/service.py +++ b/jhub_apps/service/service.py @@ -82,7 +82,10 @@ async def get_server(user: User = Depends(get_current_user), server_name=None): for s_name, server_details in user_servers.items(): if s_name == server_name: return server_details - return status.HTTP_404_NOT_FOUND + raise HTTPException( + detail=f"server '{server_name}' not found", + status_code=status.HTTP_404_NOT_FOUND, + ) else: # Get all servers return user_servers @@ -132,7 +135,10 @@ async def start_server( servername=server_name, ) if response is None: - return status.HTTP_404_NOT_FOUND + raise HTTPException( + detail=f"server '{server_name}' not found", + status_code=status.HTTP_404_NOT_FOUND, + ) return response diff --git a/jhub_apps/tests/test_api.py b/jhub_apps/tests/test_api.py index c47bad06..52d9df91 100644 --- a/jhub_apps/tests/test_api.py +++ b/jhub_apps/tests/test_api.py @@ -71,9 +71,26 @@ def test_api_start_server(create_server, client): username=MOCK_USER.name, servername=server_name, ) + assert response.status_code == 200 assert response.json() == start_server_response +@patch.object(HubClient, "start_server") +def test_api_start_server_404(start_server, client): + start_server_response = None + start_server.return_value = start_server_response + server_name = "server-name" + response = client.post( + f"/server/{server_name}", + ) + start_server.assert_called_once_with( + username=MOCK_USER.name, + servername=server_name, + ) + assert response.status_code == 404 + assert response.json() == {"detail": "server 'server-name' not found"} + + @pytest.mark.parametrize("name,remove", [ ('delete', True,), ('stop', False,), From ecb1f60eebeed820fdbc02ff6fd5a67057202b93 Mon Sep 17 00:00:00 2001 From: Amit Kumar Date: Fri, 5 Jan 2024 17:02:46 +0000 Subject: [PATCH 8/8] add tests for response status code --- jhub_apps/service/service.py | 1 + jhub_apps/tests/test_api.py | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/jhub_apps/service/service.py b/jhub_apps/service/service.py index 8fd06439..31f22cec 100644 --- a/jhub_apps/service/service.py +++ b/jhub_apps/service/service.py @@ -129,6 +129,7 @@ async def start_server( server_name=None, user: User = Depends(get_current_user), ): + """Start an already existing server.""" hub_client = HubClient() response = hub_client.start_server( username=user.name, diff --git a/jhub_apps/tests/test_api.py b/jhub_apps/tests/test_api.py index 52d9df91..bc2289b2 100644 --- a/jhub_apps/tests/test_api.py +++ b/jhub_apps/tests/test_api.py @@ -33,6 +33,7 @@ def test_api_get_server(get_user, client): get_user.return_value = create_server_response response = client.get("/server/panel-app") get_user.assert_called_once_with(MOCK_USER.name) + assert response.status_code == 200 assert response.json() == server_data["panel-app"] @@ -56,6 +57,7 @@ def test_api_create_server(create_server, client): servername="panel-app", user_options=final_user_options, ) + assert response.status_code == 200 assert response.json() == create_server_response @@ -105,6 +107,7 @@ def test_api_delete_server(delete_server, name, remove, client): server_name="panel-app", remove=remove ) + assert response.status_code == 200 assert response.json() == create_server_response @@ -130,6 +133,7 @@ def test_api_update_server(create_server, client): edit=True, user_options=final_user_options, ) + assert response.status_code == 200 assert response.json() == create_server_response @@ -147,4 +151,5 @@ def test_api_status(client): response = client.get( "/status", ) + assert response.status_code == 200 assert response.json() == {"status": "ok"}