From bac60dc249187eb2ea773691bfe0ad78092fbd06 Mon Sep 17 00:00:00 2001 From: Nadzeya Hut <84857215+nadzyah@users.noreply.github.com> Date: Wed, 21 Jun 2023 15:55:46 +0200 Subject: [PATCH] Return test-execution id from the /test-executions/start-test endpoint (#29) * Return test execution ID. Remove test_execution/ * Fix style issues * Fix type in filename --- backend/test_observer/controllers/router.py | 2 - .../controllers/test_execution/__init__.py | 0 .../controllers/test_execution/models.py | 12 --- .../test_execution/test_execution.py | 82 ------------------- .../controllers/test_executions/models.py | 31 +++++++ .../test_executions/test_executions.py | 80 +++++++++++++++++- .../test_executions/test_test_excutions.py | 37 --------- .../test_test_executions.py} | 65 +++++++++++++-- 8 files changed, 166 insertions(+), 143 deletions(-) delete mode 100644 backend/test_observer/controllers/test_execution/__init__.py delete mode 100644 backend/test_observer/controllers/test_execution/models.py delete mode 100644 backend/test_observer/controllers/test_execution/test_execution.py delete mode 100644 backend/tests/controllers/test_executions/test_test_excutions.py rename backend/tests/controllers/{test_execution/test_test_execution.py => test_executions/test_test_executions.py} (57%) diff --git a/backend/test_observer/controllers/router.py b/backend/test_observer/controllers/router.py index 474279fd..fd2725d5 100644 --- a/backend/test_observer/controllers/router.py +++ b/backend/test_observer/controllers/router.py @@ -27,12 +27,10 @@ from .artefacts import artefacts from .families import families from .promoter import promoter -from .test_execution import test_execution from .test_executions import test_executions router = APIRouter() router.include_router(promoter.router) -router.include_router(test_execution.router, prefix="/v1/test-execution") router.include_router(families.router, prefix="/v1/families") router.include_router(version.router, prefix="/v1/version") router.include_router(test_executions.router, prefix="/v1/test-executions") diff --git a/backend/test_observer/controllers/test_execution/__init__.py b/backend/test_observer/controllers/test_execution/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/backend/test_observer/controllers/test_execution/models.py b/backend/test_observer/controllers/test_execution/models.py deleted file mode 100644 index 0b431d2f..00000000 --- a/backend/test_observer/controllers/test_execution/models.py +++ /dev/null @@ -1,12 +0,0 @@ -from pydantic import BaseModel - - -class StartTestExecutionRequest(BaseModel): - family: str - name: str - version: str - revision: int | None = None - source: dict - arch: str - execution_stage: str - environment: str diff --git a/backend/test_observer/controllers/test_execution/test_execution.py b/backend/test_observer/controllers/test_execution/test_execution.py deleted file mode 100644 index ad5d6547..00000000 --- a/backend/test_observer/controllers/test_execution/test_execution.py +++ /dev/null @@ -1,82 +0,0 @@ -# Copyright 2023 Canonical Ltd. -# All rights reserved. -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . -# -# Written by: -# Omar Selo - - -from fastapi import APIRouter, Depends -from sqlalchemy.orm import Session -from test_observer.data_access.models import ( - Artefact, - ArtefactBuild, - Environment, - Stage, - TestExecution, -) -from test_observer.data_access.models_enums import TestExecutionStatus -from test_observer.data_access.repository import get_or_create -from test_observer.data_access.setup import get_db - -from .models import StartTestExecutionRequest - -router = APIRouter() - - -@router.put("/start") -def start_test_execution( - request: StartTestExecutionRequest, db: Session = Depends(get_db) -): - stage = ( - db.query(Stage) - .filter( - Stage.name == request.execution_stage, - Stage.family.has(name=request.family), - ) - .one() - ) - - artefact = get_or_create( - db, - Artefact, - name=request.name, - version=request.version, - source=request.source, - stage_id=stage.id, - ) - - environment = get_or_create( - db, - Environment, - name=request.environment, - architecture=request.arch, - ) - - artefact_build = get_or_create( - db, - ArtefactBuild, - architecture=request.arch, - revision=request.revision, - artefact_id=artefact.id, - ) - - get_or_create( - db, - TestExecution, - environment_id=environment.id, - artefact_build_id=artefact_build.id, - status=TestExecutionStatus.IN_PROGRESS, - ) diff --git a/backend/test_observer/controllers/test_executions/models.py b/backend/test_observer/controllers/test_executions/models.py index a77790ec..ffd01c2a 100644 --- a/backend/test_observer/controllers/test_executions/models.py +++ b/backend/test_observer/controllers/test_executions/models.py @@ -1,8 +1,39 @@ +# Copyright 2023 Canonical Ltd. +# All rights reserved. +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# +# Written by: +# Omar Selo + + from pydantic import BaseModel from test_observer.data_access.models_enums import TestExecutionStatus +class StartTestExecutionRequest(BaseModel): + family: str + name: str + version: str + revision: int | None = None + source: dict + arch: str + execution_stage: str + environment: str + + class TestExecutionsPatchRequest(BaseModel): c3_link: str jenkins_link: str diff --git a/backend/test_observer/controllers/test_executions/test_executions.py b/backend/test_observer/controllers/test_executions/test_executions.py index 4bc32fad..4d124ae4 100644 --- a/backend/test_observer/controllers/test_executions/test_executions.py +++ b/backend/test_observer/controllers/test_executions/test_executions.py @@ -1,14 +1,90 @@ +# Copyright 2023 Canonical Ltd. +# All rights reserved. +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# +# Written by: +# Omar Selo +# Nadzeya Hutsko + + from fastapi import APIRouter, Depends from sqlalchemy.orm import Session -from test_observer.data_access.models import TestExecution +from test_observer.data_access.models import ( + TestExecution, + Stage, + Artefact, + ArtefactBuild, + Environment, +) +from test_observer.data_access.models_enums import TestExecutionStatus +from test_observer.data_access.repository import get_or_create from test_observer.data_access.setup import get_db -from .models import TestExecutionsPatchRequest +from .models import TestExecutionsPatchRequest, StartTestExecutionRequest router = APIRouter() +@router.put("/start-test") +def start_test_execution( + request: StartTestExecutionRequest, db: Session = Depends(get_db) +): + stage = ( + db.query(Stage) + .filter( + Stage.name == request.execution_stage, + Stage.family.has(name=request.family), + ) + .one() + ) + + artefact = get_or_create( + db, + Artefact, + name=request.name, + version=request.version, + source=request.source, + stage_id=stage.id, + ) + + environment = get_or_create( + db, + Environment, + name=request.environment, + architecture=request.arch, + ) + + artefact_build = get_or_create( + db, + ArtefactBuild, + architecture=request.arch, + revision=request.revision, + artefact_id=artefact.id, + ) + + test_execution = get_or_create( + db, + TestExecution, + environment_id=environment.id, + artefact_build_id=artefact_build.id, + status=TestExecutionStatus.IN_PROGRESS, + ) + return {"id": test_execution.id} + + @router.patch("/{id}") def patch_test_execution( id: int, diff --git a/backend/tests/controllers/test_executions/test_test_excutions.py b/backend/tests/controllers/test_executions/test_test_excutions.py deleted file mode 100644 index 0a979bd5..00000000 --- a/backend/tests/controllers/test_executions/test_test_excutions.py +++ /dev/null @@ -1,37 +0,0 @@ -from fastapi.testclient import TestClient -from sqlalchemy.orm import Session -from test_observer.data_access.models import ( - Artefact, - Stage, - TestExecution, - ArtefactBuild, - Environment, -) -from test_observer.data_access.models_enums import TestExecutionStatus - - -def test_updates_test_execution(db_session: Session, test_client: TestClient): - stage = db_session.query(Stage).filter(Stage.name == "beta").one() - artefact = Artefact(name="some artefact", version="1.0.0", source={}, stage=stage) - artefact_build = ArtefactBuild(architecture="some arch", artefact=artefact) - environment = Environment(name="some environment", architecture="some arch") - test_execution = TestExecution( - environment=environment, artefact_build=artefact_build - ) - db_session.add_all([artefact, artefact_build, environment, test_execution]) - db_session.commit() - db_session.refresh(test_execution) - - test_client.patch( - f"/v1/test-executions/{test_execution.id}", - json={ - "jenkins_link": "some jenkins link", - "c3_link": "some c3 link", - "status": TestExecutionStatus.PASSED.name, - }, - ) - - db_session.refresh(test_execution) - assert test_execution.jenkins_link == "some jenkins link" - assert test_execution.c3_link == "some c3 link" - assert test_execution.status == TestExecutionStatus.PASSED diff --git a/backend/tests/controllers/test_execution/test_test_execution.py b/backend/tests/controllers/test_executions/test_test_executions.py similarity index 57% rename from backend/tests/controllers/test_execution/test_test_execution.py rename to backend/tests/controllers/test_executions/test_test_executions.py index 9867ad14..1d307732 100644 --- a/backend/tests/controllers/test_execution/test_test_execution.py +++ b/backend/tests/controllers/test_executions/test_test_executions.py @@ -1,20 +1,40 @@ +# Copyright 2023 Canonical Ltd. +# All rights reserved. +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# +# Written by: +# Omar Selo +# Nadzeya Hutsko + + from fastapi.testclient import TestClient from sqlalchemy.orm import Session - -from test_observer.controllers.test_execution.models import StartTestExecutionRequest from test_observer.data_access.models import ( Artefact, - ArtefactBuild, - Environment, Stage, TestExecution, + ArtefactBuild, + Environment, ) from test_observer.data_access.models_enums import TestExecutionStatus +from test_observer.controllers.test_executions.models import StartTestExecutionRequest def test_creates_all_data_models(db_session: Session, test_client: TestClient): - test_client.put( - "/v1/test-execution/start", + response = test_client.put( + "/v1/test-executions/start-test", json={ "family": "snap", "name": "core22", @@ -60,7 +80,7 @@ def test_creates_all_data_models(db_session: Session, test_client: TestClient): ) assert artefact_build - assert ( + test_execution = ( db_session.query(TestExecution) .filter( TestExecution.artefact_build == artefact_build, @@ -69,6 +89,8 @@ def test_creates_all_data_models(db_session: Session, test_client: TestClient): ) .one_or_none() ) + assert test_execution + assert response.json() == {"id": test_execution.id} def test_uses_existing_models(db_session: Session, test_client: TestClient): @@ -105,7 +127,7 @@ def test_uses_existing_models(db_session: Session, test_client: TestClient): db_session.commit() test_client.put( - "/v1/test-execution/start", + "/v1/test-executions/start-test", json=request.dict(), ) @@ -118,3 +140,30 @@ def test_uses_existing_models(db_session: Session, test_client: TestClient): ) .one_or_none() ) + + +def test_updates_test_execution(db_session: Session, test_client: TestClient): + stage = db_session.query(Stage).filter(Stage.name == "beta").one() + artefact = Artefact(name="some artefact", version="1.0.0", source={}, stage=stage) + artefact_build = ArtefactBuild(architecture="some arch", artefact=artefact) + environment = Environment(name="some environment", architecture="some arch") + test_execution = TestExecution( + environment=environment, artefact_build=artefact_build + ) + db_session.add_all([artefact, artefact_build, environment, test_execution]) + db_session.commit() + db_session.refresh(test_execution) + + test_client.patch( + f"/v1/test-executions/{test_execution.id}", + json={ + "jenkins_link": "some jenkins link", + "c3_link": "some c3 link", + "status": TestExecutionStatus.PASSED.name, + }, + ) + + db_session.refresh(test_execution) + assert test_execution.jenkins_link == "some jenkins link" + assert test_execution.c3_link == "some c3 link" + assert test_execution.status == TestExecutionStatus.PASSED