Skip to content

Commit

Permalink
fix: refactor e2e chain tests (#687)
Browse files Browse the repository at this point in the history
* fix: refactor e2e chain tests

* lint
  • Loading branch information
TarikGul committed Oct 1, 2021
1 parent 9936df1 commit db18f02
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 113 deletions.
108 changes: 12 additions & 96 deletions scripts/runChainTests.ts
Original file line number Diff line number Diff line change
@@ -1,68 +1,11 @@
import { ArgumentParser, Namespace } from 'argparse';
import { ChildProcessWithoutNullStreams, spawn } from 'child_process';

import { config, defaultSasBuildOpts } from './config';
import { IProcOpts, StatusCode } from './types';
import { killAll, launchProcess, setWsUrl } from './sidecarScriptApi';
import { ProcsType, StatusCode } from './types';

// Stores all the processes
const procs: { [key: string]: ChildProcessWithoutNullStreams } = {};

// Set the env variable for SAS_SUBSTRATE_WS_URL
const setWsUrl = (url: string): void => {
process.env.SAS_SUBSTRATE_WS_URL = url;
};

/**
* Launch any given process. It accepts an options object.
*
* {
* proc => the name of the process to be saved in our cache
* resolver => If the stdout contains the resolver it will resolve the process
* resolverStartupErr => If the stderr contains the resolver it will resolve the process
* args => an array of args to be attached to the `yarn` command.
* }
*
* @param IProcOpts
*/
const launchProcess = async ({
proc,
resolver,
resolverStartupErr,
args,
}: IProcOpts): Promise<StatusCode> => {
return new Promise<StatusCode>((resolve, reject) => {
const { Success, Failed } = StatusCode;

const command = 'yarn';

procs[proc] = spawn(command, args, { detached: true });

procs[proc].stdout.on('data', (data: Buffer) => {
console.log(data.toString());

if (data.toString().includes(resolver)) {
resolve(Success);
}
});

procs[proc].stderr.on('data', (data: Buffer) => {
console.error(data.toString());

if (resolverStartupErr && data.toString().includes(resolverStartupErr)) {
resolve(Failed);
}
});

procs[proc].on('close', () => {
resolve(Success);
});

procs[proc].on('error', (err) => {
console.log(err);
reject(Failed);
});
});
};
const procs: ProcsType = {};

/**
* Launches Sidecar, and if successful it will launch the jest runner. This operation
Expand All @@ -78,54 +21,27 @@ const launchChainTest = async (chain: string): Promise<boolean> => {
setWsUrl(wsUrl);

console.log('Launching Sidecar...');
const sidecarStart = await launchProcess(SasStartOpts);
const sidecarStart = await launchProcess('yarn', procs, SasStartOpts);

if (sidecarStart === Success) {
// Sidecar successfully launched, and jest will now get called
console.log('Launching jest...');
const jest = await launchProcess(JestProcOpts);
const jest = await launchProcess('yarn', procs, JestProcOpts);

if (jest === Success) {
killAll();
killAll(procs);
return true;
} else {
killAll();
killAll(procs);
return false;
}
} else {
console.error('Error launching sidecar... exiting...');
killAll();
killAll(procs);
process.exit(1);
}
};

// Kill all processes spawned and tracked by this file.
const killAll = () => {
console.log('Killing all processes...');
for (const key of Object.keys(procs)) {
if (!procs[key].killed) {
try {
console.log(`Killing ${key}`);
// Kill child and all its descendants.
process.kill(-procs[key].pid, 'SIGTERM');
process.kill(-procs[key].pid, 'SIGKILL');
} catch (e) {
/**
* The error we are catching here silently, is when `-procs[key].pid` takes
* the range of all pid's inside of the subprocess group created with
* `spawn`, and one of the process's is either already closed or doesn't exist anymore.
*
* ex: `Error: kill ESRCH`
*
* This is a very specific use case of an empty catch block and is used
* outside of the scope of the API therefore justifiable, and should be used cautiously
* elsewhere.
*/
}
}
}
};

const checkTests = (...args: boolean[]) => {
const testStatus = args.every((test) => test);

Expand All @@ -143,13 +59,13 @@ const main = async (args: Namespace): Promise<void> => {

// Build sidecar
console.log('Building Sidecar...');
const sidecarBuild = await launchProcess(defaultSasBuildOpts);
const sidecarBuild = await launchProcess('yarn', procs, defaultSasBuildOpts);

// When sidecar fails to build, we kill all process's and exit
if (sidecarBuild === Failed) {
console.error('Sidecar failed to build, exiting...');
// Kill all processes
killAll();
killAll(procs);
// Exit program
process.exit();
}
Expand Down Expand Up @@ -188,7 +104,7 @@ const args = parser.parse_args() as Namespace;
*/
process.on('SIGINT', function () {
console.log('Caught interrupt signal');
killAll();
killAll(procs);
process.exit();
});

Expand All @@ -197,7 +113,7 @@ process.on('SIGINT', function () {
*/
process.on('SIGHUP', function () {
console.log('Caught terminal termination');
killAll();
killAll(procs);
process.exit();
});

Expand Down
38 changes: 25 additions & 13 deletions scripts/runYarnPack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,17 @@ const cleanup = async () => {
resolver: 'YN0000: Done',
args: ['remove', '@substrate/api-sidecar'],
};
const sidecarUnInstallPack = await launchProcess('yarn', procs, sasUnInstallPackOpts);
const sidecarUnInstallPack = await launchProcess(
'yarn',
procs,
sasUnInstallPackOpts
);

if (sidecarUnInstallPack === Failed) {
console.error('UnInstalling sidecar package failed..');
console.error('Please uninstall the package using `yarn remove @substrate/api-sidecar`.');
console.error(
'Please uninstall the package using `yarn remove @substrate/api-sidecar`.'
);
killAll(procs);
}

Expand All @@ -36,11 +42,13 @@ const cleanup = async () => {
resolver: '',
args: ['-rf', `${__dirname}/../../package.tgz`],
};
const deleteTarball = await launchProcess('rm', procs, sasDeleteTarballOpts);
const deleteTarball = await launchProcess('rm', procs, sasDeleteTarballOpts);

if (deleteTarball === Failed) {
console.error('Error deleting tarball.');
console.error('In order to delete tarball run: `rm -rf ./package.tgz` from the root directory of the repository.');
console.error(
'In order to delete tarball run: `rm -rf ./package.tgz` from the root directory of the repository.'
);
killAll(procs);
}
};
Expand Down Expand Up @@ -85,7 +93,11 @@ const main = async () => {
resolver: 'YN0000: Done',
args: ['add', `${__dirname}/../../package.tgz`],
};
const sidecarInstallPack = await launchProcess('yarn', procs, sasInstallPackOpts);
const sidecarInstallPack = await launchProcess(
'yarn',
procs,
sasInstallPackOpts
);

if (sidecarInstallPack === Failed) {
console.error('Installing the binary failed..');
Expand All @@ -96,7 +108,7 @@ const main = async () => {
/**
* Start sidecar and see if it works
*/
setWsUrl('wss://kusama-rpc.polkadot.io');
setWsUrl('wss://kusama-rpc.polkadot.io');
console.log('Initializing Sidecar');
const sasStartPackOpts = {
proc: 'sidecar',
Expand All @@ -107,7 +119,7 @@ const main = async () => {
const sidecarStart = await launchProcess(
`${__dirname}/../../node_modules/.bin/substrate-api-sidecar`,
procs,
sasStartPackOpts
sasStartPackOpts
);

if (sidecarStart === Success) {
Expand All @@ -127,18 +139,18 @@ const main = async () => {
* Signal interrupt
*/
process.on('SIGINT', function () {
console.log('Caught interrupt signal');
killAll(procs);
process.exit();
console.log('Caught interrupt signal');
killAll(procs);
process.exit();
});

/**
* Signal hangup terminal
*/
process.on('SIGHUP', function () {
console.log('Caught terminal termination');
killAll(procs);
process.exit();
console.log('Caught terminal termination');
killAll(procs);
process.exit();
});

main().finally(() => process.exit());
8 changes: 4 additions & 4 deletions scripts/sidecarScriptApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import { IProcOpts, ProcsType, StatusCode } from './types';

/**
* Sets the url that sidecar will use in the env
*
*
* @param url ws url used in sidecar
*/
export const setWsUrl = (url: string): void => {
process.env.SAS_SUBSTRATE_WS_URL = url;
process.env.SAS_SUBSTRATE_WS_URL = url;
};

/**
Expand Down Expand Up @@ -46,7 +46,7 @@ export const killAll = (procs: ProcsType): void => {
* Launch any given process. It accepts an options object.
*
* @param cmd Optional Command will default to 'yarn'
* @param procs Object of saved processes
* @param procs Object of saved processes
* @param IProcOpts
* {
* proc => the name of the process to be saved in our cache
Expand All @@ -58,7 +58,7 @@ export const killAll = (procs: ProcsType): void => {
export const launchProcess = (
cmd: string,
procs: ProcsType,
{ proc, resolver, resolverStartupErr, args }: IProcOpts,
{ proc, resolver, resolverStartupErr, args }: IProcOpts
): Promise<StatusCode> => {
return new Promise<StatusCode>((resolve, reject) => {
const { Success, Failed } = StatusCode;
Expand Down

0 comments on commit db18f02

Please sign in to comment.