cloudron-e2e-test/e2etestrunner.js

106 lines
3.5 KiB
JavaScript
Executable File

/* jslint node:true */
'use strict';
require('supererror')({ splatchError: true });
var debug = require('debug')('e2e:runner'),
mailer = require('./mailer.js'),
mkdirp = require('mkdirp'),
path = require('path'),
rimraf = require('rimraf'),
semver = require('semver'),
shell = require('./shell.js'),
superagent = require('superagent'),
terminate = require('terminate'),
util = require('util'),
_ = require('underscore');
// override debug.log to print only as console.log
debug.log = console.log.bind(console);
var gLatestVersionInfo = null; // do not use etag since it only hashes body. do not use s3 filestamp since the upgrade test modifies it....
var gNpmTest = null;
function cleanExit() {
if (gNpmTest) {
debug('Terminating child test process %s', gNpmTest.pid);
terminate(gNpmTest.pid, process.exit);
gNpmTest = null;
} else {
debug('No child test process to terminate');
process.exit();
}
}
function start() {
setTimeout(runTestsIfNeeded.bind(null, start), 60 * 1000);
}
function runTestsIfNeeded(callback) {
debug('Getting latest box version');
getLatestBoxVersion(function (error, latestVersionInfo) {
if (error) return callback(error);
if (_.isEqual(gLatestVersionInfo, latestVersionInfo)) {
debug('Box version has not changed');
return callback();
}
debug('Box version has changed. latestVersionInfo:%j', latestVersionInfo);
var diff = (new Date() - new Date(latestVersionInfo.date));
if (diff < 5 * 60 * 1000) { // give appstore 5 mins
debug('Waiting for appstore gets the new release. diff=%s', diff);
return callback();
}
runTests(latestVersionInfo, function (error) {
debug('Finished running tests for %j %s', latestVersionInfo, error);
gLatestVersionInfo = latestVersionInfo;
callback(error);
});
});
}
function getLatestBoxVersion(callback) {
superagent.get(process.env.BOX_VERSIONS_URL).end(function (error, res) {
if (error || res.statusCode !== 200 || !res.body) {
debug('Error downloading versions file', error || res.statusCode);
return callback(new Error('Error downloading versions file'));
}
var latestVersionInfo = Object.keys(res.body).sort(semver.rcompare)[0];
var result = _.extend({ }, res.body[latestVersionInfo]);
result.version = latestVersionInfo;
callback(null, result);
});
}
function runTests(latestVersionInfo, callback) {
debug('Running tests for %j', latestVersionInfo);
gNpmTest = shell.system('e2etestrunner', 'BOX_VERSION=' + latestVersionInfo.version + ' npm run-script parallel_test', function (error, stdout, stderr) {
gNpmTest = null;
debug('Final test result', error);
var topic = util.format('E2E Test %s for box version: %s', error ? 'failed': 'passed', latestVersionInfo.version);
// do not send stderr in mail since it contains the tail log stream
mailer.sendEndToEndTestResult(topic, JSON.stringify(latestVersionInfo, null, 4), stdout ? stdout.toString('utf8') : '', '', path.resolve(path.join(__dirname, 'logs')), function (error) { if (error) console.log(error); });
return callback(error);
});
}
debug('e2etest started.');
process.on('exit', cleanExit);
process.on('SIGINT', cleanExit); // catch ctrl-c
process.on('SIGTERM', cleanExit); // catch kill
process.on('uncaughtException', cleanExit);
start();