-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
163 additions
and
6 deletions.
There are no files selected for viewing
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,40 @@ | ||
import { Resource, Server, useStore } from '@tomic/react'; | ||
import React, { useState } from 'react'; | ||
import { Button } from '../components/Button'; | ||
import { ContainerFull } from '../components/Containers'; | ||
import { Column } from '../components/Row'; | ||
|
||
export function PruneTestsRoute(): JSX.Element { | ||
const store = useStore(); | ||
const [result, setResult] = useState<Resource<Server.EndpointResponse>>(); | ||
const [isWaiting, setIsWaiting] = useState(false); | ||
|
||
const postPruneTest = async () => { | ||
setIsWaiting(true); | ||
const url = new URL('/prunetests', store.getServerUrl()); | ||
const res = await store.postToServer(url.toString()); | ||
setIsWaiting(false); | ||
setResult(res); | ||
}; | ||
|
||
return ( | ||
<main> | ||
<ContainerFull> | ||
<h1>Prune Test Data</h1> | ||
<p> | ||
Pruning test data will delete all drives on the server that have | ||
’testdrive’ in their name. | ||
</p> | ||
<Column> | ||
<Button onClick={postPruneTest} disabled={isWaiting} alert> | ||
Prune | ||
</Button> | ||
{isWaiting && <p>Pruning, this might take a while...</p>} | ||
<p data-testId='prune-result'> | ||
{result && `✅ ${result.props.responseMessage}`} | ||
</p> | ||
</Column> | ||
</ContainerFull> | ||
</main> | ||
); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { test as setup, expect } from '@playwright/test'; | ||
import { before, FRONTEND_URL, signIn } from './test-utils'; | ||
|
||
setup('delete previous test data', async ({ page }) => { | ||
await before({ page }); | ||
await signIn(page); | ||
await page.goto(`${FRONTEND_URL}/prunetests`); | ||
await expect(page.getByText('Prune Test Data')).toBeVisible(); | ||
await page.getByRole('button', { name: 'Prune' }).click(); | ||
|
||
await expect(page.getByTestId('prune-result')).toBeVisible(); | ||
}); |
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
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
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,70 @@ | ||
use crate::{ | ||
endpoints::{Endpoint, HandleGetContext, HandlePostContext}, | ||
errors::AtomicResult, | ||
storelike::Query, | ||
urls, Resource, Storelike, Value, | ||
}; | ||
|
||
pub fn prune_tests_endpoint() -> Endpoint { | ||
Endpoint { | ||
path: urls::PATH_PRUNE_TESTS.into(), | ||
params: [].into(), | ||
description: "Deletes all drives with 'testdrive-' in their name.".to_string(), | ||
shortname: "prunetests".to_string(), | ||
handle: Some(handle_get), | ||
handle_post: Some(handle_prune_tests_request), | ||
} | ||
} | ||
|
||
pub fn handle_get(context: HandleGetContext) -> AtomicResult<Resource> { | ||
prune_tests_endpoint().to_resource(context.store) | ||
} | ||
|
||
// Delete all drives with 'testdrive-' in their name. (These drive are generated with each e2e test run) | ||
fn handle_prune_tests_request(context: HandlePostContext) -> AtomicResult<Resource> { | ||
let HandlePostContext { store, .. } = context; | ||
|
||
let mut query = Query::new_class(urls::DRIVE); | ||
query.for_agent = context.for_agent.clone(); | ||
let mut deleted_drives = 0; | ||
|
||
if let Ok(mut query_result) = store.query(&query) { | ||
println!( | ||
"Received prune request, deleting {} drives", | ||
query_result.resources.len() | ||
); | ||
|
||
let total_drives = query_result.resources.len(); | ||
|
||
for resource in query_result.resources.iter_mut() { | ||
if let Value::String(name) = resource | ||
.get(urls::NAME) | ||
.unwrap_or(&Value::String("".to_string())) | ||
{ | ||
if name.contains("testdrive-") { | ||
resource.destroy(store)?; | ||
deleted_drives += 1; | ||
|
||
if (deleted_drives % 10) == 0 { | ||
println!("Deleted {} of {} drives", deleted_drives, total_drives); | ||
} | ||
} | ||
} | ||
} | ||
|
||
println!("Done pruning drives"); | ||
} else { | ||
println!("Received prune request but there are no drives to prune"); | ||
} | ||
|
||
let resource = build_response(store, 200, format!("Deleted {} drives", deleted_drives)); | ||
Ok(resource) | ||
} | ||
|
||
fn build_response(store: &impl Storelike, status: i32, message: String) -> Resource { | ||
let mut resource = Resource::new_generate_subject(store); | ||
resource.set_class(urls::ENDPOINT_RESPONSE); | ||
resource.set_propval_unsafe(urls::STATUS.to_string(), status.into()); | ||
resource.set_propval_unsafe(urls::RESPONSE_MESSAGE.to_string(), message.into()); | ||
resource | ||
} |
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