2015-10-01 17:00:41 +02:00
|
|
|
///<amd-dependency path="app/plugins/datasource/cloudwatch/datasource" />
|
|
|
|
|
///<amd-dependency path="test/specs/helpers" name="helpers" />
|
|
|
|
|
|
|
|
|
|
import {describe, beforeEach, it, sinon, expect, angularMocks} from 'test/lib/common';
|
2015-10-26 15:47:34 +09:00
|
|
|
import moment = require('moment');
|
2015-10-01 17:00:41 +02:00
|
|
|
|
|
|
|
|
declare var helpers: any;
|
|
|
|
|
|
|
|
|
|
describe('CloudWatchDatasource', function() {
|
|
|
|
|
var ctx = new helpers.ServiceTestContext();
|
|
|
|
|
|
2015-10-30 15:58:20 +01:00
|
|
|
beforeEach(angularMocks.module('grafana.core'));
|
2015-10-01 17:00:41 +02:00
|
|
|
beforeEach(angularMocks.module('grafana.services'));
|
|
|
|
|
beforeEach(angularMocks.module('grafana.controllers'));
|
2015-10-30 15:58:20 +01:00
|
|
|
|
2015-10-02 09:04:46 +02:00
|
|
|
beforeEach(ctx.providePhase(['templateSrv', 'backendSrv']));
|
2015-10-01 17:00:41 +02:00
|
|
|
beforeEach(ctx.createService('CloudWatchDatasource'));
|
2015-10-30 15:58:20 +01:00
|
|
|
|
2015-10-01 17:00:41 +02:00
|
|
|
beforeEach(function() {
|
|
|
|
|
ctx.ds = new ctx.service({
|
|
|
|
|
jsonData: {
|
|
|
|
|
defaultRegion: 'us-east-1',
|
|
|
|
|
access: 'proxy'
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('When performing CloudWatch query', function() {
|
|
|
|
|
var requestParams;
|
|
|
|
|
|
|
|
|
|
var query = {
|
|
|
|
|
range: { from: 'now-1h', to: 'now' },
|
|
|
|
|
targets: [
|
|
|
|
|
{
|
|
|
|
|
region: 'us-east-1',
|
|
|
|
|
namespace: 'AWS/EC2',
|
|
|
|
|
metricName: 'CPUUtilization',
|
|
|
|
|
dimensions: {
|
|
|
|
|
InstanceId: 'i-12345678'
|
|
|
|
|
},
|
2015-10-08 13:09:15 +02:00
|
|
|
statistics: ['Average'],
|
2015-10-01 17:00:41 +02:00
|
|
|
period: 300
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var response = {
|
|
|
|
|
Datapoints: [
|
|
|
|
|
{
|
|
|
|
|
Average: 1,
|
|
|
|
|
Timestamp: 'Wed Dec 31 1969 16:00:00 GMT-0800 (PST)'
|
2015-11-14 17:43:44 +09:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
Average: 2,
|
|
|
|
|
Timestamp: 'Wed Dec 31 1969 16:05:00 GMT-0800 (PST)'
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
Average: 5,
|
2015-11-20 13:06:25 +09:00
|
|
|
Timestamp: 'Wed Dec 31 1969 16:15:00 GMT-0800 (PST)'
|
2015-10-01 17:00:41 +02:00
|
|
|
}
|
|
|
|
|
],
|
|
|
|
|
Label: 'CPUUtilization'
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
beforeEach(function() {
|
2015-10-02 09:04:46 +02:00
|
|
|
ctx.backendSrv.datasourceRequest = function(params) {
|
|
|
|
|
requestParams = params;
|
|
|
|
|
return ctx.$q.when({data: response});
|
2015-10-01 17:00:41 +02:00
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should generate the correct query', function(done) {
|
|
|
|
|
ctx.ds.query(query).then(function() {
|
2015-10-02 09:04:46 +02:00
|
|
|
var params = requestParams.data.parameters;
|
|
|
|
|
expect(params.namespace).to.be(query.targets[0].namespace);
|
|
|
|
|
expect(params.metricName).to.be(query.targets[0].metricName);
|
|
|
|
|
expect(params.dimensions[0].Name).to.be(Object.keys(query.targets[0].dimensions)[0]);
|
|
|
|
|
expect(params.dimensions[0].Value).to.be(query.targets[0].dimensions[Object.keys(query.targets[0].dimensions)[0]]);
|
2015-10-08 13:09:15 +02:00
|
|
|
expect(params.statistics).to.eql(query.targets[0].statistics);
|
2015-10-02 09:04:46 +02:00
|
|
|
expect(params.period).to.be(query.targets[0].period);
|
2015-10-01 17:00:41 +02:00
|
|
|
done();
|
|
|
|
|
});
|
|
|
|
|
ctx.$rootScope.$apply();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should return series list', function(done) {
|
|
|
|
|
ctx.ds.query(query).then(function(result) {
|
2015-10-08 13:09:15 +02:00
|
|
|
expect(result.data[0].target).to.be('CPUUtilization_Average');
|
|
|
|
|
expect(result.data[0].datapoints[0][0]).to.be(response.Datapoints[0]['Average']);
|
2015-10-01 17:00:41 +02:00
|
|
|
done();
|
|
|
|
|
});
|
|
|
|
|
ctx.$rootScope.$apply();
|
|
|
|
|
});
|
2015-11-14 17:43:44 +09:00
|
|
|
|
|
|
|
|
it('should return null for missing data point', function(done) {
|
|
|
|
|
ctx.ds.query(query).then(function(result) {
|
|
|
|
|
expect(result.data[0].datapoints[2][0]).to.be(null);
|
|
|
|
|
done();
|
|
|
|
|
});
|
|
|
|
|
ctx.$rootScope.$apply();
|
|
|
|
|
});
|
2015-10-01 17:00:41 +02:00
|
|
|
});
|
|
|
|
|
|
2015-10-02 11:54:35 +02:00
|
|
|
function describeMetricFindQuery(query, func) {
|
|
|
|
|
describe('metricFindQuery ' + query, () => {
|
|
|
|
|
let scenario: any = {};
|
|
|
|
|
scenario.setup = setupCallback => {
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
setupCallback();
|
|
|
|
|
ctx.backendSrv.datasourceRequest = args => {
|
|
|
|
|
scenario.request = args;
|
|
|
|
|
return ctx.$q.when({data: scenario.requestResponse });
|
|
|
|
|
};
|
|
|
|
|
ctx.ds.metricFindQuery(query).then(args => {
|
|
|
|
|
scenario.result = args;
|
|
|
|
|
});
|
|
|
|
|
ctx.$rootScope.$apply();
|
|
|
|
|
});
|
2015-10-01 17:00:41 +02:00
|
|
|
};
|
2015-10-02 11:54:35 +02:00
|
|
|
|
|
|
|
|
func(scenario);
|
2015-10-01 17:00:41 +02:00
|
|
|
});
|
2015-10-02 11:54:35 +02:00
|
|
|
}
|
2015-10-01 17:00:41 +02:00
|
|
|
|
2015-10-02 11:54:35 +02:00
|
|
|
describeMetricFindQuery('regions()', scenario => {
|
|
|
|
|
scenario.setup(() => {
|
|
|
|
|
scenario.requestResponse = [{text: 'us-east-1'}];
|
|
|
|
|
});
|
2015-10-02 11:10:21 +02:00
|
|
|
|
2015-10-02 11:54:35 +02:00
|
|
|
it('should call __GetRegions and return result', () => {
|
|
|
|
|
expect(scenario.result[0].text).to.contain('us-east-1');
|
|
|
|
|
expect(scenario.request.data.action).to.be('__GetRegions');
|
2015-10-01 17:00:41 +02:00
|
|
|
});
|
2015-10-02 11:54:35 +02:00
|
|
|
});
|
2015-10-01 17:00:41 +02:00
|
|
|
|
2015-10-02 11:54:35 +02:00
|
|
|
describeMetricFindQuery('namespaces()', scenario => {
|
|
|
|
|
scenario.setup(() => {
|
|
|
|
|
scenario.requestResponse = [{text: 'AWS/EC2'}];
|
2015-10-01 17:00:41 +02:00
|
|
|
});
|
|
|
|
|
|
2015-10-02 11:54:35 +02:00
|
|
|
it('should call __GetNamespaces and return result', () => {
|
|
|
|
|
expect(scenario.result[0].text).to.contain('AWS/EC2');
|
|
|
|
|
expect(scenario.request.data.action).to.be('__GetNamespaces');
|
2015-10-01 17:00:41 +02:00
|
|
|
});
|
2015-10-02 11:54:35 +02:00
|
|
|
});
|
2015-10-01 17:00:41 +02:00
|
|
|
|
2015-10-02 11:54:35 +02:00
|
|
|
describeMetricFindQuery('metrics(AWS/EC2)', scenario => {
|
|
|
|
|
scenario.setup(() => {
|
|
|
|
|
scenario.requestResponse = [{text: 'CPUUtilization'}];
|
2015-10-01 17:00:41 +02:00
|
|
|
});
|
|
|
|
|
|
2015-10-02 11:54:35 +02:00
|
|
|
it('should call __GetMetrics and return result', () => {
|
|
|
|
|
expect(scenario.result[0].text).to.be('CPUUtilization');
|
|
|
|
|
expect(scenario.request.data.action).to.be('__GetMetrics');
|
2015-10-01 17:00:41 +02:00
|
|
|
});
|
|
|
|
|
});
|
2015-10-02 11:54:35 +02:00
|
|
|
|
2015-10-02 20:25:28 +02:00
|
|
|
describeMetricFindQuery('dimension_keys(AWS/EC2)', scenario => {
|
|
|
|
|
scenario.setup(() => {
|
|
|
|
|
scenario.requestResponse = [{text: 'InstanceId'}];
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should call __GetDimensions and return result', () => {
|
|
|
|
|
expect(scenario.result[0].text).to.be('InstanceId');
|
|
|
|
|
expect(scenario.request.data.action).to.be('__GetDimensions');
|
|
|
|
|
});
|
2015-10-02 11:54:35 +02:00
|
|
|
});
|
2015-10-02 20:25:28 +02:00
|
|
|
|
2015-11-14 19:54:24 +09:00
|
|
|
describeMetricFindQuery('dimension_values(us-east-1,AWS/EC2,CPUUtilization,InstanceId)', scenario => {
|
2015-10-02 20:25:28 +02:00
|
|
|
scenario.setup(() => {
|
|
|
|
|
scenario.requestResponse = {
|
|
|
|
|
Metrics: [
|
|
|
|
|
{
|
|
|
|
|
Namespace: 'AWS/EC2',
|
|
|
|
|
MetricName: 'CPUUtilization',
|
|
|
|
|
Dimensions: [
|
|
|
|
|
{
|
|
|
|
|
Name: 'InstanceId',
|
|
|
|
|
Value: 'i-12345678'
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
|
2015-10-08 13:09:15 +02:00
|
|
|
it('should call __ListMetrics and return result', () => {
|
2015-10-02 20:25:28 +02:00
|
|
|
expect(scenario.result[0].text).to.be('i-12345678');
|
|
|
|
|
expect(scenario.request.data.action).to.be('ListMetrics');
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2015-10-26 15:47:34 +09:00
|
|
|
describe('When performing annotationQuery', function() {
|
2015-11-13 22:09:00 +09:00
|
|
|
var parameter = {
|
|
|
|
|
annotation: {
|
|
|
|
|
region: 'us-east-1',
|
|
|
|
|
namespace: 'AWS/EC2',
|
|
|
|
|
metricName: 'CPUUtilization',
|
2015-11-30 01:32:29 +09:00
|
|
|
dimensions: {
|
|
|
|
|
InstanceId: 'i-12345678'
|
|
|
|
|
},
|
|
|
|
|
statistics: ['Average'],
|
|
|
|
|
period: 300
|
2015-11-13 22:09:00 +09:00
|
|
|
},
|
|
|
|
|
range: {
|
|
|
|
|
from: moment(1443438674760),
|
|
|
|
|
to: moment(1443460274760)
|
|
|
|
|
}
|
2015-10-26 15:47:34 +09:00
|
|
|
};
|
|
|
|
|
var alarmResponse = {
|
|
|
|
|
MetricAlarms: [
|
|
|
|
|
{
|
|
|
|
|
AlarmName: 'test_alarm_name'
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
};
|
|
|
|
|
var historyResponse = {
|
|
|
|
|
AlarmHistoryItems: [
|
|
|
|
|
{
|
|
|
|
|
Timestamp: '2015-01-01T00:00:00.000Z',
|
|
|
|
|
HistoryItemType: 'StateUpdate',
|
|
|
|
|
AlarmName: 'test_alarm_name',
|
|
|
|
|
HistoryData: '{}',
|
|
|
|
|
HistorySummary: 'test_history_summary'
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
};
|
|
|
|
|
beforeEach(function() {
|
|
|
|
|
ctx.backendSrv.datasourceRequest = function(params) {
|
|
|
|
|
switch (params.data.action) {
|
|
|
|
|
case 'DescribeAlarmsForMetric':
|
|
|
|
|
return ctx.$q.when({data: alarmResponse});
|
|
|
|
|
break;
|
|
|
|
|
case 'DescribeAlarmHistory':
|
|
|
|
|
return ctx.$q.when({data: historyResponse});
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
it('should return annotation list', function(done) {
|
2015-11-13 22:09:00 +09:00
|
|
|
ctx.ds.annotationQuery(parameter).then(function(result) {
|
2015-10-26 15:47:34 +09:00
|
|
|
expect(result[0].title).to.be('test_alarm_name');
|
|
|
|
|
expect(result[0].text).to.be('test_history_summary');
|
|
|
|
|
done();
|
|
|
|
|
});
|
|
|
|
|
ctx.$rootScope.$apply();
|
|
|
|
|
});
|
|
|
|
|
});
|
2015-10-01 17:00:41 +02:00
|
|
|
});
|