cloudron-e2e-test/e2etestrunner.js

104 lines
3.3 KiB
JavaScript
Raw Normal View History

2015-07-23 19:02:16 +00:00
/* jslint node:true */
'use strict';
require('supererror')({ splatchError: true });
2015-07-24 00:03:26 +00:00
var debug = require('debug')('e2e:runner'),
2015-07-23 19:02:16 +00:00
shell = require('./shell.js'),
semver = require('semver'),
mailer = require('./mailer.js'),
superagent = require('superagent'),
terminate = require('terminate'),
2015-07-23 19:02:16 +00:00
util = require('util');
// override debug.log to print only as console.log
debug.log = console.log.bind(console);
2015-07-24 00:03:26 +00:00
var gLastModified = null; // do not use etag since it only hashes body. touching file to rerun tests is nice
2015-07-23 19:02:16 +00:00
var gLatestBoxVersion = null; // for sending mail
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();
}
}
2015-07-23 19:02:16 +00:00
function start() {
setTimeout(runTestsIfNeeded.bind(null, start), 60 * 1000);
2015-07-23 19:02:16 +00:00
}
function runTestsIfNeeded(callback) {
debug('Getting latest box version');
getLatestBoxVersion(function (error, latestETag, latestBoxVersion, lastModified) {
2015-07-23 19:02:16 +00:00
if (error) return callback(error);
2015-07-24 00:03:26 +00:00
if (lastModified === gLastModified) {
debug('Box version has not changed. etag:%s lm:%s', latestETag, lastModified);
return callback();
2015-07-23 19:02:16 +00:00
}
debug('Box version has changed. etag %s lm:%s', latestETag, lastModified);
var diff = (new Date() - lastModified);
if (diff < 3 * 60 * 1000) { // give appstore 3 mins
debug('Waiting for appstore gets the new release. diff=%s', diff);
return callback();
}
2015-07-23 19:02:16 +00:00
runTests(latestETag, latestBoxVersion, function (error) {
2015-07-24 00:03:26 +00:00
debug('Finished running tests for etag %s / lm %s: %s', latestETag, lastModified, error);
gLastModified = lastModified;
2015-07-23 19:02:16 +00:00
gLatestBoxVersion = latestBoxVersion;
callback(error);
});
});
}
function getLatestBoxVersion(callback) {
superagent.get(process.env.BOX_VERSIONS_URL).end(function (error, res) {
if (error || res.statusCode !== 200 || !res.body) {
2015-07-23 19:45:26 +00:00
debug('Error downloading versions file', error || res.statusCode);
2015-07-23 19:02:16 +00:00
return callback(new Error('Error downloading versions file'));
}
var latestVersion = Object.keys(res.body).sort(semver.rcompare)[0];
var latestETag = res.headers['etag'];
var lastModified = new Date(res.headers['last-modified']);
2015-07-23 19:02:16 +00:00
debug('%j:', res.headers);
callback(null, latestETag, latestVersion, lastModified);
2015-07-23 19:02:16 +00:00
});
}
function runTests(latestETag, latestBoxVersion, callback) {
var topic = util.format('box version: %s etag: %s', latestBoxVersion, latestETag);
debug('Running tests for %s', topic);
gNpmTest = shell.system('e2etestrunner', 'npm test', function (error, stdout, stderr) {
gNpmTest = null;
2015-07-23 19:56:35 +00:00
debug('Final test result', error);
2015-07-23 19:02:16 +00:00
2015-07-23 19:56:35 +00:00
mailer.sendEndToEndTestResult(topic, stdout ? stdout.toString('utf8') : '', stderr ? stderr.toString('utf8') : '', function () { });
return callback(error);
2015-07-23 19:02:16 +00:00
});
}
debug('e2etest started.');
process.on('exit', cleanExit);
process.on('SIGINT', cleanExit); // catch ctrl-c
process.on('SIGTERM', cleanExit); // catch kill
process.on('uncaughtException', cleanExit);
2015-07-23 19:02:16 +00:00
start();