kibana/x-pack/plugins/security_solution/scripts/loop_cypress_tests.js
MadameSheema ea360ecd78
[Security Solution] Adds "Creates timeline" Cypress test (#76836) (#77871)
* adds "Creates timeline" test

* deletes timeline events spec

* completes assertions

* comments assertion

* fixes typecheck error

* waits for all the changes in the timeline to be performed before creating a new timeline and closing the toggle

* fixes failing problem

* fixes loop script

* makes test realiable on visual mode

* fixes merge issue

* makes test more reliable

* fixes typecheck issue

* fixes typecheck

* opens timeline from timeline settings

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
2020-09-21 08:51:07 +02:00

85 lines
2.8 KiB
JavaScript

/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
const fs = require('fs');
const os = require('os');
const process = require('process');
const spawn = require('child_process').spawn;
/* eslint-disable no-process-exit */
const MUST_RUN_FROM_DIR = 'kibana';
const OUTPUT_DIR = 'target';
const OUTPUT_FILE = `${OUTPUT_DIR}/loop-cypress-tests.txt`;
const createOutputDir = () => {
fs.mkdir(OUTPUT_DIR, { recursive: true }, (err) => {
if (err) throw err;
});
};
const showUsage = () => {
const scriptName = process.argv[1].slice(process.argv[1].lastIndexOf('/') + 1);
console.log(`\nUsage: ${scriptName} <times-to-run>`, `\nExample: ${scriptName} 5`);
};
const exitIfIncorrectWorkingDir = () => {
if (!process.cwd().endsWith(`/${MUST_RUN_FROM_DIR}`)) {
console.error(
`\nERROR: This script must be run from the '${MUST_RUN_FROM_DIR}' directory, but it was ran from '${process.cwd()}' instead.`
);
showUsage();
process.exit(1);
}
};
const exitIfTimesToRunIsInvalid = (timesToRun) => {
if (!timesToRun > 0) {
console.error(
'\nERROR: You must specify a valid number of times to run the Security Solution Cypress tests.'
);
showUsage();
process.exit(1);
}
};
const spawnChild = async () => {
const child = spawn('node', [
'scripts/functional_tests',
'--config',
'x-pack/test/security_solution_cypress/cli_config.ts',
]);
for await (const chunk of child.stdout) {
console.log(chunk.toString());
fs.appendFileSync(OUTPUT_FILE, chunk.toString());
}
for await (const chunk of child.stderr) {
console.log(chunk.toString());
fs.appendFileSync(OUTPUT_FILE, chunk.toString());
}
const exitCode = await new Promise((resolve) => {
child.on('close', resolve);
});
return exitCode;
};
const runNTimes = async (timesToRun) => {
for (let i = 0; i < timesToRun; i++) {
const startingRun = `\n\n*** Starting test run ${
i + 1
} of ${timesToRun} on host ${os.hostname()} at ${new Date()} ***\n\n`;
console.log(startingRun);
fs.appendFileSync(OUTPUT_FILE, startingRun);
const exitCode = await spawnChild();
const testRunCompleted = `\n\n*** Test run ${
i + 1
} of ${timesToRun} on host ${os.hostname()} exited with code ${exitCode} at ${new Date()} ***`;
console.log(testRunCompleted);
fs.appendFileSync(OUTPUT_FILE, testRunCompleted);
}
};
const timesToRun = Number(process.argv[2]) || 0;
exitIfIncorrectWorkingDir();
exitIfTimesToRunIsInvalid(timesToRun);
console.log(`\nCypress tests will be run ${timesToRun} times`);
console.log(`\nTest output will be appended to '${OUTPUT_FILE}'`);
createOutputDir();
runNTimes(timesToRun);