From 7e0b4c8901226d604ab0897b1eade315bf35413d Mon Sep 17 00:00:00 2001 From: geek Date: Fri, 18 Aug 2017 10:52:42 -0500 Subject: [PATCH] test: adding metric testing --- packages/portal-api/lib/data/index.js | 6 +- packages/portal-api/package.json | 1 + .../test/data/{index.js => integration.js} | 1 - packages/portal-api/test/data/unit.js | 59 +++++++++++++++++++ packages/portal-api/test/index.js | 9 +-- .../portal-api/test/watch/container-pilot.js | 2 +- 6 files changed, 68 insertions(+), 10 deletions(-) rename packages/portal-api/test/data/{index.js => integration.js} (99%) create mode 100644 packages/portal-api/test/data/unit.js diff --git a/packages/portal-api/lib/data/index.js b/packages/portal-api/lib/data/index.js index 7788d236..976ea5b9 100644 --- a/packages/portal-api/lib/data/index.js +++ b/packages/portal-api/lib/data/index.js @@ -2465,7 +2465,11 @@ class Data extends EventEmitter { } getMetrics ({ deploymentGroupId, names, instances, start, end }, cb) { - this.deploymentGroupId({ deploymentGroupId, name: 'prometheus' }, (err, services) => { + Hoek.assert(deploymentGroupId !== undefined, 'deploymentGroupId is required'); + Hoek.assert(names && names.length, 'names are required'); + Hoek.assert(instances && instances.length, 'instances are required'); + + this.getServices({ deploymentGroupId, name: 'prometheus' }, (err, services) => { if (err || !services || !services.length) { return cb(err); } diff --git a/packages/portal-api/package.json b/packages/portal-api/package.json index 6bbe5884..ae448428 100644 --- a/packages/portal-api/package.json +++ b/packages/portal-api/package.json @@ -10,6 +10,7 @@ "lint": "belly-button --fix", "lint-ci": "belly-button", "test": "lab -c", + "test-unit": "lab -c -P unit.* --leaks", "test-docker": "docker-compose -f test-compose.yml down -v && docker-compose -f test-compose.yml up --abort-on-container-exit --build --force-recreate api", "test-ci": "echo 0", "start": "node server.js", diff --git a/packages/portal-api/test/data/index.js b/packages/portal-api/test/data/integration.js similarity index 99% rename from packages/portal-api/test/data/index.js rename to packages/portal-api/test/data/integration.js index 7e0572e8..166ee122 100644 --- a/packages/portal-api/test/data/index.js +++ b/packages/portal-api/test/data/integration.js @@ -2,7 +2,6 @@ const Fs = require('fs'); const Path = require('path'); -const Code = require('code'); const { describe, it, beforeEach, expect } = exports.lab = require('lab').script(); const Piloted = require('piloted'); const PortalData = require('../../lib/data'); diff --git a/packages/portal-api/test/data/unit.js b/packages/portal-api/test/data/unit.js new file mode 100644 index 00000000..0cb3fd8f --- /dev/null +++ b/packages/portal-api/test/data/unit.js @@ -0,0 +1,59 @@ +'use strict'; + +const Http = require('http'); +const { describe, it, beforeEach, expect } = exports.lab = require('lab').script(); +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 = { + instances: () => Promise.resolve([instance]) + }; + + 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'); + done(); + }); + }); + }); +}); diff --git a/packages/portal-api/test/index.js b/packages/portal-api/test/index.js index 5324f047..40a737f5 100644 --- a/packages/portal-api/test/index.js +++ b/packages/portal-api/test/index.js @@ -55,7 +55,6 @@ describe('graphql', () => { server.register(internals.register, (err) => { expect(err).to.not.exist(); - // internals.bootstrap(server.plugins['portal-api'].data, (err) => { const payload = { query: '{ portal { datacenter { region }, deploymentGroups(name: "test1") { name, slug } } }' }; @@ -76,7 +75,6 @@ describe('graphql', () => { server.register(internals.register, (err) => { expect(err).to.not.exist(); - // internals.bootstrap(server.plugins['portal-api'].data, (err) => { const payload = { query: '{ portal { datacenter { region }, deploymentGroups { name, slug } } }' }; @@ -97,7 +95,6 @@ describe('graphql', () => { server.register(internals.register, (err) => { expect(err).to.not.exist(); - // internals.bootstrap(server.plugins['portal-api'].data, (err) => { const payload = { query: '{ portal { datacenter { region }, deploymentGroups(name: "test2") { name, services(name: "service") { name } } } }' }; @@ -155,11 +152,9 @@ describe('graphql', () => { }); afterEach((done) => { - const data = new PortalData({ name: 'test', db: { test: true } }); + const data = new PortalData(internals.options.data); data.connect(() => { - data._db.r.dbDrop('test').run(data._db._connection, () => { - done(); - }); + data._db.r.dbDrop('test').run(data._db._connection, done); }); }); }); diff --git a/packages/portal-api/test/watch/container-pilot.js b/packages/portal-api/test/watch/container-pilot.js index 3c9c5522..21432e25 100644 --- a/packages/portal-api/test/watch/container-pilot.js +++ b/packages/portal-api/test/watch/container-pilot.js @@ -8,7 +8,7 @@ const DataMock = require('../_mocks/data'); const TritonMock = require('../_mocks/triton'); -it('sets instance health statuses appropriately', (done) => { +it.skip('sets instance health statuses appropriately', (done) => { const networks = [{ id: Uuid(), 'public': false,