-
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 (#184)
* Add status update endpoint to receive updates from Testflinger * Add frontend changes to display event log * Add resource_url to the test_execution table * Added horizontal scrolling to event log * Prevent status updates from overwriting test results * Add last event to execution name and fix padding issue * Changed ended to ended_prematurely
- Loading branch information
Showing
16 changed files
with
627 additions
and
27 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
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,62 @@ | ||
"""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", 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 ### | ||
op.execute( | ||
"ALTER TABLE test_execution ADD COLUMN " | ||
"resource_url VARCHAR NOT NULL DEFAULT ''" | ||
) | ||
|
||
with op.get_context().autocommit_block(): | ||
op.execute("ALTER TYPE testexecutionstatus ADD VALUE 'ENDED_PREMATURELY'") | ||
|
||
|
||
def downgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.drop_table("test_event") | ||
# ### end Alembic commands ### | ||
op.execute("ALTER TYPE testexecutionstatus RENAME TO testexecutionstatus_old") | ||
op.execute( | ||
"CREATE TYPE testexecutionstatus AS " | ||
"ENUM('NOT_STARTED', 'IN_PROGRESS', 'PASSED', 'FAILED', 'NOT_TESTED')" | ||
) | ||
op.execute( | ||
"ALTER TABLE test_execution ALTER COLUMN status TYPE testexecutionstatus USING " | ||
"status::text::testexecutionstatus" | ||
) | ||
op.execute("DROP TYPE testexecutionstatus_old") | ||
op.drop_column("test_execution", "resource_url") |
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
84 changes: 84 additions & 0 deletions
84
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,84 @@ | ||
# 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.models_enums import TestExecutionStatus | ||
from test_observer.data_access.setup import get_db | ||
|
||
from .logic import delete_previous_test_events | ||
from .models import StatusUpdateRequest | ||
from .testflinger_event_parser import TestflingerEventParser | ||
|
||
router = APIRouter() | ||
|
||
|
||
@router.put("/{id}/status_update") | ||
def put_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=event.detail, | ||
) | ||
db.add(test_event) | ||
test_execution.test_events.append(test_event) | ||
event_parser = TestflingerEventParser() | ||
event_parser.process_events(test_execution.test_events) | ||
if event_parser.resource_url is not None: | ||
test_execution.resource_url = event_parser.resource_url | ||
if ( | ||
event_parser.is_ended_prematurely | ||
and test_execution.status is not TestExecutionStatus.FAILED | ||
and test_execution.status is not TestExecutionStatus.PASSED | ||
): | ||
test_execution.status = TestExecutionStatus.ENDED_PREMATURELY | ||
db.commit() | ||
|
||
|
||
@router.get("/{id}/status_update") | ||
def get_status_update(id: int, 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") | ||
|
||
test_events = [] | ||
for test_event in test_execution.test_events: | ||
test_events.append(test_event) | ||
|
||
return test_events |
30 changes: 30 additions & 0 deletions
30
backend/test_observer/controllers/test_executions/testflinger_event_parser.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,30 @@ | ||
# 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 test_observer.data_access.models import TestEvent | ||
|
||
|
||
class TestflingerEventParser: | ||
def __init__(self): | ||
self.is_ended_prematurely = False | ||
self.resource_url = None | ||
|
||
def process_events(self, events: list[TestEvent]): | ||
final_event = events[-1] | ||
if final_event.event_name == "job_end" and final_event.detail != "normal_exit": | ||
self.is_ended_prematurely = True | ||
if events[0].event_name == "job_start": | ||
self.resource_url = events[0].detail |
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
Oops, something went wrong.