cloudron-e2e-test/cloudron.js

442 lines
17 KiB
JavaScript
Raw Normal View History

2015-06-14 04:33:42 +00:00
#!/usr/bin/env node
'use strict';
var assert = require('assert'),
2015-09-16 19:31:00 +00:00
common = require('./common.js'),
2015-06-14 04:33:42 +00:00
debug = require('debug')('e2e:cloudron'),
dns = require('dns'),
2015-06-14 04:33:42 +00:00
querystring = require('querystring'),
request = require('superagent-sync'),
semver = require('semver'),
2015-06-14 04:33:42 +00:00
sleep = require('sleep').sleep,
2016-04-06 01:33:11 +00:00
url = require('url'),
util = require('util');
2015-06-14 04:33:42 +00:00
exports = module.exports = Cloudron;
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
function Cloudron(box) {
this._box = box;
2015-12-18 04:58:50 +00:00
this._isCustomDomain = box.domain === process.env.CUSTOM_DOMAIN;
2016-03-28 18:25:15 +00:00
this._adminFqdn = this._isCustomDomain ? 'my.' + box.domain : 'my-' + box.domain;
2015-12-18 04:58:50 +00:00
this._origin = this._isCustomDomain ? 'https://my.' + box.domain : 'https://my-' + box.domain;
2015-06-14 04:33:42 +00:00
this._credentials = {
password: null,
accessToken: null
};
}
2015-12-18 04:58:50 +00:00
Cloudron.prototype.appFqdn = function (location) {
return location + (this._isCustomDomain ? '.' : '-') + this._box.domain;
};
2015-06-14 04:33:42 +00:00
// get oauth token for logged in as certain user { username, password, email }
Cloudron.prototype.getOauthToken = function (user) {
var username = user.username;
var password = user.password;
////////// try to authorize without a session
var res = request.get(this._origin + '/api/v1/oauth/dialog/authorize').query({ redirect_uri: 'https://self', client_id: 'cid-webadmin', response_type: 'token', scope: 'root,profile,apps,roleAdmin' }).end();
var sessionCookies = res.headers['set-cookie']; // always an array
///////// should get redirected to login form with a script tag (to workaround chrome issue with redirects+cookies)
var redirectUrl = res.text.match(/window.location.href = "(.*)"/);
if (!redirectUrl) {
2015-07-29 16:16:10 +00:00
debug('Could not determine redirected url', res.text, res.headers);
assert(false);
}
var urlp = url.parse(redirectUrl[1]);
2015-06-14 04:33:42 +00:00
////////// get the login form (api/v1/session/login)
res = request.get(this._origin + urlp.pathname).set('cookie', sessionCookies[0]).query(urlp.query).end();
2016-02-26 05:27:34 +00:00
var csrfs = res.text.match(/name="_csrf" value="(.*)"/);
if (!csrfs) {
debug('Could not determine csrf', res.text, res.headers);
assert(false);
}
var csrf = csrfs[1];
2015-06-14 04:33:42 +00:00
sessionCookies = res.headers['set-cookie']; // always an array
2015-06-15 06:11:07 +00:00
assert.notStrictEqual(sessionCookies.length, 0);
2015-06-14 04:33:42 +00:00
////////// submit the login form with credentials
res = request.post(this._origin + urlp.pathname).set('cookie', sessionCookies[0]).send({ _csrf: csrf, username: username, password: password }).redirects(0).end();
if (res.statusCode !== 302) {
debug('Failed to submit the login for.', res.statusCode, res.text);
assert(false);
}
2015-06-14 04:33:42 +00:00
sessionCookies = res.headers['set-cookie']; // always an array
2015-06-15 06:11:07 +00:00
assert.notStrictEqual(sessionCookies.length, 0);
2015-06-14 04:33:42 +00:00
////////// authorize now with cookies
res = request.get(this._origin + '/api/v1/oauth/dialog/authorize').set('cookie', sessionCookies[0]).query({ redirect_uri: 'https://self', client_id: 'cid-webadmin', response_type: 'token', scope: 'root,profile,apps,roleAdmin' }).redirects(0).end();
2016-01-27 17:21:41 +00:00
common.verifyResponse(res, 'Unable to authorize');
2015-06-15 06:11:07 +00:00
assert.strictEqual(res.statusCode, 302);
2015-06-14 04:33:42 +00:00
sessionCookies = res.headers['set-cookie']; // always an array
2015-06-15 06:11:07 +00:00
assert.notStrictEqual(sessionCookies.length, 0);
2015-06-14 04:33:42 +00:00
////////// success will get redirect to callback?redirectURI=xx#access_token=yy&token_type=Bearer' (content is a <script>)
urlp = url.parse(res.headers.location);
res = request.get(this._origin + urlp.pathname).set('cookie', sessionCookies[0]).query(urlp.query).redirects(0).end();
2015-06-15 06:11:07 +00:00
assert.strictEqual(res.statusCode, 200);
2015-06-14 04:33:42 +00:00
////////// simulate what the the script of callback call does
var accessToken = querystring.parse(urlp.hash.substr(1)).access_token;
return accessToken;
2015-09-29 01:43:51 +00:00
};
2015-06-14 04:33:42 +00:00
// activate the box
Cloudron.prototype.activate = function (user) {
var setupToken = this._box.setupToken;
////////// activation
var res;
for (var i = 0; i < 40; ++i) {
sleep(5);
res = request.post(this._origin + '/api/v1/cloudron/activate').query({ setupToken: setupToken }).send(user).end();
if (res.statusCode === 201) break;
if (res.statusCode === 307) continue;
if (res.statusCode === 409) {
debug('Response error statusCode:%s error:%s body:%j', res.statusCode, res.error, res.body);
throw new Error('Cloudron already activated! This should not happen');
}
}
// final verification will fail if retries do not succeed
common.verifyResponse2xx(res, 'Could not activate the box');
2015-06-14 04:33:42 +00:00
res = request.get(this._origin + '/api/v1/cloudron/status').end();
2016-01-27 17:21:41 +00:00
common.verifyResponse2xx(res, 'Could not get Cloudron status');
2015-06-14 04:33:42 +00:00
assert.strictEqual(res.body.version, this._box.version);
2015-09-29 01:43:51 +00:00
};
2015-06-14 04:33:42 +00:00
Cloudron.prototype.waitForApp = function (appId) {
// wait for app to come up
process.stdout.write('Waiting for app to come up.');
2015-08-31 04:48:33 +00:00
var res;
2015-06-20 19:50:28 +00:00
for (var i = 0; i < 60; i++) {
2015-06-14 04:33:42 +00:00
sleep(10);
process.stdout.write('.');
2015-08-31 04:48:33 +00:00
res = request.get(this._origin + '/api/v1/apps/'+ appId).query({ access_token: this._credentials.accessToken }).end();
2016-01-27 17:21:41 +00:00
common.verifyResponse2xx(res, 'Could not query app status');
2015-06-14 04:33:42 +00:00
2015-07-24 05:48:08 +00:00
if (res.body.installationState === 'installed' && res.body.runState === 'running' && res.body.health === 'healthy') {
2015-06-14 04:33:42 +00:00
console.log();
break;
}
}
2015-06-16 05:28:48 +00:00
assert.strictEqual(res.body.installationState, 'installed');
assert.strictEqual(res.body.runState, 'running');
2016-01-26 20:33:53 +00:00
assert.strictEqual(res.body.health, 'healthy');
2015-12-17 21:58:49 +00:00
return res.body;
2015-06-14 04:33:42 +00:00
};
2015-08-31 04:48:33 +00:00
Cloudron.prototype.waitForBox = function () {
process.stdout.write('Waiting for box.');
var res;
for (var i = 0; i < 40; i++) {
sleep(10);
res = request.get(this._origin + '/api/v1/cloudron/status').end();
2016-01-26 23:04:59 +00:00
if (res.statusCode === 200) {
2015-08-31 04:48:33 +00:00
console.log();
2016-01-26 23:04:59 +00:00
return;
2015-08-31 04:48:33 +00:00
}
process.stdout.write('.');
}
2016-01-26 23:04:59 +00:00
2016-01-27 02:30:02 +00:00
throw new Error('waitForBox failed');
2015-08-31 04:48:33 +00:00
};
2015-06-14 04:33:42 +00:00
Cloudron.prototype.setCredentials = function (password, accessToken) {
this._credentials = {
password: password,
accessToken: accessToken
};
};
Cloudron.prototype.installApp = function (location, manifest, cloudronVersion) {
cloudronVersion = cloudronVersion || '0.0.74';
2015-06-14 04:33:42 +00:00
var res = request.post(this._origin + '/api/v1/apps/install')
.query({ access_token: this._credentials.accessToken })
.send({ manifest: manifest, appStoreId: '', location: location, accessRestriction: semver.lte(cloudronVersion, '0.0.73') ? '' : null, oauthProxy: false })
2015-06-14 04:33:42 +00:00
.end();
2016-01-27 17:21:41 +00:00
common.verifyResponse2xx(res, 'Cannot install app');
2015-06-14 04:33:42 +00:00
debug('App installed at %s'.green, location);
var appId = res.body.id;
2015-12-17 21:58:49 +00:00
var app = this.waitForApp(appId);
2015-06-14 04:33:42 +00:00
debug('App is running'.green);
2015-12-17 21:58:49 +00:00
res = request.get('https://' + app.fqdn).end();
2016-01-27 17:21:41 +00:00
common.verifyResponse2xx(res, 'App is unreachable');
2015-06-14 04:33:42 +00:00
console.log('App is reachable'.green);
return appId;
};
Cloudron.prototype.configureApp = function (appId, newLocation) {
var res = request.post(this._origin + '/api/v1/apps/' + appId + '/configure').query({ access_token: this._credentials.accessToken }).send({ location: newLocation, accessRestriction: null, oauthProxy: false, password: this._credentials.password }).end();
2016-01-27 17:21:41 +00:00
common.verifyResponse2xx(res, 'App could not be configured');
2015-06-14 04:33:42 +00:00
console.log('App moved to different location'.green);
2015-12-17 21:58:49 +00:00
var app = this.waitForApp(appId);
2015-06-14 04:33:42 +00:00
2015-12-17 21:58:49 +00:00
res = request.get('https://' + app.fqdn).end();
2016-01-27 17:21:41 +00:00
common.verifyResponse2xx(res, 'App is unreachable');
2015-06-14 04:33:42 +00:00
console.log('App is reachable'.green);
};
Cloudron.prototype.uninstallApp = function (appId) {
process.stdout.write('Uninstalling app');
var res = request.post(this._origin + '/api/v1/apps/' + appId + '/uninstall').query({ access_token: this._credentials.accessToken }).send({ password: this._credentials.password }).end();
2016-01-27 17:21:41 +00:00
common.verifyResponse2xx(res, 'Cannot uninstall app');
2015-06-14 04:33:42 +00:00
2016-01-26 23:04:59 +00:00
for (var i = 0; i < 40; i++) {
2015-06-14 04:33:42 +00:00
sleep(10);
process.stdout.write('.');
res = request.get(this._origin + '/api/v1/apps/'+ appId).query({ access_token: this._credentials.accessToken }).retry(0).end();
if (res.statusCode === 404) {
console.log();
2016-01-26 23:04:59 +00:00
debug('App is uninstalled'.green);
return;
2015-06-14 04:33:42 +00:00
}
}
2016-01-26 23:04:59 +00:00
assert(false, 'uninstallApp failed');
};
2015-06-14 04:33:42 +00:00
2016-04-16 02:28:33 +00:00
Cloudron.prototype.updateApp = function (appId, newManifest) {
process.stdout.write('Trying to update');
var res;
var res = request.post(this._origin + '/api/v1/apps/' + appId + '/update').query({ access_token: this._credentials.accessToken }).send({ password: this._credentials.password, manifest: newManifest }).end();
common.verifyResponse2xx(res, 'Could not update');
console.log('Update started'.green);
var app = this.waitForApp(appId);
debug('App is running'.green);
res = request.get('https://' + app.fqdn).end();
common.verifyResponse2xx(res, 'App is unreachable');
console.log('App updated'.green);
};
2015-06-14 04:33:42 +00:00
Cloudron.prototype.update = function (toVersion) {
2015-07-16 22:05:06 +00:00
process.stdout.write('Trying to update');
2015-08-31 04:48:33 +00:00
var res;
2016-01-26 23:04:59 +00:00
for (var i = 0; i < 40; i++) {
2015-07-16 22:05:06 +00:00
sleep(10);
process.stdout.write('.');
2015-08-31 04:48:33 +00:00
res = request.post(this._origin + '/api/v1/cloudron/update').query({ access_token: this._credentials.accessToken }).send({ password: this._credentials.password }).end();
2015-07-16 22:05:06 +00:00
if (res.statusCode === 422) continue; // box has not seen the update yet
if (res.statusCode === 409) break; // update is in progress, lock was acquired
2016-01-27 17:21:41 +00:00
common.verifyResponse2xx(res, 'Could not update');
2015-07-16 22:05:06 +00:00
break;
}
2015-06-14 04:33:42 +00:00
2015-07-16 22:05:06 +00:00
console.log('Update started'.green);
2015-06-14 04:33:42 +00:00
process.stdout.write('Waiting for update.');
2016-01-27 02:30:02 +00:00
for (i = 0; i < 40; i++) {
2015-06-14 04:33:42 +00:00
sleep(10);
res = request.get(this._origin + '/api/v1/cloudron/status').end();
if (res.statusCode === 200 && res.body.version === toVersion) {
console.log();
break;
}
process.stdout.write('.');
}
assert.strictEqual(res.body.version, toVersion);
assert.strictEqual(res.body.activated, true);
console.log('Updated successfully'.green);
2015-08-31 04:48:33 +00:00
};
2015-06-14 04:33:42 +00:00
Cloudron.prototype.addUser = function (username, email) {
2016-01-18 19:47:26 +00:00
var res = request.post(this._origin + '/api/v1/users').query({ access_token: this._credentials.accessToken }).send({ username: username, email: email, invite: false }).end();
2016-01-27 17:21:41 +00:00
common.verifyResponse2xx(res, 'Could not add user');
2016-04-06 19:27:11 +00:00
return res.body;
};
Cloudron.prototype.resetPassword = function (resetToken, password) {
var res = request.get(this._origin + '/api/v1/session/password/reset.html').query({ reset_token: resetToken }).end();
2016-01-27 17:21:41 +00:00
common.verifyResponse2xx(res, 'Could not get password setup site');
2015-06-15 06:11:07 +00:00
var sessionCookies = res.headers['set-cookie']; // always an array
var csrf = res.text.match(/name="_csrf" value="(.*)"/)[1];
2015-06-15 06:11:07 +00:00
res = request.post(this._origin + '/api/v1/session/password/reset')
.set('cookie', sessionCookies[0])
.type('form').send({ _csrf: csrf, resetToken: resetToken, password: password, passwordRepeat: password }).end();
2016-01-27 17:44:45 +00:00
common.verifyResponse(res, 'Could not setup password for user');
assert.strictEqual(res.statusCode, 302);
};
2015-06-16 19:00:46 +00:00
Cloudron.prototype.backup = function () {
var res = request.get(this._origin + '/api/v1/backups').query({ access_token: this._credentials.accessToken }).end();
2016-01-27 17:21:41 +00:00
common.verifyResponse2xx(res, 'Could not get backups');
2015-06-16 19:00:46 +00:00
var existingBackups = res.body.backups;
2015-06-17 15:06:01 +00:00
res = request.post(this._origin + '/api/v1/backups').query({ access_token: this._credentials.accessToken }).end();
2016-01-27 17:21:41 +00:00
common.verifyResponse2xx(res, 'Could not schedule backup');
2015-06-16 19:00:46 +00:00
2016-01-27 02:30:02 +00:00
for (var i = 0; i < 40; i++) {
2015-07-10 03:27:30 +00:00
sleep(10); // backup sometimes takes a while to start
2015-06-16 19:00:46 +00:00
res = request.get(this._origin + '/api/v1/cloudron/progress').end();
if (res.body.backup === null || res.body.backup.percent === 100) {
debug('backup done');
break;
}
debug('Backing up: %s %s', res.body.backup.percent, res.body.backup.message);
}
2016-01-27 02:30:02 +00:00
if (i === 40) throw new Error('backup: timedout');
2015-06-16 19:00:46 +00:00
res = request.get(this._origin + '/api/v1/backups').query({ access_token: this._credentials.accessToken }).end();
2016-01-27 17:21:41 +00:00
common.verifyResponse2xx(res, 'Could not get backups');
2015-06-16 19:00:46 +00:00
var latestBackups = res.body.backups;
assert.strictEqual(latestBackups.length, existingBackups.length + 1);
2016-04-05 00:02:21 +00:00
return latestBackups[0]; // { creationTime, boxVersion, id, dependsOn }
2015-06-16 19:00:46 +00:00
};
2015-08-31 04:48:33 +00:00
Cloudron.prototype.reboot = function () {
2015-08-31 04:49:09 +00:00
var res = request.post(this._origin + '/api/v1/cloudron/reboot').query({ access_token: this._credentials.accessToken }).send({ }).end();
2016-01-27 17:21:41 +00:00
common.verifyResponse2xx(res, 'Box could not be rebooted');
2015-08-31 04:48:33 +00:00
this.waitForBox();
};
2015-09-30 00:32:21 +00:00
Cloudron.prototype.checkA = function (callback) {
var that = this;
dns.resolve4(this._box.domain, function (error, records) {
if (error) return callback(error);
if (records.length !== 1) return callback(new Error('Got ' + JSON.stringify(records) + ' A records. Expecting 1 length array'));
2015-09-30 03:04:08 +00:00
if (records[0] !== that._box.ip) return callback(new Error('Bad A record. ' + records[0] + '. Expecting ' + that._box.ip));
2015-09-30 00:32:21 +00:00
2015-09-30 04:39:11 +00:00
callback(null, records);
2015-09-30 00:32:21 +00:00
});
};
2016-04-06 01:43:30 +00:00
Cloudron.prototype._checkGraphs = function (targets, from) {
2016-04-06 01:33:11 +00:00
var params = {
2016-04-06 05:17:40 +00:00
target: targets.length === 1 ? targets[0] : targets,
2016-04-06 01:33:11 +00:00
format: 'json',
2016-04-06 01:43:30 +00:00
from: from,
2016-04-06 01:33:11 +00:00
access_token: this._credentials.accessToken
};
var res;
for (var i = 0; i < 40; i++) {
sleep(10);
res = request.get(this._origin + '/api/v1/cloudron/graphs').query(params).end();
process.stdout.write('.');
2016-04-06 01:43:30 +00:00
if (res.statusCode !== 200) continue;
2016-04-06 04:39:40 +00:00
if (!util.isArray(res.body) || res.body.length !== targets.length) continue;
2016-04-06 01:43:30 +00:00
for (var j = 0; j < res.body.length; j++) {
if (res.body[j].datapoints.length === 0) break; // no data
console.log();
console.log(res.body);
return; // success
}
2016-04-06 01:33:11 +00:00
}
assert(false, 'Graphs are not populated');
};
2016-04-06 01:43:30 +00:00
Cloudron.prototype.checkAppGraphs = function (appId) {
var timePeriod = 2 * 60; // in minutes
var timeBucketSize = 30; // in minutes
var target = 'summarize(collectd.localhost.table-' + appId + '-memory.gauge-rss, "' + timeBucketSize + 'min", "avg")';
this._checkGraphs([target], '-' + timePeriod + 'min');
};
Cloudron.prototype.checkDiskGraphs = function () {
var targets = [
'averageSeries(collectd.localhost.df-loop*.df_complex-free)',
'averageSeries(collectd.localhost.df-loop*.df_complex-reserved)',
'averageSeries(collectd.localhost.df-loop*.df_complex-used)'
];
this._checkGraphs(targets, '-1min');
};
Cloudron.prototype.checkSPF = function (callback) {
2015-09-29 03:36:11 +00:00
var that = this;
dns.resolveTxt(this._box.domain, function (error, records) {
if (error) return callback(error);
2015-09-29 03:36:11 +00:00
if (records.length !== 1 || records[0].length !== 1) return callback(new Error('Got ' + JSON.stringify(records) + ' TXT records. Expecting 1 length 2d array'));
2016-03-28 18:25:15 +00:00
if (records[0][0].search(new RegExp('^v=spf1 a:' + that._adminFqdn + ' ~all$')) !== 0) return callback(new Error('Bad SPF record. ' + records[0][0]));
2015-09-30 04:39:11 +00:00
callback(null, records);
});
};
Cloudron.prototype.checkDKIM = function (callback) {
2015-10-31 04:52:00 +00:00
dns.resolveTxt('cloudron._domainkey.' + this._box.domain, function (error, records) {
if (error) return callback(error);
2015-09-29 03:36:11 +00:00
if (records.length !== 1 || records[0].length !== 1) return callback(new Error('Got ' + JSON.stringify(records) + ' TXT records. Expecting 1 length 2d array'));
// node removes the quotes or maybe this is why a 2d-array?
2015-09-29 04:10:39 +00:00
if (records[0][0].search(/^v=DKIM1; t=s; p=.*$/) !== 0) return callback(new Error('Bad DKIM record. ' + records[0][0]));
2015-09-30 04:39:11 +00:00
callback(null, records);
});
};
Cloudron.prototype.checkDMARC = function (callback) {
dns.resolveTxt('_dmarc.' + this._box.domain, function (error, records) {
if (error) return callback(error);
2015-09-29 03:36:11 +00:00
if (records.length !== 1 || records[0].length !== 1) return callback(new Error('Got ' + JSON.stringify(records) + ' TXT records. Expecting 1 length 2d array'));
// node removes the quotes or maybe this is why a 2d-array?
2015-09-29 04:10:39 +00:00
if (records[0][0].search(/^v=DMARC1; p=none; pct=100; rua=mailto:.*; ruf=.*$/) !== 0) return callback(new Error('Bad DMARC record. ' + records[0][0]));
2015-09-30 04:39:11 +00:00
callback(null, records);
});
};
2015-10-21 00:53:57 +00:00
Cloudron.prototype.checkAddons = function (location, owner) {
var lastError;
// try many times because the scheduler takes sometime to run
for (var i = 0; i < 100; i++) {
2015-12-18 04:58:50 +00:00
var res = request.post('https://' + this.appFqdn(location) + '/check_addons').query({ username: owner.username, password: owner.password }).end();
2015-10-21 00:53:57 +00:00
try {
assert.strictEqual(res.statusCode, 200);
for (var addon in res.body) {
assert.strictEqual(res.body[addon], 'OK');
}
2015-10-21 00:53:57 +00:00
return;
} catch (e) {
lastError = e;
console.error(e);
console.log('Attempt %s failed. Trying again in 10 seconds', i);
sleep(10);
}
}
throw lastError;
};
2015-10-31 04:12:41 +00:00
Cloudron.prototype.setDnsConfig = function (dnsConfig) {
var res = request.post(this._origin + '/api/v1/settings/dns_config').query({ access_token: this._credentials.accessToken }).send(dnsConfig).end();
2016-01-27 17:21:41 +00:00
common.verifyResponse2xx(res, 'Could not set dns config');
2015-10-31 04:12:41 +00:00
};