Files
cloudron-box/src/test/updatechecker-test.js

347 lines
11 KiB
JavaScript
Raw Normal View History

2016-01-23 15:57:56 -08:00
/* jslint node:true */
/* global it:false */
/* global describe:false */
/* global before:false */
/* global after:false */
'use strict';
var appdb = require('../appdb.js'),
async = require('async'),
config = require('../config.js'),
database = require('../database.js'),
2016-01-24 00:06:32 -08:00
deepExtend = require('deep-extend'),
2016-01-23 15:57:56 -08:00
expect = require('expect.js'),
2016-04-12 13:12:17 -07:00
mailer = require('../mailer.js'),
2016-01-23 15:57:56 -08:00
nock = require('nock'),
settings = require('../settings.js'),
updatechecker = require('../updatechecker.js'),
2016-04-12 13:12:17 -07:00
user = require('../user.js'),
2016-01-23 15:57:56 -08:00
_ = require('underscore');
2016-01-24 00:06:32 -08:00
var RELEASE_1 = {
"sourceTarballUrl": "https://dev-cloudron-releases.s3.amazonaws.com/box-3314658ce81f328462508e14b6d388acf36ca81c.tar.gz",
"imageId": 100,
"imageName": "box-dev-2c7a52b-2016-01-22-150657",
"changelog": [ ],
"date": "2016-01-23T23:53:01.566Z",
"author": "Girish Ramakrishnan <girish@cloudron.io>",
2016-04-12 13:01:42 -07:00
"next": "2.0.0-pre0"
2016-01-23 15:57:56 -08:00
};
2016-01-24 00:06:32 -08:00
var RELEASE_2_PRERELEASE = {
"sourceTarballUrl": "https://dev-cloudron-releases.s3.amazonaws.com/box-3314658ce81f328462508e14b6d388acf36ca81c.tar.gz",
"imageId": 2001,
"imageName": "box-dev-2c7a52b-2016-01-22-150657",
"changelog": [ ],
"upgrade": false,
"date": "2016-01-23T23:53:01.566Z",
"author": "Girish Ramakrishnan <girish@cloudron.io>",
"next": "2.0.0"
};
var RELEASE_2 = {
"sourceTarballUrl": "https://dev-cloudron-releases.s3.amazonaws.com/box-3314658ce81f328462508e14b6d388acf36ca81c.tar.gz",
"imageId": 200,
"imageName": "box-dev-2c7a52b-2016-01-22-150657",
"changelog": [ ],
"upgrade": false,
"date": "2016-01-23T23:53:01.566Z",
"author": "Girish Ramakrishnan <girish@cloudron.io>",
"next": null
};
var RELEASES = {
"1.0.0": RELEASE_1,
"1.0.1": RELEASE_1,
2016-04-12 13:01:42 -07:00
"2.0.0-pre0": RELEASE_2_PRERELEASE,
2016-01-24 00:06:32 -08:00
"2.0.0": RELEASE_2
};
2016-04-12 13:12:17 -07:00
// owner
var USER_0 = {
username: 'username0',
password: 'Username0pass?1234',
email: 'user0@email.com',
displayName: 'User 0'
};
2016-05-01 20:01:34 -07:00
var AUDIT_SOURCE = {
ip: '1.2.3.4'
};
2016-04-12 13:15:40 -07:00
function checkMails(number, done) {
// mails are enqueued async
setTimeout(function () {
expect(mailer._getMailQueue().length).to.equal(number);
mailer._clearMailQueue();
done();
}, 500);
}
2016-01-24 00:06:32 -08:00
describe('updatechecker - checkBoxUpdates', function () {
2016-01-23 15:57:56 -08:00
before(function (done) {
2016-01-24 00:06:32 -08:00
config.set('version', '1.0.0');
2016-04-12 13:12:17 -07:00
config.set('boxVersionsUrl', 'http://localhost:4444/release.json');
2016-01-23 15:57:56 -08:00
async.series([
2016-04-12 13:12:17 -07:00
database.initialize,
mailer._clearMailQueue,
2016-05-01 20:01:34 -07:00
user.createOwner.bind(null, USER_0.username, USER_0.password, USER_0.email, USER_0.displayName, AUDIT_SOURCE)
2016-01-23 15:57:56 -08:00
], done);
});
2016-04-12 13:12:17 -07:00
function after(done) {
mailer._clearMailQueue();
2016-01-23 15:57:56 -08:00
database._clear(done);
2016-04-12 13:12:17 -07:00
}
2016-01-23 15:57:56 -08:00
2016-01-24 00:06:32 -08:00
it('no updates', function (done) {
2016-01-23 15:57:56 -08:00
nock.cleanAll();
2016-01-24 00:06:32 -08:00
var releaseCopy = deepExtend({}, RELEASES);
releaseCopy['1.0.0'].next = null;
2016-01-23 15:57:56 -08:00
var scope = nock('http://localhost:4444')
.get('/release.json')
2016-01-24 00:06:32 -08:00
.reply(200, releaseCopy);
2016-01-23 15:57:56 -08:00
updatechecker.checkBoxUpdates(function (error) {
expect(!error).to.be.ok();
2016-01-24 00:06:32 -08:00
expect(updatechecker.getUpdateInfo().box).to.be(null);
2016-04-12 13:15:40 -07:00
checkMails(0, done);
2016-01-24 00:06:32 -08:00
});
});
it('new version', function (done) {
nock.cleanAll();
var releaseCopy = deepExtend({}, RELEASES);
2016-04-12 13:01:42 -07:00
delete releaseCopy['2.0.0-pre0'];
2016-01-24 00:06:32 -08:00
releaseCopy['1.0.0'].next = '2.0.0';
var scope = nock('http://localhost:4444')
.get('/release.json')
.reply(200, releaseCopy);
2016-01-23 15:57:56 -08:00
2016-01-24 00:06:32 -08:00
updatechecker.checkBoxUpdates(function (error) {
expect(!error).to.be.ok();
expect(updatechecker.getUpdateInfo().box.version).to.be('2.0.0');
2016-04-12 13:15:40 -07:00
checkMails(1, done);
2016-01-24 00:06:32 -08:00
});
});
it('existing version missing offers latest version', function (done) {
nock.cleanAll();
var releaseCopy = deepExtend({}, RELEASES);
delete releaseCopy['1.0.0'];
var scope = nock('http://localhost:4444')
.get('/release.json')
.reply(200, releaseCopy);
updatechecker.checkBoxUpdates(function (error) {
expect(!error).to.be.ok();
expect(updatechecker.getUpdateInfo().box.version).to.be('2.0.0');
2016-04-12 13:15:40 -07:00
checkMails(0, done); // already notified for 2.0.0
2016-01-23 15:57:56 -08:00
});
});
2016-01-24 00:06:32 -08:00
it('does not offer prerelease', function (done) {
nock.cleanAll();
var releaseCopy = deepExtend({}, RELEASES);
var scope = nock('http://localhost:4444')
.get('/release.json')
.reply(200, releaseCopy);
updatechecker.checkBoxUpdates(function (error) {
expect(!error).to.be.ok();
expect(updatechecker.getUpdateInfo().box).to.be(null);
checkMails(0, done);
2016-01-24 00:06:32 -08:00
});
});
it('offers prerelease', function (done) {
nock.cleanAll();
settings.setUpdateConfig({ prerelease: true }, function (error) {
if (error) return done(error);
var releaseCopy = deepExtend({}, RELEASES);
var scope = nock('http://localhost:4444')
.get('/release.json')
.reply(200, releaseCopy);
updatechecker.checkBoxUpdates(function (error) {
expect(!error).to.be.ok();
2016-04-12 13:01:42 -07:00
expect(updatechecker.getUpdateInfo().box.version).to.be('2.0.0-pre0');
checkMails(1, done);
2016-01-24 00:06:32 -08:00
});
});
});
it('does not send mail for patch releases', function (done) {
var releaseCopy = deepExtend({}, RELEASES);
releaseCopy['1.0.0'].next = '1.0.1';
var scope = nock('http://localhost:4444')
.get('/release.json')
.reply(200, releaseCopy);
updatechecker.checkBoxUpdates(function (error) {
expect(!error).to.be.ok();
expect(updatechecker.getUpdateInfo().box.version).to.be('1.0.1'); // got the update
checkMails(0, done); // but no email sent since patch release
});
});
2016-01-24 00:06:32 -08:00
it('bad response offers nothing', function (done) {
nock.cleanAll();
var releaseCopy = _.extend({}, RELEASES);
var scope = nock('http://localhost:4444')
.get('/release.json')
.reply(404, releaseCopy);
updatechecker.checkBoxUpdates(function (error) {
expect(error).to.be.ok();
expect(updatechecker.getUpdateInfo().box).to.be(null);
checkMails(0, done);
2016-01-24 00:06:32 -08:00
});
});
});
2016-01-24 00:44:46 -08:00
describe('updatechecker - checkAppUpdates', function () {
var APP_0 = {
id: 'appid-0',
appStoreId: 'io.cloudron.app',
installationState: appdb.ISTATE_PENDING_INSTALL,
installationProgress: null,
runState: null,
location: 'some-location-0',
manifest: {
version: '1.0.0', dockerImage: 'docker/app0', healthCheckPath: '/', httpPort: 80, title: 'app0',
tcpPorts: {
PORT: {
description: 'this is a port that i expose',
containerPort: '1234'
}
}
},
httpPort: null,
containerId: null,
portBindings: { PORT: 5678 },
healthy: null,
accessRestriction: null,
2016-02-10 12:28:57 +01:00
memoryLimit: 0
2016-01-24 00:44:46 -08:00
};
before(function (done) {
config.set('version', '1.0.0');
config.set('apiServerOrigin', 'http://localhost:4444');
async.series([
database.initialize,
database._clear,
mailer._clearMailQueue,
2016-04-26 14:40:21 -07:00
appdb.add.bind(null, APP_0.id, APP_0.appStoreId, APP_0.manifest, APP_0.location, APP_0.portBindings, APP_0.accessRestriction, APP_0.memoryLimit, null),
2016-05-01 20:01:34 -07:00
user.createOwner.bind(null, USER_0.username, USER_0.password, USER_0.email, USER_0.displayName, AUDIT_SOURCE)
2016-01-24 00:44:46 -08:00
], done);
});
after(function (done) {
database._clear(done);
});
it('no updates', function (done) {
nock.cleanAll();
var scope = nock('http://localhost:4444')
.post('/api/v1/appupdates')
.reply(200, { appVersions: { 'io.cloudron.app': { manifest: { version: '1.0.0' } } } });
updatechecker.checkAppUpdates(function (error) {
expect(!error).to.be.ok();
expect(updatechecker.getUpdateInfo().apps).to.eql({});
checkMails(0, done);
2016-01-24 00:44:46 -08:00
});
});
it('bad response', function (done) {
nock.cleanAll();
var scope = nock('http://localhost:4444')
.post('/api/v1/appupdates')
.reply(500, { appVersions: { 'io.cloudron.app': { manifest: { version: '1.0.0' } } } });
updatechecker.checkAppUpdates(function (error) {
expect(error).to.be.ok();
expect(updatechecker.getUpdateInfo().apps).to.eql({});
checkMails(0, done);
2016-01-24 00:44:46 -08:00
});
});
it('missing info', function (done) {
nock.cleanAll();
var scope = nock('http://localhost:4444')
.post('/api/v1/appupdates')
.reply(200, { appVersions: { 'io.cloudron.app2': { manifest: { version: '1.0.0' } } } });
updatechecker.checkAppUpdates(function (error) {
expect(!error).to.be.ok();
expect(updatechecker.getUpdateInfo().apps).to.eql({});
checkMails(0, done);
2016-01-24 00:44:46 -08:00
});
});
it('offers new version', function (done) {
nock.cleanAll();
var scope = nock('http://localhost:4444')
.post('/api/v1/appupdates')
.reply(200, { appVersions: { 'io.cloudron.app': { manifest: { version: '2.0.0' } } } });
updatechecker.checkAppUpdates(function (error) {
expect(!error).to.be.ok();
expect(updatechecker.getUpdateInfo().apps).to.eql({ 'appid-0': { manifest: { version: '2.0.0' } } });
checkMails(1, done);
2016-01-24 00:44:46 -08:00
});
});
it('does not send mail for patch releases', function (done) {
nock.cleanAll();
var scope = nock('http://localhost:4444')
.post('/api/v1/appupdates')
.reply(200, { appVersions: { 'io.cloudron.app': { manifest: { version: '1.0.1' } } } });
updatechecker.checkAppUpdates(function (error) {
expect(!error).to.be.ok();
expect(updatechecker.getUpdateInfo().apps).to.eql({ 'appid-0': { manifest: { version: '1.0.1' } } }); // got the update
checkMails(0, done); // but no email sent since patch release
});
});
2016-01-24 00:44:46 -08:00
it('does not offer old version', function (done) {
nock.cleanAll();
var scope = nock('http://localhost:4444')
.post('/api/v1/appupdates')
.reply(200, { appVersions: { 'io.cloudron.app': { manifest: { version: '0.1.0' } } } });
updatechecker.checkAppUpdates(function (error) {
expect(!error).to.be.ok();
expect(updatechecker.getUpdateInfo().apps).to.eql({ });
checkMails(0, done);
2016-01-24 00:44:46 -08:00
});
});
});