2017-05-11 23:18:51 +03:00
|
|
|
'use strict';
|
|
|
|
|
2017-05-16 18:54:39 +03:00
|
|
|
const pkg = require('../package.json');
|
|
|
|
const { expect } = require('code');
|
|
|
|
const { before, describe, it, script } = require('lab');
|
|
|
|
const { Server } = require('zerorpc');
|
2017-05-11 23:18:51 +03:00
|
|
|
const PortalData = require('../');
|
|
|
|
|
2017-05-16 18:54:39 +03:00
|
|
|
exports.lab = script();
|
2017-05-11 23:18:51 +03:00
|
|
|
|
2017-05-16 18:54:39 +03:00
|
|
|
const server = new Server({
|
|
|
|
up: function(options, manifest, fn) {
|
|
|
|
fn(null, {
|
|
|
|
projectName: options.project_name
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2017-05-11 23:18:51 +03:00
|
|
|
|
|
|
|
const internals = {
|
2017-05-16 18:54:39 +03:00
|
|
|
options: { test: true, name: 'test', dockerHost: 'tcp://0.0.0.0:4242'}
|
2017-05-11 23:18:51 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-05-16 18:54:39 +03:00
|
|
|
before(() => {
|
|
|
|
server.bind(internals.options.dockerHost);
|
|
|
|
});
|
|
|
|
|
|
|
|
after(() => {
|
|
|
|
server.close();
|
|
|
|
});
|
|
|
|
|
2017-05-11 23:18:51 +03:00
|
|
|
describe('createDeployment()', () => {
|
2017-05-16 18:54:39 +03:00
|
|
|
it('creates a deployment record in the deployment table', done => {
|
2017-05-11 23:18:51 +03:00
|
|
|
const data = new PortalData(internals.options);
|
2017-05-16 18:54:39 +03:00
|
|
|
const deployment = {
|
|
|
|
name: 'User Services',
|
|
|
|
datacenter: 'us-sw-1'
|
|
|
|
};
|
2017-05-11 23:18:51 +03:00
|
|
|
|
2017-05-16 18:54:39 +03:00
|
|
|
data.connect().then(() => {
|
|
|
|
data.createDeployment({
|
|
|
|
deployment
|
|
|
|
}).then(deployment => {
|
2017-05-11 23:18:51 +03:00
|
|
|
expect(deployment.id).to.exist();
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('getDeployment()', () => {
|
2017-05-16 18:54:39 +03:00
|
|
|
it('will retrieve an existing deployment', done => {
|
2017-05-11 23:18:51 +03:00
|
|
|
const data = new PortalData(internals.options);
|
|
|
|
data.connect().then(() => {
|
|
|
|
const deployment = {
|
|
|
|
name: 'User Services',
|
|
|
|
datacenter: 'us-sw-1'
|
|
|
|
};
|
|
|
|
|
2017-05-16 18:54:39 +03:00
|
|
|
data.createDeployment(deployment).then(deployment => {
|
2017-05-11 23:18:51 +03:00
|
|
|
expect(deployment.id).to.exist();
|
2017-05-16 18:54:39 +03:00
|
|
|
data.getDeployment(deployment.id).then(retrievedDeployment => {
|
2017-05-11 23:18:51 +03:00
|
|
|
expect(deployment).to.equal(retrievedDeployment);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2017-05-12 22:59:37 +03:00
|
|
|
|
|
|
|
describe('updateService()', () => {
|
2017-05-16 18:54:39 +03:00
|
|
|
it('will update the services for an existing deployment', done => {
|
2017-05-12 22:59:37 +03:00
|
|
|
const data = new PortalData(internals.options);
|
|
|
|
data.connect().then(() => {
|
|
|
|
const deployment = {
|
|
|
|
name: 'User Services',
|
|
|
|
datacenter: 'us-sw-1'
|
|
|
|
};
|
|
|
|
const service = {
|
|
|
|
name: 'consul',
|
|
|
|
containers: [
|
|
|
|
{
|
|
|
|
server_id: '423e7432-b760-11e2-bf6c-002590c3f1a0',
|
|
|
|
alias: 'nodejsexample_consul_1',
|
|
|
|
image_id: '91b757b5-bd29-2126-5ff9-ae9235011ff5',
|
|
|
|
owner_id: '30f62ec2-24a2-6f8e-8fad-d46b04c8a0b9',
|
|
|
|
id: '81205d4a-92f4-c4d9-da8a-aafd689eeabb'
|
|
|
|
}
|
|
|
|
],
|
|
|
|
count: 1
|
|
|
|
};
|
|
|
|
|
2017-05-16 18:54:39 +03:00
|
|
|
data.createDeployment(deployment).then(deployment => {
|
2017-05-12 22:59:37 +03:00
|
|
|
expect(deployment.id).to.exist();
|
2017-05-16 18:54:39 +03:00
|
|
|
data.updateService(deployment.id, service).then(updatedService => {
|
2017-05-12 22:59:37 +03:00
|
|
|
expect(updatedService).to.equal(service);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('deploymentChanges()', () => {
|
2017-05-16 18:54:39 +03:00
|
|
|
it('will execute the handler when a deployment service changes', done => {
|
2017-05-12 22:59:37 +03:00
|
|
|
const data = new PortalData(internals.options);
|
|
|
|
data.connect().then(() => {
|
|
|
|
const deployment = {
|
|
|
|
name: 'User Services',
|
|
|
|
datacenter: 'us-sw-1'
|
|
|
|
};
|
|
|
|
const service1 = {
|
|
|
|
name: 'consul',
|
|
|
|
containers: [
|
|
|
|
{
|
|
|
|
server_id: '423e7432-b760-11e2-bf6c-002590c3f1a0',
|
|
|
|
alias: 'nodejsexample_consul_1',
|
|
|
|
image_id: '91b757b5-bd29-2126-5ff9-ae9235011ff5',
|
|
|
|
owner_id: '30f62ec2-24a2-6f8e-8fad-d46b04c8a0b9',
|
|
|
|
id: '81205d4a-92f4-c4d9-da8a-aafd689eeabb'
|
|
|
|
}
|
|
|
|
],
|
|
|
|
count: 1
|
|
|
|
};
|
|
|
|
|
|
|
|
const service2 = {
|
|
|
|
name: 'consul',
|
|
|
|
containers: [
|
|
|
|
{
|
|
|
|
server_id: '423e7432-b760-11e2-bf6c-002590c3f1a0',
|
|
|
|
alias: 'nodejsexample_consul_1',
|
|
|
|
image_id: '91b757b5-bd29-2126-5ff9-ae9235011ff5',
|
|
|
|
owner_id: '30f62ec2-24a2-6f8e-8fad-d46b04c8a0b9',
|
|
|
|
id: '81205d4a-92f4-c4d9-da8a-aafd689eeabb'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
server_id: '423e7432-b760-11e2-bf6c-002590c3f1a0',
|
|
|
|
alias: 'nodejsexample_consul_2',
|
|
|
|
image_id: '91b757b5-bd29-2126-5ff9-ae9235011ff5',
|
|
|
|
owner_id: '30f62ec2-24a2-6f8e-8fad-d46b04c8a0b9',
|
|
|
|
id: '81205d4a-92f4-c4d9-da8a-aafd689eeabb'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
server_id: '423e7432-b760-11e2-bf6c-002590c3f1a0',
|
|
|
|
alias: 'nodejsexample_consul_3',
|
|
|
|
image_id: '91b757b5-bd29-2126-5ff9-ae9235011ff5',
|
|
|
|
owner_id: '30f62ec2-24a2-6f8e-8fad-d46b04c8a0b9',
|
|
|
|
id: '81205d4a-92f4-c4d9-da8a-aafd689eeabb'
|
|
|
|
}
|
|
|
|
],
|
|
|
|
count: 3
|
|
|
|
};
|
|
|
|
|
2017-05-16 18:54:39 +03:00
|
|
|
data.createDeployment(deployment).then(deployment => {
|
2017-05-12 22:59:37 +03:00
|
|
|
expect(deployment.id).to.exist();
|
2017-05-16 18:54:39 +03:00
|
|
|
data.updateService(deployment.id, service1).then(updatedService1 => {
|
2017-05-12 22:59:37 +03:00
|
|
|
expect(updatedService1).to.equal(service1);
|
|
|
|
|
|
|
|
let executed = false;
|
2017-05-16 18:54:39 +03:00
|
|
|
data
|
|
|
|
.deploymentChanges((err, changes) => {
|
|
|
|
if (executed) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(changes.before).to.exist();
|
|
|
|
expect(changes.after).to.exist();
|
|
|
|
done();
|
|
|
|
executed = true;
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
data
|
|
|
|
.updateService(deployment.id, service2)
|
|
|
|
.then(updatedService2 => {
|
|
|
|
expect(updatedService2).to.equal(service2);
|
|
|
|
});
|
2017-05-12 22:59:37 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('insertMetrics()', () => {
|
2017-05-16 18:54:39 +03:00
|
|
|
it("will add new metrics to a service and won't overwrite existing ones", done => {
|
2017-05-12 22:59:37 +03:00
|
|
|
const data = new PortalData(internals.options);
|
|
|
|
data.connect().then(() => {
|
|
|
|
const containerId = '81205d4a-92f4-c4d9-da8a-aafd689eeabb';
|
|
|
|
const metrics1 = [
|
|
|
|
{
|
|
|
|
timestamp: 1494360995851,
|
|
|
|
cpu: 1.2,
|
|
|
|
memory: 23344523,
|
|
|
|
network: 5024
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
const metrics2 = [
|
|
|
|
{
|
|
|
|
timestamp: 1495360995851,
|
|
|
|
cpu: 1.3,
|
|
|
|
memory: 23344523,
|
|
|
|
network: 4024
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
2017-05-16 18:54:39 +03:00
|
|
|
data.insertMetrics(containerId, metrics1).then(result1 => {
|
2017-05-12 22:59:37 +03:00
|
|
|
expect(result1.id).to.equal(containerId);
|
|
|
|
expect(result1.metrics).to.equal(metrics1);
|
2017-05-16 18:54:39 +03:00
|
|
|
data.insertMetrics(containerId, metrics2).then(result2 => {
|
2017-05-12 22:59:37 +03:00
|
|
|
expect(result2.id).to.equal(containerId);
|
2017-05-16 18:54:39 +03:00
|
|
|
data.getMetrics(containerId).then(results => {
|
2017-05-12 22:59:37 +03:00
|
|
|
expect(results.metrics.length).to.equal(2);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|