feat: initial commit
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
import {Datasource} from "../module";
|
||||
import Q from "q";
|
||||
import sinon from 'sinon';
|
||||
|
||||
describe('DataDogDatasource', () => {
|
||||
let ctx = {};
|
||||
|
||||
beforeEach(function() {
|
||||
ctx.$q = Q;
|
||||
ctx.backendSrv = {
|
||||
datasourceRequest: () => {
|
||||
return ctx.$q.when({
|
||||
status: 200
|
||||
});
|
||||
}
|
||||
};
|
||||
ctx.templateSrv = {
|
||||
replace: (str) => str,
|
||||
getAdhocFilters: () => []
|
||||
};
|
||||
|
||||
let instanceSettings = {
|
||||
url: 'https://app.datadoghq.com/api/v1',
|
||||
jsonData: {
|
||||
api_key: '0000deadbeaf0000',
|
||||
app_key: '0000abcd0000abcd'
|
||||
}
|
||||
};
|
||||
ctx.ds = new Datasource(instanceSettings, ctx.backendSrv, ctx.templateSrv);
|
||||
});
|
||||
|
||||
describe('When doing DataDog API request', () => {
|
||||
beforeEach(function() {
|
||||
let targets = [
|
||||
{ query: 'avg:system.load.5{*}', rawQuery: true }
|
||||
];
|
||||
|
||||
ctx.options = {
|
||||
range: {
|
||||
from: 12340000,
|
||||
to: 12340000
|
||||
},
|
||||
targets: targets
|
||||
};
|
||||
});
|
||||
|
||||
it('should send request with proper params', (done) => {
|
||||
let expected_params = {
|
||||
method: 'GET',
|
||||
url: 'https://app.datadoghq.com/api/v1/query',
|
||||
params: {
|
||||
api_key: '0000deadbeaf0000',
|
||||
application_key: '0000abcd0000abcd',
|
||||
from: 12340,
|
||||
to: 12340,
|
||||
query: 'avg:system.load.5{*}'
|
||||
}
|
||||
};
|
||||
let datasourceRequest = sinon.spy(ctx.ds.backendSrv, 'datasourceRequest');
|
||||
|
||||
ctx.ds.query(ctx.options);
|
||||
expect(datasourceRequest).to.have.been.calledWith(expected_params);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,79 @@
|
||||
import dfunc from '../dfunc';
|
||||
|
||||
describe('when creating func instance from func names', function() {
|
||||
it('should return func instance', function() {
|
||||
var func = dfunc.createFuncInstance('top');
|
||||
expect(func).to.be.ok;
|
||||
expect(func.def.name).to.equal('top');
|
||||
expect(func.def.params.length).to.equal(3);
|
||||
expect(func.def.defaultParams.length).to.equal(3);
|
||||
});
|
||||
|
||||
|
||||
it('should return func instance from funcDef', function() {
|
||||
var func = dfunc.createFuncInstance('top');
|
||||
var func2 = dfunc.createFuncInstance(func.def);
|
||||
expect(func2).to.be.ok;
|
||||
});
|
||||
|
||||
it('func instance should have text representation', function() {
|
||||
var func = dfunc.createFuncInstance('top');
|
||||
func.params[0] = 5;
|
||||
func.params[1] = 'mean';
|
||||
func.params[2] = 'dir';
|
||||
func.updateText();
|
||||
expect(func.text).to.equal("top(5, mean, dir)");
|
||||
});
|
||||
});
|
||||
|
||||
describe('when rendering func instance', function() {
|
||||
|
||||
it('should handle single metric param', function() {
|
||||
var func = dfunc.createFuncInstance('abs');
|
||||
expect(func.render('a')).to.equal("abs(a)");
|
||||
});
|
||||
|
||||
it('should include default params if options enable it', function() {
|
||||
var func = dfunc.createFuncInstance('top', { withDefaultParams: true });
|
||||
expect(func.render('a')).to.equal("top(a, 5, mean, dir)");
|
||||
});
|
||||
|
||||
it('should handle int or interval params with number', function() {
|
||||
var func = dfunc.createFuncInstance('anomalies');
|
||||
func.params[0] = 'basic';
|
||||
func.params[1] = '5';
|
||||
expect(func.render('hello')).to.equal("anomalies(hello, basic, 5)");
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('when requesting function categories', function() {
|
||||
it('should return function categories', function() {
|
||||
var catIndex = dfunc.getCategories();
|
||||
expect(catIndex.Arithmatic.length).to.be.greaterThan(3);
|
||||
});
|
||||
});
|
||||
|
||||
describe('when updating func param', function() {
|
||||
it('should update param value and update text representation', function() {
|
||||
var func = dfunc.createFuncInstance('top', { withDefaultParams: true });
|
||||
func.updateParam('10', 0);
|
||||
expect(func.params[0]).to.be.equal('10');
|
||||
func.updateText();
|
||||
expect(func.text).to.be.equal('top(10, mean, dir)');
|
||||
});
|
||||
|
||||
it('should parse numbers as float', function() {
|
||||
var func = dfunc.createFuncInstance('outliers');
|
||||
func.updateParam('0.5', 1);
|
||||
expect(func.params[1]).to.be.equal('0.5');
|
||||
});
|
||||
});
|
||||
|
||||
describe('when updating func param with optional second parameter', function() {
|
||||
it('should update value and text', function() {
|
||||
var func = dfunc.createFuncInstance('fill');
|
||||
func.updateParam('null', 0);
|
||||
expect(func.params[0]).to.be.equal('null');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,184 @@
|
||||
|
||||
import {QueryCtrl} from "../module";
|
||||
import 'app/core/services/segment_srv';
|
||||
import sinon from 'sinon';
|
||||
|
||||
import dfunc from '../dfunc';
|
||||
|
||||
describe('DataDogQueryCtrl', function() {
|
||||
let ctx = {};
|
||||
|
||||
beforeEach(angularMocks.module('grafana.core'));
|
||||
beforeEach(angularMocks.module('grafana.controllers'));
|
||||
beforeEach(angularMocks.module('grafana.services'));
|
||||
|
||||
beforeEach(ctx.providePhase());
|
||||
beforeEach(angularMocks.inject(($rootScope, $controller, $q) => {
|
||||
ctx.$q = $q;
|
||||
ctx.scope = $rootScope.$new();
|
||||
ctx.target = {};
|
||||
ctx.datasource.metricFindQuery = sinon.stub().returns(ctx.$q.when([]));
|
||||
ctx.panelCtrl = {panel: {}};
|
||||
ctx.panelCtrl.refresh = sinon.spy();
|
||||
|
||||
ctx.ctrl = $controller(QueryCtrl, {$scope: ctx.scope}, {
|
||||
panelCtrl: ctx.panelCtrl,
|
||||
datasource: ctx.datasource,
|
||||
target: ctx.target
|
||||
});
|
||||
ctx.scope.$digest();
|
||||
}));
|
||||
|
||||
describe('init', function() {
|
||||
it('should validate metric key exists', function() {
|
||||
expect(ctx.datasource.metricFindQuery.getCall(0).args[0]).to.be('test.prod.*');
|
||||
});
|
||||
|
||||
it('should delete last segment if no metrics are found', function() {
|
||||
expect(ctx.ctrl.segments[2].value).to.be('select metric');
|
||||
});
|
||||
|
||||
it('should parse expression and build function model', function() {
|
||||
expect(ctx.ctrl.functions.length).to.be(2);
|
||||
});
|
||||
});
|
||||
|
||||
describe('when adding function', function() {
|
||||
beforeEach(function() {
|
||||
ctx.ctrl.target.target = 'test.prod.*.count';
|
||||
ctx.ctrl.datasource.metricFindQuery = sinon.stub().returns(ctx.$q.when([{expandable: false}]));
|
||||
ctx.ctrl.parseTarget();
|
||||
ctx.ctrl.addFunction(gfunc.getFuncDef('aliasByNode'));
|
||||
});
|
||||
|
||||
it('should add function with correct node number', function() {
|
||||
expect(ctx.ctrl.functions[0].params[0]).to.be(2);
|
||||
});
|
||||
|
||||
it('should update target', function() {
|
||||
expect(ctx.ctrl.target.target).to.be('aliasByNode(test.prod.*.count, 2)');
|
||||
});
|
||||
|
||||
it('should call refresh', function() {
|
||||
expect(ctx.panelCtrl.refresh.called).to.be(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('when adding function before any metric segment', function() {
|
||||
beforeEach(function() {
|
||||
ctx.ctrl.target.target = '';
|
||||
ctx.ctrl.datasource.metricFindQuery.returns(ctx.$q.when([{expandable: true}]));
|
||||
ctx.ctrl.parseTarget();
|
||||
ctx.ctrl.addFunction(gfunc.getFuncDef('asPercent'));
|
||||
});
|
||||
|
||||
it('should add function and remove select metric link', function() {
|
||||
expect(ctx.ctrl.segments.length).to.be(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('when initalizing target without metric expression and only function', function() {
|
||||
beforeEach(function() {
|
||||
ctx.ctrl.target.target = 'asPercent(#A, #B)';
|
||||
ctx.ctrl.datasource.metricFindQuery.returns(ctx.$q.when([]));
|
||||
ctx.ctrl.parseTarget();
|
||||
ctx.scope.$digest();
|
||||
});
|
||||
|
||||
it('should not add select metric segment', function() {
|
||||
expect(ctx.ctrl.segments.length).to.be(0);
|
||||
});
|
||||
|
||||
it('should add both series refs as params', function() {
|
||||
expect(ctx.ctrl.functions[0].params.length).to.be(2);
|
||||
});
|
||||
});
|
||||
|
||||
describe('when initializing a target with single param func using variable', function() {
|
||||
beforeEach(function() {
|
||||
ctx.ctrl.target.target = 'movingAverage(prod.count, $var)';
|
||||
ctx.ctrl.datasource.metricFindQuery.returns(ctx.$q.when([]));
|
||||
ctx.ctrl.parseTarget();
|
||||
});
|
||||
|
||||
it('should add 2 segments', function() {
|
||||
expect(ctx.ctrl.segments.length).to.be(2);
|
||||
});
|
||||
|
||||
it('should add function param', function() {
|
||||
expect(ctx.ctrl.functions[0].params.length).to.be(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('when initalizing target without metric expression and function with series-ref', function() {
|
||||
beforeEach(function() {
|
||||
ctx.ctrl.target.target = 'asPercent(metric.node.count, #A)';
|
||||
ctx.ctrl.datasource.metricFindQuery.returns(ctx.$q.when([]));
|
||||
ctx.ctrl.parseTarget();
|
||||
});
|
||||
|
||||
it('should add segments', function() {
|
||||
expect(ctx.ctrl.segments.length).to.be(3);
|
||||
});
|
||||
|
||||
it('should have correct func params', function() {
|
||||
expect(ctx.ctrl.functions[0].params.length).to.be(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('when getting altSegments and metricFindQuery retuns empty array', function() {
|
||||
beforeEach(function() {
|
||||
ctx.ctrl.target.target = 'test.count';
|
||||
ctx.ctrl.datasource.metricFindQuery.returns(ctx.$q.when([]));
|
||||
ctx.ctrl.parseTarget();
|
||||
ctx.ctrl.getAltSegments(1).then(function(results) {
|
||||
ctx.altSegments = results;
|
||||
});
|
||||
ctx.scope.$digest();
|
||||
});
|
||||
|
||||
it('should have no segments', function() {
|
||||
expect(ctx.altSegments.length).to.be(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('targetChanged', function() {
|
||||
beforeEach(function() {
|
||||
ctx.ctrl.datasource.metricFindQuery = sinon.stub().returns(ctx.$q.when([{expandable: false}]));
|
||||
ctx.ctrl.parseTarget();
|
||||
ctx.ctrl.target.target = '';
|
||||
ctx.ctrl.targetChanged();
|
||||
});
|
||||
|
||||
it('should rebuld target after expression model', function() {
|
||||
expect(ctx.ctrl.target.target).to.be('aliasByNode(scaleToSeconds(test.prod.*, 1), 2)');
|
||||
});
|
||||
|
||||
it('should call panelCtrl.refresh', function() {
|
||||
expect(ctx.panelCtrl.refresh.called).to.be(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('when updating targets with nested query', function() {
|
||||
beforeEach(function() {
|
||||
ctx.ctrl.target.target = 'scaleToSeconds(#A)';
|
||||
ctx.ctrl.datasource.metricFindQuery = sinon.stub().returns(ctx.$q.when([{expandable: false}]));
|
||||
ctx.ctrl.parseTarget();
|
||||
|
||||
ctx.ctrl.panelCtrl.panel.targets = [ {
|
||||
target: 'nested.query.count',
|
||||
refId: 'A'
|
||||
}];
|
||||
|
||||
ctx.ctrl.updateModelTarget();
|
||||
});
|
||||
|
||||
it('target should remain the same', function() {
|
||||
expect(ctx.ctrl.target.target).to.be('scaleToSeconds(#A)');
|
||||
});
|
||||
|
||||
it('targetFull should include nexted queries', function() {
|
||||
expect(ctx.ctrl.target.targetFull).to.be('scaleToSeconds(nested.query.count)');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,50 @@
|
||||
// JSHint options
|
||||
/* globals global: false */
|
||||
|
||||
import prunk from 'prunk';
|
||||
import {jsdom} from 'jsdom';
|
||||
import chai from 'chai';
|
||||
import sinonChai from 'sinon-chai';
|
||||
|
||||
// Mock angular module
|
||||
var angularMocks = {
|
||||
module: function() {
|
||||
return {
|
||||
directive: function() {}
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
var datemathMock = {
|
||||
parse: function() {}
|
||||
};
|
||||
|
||||
var momentMock = {
|
||||
duration: function(num, str) {
|
||||
return 60;
|
||||
}
|
||||
};
|
||||
|
||||
// Mock Grafana modules that are not available outside of the core project
|
||||
// Required for loading module.js
|
||||
prunk.mock('./css/query-editor.css!', 'no css, dude.');
|
||||
prunk.mock('app/plugins/sdk', {
|
||||
QueryCtrl: null
|
||||
});
|
||||
prunk.mock('app/core/utils/datemath', datemathMock);
|
||||
prunk.mock('moment', momentMock);
|
||||
prunk.mock('angular', angularMocks);
|
||||
prunk.mock('jquery', 'module not found');
|
||||
|
||||
// Setup jsdom
|
||||
// Required for loading angularjs
|
||||
global.document = jsdom('<html><head><script></script></head><body></body></html>');
|
||||
global.window = global.document.parentWindow;
|
||||
global.navigator = window.navigator = {};
|
||||
global.Node = window.Node;
|
||||
|
||||
// Setup Chai
|
||||
chai.should();
|
||||
chai.use(sinonChai);
|
||||
global.assert = chai.assert;
|
||||
global.expect = chai.expect;
|
||||
Reference in New Issue
Block a user