1
0
mirror of https://github.com/yldio/copilot.git synced 2024-11-28 22:20:06 +02:00
copilot/packages/portal-api/test/data/unit.js

130 lines
4.2 KiB
JavaScript
Raw Normal View History

2017-08-18 18:52:42 +03:00
'use strict';
const Http = require('http');
2017-08-21 19:11:49 +03:00
const { describe, it, expect } = exports.lab = require('lab').script();
2017-08-18 18:52:42 +03:00
const PortalData = require('../../lib/data');
describe('getMetrics()', () => {
it('queries prometheus for the instance metrics', (done) => {
const server = Http.createServer((req, res) => {
const payload = JSON.stringify({ 'status': 'success', 'data': { 'resultType': 'matrix', 'result': [{ 'metric': { '__name__': 'mem_agg_usage', 'instance': 'joyentportal_compose-api_1', 'job': 'triton' }, 'values': [[1502896217.371, '60518400'], [1502899817.371, '60641280'], [1502903417.371, '60575744']] } ] } });
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(payload);
});
server.listen(9090, (err) => {
expect(err).to.not.exist();
const options = {
deploymentGroupId: 'deploymentGroup',
names: ['AVG_MEM_BYTES'],
instances: ['joyentportal_compose-api_1'],
start: new Date(Date.now() - 10000).toISOString(),
end: new Date(Date.now()).toISOString()
};
const dataThis = {
getServices: ({ deploymentGroupId, name }, next) => {
const instance = {
machine_id: 'machineId'
};
const service = {
2017-08-21 19:11:49 +03:00
instances: () => { return Promise.resolve([instance]); }
2017-08-18 18:52:42 +03:00
};
next(null, [service]);
},
_triton: {
getInstance: (id, next) => {
const tritonInstance = {
primaryIp: 'localhost'
};
next(null, tritonInstance);
}
}
};
PortalData.prototype.getMetrics.call(dataThis, options, (err, metrics) => {
expect(err).to.not.be.an.error();
expect(metrics.length).to.equal(1);
expect(metrics[0].name).to.equal('AVG_MEM_BYTES');
2017-08-23 16:25:45 +03:00
server.close(() => { return done(); });
2017-08-22 19:55:37 +03:00
});
});
});
});
describe('getInstance()', () => {
it('retrieves an instance that can also be used to query for metrics', (done) => {
const server = Http.createServer((req, res) => {
const payload = JSON.stringify({ 'status': 'success', 'data': { 'resultType': 'matrix', 'result': [{ 'metric': { '__name__': 'mem_agg_usage', 'instance': 'joyentportal_compose-api_1', 'job': 'triton' }, 'values': [[1502896217.371, '60518400'], [1502899817.371, '60641280'], [1502903417.371, '60575744']] }] } });
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(payload);
});
server.listen(9090, (err) => {
expect(err).to.not.exist();
const FakePortalData = function () {
this._db = {
instances: {
single: (options, next) => {
expect(options.id).to.exist();
const instance = {
id: options.id,
deployment_group_id: 'deploymentGroup',
service_id: 'service',
machine_id: 'joyentportal_compose-api_1'
};
next(null, instance);
}
}
};
this._triton = {
getInstance: (id, next) => {
const tritonInstance = {
primaryIp: 'localhost'
};
next(null, tritonInstance);
}
};
};
FakePortalData.prototype = PortalData.prototype;
FakePortalData.prototype.getServices = ({ deploymentGroupId, name }, next) => {
const instance = {
machine_id: 'machineId'
};
const service = {
instances: () => { return Promise.resolve([instance]); }
};
next(null, [service]);
};
const fakePortalData = new FakePortalData();
fakePortalData.getInstance({ id: 'joyentportal_compose-api_1' }, (err, instance) => {
expect(err).to.not.be.an.error();
expect(instance).to.exist();
instance.metrics({ names: ['AVG_MEM_BYTES'], start: new Date(Date.now() - 10000).toISOString(), end: new Date(Date.now()).toISOString() }).catch((err) => {
expect(err).to.not.exist();
}).then((metrics) => {
expect(metrics.length).to.equal(1);
expect(metrics[0].name).to.equal('AVG_MEM_BYTES');
2017-08-23 16:25:45 +03:00
server.close(() => { return done(); });
2017-08-22 19:55:37 +03:00
});
2017-08-18 18:52:42 +03:00
});
});
});
});