feat: extend e2e procedure

This commit is contained in:
Rainer Killinger
2019-11-25 16:47:54 +01:00
parent 91de58b5ae
commit dc79dc8feb
3 changed files with 173 additions and 49 deletions

View File

@@ -12,13 +12,14 @@
* You should have received a copy of the GNU General Public License along with * You should have received a copy of the GNU General Public License along with
* this program. If not, see <https://www.gnu.org/licenses/>. * this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
import {SCThingType} from '@openstapps/core';
import {Logger} from '@openstapps/logger'; import {Logger} from '@openstapps/logger';
import * as commander from 'commander'; import * as commander from 'commander';
import {readFileSync} from 'fs'; import {readFileSync} from 'fs';
import {join} from 'path'; import {join} from 'path';
import {URL} from 'url'; import {URL} from 'url';
import {copy} from './copy'; import {copy} from './copy';
import {indexSamples} from './e2e'; import {e2eRun} from './e2e';
import {HttpClient} from './http-client'; import {HttpClient} from './http-client';
const pkgJson = JSON.parse(readFileSync(join(__dirname, '..', 'package.json')) const pkgJson = JSON.parse(readFileSync(join(__dirname, '..', 'package.json'))
@@ -34,7 +35,7 @@ process.on('unhandledRejection', async (error) => {
commander commander
.command('e2e <to>') .command('e2e <to>')
.description('Run in end to end test mode. Indexing all test files from @openstapp/core to the backend') .description('Run in end to end test mode. Indexing and afterwards retrieving all test files from @openstapp/core to the backend')
.option('-s --samples [path]', 'Path to @openstapp/core test files', './node_modules/@openstapps/core/test/resources') .option('-s --samples [path]', 'Path to @openstapp/core test files', './node_modules/@openstapps/core/test/resources')
.action(async (to, e2eCommand) => { .action(async (to, e2eCommand) => {
let toURL = ''; let toURL = '';
@@ -49,7 +50,7 @@ commander
actionDone = true; actionDone = true;
indexSamples(client, {to: toURL, samples: e2eCommand.samples}) e2eRun(client, {to: toURL, samplesLocation: e2eCommand.samples})
.then(() => { .then(() => {
Logger.ok('Done'); Logger.ok('Done');
}) })
@@ -108,7 +109,7 @@ commander
from: fromURL, from: fromURL,
source: copyCommand.bulkSource, source: copyCommand.bulkSource,
to: toURL, to: toURL,
type: type, type: type as SCThingType,
version: copyCommand.appVersion, version: copyCommand.appVersion,
}) })
.then(() => { .then(() => {

View File

@@ -13,13 +13,18 @@
* this program. If not, see <https://www.gnu.org/licenses/>. * this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
import {SCThings, SCThingType} from '@openstapps/core'; import {SCSearchRequest, SCThings, SCThingType} from '@openstapps/core';
import {Logger} from '@openstapps/logger';
import {deepStrictEqual} from 'assert';
import {readdir, readFile} from 'fs'; import {readdir, readFile} from 'fs';
import {join} from 'path'; import {join} from 'path';
import {promisify} from 'util'; import {promisify} from 'util';
import {ConnectorClient} from './connector-client'; import {ConnectorClient} from './connector-client';
import {HttpClientInterface} from './http-client-interface'; import {HttpClientInterface} from './http-client-interface';
const localItemMap: Map<string, SCThings> = new Map();
const remoteItemMap: Map<string, SCThings> = new Map();
/** /**
* Options to set up indexing core test files to backend * Options to set up indexing core test files to backend
*/ */
@@ -27,7 +32,7 @@ export interface E2EOptions {
/** /**
* File path of the directory containing core test files * File path of the directory containing core test files
*/ */
samples: string; samplesLocation: string;
/** /**
* URL of the backend to index to * URL of the backend to index to
@@ -36,18 +41,73 @@ export interface E2EOptions {
} }
/** /**
* Function to add all the SCThings that getItemsFromSamples() returns to the backend * Function that can be used for integration tests.
* * Adds all the SCThings that getItemsFromSamples() returns to the backend.
* @param client HTTP client * Afterwards retrieves the items from backend and checks for differences with original ones.
* @param options Map of options
*/ */
export async function indexSamples(client: HttpClientInterface, options: E2EOptions): Promise<void> { export async function e2eRun(client: HttpClientInterface, options: E2EOptions): Promise<void> {
const api = new ConnectorClient(client, options.to); localItemMap.clear();
remoteItemMap.clear();
const items = await getItemsFromSamples(options.samples); const api = new ConnectorClient(client, options.to);
try {
await indexSamples(api, options);
Logger.info(`All samples have been indexed via the backend`);
await retrieveItems(api);
Logger.info(`All samples have been retrieved from the backend`);
compareItems();
} catch (error) {
throw error;
}
}
/**
* Retieves all samples previously index using the api
*/
async function retrieveItems(api: ConnectorClient): Promise<void> {
const singleItemSearchRequest: SCSearchRequest = {
filter: {
arguments: {
field: 'uid',
value: 'replace-me',
},
type: 'value',
},
};
for (const uid of localItemMap.keys()) {
singleItemSearchRequest.filter!.arguments.value = uid;
const searchResonse = await api.search(singleItemSearchRequest);
if (searchResonse.data.length !== 1) {
throw Error(`Search for single SCThing with uid: ${uid} returned ${searchResonse.data.length} results`);
}
remoteItemMap.set(uid, searchResonse.data[0]);
}
}
/**
* Compares all samples (local and remote) with the same uid and throws if they're not deep equal
*/
function compareItems() {
for (const localThing of localItemMap.values()) {
/* istanbul ignore next retrieveItems will throw before*/
if (!remoteItemMap.has(localThing.uid)) {
throw 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`);
}
Logger.info(`All samples retrieved from the backend are the same (deep equal) as the original ones submitted`);
}
/**
* Function to add all the SCThings that getItemsFromSamples() returns to the backend
*/
async function indexSamples(api: ConnectorClient, options: E2EOptions): Promise<void> {
const items = await getItemsFromSamples(options.samplesLocation);
if (items.length === 0) { if (items.length === 0) {
throw new Error('Could not index samples. None were retrived from the file system.'); throw new Error('Could not index samples. None were retrieved from the file system.');
} }
try { try {
@@ -57,17 +117,14 @@ export async function indexSamples(client: HttpClientInterface, options: E2EOpti
if (!itemMap.has(item.type)) { if (!itemMap.has(item.type)) {
itemMap.set(item.type, []); itemMap.set(item.type, []);
} }
const currentItems = itemMap.get(item.type) as SCThings[]; const itemsOfSameType = itemMap.get(item.type) as SCThings[];
currentItems.push(item); itemsOfSameType.push(item);
itemMap.set(item.type, currentItems); itemMap.set(item.type, itemsOfSameType);
localItemMap.set(item.uid, item);
} }
// add items depending on their type property with one type per bulk // add items depending on their type property with one type per bulk
for (const type of itemMap.keys()) { for (const type of itemMap.keys()) {
const currentBulk = await api.bulk(type, 'stapps-core-sample-data'); await api.index(itemMap.get(type) as SCThings[], 'stapps-core-sample-data');
for (const item of (itemMap.get(type) as SCThings[])) {
await currentBulk!.add(item);
}
await currentBulk.done();
} }
} catch (err) { } catch (err) {
throw err; throw err;

View File

@@ -12,6 +12,9 @@
* You should have received a copy of the GNU General Public License along with * You should have received a copy of the GNU General Public License along with
* this program. If not, see <https://www.gnu.org/licenses/>. * this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
// tslint:disable-next-line: max-line-length
// tslint:disable: completed-docs no-implicit-dependencies prefer-function-over-method newline-per-chained-call member-ordering
import { import {
SCBulkAddResponse, SCBulkAddResponse,
SCBulkAddRoute, SCBulkAddRoute,
@@ -19,19 +22,23 @@ import {
SCBulkDoneRoute, SCBulkDoneRoute,
SCBulkResponse, SCBulkResponse,
SCBulkRoute, SCBulkRoute,
SCSearchResponse,
SCSearchRoute,
SCThing, SCThing,
SCThings,
} from '@openstapps/core'; } from '@openstapps/core';
import * as chai from 'chai'; import * as chai from 'chai';
import * as chaiAsPromised from 'chai-as-promised'; import * as chaiAsPromised from 'chai-as-promised';
import * as chaiSpies from 'chai-spies'; import * as chaiSpies from 'chai-spies';
import clone = require('fast-clone');
import {existsSync, mkdirSync, rmdirSync, unlinkSync} from 'fs'; import {existsSync, mkdirSync, rmdirSync, unlinkSync} from 'fs';
import {createFileSync} from 'fs-extra';
import {suite, test} from 'mocha-typescript'; import {suite, test} from 'mocha-typescript';
import {join} from 'path'; import {join} from 'path';
import {getItemsFromSamples, indexSamples} from '../src/e2e'; import {e2eRun, getItemsFromSamples} from '../src/e2e';
import {ApiError} from '../src/errors'; import {ApiError} from '../src/errors';
import {HttpClient, RequestOptions, Response} from '../src/http-client'; import {HttpClient, RequestOptions, Response} from '../src/http-client';
import {RecursivePartial} from './client.spec'; import {RecursivePartial} from './client.spec';
import {createFileSync} from 'fs-extra';
chai.should(); chai.should();
chai.use(chaiSpies); chai.use(chaiSpies);
@@ -43,8 +50,12 @@ const bulkRoute = new SCBulkRoute();
const bulkAddRoute = new SCBulkAddRoute(); const bulkAddRoute = new SCBulkAddRoute();
const bulkDoneRoute = new SCBulkDoneRoute(); const bulkDoneRoute = new SCBulkDoneRoute();
const searchRoute = new SCSearchRoute();
const httpClient = new HttpClient(); const httpClient = new HttpClient();
const storedThings: Map<string, SCThings> = new Map();
@suite @suite
export class E2EConnectorSpec { export class E2EConnectorSpec {
async after() { async after() {
@@ -52,18 +63,16 @@ export class E2EConnectorSpec {
} }
@test @test
getCoreTestSamples() { async getCoreTestSamples() {
return getItemsFromSamples('./node_modules/@openstapps/core/test/resources') const items = await getItemsFromSamples('./node_modules/@openstapps/core/test/resources');
.then(<T extends SCThing>(items: T[]) => { // tslint:disable-next-line: no-unused-expression
// tslint:disable-next-line: no-unused-expression chai.expect(items).to.not.be.a.instanceof(Error);
chai.expect(items).to.not.be.a.instanceof(Error); // tslint:disable-next-line: no-unused-expression
// tslint:disable-next-line: no-unused-expression chai.expect(items).to.not.be.empty;
chai.expect(items).to.not.be.empty;
});
} }
@test @test
getCoreTestSamplesShouldFail() { async getCoreTestSamplesShouldFail() {
return getItemsFromSamples('./nonexistantdirectory') return getItemsFromSamples('./nonexistantdirectory')
.then(<T extends SCThing>(items: T[]) => { .then(<T extends SCThing>(items: T[]) => {
// tslint:disable-next-line: no-unused-expression // tslint:disable-next-line: no-unused-expression
@@ -72,11 +81,15 @@ export class E2EConnectorSpec {
} }
@test @test
async index() { async e2eRunSimulation() {
type responses = Response<SCBulkAddResponse | SCBulkDoneResponse | SCBulkResponse>; type responses = Response<SCBulkAddResponse | SCBulkDoneResponse | SCBulkResponse | SCSearchResponse>;
let failOnCompare = false;
let failOnLookup = false;
sandbox.on(httpClient, 'request', async (request: RequestOptions): Promise<RecursivePartial<responses>> => { sandbox.on(httpClient, 'request', async (request: RequestOptions): Promise<RecursivePartial<responses>> => {
if (request.url.toString() === 'http://localhost' + bulkRoute.getUrlFragment().toString()) { if (request.url.toString() === `http://localhost${bulkRoute.getUrlFragment().toString()}`) {
return { return {
body: { body: {
state: 'in progress', state: 'in progress',
@@ -84,21 +97,70 @@ export class E2EConnectorSpec {
}, },
statusCode: bulkRoute.statusCodeSuccess, statusCode: bulkRoute.statusCodeSuccess,
}; };
} else if (request.url.toString() === 'http://localhost' + bulkAddRoute.getUrlFragment({ }
UID: 'foo',
}).toString()) { if (request.url.toString() === `http://localhost${bulkAddRoute.getUrlFragment({UID: 'foo'}).toString()}`) {
storedThings.set(request.body.uid, clone(request.body));
return { return {
body: {}, body: {},
statusCode: bulkAddRoute.statusCodeSuccess, statusCode: bulkAddRoute.statusCodeSuccess,
}; };
} }
if (request.url.toString() === `http://localhost${bulkDoneRoute.getUrlFragment({UID: 'foo'}).toString()}`) {
return {
body: {},
statusCode: bulkDoneRoute.statusCodeSuccess,
};
}
if (request.url.toString() === `http://localhost${searchRoute.getUrlFragment().toString()}`) {
const thing = storedThings.get(request.body.filter.arguments.value);
if (failOnCompare) {
thing!.origin!.modified = 'altered';
}
const returnThing = failOnLookup ? [] : [thing];
const returnBody = {
data: returnThing,
facets: [],
pagination: {
count: returnThing.length,
offset: 0,
total: returnThing.length,
},
stats: {
time: 42,
},
};
return {
body: returnBody,
statusCode: searchRoute.statusCodeSuccess,
};
}
return { return {
body: {}, body: {},
statusCode: bulkDoneRoute.statusCodeSuccess, statusCode: searchRoute.statusCodeSuccess,
}; };
}); });
await indexSamples(httpClient, {to: 'http://localhost', samples: './node_modules/@openstapps/core/test/resources'});
// tslint:disable-next-line: max-line-length
await e2eRun(httpClient, {to: 'http://localhost', samplesLocation: './node_modules/@openstapps/core/test/resources'});
failOnLookup = true;
failOnCompare = false;
// tslint:disable-next-line: max-line-length
await e2eRun(httpClient, {to: 'http://localhost', samplesLocation: './node_modules/@openstapps/core/test/resources'})
.should.be.rejectedWith('Search for single SCThing with uid');
failOnLookup = false;
failOnCompare = true;
// tslint:disable-next-line: max-line-length
await e2eRun(httpClient, {to: 'http://localhost', samplesLocation: './node_modules/@openstapps/core/test/resources'})
.should.be.rejectedWith('Unexpected difference');
} }
@test @test
@@ -106,36 +168,40 @@ export class E2EConnectorSpec {
type responses = Response<SCBulkAddResponse | SCBulkDoneResponse | SCBulkResponse>; type responses = Response<SCBulkAddResponse | SCBulkDoneResponse | SCBulkResponse>;
sandbox.on(httpClient, 'request', async (): Promise<RecursivePartial<responses>> => { sandbox.on(httpClient, 'request', async (): Promise<RecursivePartial<responses>> => {
return { return {
body: {}, body: {},
statusCode: Number.MAX_SAFE_INTEGER, statusCode: Number.MAX_SAFE_INTEGER,
}; };
}); });
return indexSamples(httpClient, {to: 'http://localhost', samples: './node_modules/@openstapps/core/test/resources'})
// tslint:disable-next-line: max-line-length
return e2eRun(httpClient, {to: 'http://localhost', samplesLocation: './node_modules/@openstapps/core/test/resources'})
.should.be.rejectedWith(ApiError); .should.be.rejectedWith(ApiError);
} }
@test @test
async indexShouldFailDirectoryWithoutData() { async indexShouldFailDirectoryWithoutData() {
const emptyDirPath = join(__dirname, 'emptyDir'); const emptyDirPath = join(__dirname, 'emptyDir');
if(!existsSync(emptyDirPath)) if (!existsSync(emptyDirPath)) {
mkdirSync(emptyDirPath); mkdirSync(emptyDirPath);
await indexSamples(httpClient, {to: 'http://localhost', samples: emptyDirPath}) }
.should.be.rejectedWith('Could not index samples. None were retrived from the file system.'); await e2eRun(httpClient, {to: 'http://localhost', samplesLocation: emptyDirPath})
.should.be.rejectedWith('Could not index samples. None were retrieved from the file system.');
rmdirSync(emptyDirPath); rmdirSync(emptyDirPath);
} }
@test @test
async indexShouldFailDirectoryWithoutJsonData() { async indexShouldFailDirectoryWithoutJsonData() {
const somewhatFilledDirPath = join(__dirname, 'somewhatFilledDir'); const somewhatFilledDirPath = join(__dirname, 'somewhatFilledDir');
if(!existsSync(somewhatFilledDirPath)) if (!existsSync(somewhatFilledDirPath)) {
mkdirSync(somewhatFilledDirPath); mkdirSync(somewhatFilledDirPath);
}
const nonJsonFile = join (somewhatFilledDirPath, 'nonjson.txt'); const nonJsonFile = join (somewhatFilledDirPath, 'nonjson.txt');
createFileSync(nonJsonFile); createFileSync(nonJsonFile);
await indexSamples(httpClient, {to: 'http://localhost', samples: somewhatFilledDirPath}) await e2eRun(httpClient, {to: 'http://localhost', samplesLocation: somewhatFilledDirPath})
.should.be.rejectedWith('Could not index samples. None were retrived from the file system.'); .should.be.rejectedWith('Could not index samples. None were retrieved from the file system.');
unlinkSync(nonJsonFile); unlinkSync(nonJsonFile);
rmdirSync(somewhatFilledDirPath); rmdirSync(somewhatFilledDirPath);
} }
} }