cloudron-e2e-test/e2etestrunner.js
2015-07-23 17:03:26 -07:00

86 lines
2.8 KiB
JavaScript
Executable File

/* jslint node:true */
'use strict';
require('supererror')({ splatchError: true });
var debug = require('debug')('e2e:runner'),
shell = require('./shell.js'),
semver = require('semver'),
mailer = require('./mailer.js'),
superagent = require('superagent'),
util = require('util');
// override debug.log to print only as console.log
debug.log = console.log.bind(console);
var gLastModified = null; // do not use etag since it only hashes body. touching file to rerun tests is nice
var gLatestBoxVersion = null; // for sending mail
function start() {
runTestsIfNeeded(function () {
setTimeout(start, 60 * 1000);
});
}
function runTestsIfNeeded(callback) {
debug('Getting latest box version');
getLatestBoxVersion(function (error, latestETag, latestBoxVersion, lastModified) {
if (error) return callback(error);
if (lastModified === gLastModified) {
debug('Box version has not changed. etag:%s lm:%s', latestETag, lastModified);
return callback();
}
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();
}
runTests(latestETag, latestBoxVersion, function (error) {
debug('Finished running tests for etag %s / lm %s: %s', latestETag, lastModified, error);
gLastModified = lastModified;
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) {
debug('Error downloading versions file', error || res.statusCode);
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']);
debug('%j:', res.headers);
callback(null, latestETag, latestVersion, lastModified);
});
}
function runTests(latestETag, latestBoxVersion, callback) {
var topic = util.format('box version: %s etag: %s', latestBoxVersion, latestETag);
debug('Running tests for %s', topic);
shell.system('e2etestrunner', 'npm test', function (error, stdout, stderr) {
debug('Final test result', error);
mailer.sendEndToEndTestResult(topic, stdout ? stdout.toString('utf8') : '', stderr ? stderr.toString('utf8') : '', function () { });
return callback(error);
});
}
debug('e2etest started.');
start();