Skip to content

Commit

Permalink
add task test
Browse files Browse the repository at this point in the history
  • Loading branch information
elikoga committed Jan 20, 2025
1 parent d8e2484 commit ae05b20
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 3 deletions.
7 changes: 7 additions & 0 deletions controller/thymis_controller/crud/task.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import uuid

from sqlalchemy import select
Expand Down Expand Up @@ -85,3 +86,9 @@ def fail_running_tasks(db_session):
def child_task_states(db_session, tasks: list[uuid.UUID]) -> set[str]:
tasks = db_session.query(db_models.Task).filter(db_models.Task.id.in_(tasks)).all()
return set(task.state for task in tasks)


def delete_all_tasks(db_session: Session):
if "RUNNING_IN_PLAYWRIGHT" in os.environ:
db_session.query(db_models.Task).delete()
db_session.commit()
10 changes: 10 additions & 0 deletions controller/thymis_controller/routers/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from fastapi import APIRouter, Response, WebSocket
from thymis_controller.dependencies import SessionAD, TaskControllerAD
from thymis_controller.routers.frontend import is_running_in_playwright

router = APIRouter()

Expand Down Expand Up @@ -50,3 +51,12 @@ async def retry_task(
):
task_controller.retry_task(task_id, db_session)
return {"message": "Task retried"}


@router.post("/tasks/delete_all")
async def delete_all_tasks(task_controller: TaskControllerAD, db_session: SessionAD):
if is_running_in_playwright():
task_controller.delete_all_tasks(db_session)
return {"message": "All tasks deleted"}
else:
return Response(status_code=403)
10 changes: 10 additions & 0 deletions controller/thymis_controller/task/controller.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import contextlib
import logging
import os
from datetime import datetime

import sqlalchemy
Expand Down Expand Up @@ -102,3 +103,12 @@ def retry_task(self, task_id: str, db_session: Session):
task = crud.task.get_task_by_id(db_session, task_id)
task_data = TaskSubmission.from_orm_task(task).data
self.submit(task_data, db_session)

def delete_all_tasks(self, db_session: Session):
if "RUNNING_IN_PLAYWRIGHT" in os.environ:
for task in crud.task.get_pending_tasks(db_session):
task.state = "failed"
task.add_exception("Task was running when controller was shut down")
self.executor.cancel_all_tasks()
db_session.commit()
crud.task.delete_all_tasks(db_session)
4 changes: 4 additions & 0 deletions controller/thymis_controller/task/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ def cancel_task(self, task_id: uuid.UUID):
if task_id in self.futures:
self.futures[task_id][1].send(models_task.CancelTask(id=task_id))

def cancel_all_tasks(self):
for task_id in self.futures:
self.cancel_task(task_id)

def listen_child_messages(self, conn: Connection, task_id: uuid.UUID):
message = None
try:
Expand Down
2 changes: 1 addition & 1 deletion frontend/playwright.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ const config: PlaywrightTestConfig = {
reporter: [['list'], ['html']],
expect: {
toHaveScreenshot: {
maxDiffPixels: 25,
maxDiffPixels: 15,
threshold: 0.1
}
},
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/lib/components/RenderUnixTimestamp.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</script>

{#if timestamp && date}
<p title={date.toISOString()}>
<p title={date.toISOString()} class="playwright-snapshot-unstable">
<time datetime={date.toISOString()}>
{date.toLocaleString(undefined, { dateStyle: 'short', timeStyle: 'long' })}
</time>
Expand Down
23 changes: 22 additions & 1 deletion frontend/tests/screencaps.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { test, expect } from '../playwright/fixtures';
import { clearState, expectPageToHaveScreenshotWithHighlight } from './utils';
import { clearState, deleteAllTasks, expectPageToHaveScreenshotWithHighlight } from './utils';

test.describe.configure({ mode: 'serial' });

Expand All @@ -11,6 +11,7 @@ colorSchemes.forEach((colorScheme) => {

test('overview page shows overview', async ({ page, request }) => {
await clearState(page, request);
await deleteAllTasks(page, request);

await page.goto('/login');

Expand All @@ -25,6 +26,7 @@ colorSchemes.forEach((colorScheme) => {

test('shows devices', async ({ page, request }) => {
await clearState(page, request);
await deleteAllTasks(page, request);
const resp = await page.goto('/devices');

// 200 OK is expected
Expand Down Expand Up @@ -74,6 +76,7 @@ colorSchemes.forEach((colorScheme) => {

test('explores more pages', async ({ page, request }) => {
await clearState(page, request);
await deleteAllTasks(page, request);

// Navigate to the Devices page
await page.goto('/devices');
Expand All @@ -100,6 +103,7 @@ colorSchemes.forEach((colorScheme) => {

test('create whoami tag', async ({ page, request }) => {
await clearState(page, request);
await deleteAllTasks(page, request);

// Navigate to the Tags page and create a new tag
await page.goto('/tags');
Expand Down Expand Up @@ -193,5 +197,22 @@ colorSchemes.forEach((colorScheme) => {

await expect(page).toHaveScreenshot();
});

test('Create update tasks', async ({ page, request }) => {
await clearState(page, request);
await deleteAllTasks(page, request);

// Go to devices page
await page.goto('/devices');

// Click on "Update" button
const updateButton = page.locator('button').filter({ hasText: 'Update' });
await updateButton.click();

// Expect a task to be created
await expect(page).toHaveScreenshot({
mask: [page.locator('.playwright-snapshot-unstable')]
});
});
});
});
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions frontend/tests/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,9 @@ export const clearState = async (page: Page, request: APIRequestContext) => {
state['devices'] = [];
await request.patch('/api/state', { data: state });
};

export const deleteAllTasks = async (page: Page, request: APIRequestContext) => {
const tasksRequest = await request.post('/api/tasks/delete_all');
await tasksRequest.json();
await page.reload();
};

0 comments on commit ae05b20

Please sign in to comment.