From be2f687e774640cdac7c817d1d8c919de5c2413a Mon Sep 17 00:00:00 2001 From: Dan Date: Fri, 16 Aug 2024 15:44:34 +0100 Subject: [PATCH 1/7] Fix unit test for service_run --- softpack_core/artifacts.py | 4 ++-- softpack_core/graphql.py | 10 ---------- softpack_core/service.py | 4 ++++ tests/unit/conftest.py | 16 ---------------- tests/unit/test_service.py | 25 +++++++++++++++++++++++++ 5 files changed, 31 insertions(+), 28 deletions(-) delete mode 100644 tests/unit/conftest.py create mode 100644 tests/unit/test_service.py diff --git a/softpack_core/artifacts.py b/softpack_core/artifacts.py index 96fae0d..c295f6e 100644 --- a/softpack_core/artifacts.py +++ b/softpack_core/artifacts.py @@ -229,7 +229,7 @@ def clone_repo(self, branch: Optional[str] = None) -> None: path = self.settings.artifacts.path.expanduser() / ".git" if path.is_dir(): shutil.rmtree(path) - + self.repo = pygit2.clone_repository( self.settings.artifacts.repo.url, path=path, @@ -237,7 +237,7 @@ def clone_repo(self, branch: Optional[str] = None) -> None: bare=True, checkout_branch=branch, ) - + self.reference = "/".join( [ "refs/remotes", diff --git a/softpack_core/graphql.py b/softpack_core/graphql.py index 227af70..5dac160 100644 --- a/softpack_core/graphql.py +++ b/softpack_core/graphql.py @@ -32,16 +32,6 @@ class GraphQL(API): ] commands = Typer(help="GraphQL commands.") - @staticmethod - @commands.command("query", help="Execute a GraphQL query.") - def query_command() -> None: - """Execute a GraphQL query. - - Returns: - None. - """ - app.echo("GraphQL Query") - class Schema(strawberry.Schema): """GraphQL Schema class.""" diff --git a/softpack_core/service.py b/softpack_core/service.py index 0b2dc42..454cb14 100644 --- a/softpack_core/service.py +++ b/softpack_core/service.py @@ -5,6 +5,7 @@ """ +import multiprocessing import urllib.parse from pathlib import Path @@ -51,6 +52,7 @@ def run( help="Create and use this branch of Artefacts repo.", ), ] = 'main', + serviceReady = None, ) -> None: """Start the SoftPack Core REST API service. @@ -68,6 +70,8 @@ def run( artifacts.clone_repo(branch=branch) + if serviceReady is not None: + serviceReady.set() uvicorn.run( "softpack_core.app:app.router", host=app.settings.server.host, diff --git a/tests/unit/conftest.py b/tests/unit/conftest.py deleted file mode 100644 index 4000028..0000000 --- a/tests/unit/conftest.py +++ /dev/null @@ -1,16 +0,0 @@ -"""Copyright (c) 2023 Genome Research Ltd. - -This source code is licensed under the MIT license found in the -LICENSE file in the root directory of this source tree. -""" - - -import pytest -from fastapi.testclient import TestClient - -from softpack_core.app import app - - -@pytest.fixture -def client() -> TestClient: - return TestClient(app.router) diff --git a/tests/unit/test_service.py b/tests/unit/test_service.py new file mode 100644 index 0000000..9f4f463 --- /dev/null +++ b/tests/unit/test_service.py @@ -0,0 +1,25 @@ +"""Copyright (c) 2023 Genome Research Ltd. + +This source code is licensed under the MIT license found in the +LICENSE file in the root directory of this source tree. +""" + +import httpx +from box import Box + +from softpack_core import __version__ +from softpack_core.app import app +import multiprocessing +from softpack_core.service import ServiceAPI +from time import sleep + + +def test_service_run() -> None: + ready = multiprocessing.Event() + run = multiprocessing.Process(target=ServiceAPI.run, kwargs={"serviceReady": ready}) + run.start() + ready.wait(timeout=120) + response = httpx.get(app.url()) + run.terminate() + status = Box(response.json()) + assert status.softpack.core.version == __version__ From 4e9c5a5d8c519f34c2df991f0bb2598924197531 Mon Sep 17 00:00:00 2001 From: Dan Date: Mon, 19 Aug 2024 12:44:17 +0100 Subject: [PATCH 2/7] Add conftest.py for unit tests and update test_service.py --- tests/unit/conftest.py | 16 ++++++++++++++++ tests/unit/test_service.py | 2 +- 2 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 tests/unit/conftest.py diff --git a/tests/unit/conftest.py b/tests/unit/conftest.py new file mode 100644 index 0000000..4000028 --- /dev/null +++ b/tests/unit/conftest.py @@ -0,0 +1,16 @@ +"""Copyright (c) 2023 Genome Research Ltd. + +This source code is licensed under the MIT license found in the +LICENSE file in the root directory of this source tree. +""" + + +import pytest +from fastapi.testclient import TestClient + +from softpack_core.app import app + + +@pytest.fixture +def client() -> TestClient: + return TestClient(app.router) diff --git a/tests/unit/test_service.py b/tests/unit/test_service.py index 9f4f463..4d5158e 100644 --- a/tests/unit/test_service.py +++ b/tests/unit/test_service.py @@ -19,7 +19,7 @@ def test_service_run() -> None: run = multiprocessing.Process(target=ServiceAPI.run, kwargs={"serviceReady": ready}) run.start() ready.wait(timeout=120) - response = httpx.get(app.url()) + sleep(10) run.terminate() status = Box(response.json()) assert status.softpack.core.version == __version__ From 39567c10d54b2909a4b6301da1b7bebcd4cd9db9 Mon Sep 17 00:00:00 2001 From: Dan Date: Mon, 19 Aug 2024 13:44:34 +0100 Subject: [PATCH 3/7] Fix service test --- tests/unit/test_service.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/unit/test_service.py b/tests/unit/test_service.py index 4d5158e..b6a82df 100644 --- a/tests/unit/test_service.py +++ b/tests/unit/test_service.py @@ -18,8 +18,9 @@ def test_service_run() -> None: ready = multiprocessing.Event() run = multiprocessing.Process(target=ServiceAPI.run, kwargs={"serviceReady": ready}) run.start() - ready.wait(timeout=120) + ready.wait(timeout=300) sleep(10) + response = httpx.get(app.url()) run.terminate() status = Box(response.json()) assert status.softpack.core.version == __version__ From 12d8015d744b6630c39c99125e28486b6df27cea Mon Sep 17 00:00:00 2001 From: Dan Date: Mon, 19 Aug 2024 14:01:24 +0100 Subject: [PATCH 4/7] Fix linting errors --- softpack_core/service.py | 4 ++-- tests/integration/conftest.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/softpack_core/service.py b/softpack_core/service.py index 454cb14..71a3ce5 100644 --- a/softpack_core/service.py +++ b/softpack_core/service.py @@ -5,8 +5,8 @@ """ -import multiprocessing import urllib.parse +from multiprocessing.synchronize import Event as EventClass from pathlib import Path import typer @@ -52,7 +52,7 @@ def run( help="Create and use this branch of Artefacts repo.", ), ] = 'main', - serviceReady = None, + serviceReady: EventClass = None, ) -> None: """Start the SoftPack Core REST API service. diff --git a/tests/integration/conftest.py b/tests/integration/conftest.py index 0cca742..72f9004 100644 --- a/tests/integration/conftest.py +++ b/tests/integration/conftest.py @@ -43,7 +43,7 @@ def httpx_post(mocker): @pytest.fixture -def testable_env_input(mocker) -> EnvironmentInput: +def testable_env_input(mocker) -> EnvironmentInput: # type: ignore ad = new_test_artifacts() artifacts: Artifacts = ad["artifacts"] user = ad["test_user"] From b7bf811ae38caa95adb86f674938809e942fa951 Mon Sep 17 00:00:00 2001 From: Dan Date: Mon, 19 Aug 2024 14:20:38 +0100 Subject: [PATCH 5/7] Fix default value for serviceReady --- softpack_core/service.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/softpack_core/service.py b/softpack_core/service.py index 71a3ce5..caecf60 100644 --- a/softpack_core/service.py +++ b/softpack_core/service.py @@ -6,6 +6,7 @@ import urllib.parse +from multiprocessing import Event from multiprocessing.synchronize import Event as EventClass from pathlib import Path @@ -52,7 +53,7 @@ def run( help="Create and use this branch of Artefacts repo.", ), ] = 'main', - serviceReady: EventClass = None, + serviceReady: EventClass = Event(), ) -> None: """Start the SoftPack Core REST API service. @@ -70,8 +71,7 @@ def run( artifacts.clone_repo(branch=branch) - if serviceReady is not None: - serviceReady.set() + serviceReady.set() uvicorn.run( "softpack_core.app:app.router", host=app.settings.server.host, From f3e2581b73874aef431f712935883a607a580fa5 Mon Sep 17 00:00:00 2001 From: Dan Date: Mon, 19 Aug 2024 15:31:36 +0100 Subject: [PATCH 6/7] Rewrite test_service.py without need of extra argument --- softpack_core/service.py | 3 --- tests/unit/test_service.py | 14 +++++++++----- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/softpack_core/service.py b/softpack_core/service.py index caecf60..31ec4e9 100644 --- a/softpack_core/service.py +++ b/softpack_core/service.py @@ -6,7 +6,6 @@ import urllib.parse -from multiprocessing import Event from multiprocessing.synchronize import Event as EventClass from pathlib import Path @@ -53,7 +52,6 @@ def run( help="Create and use this branch of Artefacts repo.", ), ] = 'main', - serviceReady: EventClass = Event(), ) -> None: """Start the SoftPack Core REST API service. @@ -71,7 +69,6 @@ def run( artifacts.clone_repo(branch=branch) - serviceReady.set() uvicorn.run( "softpack_core.app:app.router", host=app.settings.server.host, diff --git a/tests/unit/test_service.py b/tests/unit/test_service.py index b6a82df..6da4dc8 100644 --- a/tests/unit/test_service.py +++ b/tests/unit/test_service.py @@ -15,12 +15,16 @@ def test_service_run() -> None: - ready = multiprocessing.Event() - run = multiprocessing.Process(target=ServiceAPI.run, kwargs={"serviceReady": ready}) + run = multiprocessing.Process(target=ServiceAPI.run) run.start() - ready.wait(timeout=300) - sleep(10) - response = httpx.get(app.url()) + while True: + try: + response = httpx.get(app.url()) + break + except httpx.RequestError: + if not run.is_alive(): + raise Exception("Service failed to start.") + sleep(5) run.terminate() status = Box(response.json()) assert status.softpack.core.version == __version__ From b120fc2e1e899406805e16f6c7876c0d200a7274 Mon Sep 17 00:00:00 2001 From: Michael Woolnough Date: Mon, 19 Aug 2024 16:52:02 +0100 Subject: [PATCH 7/7] Delint'd. --- softpack_core/artifacts.py | 4 ++-- softpack_core/graphql.py | 1 - softpack_core/service.py | 1 - tests/integration/conftest.py | 2 +- tests/unit/test_service.py | 5 +++-- 5 files changed, 6 insertions(+), 7 deletions(-) diff --git a/softpack_core/artifacts.py b/softpack_core/artifacts.py index c295f6e..96fae0d 100644 --- a/softpack_core/artifacts.py +++ b/softpack_core/artifacts.py @@ -229,7 +229,7 @@ def clone_repo(self, branch: Optional[str] = None) -> None: path = self.settings.artifacts.path.expanduser() / ".git" if path.is_dir(): shutil.rmtree(path) - + self.repo = pygit2.clone_repository( self.settings.artifacts.repo.url, path=path, @@ -237,7 +237,7 @@ def clone_repo(self, branch: Optional[str] = None) -> None: bare=True, checkout_branch=branch, ) - + self.reference = "/".join( [ "refs/remotes", diff --git a/softpack_core/graphql.py b/softpack_core/graphql.py index 5dac160..3a846e0 100644 --- a/softpack_core/graphql.py +++ b/softpack_core/graphql.py @@ -14,7 +14,6 @@ from typing_extensions import Type from .api import API -from .app import app from .schemas.base import BaseSchema from .schemas.environment import EnvironmentSchema from .schemas.groups import GroupsSchema diff --git a/softpack_core/service.py b/softpack_core/service.py index 31ec4e9..0b2dc42 100644 --- a/softpack_core/service.py +++ b/softpack_core/service.py @@ -6,7 +6,6 @@ import urllib.parse -from multiprocessing.synchronize import Event as EventClass from pathlib import Path import typer diff --git a/tests/integration/conftest.py b/tests/integration/conftest.py index 72f9004..8bdbcbf 100644 --- a/tests/integration/conftest.py +++ b/tests/integration/conftest.py @@ -43,7 +43,7 @@ def httpx_post(mocker): @pytest.fixture -def testable_env_input(mocker) -> EnvironmentInput: # type: ignore +def testable_env_input(mocker) -> EnvironmentInput: # type: ignore ad = new_test_artifacts() artifacts: Artifacts = ad["artifacts"] user = ad["test_user"] diff --git a/tests/unit/test_service.py b/tests/unit/test_service.py index 6da4dc8..a41f2cf 100644 --- a/tests/unit/test_service.py +++ b/tests/unit/test_service.py @@ -4,14 +4,15 @@ LICENSE file in the root directory of this source tree. """ +import multiprocessing +from time import sleep + import httpx from box import Box from softpack_core import __version__ from softpack_core.app import app -import multiprocessing from softpack_core.service import ServiceAPI -from time import sleep def test_service_run() -> None: