refactor: app deployment

This commit is contained in:
2023-06-16 11:40:23 +02:00
parent d61d16e752
commit 5b4d2bd16c
39 changed files with 523 additions and 246 deletions

View File

@@ -53,6 +53,7 @@ commander
'./node_modules/@openstapps/core/test/resources/indexable',
)
.option('-w --waiton [resource]', 'wait-on resource parameter see "www.npmjs.com/wait-on"')
.option('-r --reportPath [reportPath]', 'JUnit Report Path')
// eslint-disable-next-line unicorn/prevent-abbreviations
.action(async (to, e2eCommand) => {
let toURL = '';
@@ -73,7 +74,11 @@ commander
});
Logger.info(`Resource became available`);
}
await e2eRun(client, {to: toURL, samplesLocation: e2eCommand.samples});
await e2eRun(client, {
to: toURL,
samplesLocation: e2eCommand.samples,
reportLocation: e2eCommand.reportPath,
});
Logger.ok('Done');
} catch (error) {
await Logger.error(error);

View File

@@ -21,10 +21,29 @@ import {readdir, readFile} from 'fs';
import path from 'path';
import {promisify} from 'util';
import {ConnectorClient, HttpClientInterface} from '@openstapps/api';
import junit from 'junit-report-builder';
const localItemMap: Map<string, SCThings> = new Map();
const remoteItemMap: Map<string, SCThings> = new Map();
async function runTest(name: string, scope: () => Promise<void>, suite: junit.TestSuite, errors: string[]) {
const testCase = suite.testCase().name(name);
process.stdout.addListener('data', testCase.standardOutput);
process.stderr.addListener('data', testCase.standardError);
const start = performance.now();
await scope().catch(async error => {
await Logger.error(error);
testCase.failure(error.message);
errors.push(error.message);
});
process.stdout.removeListener('data', testCase.standardOutput);
process.stderr.removeListener('data', testCase.standardError);
const end = performance.now();
testCase.time((end - start) / 1000);
}
/**
* Options to set up indexing core test files to backend
*/
@@ -38,34 +57,54 @@ export interface E2EOptions {
* URL of the backend to index to
*/
to: string;
/**
* Location of the report
*/
reportLocation?: string;
}
/**
* Function that can be used for integration tests.
* Adds all the SCThings that getItemsFromSamples() returns to the backend.
* Afterwards retrieves the items from backend and checks for differences with original ones.
* Afterward, retrieves the items from backend and checks for differences with original ones.
*/
export async function e2eRun(client: HttpClientInterface, options: E2EOptions): Promise<void> {
export async function e2eRun(client: HttpClientInterface, options: E2EOptions): Promise<string[]> {
localItemMap.clear();
remoteItemMap.clear();
const builder = junit.newBuilder();
const errors: string[] = [];
const api = new ConnectorClient(client, options.to);
try {
await indexSamples(api, options);
const indexSuite = builder.testSuite().name('e2e index');
await indexSamples(api, options, indexSuite, errors);
Logger.info(`All samples have been indexed via the backend`);
await retrieveItems(api);
const retrieveSuite = builder.testSuite().name('e2e retrieve');
await retrieveItems(api, retrieveSuite, errors);
Logger.info(`All samples have been retrieved from the backend`);
compareItems();
const compareSuite = builder.testSuite().name('e2e compare');
await compareItems(compareSuite, errors);
} catch (error) {
throw error;
}
if (options.reportLocation) {
builder.writeTo(options.reportLocation);
}
await (errors.length > 0
? Logger.error(`\n${errors.length} failed test cases`)
: Logger.ok('All tests passed.'));
return errors;
}
/**
* Retieves all samples previously index using the api
* Retrieves all samples previously index using the api
*/
async function retrieveItems(api: ConnectorClient): Promise<void> {
async function retrieveItems(api: ConnectorClient, suite: junit.TestSuite, errors: string[]): Promise<void> {
const singleItemSearchRequest: SCSearchRequest = {
filter: {
arguments: {
@@ -76,61 +115,83 @@ async function retrieveItems(api: ConnectorClient): Promise<void> {
},
};
for (const uid of localItemMap.keys()) {
singleItemSearchRequest.filter!.arguments.value = uid;
const searchResonse = await api.search(singleItemSearchRequest);
if (searchResonse.data.length !== 1) {
throw new Error(
`Search for single SCThing with uid: ${uid} returned ${searchResonse.data.length} results`,
);
}
remoteItemMap.set(uid, searchResonse.data[0]);
await runTest(
`Should find ${uid}`,
async () => {
singleItemSearchRequest.filter!.arguments.value = uid;
const searchResponse = await api.search(singleItemSearchRequest);
if (searchResponse.data.length !== 1) {
throw new Error(
`Search for single SCThing with uid: ${uid} returned ${searchResponse.data.length} results`,
);
}
remoteItemMap.set(uid, searchResponse.data[0]);
},
suite,
errors,
);
}
}
/**
* Compares all samples (local and remote) with the same uid and throws if they're not deep equal
*/
function compareItems() {
async function compareItems(suite: junit.TestSuite, errors: string[]) {
for (const localThing of localItemMap.values()) {
/* istanbul ignore next retrieveItems will throw before*/
if (!remoteItemMap.has(localThing.uid)) {
throw new Error(`Did not retrieve expected SCThing with uid: ${localThing.uid}`);
}
const remoteThing = remoteItemMap.get(localThing.uid);
deepStrictEqual(remoteThing, localThing, `Unexpected difference between original and retrieved sample`);
await runTest(
`Should be the same for ${localThing.uid}`,
async () => {
/* istanbul ignore next retrieveItems will throw before*/
if (!remoteItemMap.has(localThing.uid)) {
throw new Error(`Did not retrieve expected SCThing with uid: ${localThing.uid}`);
}
const remoteThing = remoteItemMap.get(localThing.uid);
deepStrictEqual(
remoteThing,
localThing,
`Unexpected difference between original and retrieved sample`,
);
},
suite,
errors,
);
}
Logger.info(
`All samples retrieved from the backend are the same (deep equal) as the original ones submitted`,
);
Logger.info(`All samples retrieved from the backend have been compared`);
}
/**
* Function to add all the SCThings that getItemsFromSamples() returns to the backend
*/
async function indexSamples(api: ConnectorClient, options: E2EOptions): Promise<void> {
try {
const items = await getItemsFromSamples(options.samplesLocation);
async function indexSamples(
api: ConnectorClient,
options: E2EOptions,
suite: junit.TestSuite,
errors: string[],
): Promise<void> {
const items = await getItemsFromSamples(options.samplesLocation);
if (items.length === 0) {
throw new Error('Could not index samples. None were retrieved from the file system.');
}
if (items.length === 0) {
throw new Error('Could not index samples. None were retrieved from the file system.');
}
// sort items by type
const itemMap: Map<SCThingType, SCThings[]> = new Map();
for (const item of items) {
if (!itemMap.has(item.type)) {
itemMap.set(item.type, []);
}
const itemsOfSameType = itemMap.get(item.type) as SCThings[];
itemsOfSameType.push(item);
itemMap.set(item.type, itemsOfSameType);
localItemMap.set(item.uid, item);
// sort items by type
const itemMap: Map<SCThingType, SCThings[]> = new Map();
for (const item of items) {
if (!itemMap.has(item.type)) {
itemMap.set(item.type, []);
}
// add items depending on their type property with one type per bulk
for (const type of itemMap.keys()) {
await api.index(itemMap.get(type) as SCThings[], 'stapps-core-sample-data');
}
} catch (error) {
throw error;
const itemsOfSameType = itemMap.get(item.type) as SCThings[];
itemsOfSameType.push(item);
itemMap.set(item.type, itemsOfSameType);
localItemMap.set(item.uid, item);
}
// add items depending on their type property with one type per bulk
for (const type of itemMap.keys()) {
await runTest(
`Should index ${type}`,
async () => api.index(itemMap.get(type) as SCThings[], 'stapps-core-sample-data'),
suite,
errors,
);
}
}