-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add status update endpoint to receive updates from Testflinger
- Loading branch information
Showing
7 changed files
with
268 additions
and
1 deletion.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
backend/migrations/versions/2024_06_11_2002-2745d4e5bc72_create_test_status_update_tables.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
"""Create test status update tables | ||
Revision ID: 2745d4e5bc72 | ||
Revises: 33c0383ea9ca | ||
Create Date: 2024-06-11 20:02:00.064753+00:00 | ||
""" | ||
from alembic import op | ||
import sqlalchemy as sa | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision = "2745d4e5bc72" | ||
down_revision = "33c0383ea9ca" | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.create_table( | ||
"test_event", | ||
sa.Column("event_name", sa.String(), nullable=False), | ||
sa.Column("timestamp", sa.DateTime(), nullable=False), | ||
sa.Column("detail_msg", sa.String(), nullable=False), | ||
sa.Column("test_execution_id", sa.Integer(), nullable=False), | ||
sa.Column("id", sa.Integer(), autoincrement=True, nullable=False), | ||
sa.Column("created_at", sa.DateTime(), nullable=False), | ||
sa.Column("updated_at", sa.DateTime(), nullable=False), | ||
sa.ForeignKeyConstraint( | ||
["test_execution_id"], | ||
["test_execution.id"], | ||
name=op.f("test_event_test_execution_id_fkey"), | ||
ondelete="CASCADE", | ||
), | ||
sa.PrimaryKeyConstraint("id", name=op.f("test_event_pkey")), | ||
) | ||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.drop_table("test_event") | ||
# ### end Alembic commands ### |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
backend/test_observer/controllers/test_executions/status_update.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# Copyright 2024 Canonical Ltd. | ||
# | ||
# 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 <http://www.gnu.org/licenses/>. | ||
# | ||
|
||
from fastapi import APIRouter, Depends, HTTPException | ||
from sqlalchemy.orm import Session, joinedload | ||
|
||
from test_observer.data_access.models import ( | ||
TestEvent, | ||
TestExecution, | ||
) | ||
|
||
from test_observer.data_access.setup import get_db | ||
|
||
from .logic import delete_previous_test_events | ||
from .models import StatusUpdateRequest | ||
|
||
router = APIRouter() | ||
|
||
|
||
@router.put("/{id}/status_update") | ||
def status_update(id: int, request: StatusUpdateRequest, db: Session = Depends(get_db)): | ||
test_execution = db.get( | ||
TestExecution, | ||
id, | ||
options=[joinedload(TestExecution.test_events)], | ||
) | ||
if test_execution is None: | ||
raise HTTPException(status_code=404, detail="TestExecution not found") | ||
|
||
delete_previous_test_events(db, test_execution) | ||
|
||
for event in request.events: | ||
test_event = TestEvent( | ||
event_name=event.event_name, | ||
timestamp=event.timestamp, | ||
detail_msg=event.detail_msg, | ||
) | ||
db.add(test_event) | ||
test_execution.test_events.append(test_event) | ||
db.commit() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
126 changes: 126 additions & 0 deletions
126
backend/tests/controllers/test_executions/test_status_update.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
# Copyright 2024 Canonical Ltd. | ||
# | ||
# 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 <http://www.gnu.org/licenses/>. | ||
# | ||
|
||
import datetime | ||
|
||
from fastapi.testclient import TestClient | ||
|
||
from tests.data_generator import DataGenerator | ||
|
||
|
||
def test_status_updates_stored(test_client: TestClient, generator: DataGenerator): | ||
artefact = generator.gen_artefact("beta") | ||
artefact_build = generator.gen_artefact_build(artefact) | ||
environment = generator.gen_environment() | ||
test_execution = generator.gen_test_execution( | ||
artefact_build, environment, ci_link="http://localhost" | ||
) | ||
|
||
response = test_client.put( | ||
f"/v1/test-executions/{test_execution.id}/status_update", | ||
json={ | ||
"agent_id": "test_agent", | ||
"job_queue": "test_job_queue", | ||
"events": [ | ||
{ | ||
"event_name": "started_setup", | ||
"timestamp": "2015-03-21T11:08:14.859831", | ||
"detail_msg": "my_detail_msg_one", | ||
}, | ||
{ | ||
"event_name": "ended_setup", | ||
"timestamp": "2015-03-21T11:08:15.859831", | ||
"detail_msg": "my_detail_msg_two", | ||
}, | ||
], | ||
}, | ||
) | ||
assert response.status_code == 200 | ||
assert test_execution.test_events[0].event_name == "started_setup" | ||
assert test_execution.test_events[0].timestamp == datetime.datetime.fromisoformat( | ||
"2015-03-21T11:08:14.859831" | ||
) | ||
assert test_execution.test_events[0].detail_msg == "my_detail_msg_one" | ||
assert test_execution.test_events[1].event_name == "ended_setup" | ||
assert test_execution.test_events[1].timestamp == datetime.datetime.fromisoformat( | ||
"2015-03-21T11:08:15.859831" | ||
) | ||
assert test_execution.test_events[1].detail_msg == "my_detail_msg_two" | ||
|
||
|
||
def test_status_updates_is_idempotent( | ||
test_client: TestClient, generator: DataGenerator | ||
): | ||
artefact = generator.gen_artefact("beta") | ||
artefact_build = generator.gen_artefact_build(artefact) | ||
environment = generator.gen_environment() | ||
test_execution = generator.gen_test_execution( | ||
artefact_build, environment, ci_link="http://localhost" | ||
) | ||
|
||
for _ in range(3): | ||
test_client.put( | ||
f"/v1/test-executions/{test_execution.id}/status_update", | ||
json={ | ||
"agent_id": "test_agent", | ||
"job_queue": "test_job_queue", | ||
"events": [ | ||
{ | ||
"event_name": "started_setup", | ||
"timestamp": "2015-03-21T11:08:14.859831", | ||
"detail_msg": "my_detail_msg_one", | ||
}, | ||
{ | ||
"event_name": "ended_setup", | ||
"timestamp": "2015-03-21T11:08:15.859831", | ||
"detail_msg": "my_detail_msg_two", | ||
}, | ||
], | ||
}, | ||
) | ||
assert len(test_execution.test_events) == 2 | ||
|
||
|
||
def test_status_updates_invalid_timestamp( | ||
test_client: TestClient, generator: DataGenerator | ||
): | ||
artefact = generator.gen_artefact("beta") | ||
artefact_build = generator.gen_artefact_build(artefact) | ||
environment = generator.gen_environment() | ||
test_execution = generator.gen_test_execution( | ||
artefact_build, environment, ci_link="http://localhost" | ||
) | ||
|
||
response = test_client.put( | ||
f"/v1/test-executions/{test_execution.id}/status_update", | ||
json={ | ||
"agent_id": "test_agent", | ||
"job_queue": "test_job_queue", | ||
"events": [ | ||
{ | ||
"event_name": "started_setup", | ||
"timestamp": "201-03-21T11:08:14.859831", | ||
"detail_msg": "my_detail_msg_one", | ||
}, | ||
{ | ||
"event_name": "ended_setup", | ||
"timestamp": "20-03-21T11:08:15.859831", | ||
"detail_msg": "my_detail_msg_two", | ||
}, | ||
], | ||
}, | ||
) | ||
assert response.status_code == 422 |