Files
grafana/public/app/plugins/datasource/cloudwatch/specs/datasource.jest.ts

454 lines
13 KiB
TypeScript
Raw Normal View History

2017-12-20 12:33:33 +01:00
import '../datasource';
import CloudWatchDatasource from '../datasource';
2018-07-04 11:16:31 +02:00
import * as dateMath from 'app/core/utils/datemath';
import _ from 'lodash';
2017-12-20 12:33:33 +01:00
describe('CloudWatchDatasource', function() {
2018-07-03 13:20:36 +02:00
let instanceSettings = {
2017-12-20 12:33:33 +01:00
jsonData: { defaultRegion: 'us-east-1', access: 'proxy' },
};
let templateSrv = {
2018-07-09 15:42:34 +02:00
data: {},
templateSettings: { interpolate: /\[\[([\s\S]+?)\]\]/g },
2018-07-09 15:42:34 +02:00
replace: text => _.template(text, templateSrv.templateSettings)(templateSrv.data),
variableExists: () => false,
};
2018-07-04 11:16:31 +02:00
let timeSrv = {
time: { from: 'now-1h', to: 'now' },
2018-07-09 15:42:34 +02:00
timeRange: () => {
2018-07-04 11:16:31 +02:00
return {
from: dateMath.parse(timeSrv.time.from, false),
to: dateMath.parse(timeSrv.time.to, true),
};
2018-07-09 15:42:34 +02:00
},
2018-07-04 11:16:31 +02:00
};
2018-07-03 13:20:36 +02:00
let backendSrv = {};
let ctx = <any>{
backendSrv,
2018-07-03 13:21:59 +02:00
templateSrv,
2018-07-03 13:20:36 +02:00
};
2018-07-03 13:20:36 +02:00
beforeEach(() => {
ctx.ds = new CloudWatchDatasource(instanceSettings, {}, backendSrv, templateSrv, timeSrv);
});
2017-12-20 12:33:33 +01:00
describe('When performing CloudWatch query', function() {
var requestParams;
var query = {
2017-12-20 12:33:33 +01:00
range: { from: 'now-1h', to: 'now' },
2017-04-03 21:50:40 +09:00
rangeRaw: { from: 1483228800, to: 1483232400 },
targets: [
{
2017-12-20 12:33:33 +01:00
region: 'us-east-1',
namespace: 'AWS/EC2',
metricName: 'CPUUtilization',
dimensions: {
2017-12-20 12:33:33 +01:00
InstanceId: 'i-12345678',
},
2017-12-20 12:33:33 +01:00
statistics: ['Average'],
period: '300',
},
],
};
var response = {
2017-04-03 21:50:40 +09:00
timings: [null],
results: {
A: {
2017-12-20 12:33:33 +01:00
error: '',
refId: 'A',
2017-04-03 21:50:40 +09:00
series: [
{
2017-12-20 12:33:33 +01:00
name: 'CPUUtilization_Average',
points: [[1, 1483228800000], [2, 1483229100000], [5, 1483229700000]],
2017-04-03 21:50:40 +09:00
tags: {
2017-12-20 12:33:33 +01:00
InstanceId: 'i-12345678',
},
},
],
},
},
};
beforeEach(() => {
ctx.backendSrv.datasourceRequest = jest.fn(params => {
2017-11-12 00:41:14 +09:00
requestParams = params.data;
2018-07-03 13:20:36 +02:00
return Promise.resolve({ data: response });
});
});
2017-12-20 12:33:33 +01:00
it('should generate the correct query', function(done) {
ctx.ds.query(query).then(function() {
2017-04-03 21:50:40 +09:00
var params = requestParams.queries[0];
2018-07-03 13:20:36 +02:00
expect(params.namespace).toBe(query.targets[0].namespace);
expect(params.metricName).toBe(query.targets[0].metricName);
expect(params.dimensions['InstanceId']).toBe('i-12345678');
expect(params.statistics).toEqual(query.targets[0].statistics);
expect(params.period).toBe(query.targets[0].period);
done();
});
});
2017-12-20 12:33:33 +01:00
it('should generate the correct query with interval variable', function(done) {
ctx.templateSrv.data = {
2017-12-20 12:33:33 +01:00
period: '10m',
};
var query = {
2017-12-20 12:33:33 +01:00
range: { from: 'now-1h', to: 'now' },
2017-04-03 21:50:40 +09:00
rangeRaw: { from: 1483228800, to: 1483232400 },
targets: [
{
2017-12-20 12:33:33 +01:00
region: 'us-east-1',
namespace: 'AWS/EC2',
metricName: 'CPUUtilization',
dimensions: {
2017-12-20 12:33:33 +01:00
InstanceId: 'i-12345678',
},
2017-12-20 12:33:33 +01:00
statistics: ['Average'],
period: '[[period]]',
},
],
};
ctx.ds.query(query).then(function() {
2017-04-03 21:50:40 +09:00
var params = requestParams.queries[0];
2018-07-03 13:20:36 +02:00
expect(params.period).toBe('600');
done();
});
});
2017-12-20 12:33:33 +01:00
it('should return series list', function(done) {
ctx.ds.query(query).then(function(result) {
2018-07-03 13:20:36 +02:00
expect(result.data[0].target).toBe(response.results.A.series[0].name);
expect(result.data[0].datapoints[0][0]).toBe(response.results.A.series[0].points[0][0]);
done();
});
});
});
describe('When query region is "default"', function() {
it('should return the datasource region if empty or "default"', function() {
var defaultRegion = instanceSettings.jsonData.defaultRegion;
2018-07-03 13:20:36 +02:00
expect(ctx.ds.getActualRegion()).toBe(defaultRegion);
expect(ctx.ds.getActualRegion('')).toBe(defaultRegion);
expect(ctx.ds.getActualRegion('default')).toBe(defaultRegion);
});
2017-12-20 12:33:33 +01:00
it('should return the specified region if specified', function() {
2018-07-03 13:20:36 +02:00
expect(ctx.ds.getActualRegion('some-fake-region-1')).toBe('some-fake-region-1');
});
var requestParams;
beforeEach(function() {
2018-07-03 13:21:59 +02:00
ctx.ds.performTimeSeriesQuery = jest.fn(request => {
requestParams = request;
2018-07-03 13:20:36 +02:00
return Promise.resolve({ data: {} });
});
});
it('should query for the datasource region if empty or "default"', function(done) {
var query = {
2017-12-20 12:33:33 +01:00
range: { from: 'now-1h', to: 'now' },
rangeRaw: { from: 1483228800, to: 1483232400 },
targets: [
{
2017-12-20 12:33:33 +01:00
region: 'default',
namespace: 'AWS/EC2',
metricName: 'CPUUtilization',
dimensions: {
2017-12-20 12:33:33 +01:00
InstanceId: 'i-12345678',
},
2017-12-20 12:33:33 +01:00
statistics: ['Average'],
period: 300,
},
],
};
ctx.ds.query(query).then(function(result) {
2018-07-03 13:20:36 +02:00
expect(requestParams.queries[0].region).toBe(instanceSettings.jsonData.defaultRegion);
done();
});
});
});
2017-12-20 12:33:33 +01:00
describe('When performing CloudWatch query for extended statistics', function() {
var query = {
2017-12-20 12:33:33 +01:00
range: { from: 'now-1h', to: 'now' },
2017-04-03 21:50:40 +09:00
rangeRaw: { from: 1483228800, to: 1483232400 },
targets: [
{
2017-12-20 12:33:33 +01:00
region: 'us-east-1',
namespace: 'AWS/ApplicationELB',
metricName: 'TargetResponseTime',
dimensions: {
2017-12-20 12:33:33 +01:00
LoadBalancer: 'lb',
TargetGroup: 'tg',
},
2017-12-20 12:33:33 +01:00
statistics: ['p90.00'],
period: 300,
},
],
};
var response = {
2017-04-03 21:50:40 +09:00
timings: [null],
results: {
A: {
2017-12-20 12:33:33 +01:00
error: '',
refId: 'A',
2017-04-03 21:50:40 +09:00
series: [
{
2017-12-20 12:33:33 +01:00
name: 'TargetResponseTime_p90.00',
points: [[1, 1483228800000], [2, 1483229100000], [5, 1483229700000]],
2017-04-03 21:50:40 +09:00
tags: {
2017-12-20 12:33:33 +01:00
LoadBalancer: 'lb',
TargetGroup: 'tg',
},
},
],
},
},
};
beforeEach(function() {
2018-07-03 13:21:59 +02:00
ctx.backendSrv.datasourceRequest = jest.fn(params => {
2018-07-03 13:20:36 +02:00
return Promise.resolve({ data: response });
});
});
2017-12-20 12:33:33 +01:00
it('should return series list', function(done) {
ctx.ds.query(query).then(function(result) {
2018-07-03 13:20:36 +02:00
expect(result.data[0].target).toBe(response.results.A.series[0].name);
expect(result.data[0].datapoints[0][0]).toBe(response.results.A.series[0].points[0][0]);
done();
});
});
});
function describeMetricFindQuery(query, func) {
2017-12-20 12:33:33 +01:00
describe('metricFindQuery ' + query, () => {
let scenario: any = {};
scenario.setup = setupCallback => {
beforeEach(() => {
setupCallback();
2018-07-03 13:21:59 +02:00
ctx.backendSrv.datasourceRequest = jest.fn(args => {
2017-11-12 00:41:14 +09:00
scenario.request = args.data;
2018-07-03 13:20:36 +02:00
return Promise.resolve({ data: scenario.requestResponse });
});
ctx.ds.metricFindQuery(query).then(args => {
scenario.result = args;
});
});
};
func(scenario);
});
}
2017-12-20 12:33:33 +01:00
describeMetricFindQuery('regions()', scenario => {
scenario.setup(() => {
2017-09-13 21:11:25 +09:00
scenario.requestResponse = {
results: {
metricFindQuery: {
2017-12-20 12:33:33 +01:00
tables: [{ rows: [['us-east-1', 'us-east-1']] }],
},
},
2017-09-13 21:11:25 +09:00
};
});
2017-12-20 12:33:33 +01:00
it('should call __GetRegions and return result', () => {
2018-07-03 13:20:36 +02:00
expect(scenario.result[0].text).toContain('us-east-1');
expect(scenario.request.queries[0].type).toBe('metricFindQuery');
expect(scenario.request.queries[0].subtype).toBe('regions');
});
});
2017-12-20 12:33:33 +01:00
describeMetricFindQuery('namespaces()', scenario => {
scenario.setup(() => {
2017-09-13 21:11:25 +09:00
scenario.requestResponse = {
results: {
metricFindQuery: {
2017-12-20 12:33:33 +01:00
tables: [{ rows: [['AWS/EC2', 'AWS/EC2']] }],
},
},
2017-09-13 21:11:25 +09:00
};
});
2017-12-20 12:33:33 +01:00
it('should call __GetNamespaces and return result', () => {
2018-07-03 13:20:36 +02:00
expect(scenario.result[0].text).toContain('AWS/EC2');
expect(scenario.request.queries[0].type).toBe('metricFindQuery');
expect(scenario.request.queries[0].subtype).toBe('namespaces');
});
});
2017-12-20 12:33:33 +01:00
describeMetricFindQuery('metrics(AWS/EC2)', scenario => {
scenario.setup(() => {
2017-09-13 21:11:25 +09:00
scenario.requestResponse = {
results: {
metricFindQuery: {
2017-12-20 12:33:33 +01:00
tables: [{ rows: [['CPUUtilization', 'CPUUtilization']] }],
},
},
2017-09-13 21:11:25 +09:00
};
});
2017-12-20 12:33:33 +01:00
it('should call __GetMetrics and return result', () => {
2018-07-03 13:20:36 +02:00
expect(scenario.result[0].text).toBe('CPUUtilization');
expect(scenario.request.queries[0].type).toBe('metricFindQuery');
expect(scenario.request.queries[0].subtype).toBe('metrics');
});
});
2017-12-20 12:33:33 +01:00
describeMetricFindQuery('dimension_keys(AWS/EC2)', scenario => {
scenario.setup(() => {
2017-09-13 21:11:25 +09:00
scenario.requestResponse = {
results: {
metricFindQuery: {
2017-12-20 12:33:33 +01:00
tables: [{ rows: [['InstanceId', 'InstanceId']] }],
},
},
2017-09-13 21:11:25 +09:00
};
});
2017-12-20 12:33:33 +01:00
it('should call __GetDimensions and return result', () => {
2018-07-03 13:20:36 +02:00
expect(scenario.result[0].text).toBe('InstanceId');
expect(scenario.request.queries[0].type).toBe('metricFindQuery');
expect(scenario.request.queries[0].subtype).toBe('dimension_keys');
});
});
describeMetricFindQuery('dimension_values(us-east-1,AWS/EC2,CPUUtilization,InstanceId)', scenario => {
scenario.setup(() => {
scenario.requestResponse = {
results: {
metricFindQuery: {
tables: [{ rows: [['i-12345678', 'i-12345678']] }],
2017-12-20 12:33:33 +01:00
},
},
};
});
it('should call __ListMetrics and return result', () => {
2018-07-03 13:20:36 +02:00
expect(scenario.result[0].text).toContain('i-12345678');
expect(scenario.request.queries[0].type).toBe('metricFindQuery');
expect(scenario.request.queries[0].subtype).toBe('dimension_values');
});
});
describeMetricFindQuery('dimension_values(default,AWS/EC2,CPUUtilization,InstanceId)', scenario => {
scenario.setup(() => {
scenario.requestResponse = {
results: {
metricFindQuery: {
tables: [{ rows: [['i-12345678', 'i-12345678']] }],
2017-12-20 12:33:33 +01:00
},
},
};
});
it('should call __ListMetrics and return result', () => {
2018-07-03 13:20:36 +02:00
expect(scenario.result[0].text).toContain('i-12345678');
expect(scenario.request.queries[0].type).toBe('metricFindQuery');
expect(scenario.request.queries[0].subtype).toBe('dimension_values');
});
});
2017-12-20 12:33:33 +01:00
it('should caclculate the correct period', function() {
var hourSec = 60 * 60;
var daySec = hourSec * 24;
2017-08-16 16:40:46 +09:00
var start = 1483196400 * 1000;
var testData: any[] = [
2017-08-16 16:40:46 +09:00
[
2017-12-20 12:33:33 +01:00
{ period: 60, namespace: 'AWS/EC2' },
2017-08-16 16:40:46 +09:00
{ range: { from: new Date(start), to: new Date(start + 3600 * 1000) } },
hourSec * 3,
2017-12-20 12:33:33 +01:00
60,
2017-08-16 16:40:46 +09:00
],
[
2017-12-20 12:33:33 +01:00
{ period: null, namespace: 'AWS/EC2' },
2017-08-16 16:40:46 +09:00
{ range: { from: new Date(start), to: new Date(start + 3600 * 1000) } },
hourSec * 3,
2017-12-20 12:33:33 +01:00
300,
2017-08-16 16:40:46 +09:00
],
[
2017-12-20 12:33:33 +01:00
{ period: 60, namespace: 'AWS/ELB' },
2017-08-16 16:40:46 +09:00
{ range: { from: new Date(start), to: new Date(start + 3600 * 1000) } },
hourSec * 3,
2017-12-20 12:33:33 +01:00
60,
2017-08-16 16:40:46 +09:00
],
[
2017-12-20 12:33:33 +01:00
{ period: null, namespace: 'AWS/ELB' },
2017-08-16 16:40:46 +09:00
{ range: { from: new Date(start), to: new Date(start + 3600 * 1000) } },
hourSec * 3,
2017-12-20 12:33:33 +01:00
60,
2017-08-16 16:40:46 +09:00
],
[
2017-12-20 12:33:33 +01:00
{ period: 1, namespace: 'CustomMetricsNamespace' },
{
range: {
from: new Date(start),
2017-12-20 12:33:33 +01:00
to: new Date(start + (1440 - 1) * 1000),
},
},
hourSec * 3 - 1,
2017-12-20 12:33:33 +01:00
1,
2017-08-16 16:40:46 +09:00
],
[
2017-12-20 12:33:33 +01:00
{ period: 1, namespace: 'CustomMetricsNamespace' },
2017-08-16 16:40:46 +09:00
{ range: { from: new Date(start), to: new Date(start + 3600 * 1000) } },
hourSec * 3 - 1,
2017-12-20 12:33:33 +01:00
60,
2017-08-16 16:40:46 +09:00
],
[
2017-12-20 12:33:33 +01:00
{ period: 60, namespace: 'CustomMetricsNamespace' },
2017-08-16 16:40:46 +09:00
{ range: { from: new Date(start), to: new Date(start + 3600 * 1000) } },
hourSec * 3,
2017-12-20 12:33:33 +01:00
60,
2017-08-16 16:40:46 +09:00
],
[
2017-12-20 12:33:33 +01:00
{ period: null, namespace: 'CustomMetricsNamespace' },
2017-08-16 16:40:46 +09:00
{ range: { from: new Date(start), to: new Date(start + 3600 * 1000) } },
hourSec * 3 - 1,
2017-12-20 12:33:33 +01:00
60,
2017-08-16 16:40:46 +09:00
],
[
2017-12-20 12:33:33 +01:00
{ period: null, namespace: 'CustomMetricsNamespace' },
2017-08-16 16:40:46 +09:00
{ range: { from: new Date(start), to: new Date(start + 3600 * 1000) } },
hourSec * 3,
2017-12-20 12:33:33 +01:00
60,
2017-08-16 16:40:46 +09:00
],
[
2017-12-20 12:33:33 +01:00
{ period: null, namespace: 'CustomMetricsNamespace' },
2017-08-16 16:40:46 +09:00
{ range: { from: new Date(start), to: new Date(start + 3600 * 1000) } },
daySec * 15,
2017-12-20 12:33:33 +01:00
60,
2017-08-16 16:40:46 +09:00
],
[
2017-12-20 12:33:33 +01:00
{ period: null, namespace: 'CustomMetricsNamespace' },
2017-08-16 16:40:46 +09:00
{ range: { from: new Date(start), to: new Date(start + 3600 * 1000) } },
daySec * 63,
2017-12-20 12:33:33 +01:00
300,
2017-08-16 16:40:46 +09:00
],
[
2017-12-20 12:33:33 +01:00
{ period: null, namespace: 'CustomMetricsNamespace' },
2017-08-16 16:40:46 +09:00
{ range: { from: new Date(start), to: new Date(start + 3600 * 1000) } },
daySec * 455,
2017-12-20 12:33:33 +01:00
3600,
],
];
for (let t of testData) {
let target = t[0];
2017-08-16 16:40:46 +09:00
let options = t[1];
let now = new Date(options.range.from.valueOf() + t[2] * 1000);
let expected = t[3];
let actual = ctx.ds.getPeriod(target, options, now);
2018-07-03 13:20:36 +02:00
expect(actual).toBe(expected);
}
});
});