datasource_specs.ts 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. import "../datasource";
  2. import {describe, beforeEach, it, sinon, expect, angularMocks} from 'test/lib/common';
  3. import moment from 'moment';
  4. import helpers from 'test/specs/helpers';
  5. import {CloudWatchDatasource} from "../datasource";
  6. describe('CloudWatchDatasource', function() {
  7. var ctx = new helpers.ServiceTestContext();
  8. var instanceSettings = {
  9. jsonData: {defaultRegion: 'us-east-1', access: 'proxy'},
  10. };
  11. beforeEach(angularMocks.module('grafana.core'));
  12. beforeEach(angularMocks.module('grafana.services'));
  13. beforeEach(angularMocks.module('grafana.controllers'));
  14. beforeEach(ctx.providePhase(['templateSrv', 'backendSrv']));
  15. beforeEach(angularMocks.inject(function($q, $rootScope, $httpBackend, $injector) {
  16. ctx.$q = $q;
  17. ctx.$httpBackend = $httpBackend;
  18. ctx.$rootScope = $rootScope;
  19. ctx.ds = $injector.instantiate(CloudWatchDatasource, {instanceSettings: instanceSettings});
  20. $httpBackend.when('GET', /\.html$/).respond('');
  21. }));
  22. describe('When performing CloudWatch query', function() {
  23. var requestParams;
  24. var query = {
  25. range: { from: 'now-1h', to: 'now' },
  26. targets: [
  27. {
  28. region: 'us-east-1',
  29. namespace: 'AWS/EC2',
  30. metricName: 'CPUUtilization',
  31. dimensions: {
  32. InstanceId: 'i-12345678'
  33. },
  34. statistics: ['Average'],
  35. period: 300
  36. }
  37. ]
  38. };
  39. var response = {
  40. Datapoints: [
  41. {
  42. Average: 1,
  43. Timestamp: 'Wed Dec 31 1969 16:00:00 GMT-0800 (PST)'
  44. },
  45. {
  46. Average: 2,
  47. Timestamp: 'Wed Dec 31 1969 16:05:00 GMT-0800 (PST)'
  48. },
  49. {
  50. Average: 5,
  51. Timestamp: 'Wed Dec 31 1969 16:15:00 GMT-0800 (PST)'
  52. }
  53. ],
  54. Label: 'CPUUtilization'
  55. };
  56. beforeEach(function() {
  57. ctx.backendSrv.datasourceRequest = function(params) {
  58. requestParams = params;
  59. return ctx.$q.when({data: response});
  60. };
  61. });
  62. it('should generate the correct query', function(done) {
  63. ctx.ds.query(query).then(function() {
  64. var params = requestParams.data.parameters;
  65. expect(params.namespace).to.be(query.targets[0].namespace);
  66. expect(params.metricName).to.be(query.targets[0].metricName);
  67. expect(params.dimensions[0].Name).to.be(Object.keys(query.targets[0].dimensions)[0]);
  68. expect(params.dimensions[0].Value).to.be(query.targets[0].dimensions[Object.keys(query.targets[0].dimensions)[0]]);
  69. expect(params.statistics).to.eql(query.targets[0].statistics);
  70. expect(params.period).to.be(query.targets[0].period);
  71. done();
  72. });
  73. ctx.$rootScope.$apply();
  74. });
  75. it('should generate the correct query with interval variable', function(done) {
  76. ctx.templateSrv.data = {
  77. period: '10m'
  78. };
  79. var query = {
  80. range: { from: 'now-1h', to: 'now' },
  81. targets: [
  82. {
  83. region: 'us-east-1',
  84. namespace: 'AWS/EC2',
  85. metricName: 'CPUUtilization',
  86. dimensions: {
  87. InstanceId: 'i-12345678'
  88. },
  89. statistics: ['Average'],
  90. period: '[[period]]'
  91. }
  92. ]
  93. };
  94. ctx.ds.query(query).then(function() {
  95. var params = requestParams.data.parameters;
  96. expect(params.period).to.be(600);
  97. done();
  98. });
  99. ctx.$rootScope.$apply();
  100. });
  101. it('should return series list', function(done) {
  102. ctx.ds.query(query).then(function(result) {
  103. expect(result.data[0].target).to.be('CPUUtilization_Average');
  104. expect(result.data[0].datapoints[0][0]).to.be(response.Datapoints[0]['Average']);
  105. done();
  106. });
  107. ctx.$rootScope.$apply();
  108. });
  109. it('should return null for missing data point', function(done) {
  110. ctx.ds.query(query).then(function(result) {
  111. expect(result.data[0].datapoints[2][0]).to.be(null);
  112. done();
  113. });
  114. ctx.$rootScope.$apply();
  115. });
  116. it('should generate the correct targets by expanding template variables', function() {
  117. var templateSrv = {
  118. variables: [
  119. {
  120. name: 'instance_id',
  121. options: [
  122. { text: 'i-23456789', value: 'i-23456789', selected: false },
  123. { text: 'i-34567890', value: 'i-34567890', selected: true }
  124. ]
  125. }
  126. ],
  127. replace: function (target, scopedVars) {
  128. if (target === '$instance_id' && scopedVars['instance_id']['text'] === 'i-34567890') {
  129. return 'i-34567890';
  130. } else {
  131. return '';
  132. }
  133. },
  134. getVariableName: function (e) { return 'instance_id'; },
  135. variableExists: function (e) { return true; },
  136. containsVariable: function (str, variableName) { return str.indexOf('$' + variableName) !== -1; }
  137. };
  138. var targets = [
  139. {
  140. region: 'us-east-1',
  141. namespace: 'AWS/EC2',
  142. metricName: 'CPUUtilization',
  143. dimensions: {
  144. InstanceId: '$instance_id'
  145. },
  146. statistics: ['Average'],
  147. period: 300
  148. }
  149. ];
  150. var result = ctx.ds.expandTemplateVariable(targets, {}, templateSrv);
  151. expect(result[0].dimensions.InstanceId).to.be('i-34567890');
  152. });
  153. });
  154. describe('When performing CloudWatch query for extended statistics', function() {
  155. var requestParams;
  156. var query = {
  157. range: { from: 'now-1h', to: 'now' },
  158. targets: [
  159. {
  160. region: 'us-east-1',
  161. namespace: 'AWS/ApplicationELB',
  162. metricName: 'TargetResponseTime',
  163. dimensions: {
  164. LoadBalancer: 'lb',
  165. TargetGroup: 'tg'
  166. },
  167. statistics: ['p90.00'],
  168. period: 300
  169. }
  170. ]
  171. };
  172. var response = {
  173. Datapoints: [
  174. {
  175. ExtendedStatistics: {
  176. 'p90.00': 1
  177. },
  178. Timestamp: 'Wed Dec 31 1969 16:00:00 GMT-0800 (PST)'
  179. },
  180. {
  181. ExtendedStatistics: {
  182. 'p90.00': 2
  183. },
  184. Timestamp: 'Wed Dec 31 1969 16:05:00 GMT-0800 (PST)'
  185. },
  186. {
  187. ExtendedStatistics: {
  188. 'p90.00': 5
  189. },
  190. Timestamp: 'Wed Dec 31 1969 16:15:00 GMT-0800 (PST)'
  191. }
  192. ],
  193. Label: 'TargetResponseTime'
  194. };
  195. beforeEach(function() {
  196. ctx.backendSrv.datasourceRequest = function(params) {
  197. requestParams = params;
  198. return ctx.$q.when({data: response});
  199. };
  200. });
  201. it('should return series list', function(done) {
  202. ctx.ds.query(query).then(function(result) {
  203. expect(result.data[0].target).to.be('TargetResponseTime_p90.00');
  204. expect(result.data[0].datapoints[0][0]).to.be(response.Datapoints[0].ExtendedStatistics['p90.00']);
  205. done();
  206. });
  207. ctx.$rootScope.$apply();
  208. });
  209. });
  210. function describeMetricFindQuery(query, func) {
  211. describe('metricFindQuery ' + query, () => {
  212. let scenario: any = {};
  213. scenario.setup = setupCallback => {
  214. beforeEach(() => {
  215. setupCallback();
  216. ctx.backendSrv.datasourceRequest = args => {
  217. scenario.request = args;
  218. return ctx.$q.when({data: scenario.requestResponse });
  219. };
  220. ctx.ds.metricFindQuery(query).then(args => {
  221. scenario.result = args;
  222. });
  223. ctx.$rootScope.$apply();
  224. });
  225. };
  226. func(scenario);
  227. });
  228. }
  229. describeMetricFindQuery('regions()', scenario => {
  230. scenario.setup(() => {
  231. scenario.requestResponse = [{text: 'us-east-1'}];
  232. });
  233. it('should call __GetRegions and return result', () => {
  234. expect(scenario.result[0].text).to.contain('us-east-1');
  235. expect(scenario.request.data.action).to.be('__GetRegions');
  236. });
  237. });
  238. describeMetricFindQuery('namespaces()', scenario => {
  239. scenario.setup(() => {
  240. scenario.requestResponse = [{text: 'AWS/EC2'}];
  241. });
  242. it('should call __GetNamespaces and return result', () => {
  243. expect(scenario.result[0].text).to.contain('AWS/EC2');
  244. expect(scenario.request.data.action).to.be('__GetNamespaces');
  245. });
  246. });
  247. describeMetricFindQuery('metrics(AWS/EC2)', scenario => {
  248. scenario.setup(() => {
  249. scenario.requestResponse = [{text: 'CPUUtilization'}];
  250. });
  251. it('should call __GetMetrics and return result', () => {
  252. expect(scenario.result[0].text).to.be('CPUUtilization');
  253. expect(scenario.request.data.action).to.be('__GetMetrics');
  254. });
  255. });
  256. describeMetricFindQuery('dimension_keys(AWS/EC2)', scenario => {
  257. scenario.setup(() => {
  258. scenario.requestResponse = [{text: 'InstanceId'}];
  259. });
  260. it('should call __GetDimensions and return result', () => {
  261. expect(scenario.result[0].text).to.be('InstanceId');
  262. expect(scenario.request.data.action).to.be('__GetDimensions');
  263. });
  264. });
  265. describeMetricFindQuery('dimension_values(us-east-1,AWS/EC2,CPUUtilization,InstanceId)', scenario => {
  266. scenario.setup(() => {
  267. scenario.requestResponse = {
  268. Metrics: [
  269. {
  270. Namespace: 'AWS/EC2',
  271. MetricName: 'CPUUtilization',
  272. Dimensions: [
  273. {
  274. Name: 'InstanceId',
  275. Value: 'i-12345678'
  276. }
  277. ]
  278. }
  279. ]
  280. };
  281. });
  282. it('should call __ListMetrics and return result', () => {
  283. expect(scenario.result[0].text).to.be('i-12345678');
  284. expect(scenario.request.data.action).to.be('ListMetrics');
  285. });
  286. });
  287. });