datasource_specs.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. import "../datasource";
  2. import {describe, beforeEach, it, expect, angularMocks} from 'test/lib/common';
  3. import helpers from 'test/specs/helpers';
  4. import CloudWatchDatasource from "../datasource";
  5. describe('CloudWatchDatasource', function() {
  6. var ctx = new helpers.ServiceTestContext();
  7. var instanceSettings = {
  8. jsonData: {defaultRegion: 'us-east-1', access: 'proxy'},
  9. };
  10. beforeEach(angularMocks.module('grafana.core'));
  11. beforeEach(angularMocks.module('grafana.services'));
  12. beforeEach(angularMocks.module('grafana.controllers'));
  13. beforeEach(ctx.providePhase(['templateSrv', 'backendSrv']));
  14. beforeEach(angularMocks.inject(function($q, $rootScope, $httpBackend, $injector) {
  15. ctx.$q = $q;
  16. ctx.$httpBackend = $httpBackend;
  17. ctx.$rootScope = $rootScope;
  18. ctx.ds = $injector.instantiate(CloudWatchDatasource, {instanceSettings: instanceSettings});
  19. $httpBackend.when('GET', /\.html$/).respond('');
  20. }));
  21. describe('When performing CloudWatch query', function() {
  22. var requestParams;
  23. var query = {
  24. range: { from: 'now-1h', to: 'now' },
  25. rangeRaw: { from: 1483228800, to: 1483232400 },
  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. timings: [null],
  41. results: {
  42. A: {
  43. error: '',
  44. refId: 'A',
  45. series: [
  46. {
  47. name: 'CPUUtilization_Average',
  48. points: [
  49. [1, 1483228800000],
  50. [2, 1483229100000],
  51. [5, 1483229700000],
  52. ],
  53. tags: {
  54. InstanceId: 'i-12345678'
  55. }
  56. }
  57. ]
  58. }
  59. }
  60. };
  61. beforeEach(function() {
  62. ctx.backendSrv.datasourceRequest = function(params) {
  63. requestParams = params.data;
  64. return ctx.$q.when({data: response});
  65. };
  66. });
  67. it('should generate the correct query', function(done) {
  68. ctx.ds.query(query).then(function() {
  69. var params = requestParams.queries[0];
  70. expect(params.namespace).to.be(query.targets[0].namespace);
  71. expect(params.metricName).to.be(query.targets[0].metricName);
  72. expect(params.dimensions['InstanceId']).to.be('i-12345678');
  73. expect(params.statistics).to.eql(query.targets[0].statistics);
  74. expect(params.period).to.be(query.targets[0].period);
  75. done();
  76. });
  77. ctx.$rootScope.$apply();
  78. });
  79. it('should generate the correct query with interval variable', function(done) {
  80. ctx.templateSrv.data = {
  81. period: '10m'
  82. };
  83. var query = {
  84. range: { from: 'now-1h', to: 'now' },
  85. rangeRaw: { from: 1483228800, to: 1483232400 },
  86. targets: [
  87. {
  88. region: 'us-east-1',
  89. namespace: 'AWS/EC2',
  90. metricName: 'CPUUtilization',
  91. dimensions: {
  92. InstanceId: 'i-12345678'
  93. },
  94. statistics: ['Average'],
  95. period: '[[period]]'
  96. }
  97. ]
  98. };
  99. ctx.ds.query(query).then(function() {
  100. var params = requestParams.queries[0];
  101. expect(params.period).to.be('600');
  102. done();
  103. });
  104. ctx.$rootScope.$apply();
  105. });
  106. it('should return series list', function(done) {
  107. ctx.ds.query(query).then(function(result) {
  108. expect(result.data[0].target).to.be(response.results.A.series[0].name);
  109. expect(result.data[0].datapoints[0][0]).to.be(response.results.A.series[0].points[0][0]);
  110. done();
  111. });
  112. ctx.$rootScope.$apply();
  113. });
  114. it('should generate the correct targets by expanding template variables', function() {
  115. var templateSrv = {
  116. variables: [
  117. {
  118. name: 'instance_id',
  119. options: [
  120. { text: 'i-23456789', value: 'i-23456789', selected: false },
  121. { text: 'i-34567890', value: 'i-34567890', selected: true }
  122. ]
  123. }
  124. ],
  125. replace: function (target, scopedVars) {
  126. if (target === '$instance_id' && scopedVars['instance_id']['text'] === 'i-34567890') {
  127. return 'i-34567890';
  128. } else {
  129. return '';
  130. }
  131. },
  132. getVariableName: function (e) { return 'instance_id'; },
  133. variableExists: function (e) { return true; },
  134. containsVariable: function (str, variableName) { return str.indexOf('$' + variableName) !== -1; }
  135. };
  136. var targets = [
  137. {
  138. region: 'us-east-1',
  139. namespace: 'AWS/EC2',
  140. metricName: 'CPUUtilization',
  141. dimensions: {
  142. InstanceId: '$instance_id'
  143. },
  144. statistics: ['Average'],
  145. period: 300
  146. }
  147. ];
  148. var result = ctx.ds.expandTemplateVariable(targets, {}, templateSrv);
  149. expect(result[0].dimensions.InstanceId).to.be('i-34567890');
  150. });
  151. });
  152. describe('When query region is "default"', function () {
  153. it('should return the datasource region if empty or "default"', function() {
  154. var defaultRegion = instanceSettings.jsonData.defaultRegion;
  155. expect(ctx.ds.getActualRegion()).to.be(defaultRegion);
  156. expect(ctx.ds.getActualRegion('')).to.be(defaultRegion);
  157. expect(ctx.ds.getActualRegion("default")).to.be(defaultRegion);
  158. });
  159. it('should return the specified region if specified', function() {
  160. expect(ctx.ds.getActualRegion('some-fake-region-1')).to.be('some-fake-region-1');
  161. });
  162. var requestParams;
  163. beforeEach(function() {
  164. ctx.ds.performTimeSeriesQuery = function(request) {
  165. requestParams = request;
  166. return ctx.$q.when({data: {}});
  167. };
  168. });
  169. it('should query for the datasource region if empty or "default"', function(done) {
  170. var query = {
  171. range: { from: 'now-1h', to: 'now' },
  172. rangeRaw: { from: 1483228800, to: 1483232400 },
  173. targets: [
  174. {
  175. region: 'default',
  176. namespace: 'AWS/EC2',
  177. metricName: 'CPUUtilization',
  178. dimensions: {
  179. InstanceId: 'i-12345678'
  180. },
  181. statistics: ['Average'],
  182. period: 300
  183. }
  184. ]
  185. };
  186. ctx.ds.query(query).then(function(result) {
  187. expect(requestParams.queries[0].region).to.be(instanceSettings.jsonData.defaultRegion);
  188. done();
  189. });
  190. ctx.$rootScope.$apply();
  191. });
  192. });
  193. describe('When performing CloudWatch query for extended statistics', function() {
  194. var query = {
  195. range: { from: 'now-1h', to: 'now' },
  196. rangeRaw: { from: 1483228800, to: 1483232400 },
  197. targets: [
  198. {
  199. region: 'us-east-1',
  200. namespace: 'AWS/ApplicationELB',
  201. metricName: 'TargetResponseTime',
  202. dimensions: {
  203. LoadBalancer: 'lb',
  204. TargetGroup: 'tg'
  205. },
  206. statistics: ['p90.00'],
  207. period: 300
  208. }
  209. ]
  210. };
  211. var response = {
  212. timings: [null],
  213. results: {
  214. A: {
  215. error: '',
  216. refId: 'A',
  217. series: [
  218. {
  219. name: 'TargetResponseTime_p90.00',
  220. points: [
  221. [1, 1483228800000],
  222. [2, 1483229100000],
  223. [5, 1483229700000],
  224. ],
  225. tags: {
  226. LoadBalancer: 'lb',
  227. TargetGroup: 'tg'
  228. }
  229. }
  230. ]
  231. }
  232. }
  233. };
  234. beforeEach(function() {
  235. ctx.backendSrv.datasourceRequest = function(params) {
  236. return ctx.$q.when({data: response});
  237. };
  238. });
  239. it('should return series list', function(done) {
  240. ctx.ds.query(query).then(function(result) {
  241. expect(result.data[0].target).to.be(response.results.A.series[0].name);
  242. expect(result.data[0].datapoints[0][0]).to.be(response.results.A.series[0].points[0][0]);
  243. done();
  244. });
  245. ctx.$rootScope.$apply();
  246. });
  247. });
  248. function describeMetricFindQuery(query, func) {
  249. describe('metricFindQuery ' + query, () => {
  250. let scenario: any = {};
  251. scenario.setup = setupCallback => {
  252. beforeEach(() => {
  253. setupCallback();
  254. ctx.backendSrv.datasourceRequest = args => {
  255. scenario.request = args.data;
  256. return ctx.$q.when({data: scenario.requestResponse});
  257. };
  258. ctx.ds.metricFindQuery(query).then(args => {
  259. scenario.result = args;
  260. });
  261. ctx.$rootScope.$apply();
  262. });
  263. };
  264. func(scenario);
  265. });
  266. }
  267. describeMetricFindQuery('regions()', scenario => {
  268. scenario.setup(() => {
  269. scenario.requestResponse = {
  270. results: {
  271. metricFindQuery: {
  272. tables: [
  273. { rows: [['us-east-1', 'us-east-1']] }
  274. ]
  275. }
  276. }
  277. };
  278. });
  279. it('should call __GetRegions and return result', () => {
  280. expect(scenario.result[0].text).to.contain('us-east-1');
  281. expect(scenario.request.queries[0].type).to.be('metricFindQuery');
  282. expect(scenario.request.queries[0].subtype).to.be('regions');
  283. });
  284. });
  285. describeMetricFindQuery('namespaces()', scenario => {
  286. scenario.setup(() => {
  287. scenario.requestResponse = {
  288. results: {
  289. metricFindQuery: {
  290. tables: [
  291. { rows: [['AWS/EC2', 'AWS/EC2']] }
  292. ]
  293. }
  294. }
  295. };
  296. });
  297. it('should call __GetNamespaces and return result', () => {
  298. expect(scenario.result[0].text).to.contain('AWS/EC2');
  299. expect(scenario.request.queries[0].type).to.be('metricFindQuery');
  300. expect(scenario.request.queries[0].subtype).to.be('namespaces');
  301. });
  302. });
  303. describeMetricFindQuery('metrics(AWS/EC2)', scenario => {
  304. scenario.setup(() => {
  305. scenario.requestResponse = {
  306. results: {
  307. metricFindQuery: {
  308. tables: [
  309. { rows: [['CPUUtilization', 'CPUUtilization']] }
  310. ]
  311. }
  312. }
  313. };
  314. });
  315. it('should call __GetMetrics and return result', () => {
  316. expect(scenario.result[0].text).to.be('CPUUtilization');
  317. expect(scenario.request.queries[0].type).to.be('metricFindQuery');
  318. expect(scenario.request.queries[0].subtype).to.be('metrics');
  319. });
  320. });
  321. describeMetricFindQuery('dimension_keys(AWS/EC2)', scenario => {
  322. scenario.setup(() => {
  323. scenario.requestResponse = {
  324. results: {
  325. metricFindQuery: {
  326. tables: [
  327. { rows: [['InstanceId', 'InstanceId']] }
  328. ]
  329. }
  330. }
  331. };
  332. });
  333. it('should call __GetDimensions and return result', () => {
  334. expect(scenario.result[0].text).to.be('InstanceId');
  335. expect(scenario.request.queries[0].type).to.be('metricFindQuery');
  336. expect(scenario.request.queries[0].subtype).to.be('dimension_keys');
  337. });
  338. });
  339. describeMetricFindQuery('dimension_values(us-east-1,AWS/EC2,CPUUtilization,InstanceId)', scenario => {
  340. scenario.setup(() => {
  341. scenario.requestResponse = {
  342. results: {
  343. metricFindQuery: {
  344. tables: [
  345. { rows: [['i-12345678', 'i-12345678']] }
  346. ]
  347. }
  348. }
  349. };
  350. });
  351. it('should call __ListMetrics and return result', () => {
  352. expect(scenario.result[0].text).to.contain('i-12345678');
  353. expect(scenario.request.queries[0].type).to.be('metricFindQuery');
  354. expect(scenario.request.queries[0].subtype).to.be('dimension_values');
  355. });
  356. });
  357. describeMetricFindQuery('dimension_values(default,AWS/EC2,CPUUtilization,InstanceId)', scenario => {
  358. scenario.setup(() => {
  359. scenario.requestResponse = {
  360. results: {
  361. metricFindQuery: {
  362. tables: [
  363. { rows: [['i-12345678', 'i-12345678']] }
  364. ]
  365. }
  366. }
  367. };
  368. });
  369. it('should call __ListMetrics and return result', () => {
  370. expect(scenario.result[0].text).to.contain('i-12345678');
  371. expect(scenario.request.queries[0].type).to.be('metricFindQuery');
  372. expect(scenario.request.queries[0].subtype).to.be('dimension_values');
  373. });
  374. });
  375. it('should caclculate the correct period', function () {
  376. var hourSec = 60 * 60;
  377. var daySec = hourSec * 24;
  378. var start = 1483196400 * 1000;
  379. var testData: any[] = [
  380. [
  381. { period: 60, namespace: 'AWS/EC2' },
  382. { range: { from: new Date(start), to: new Date(start + 3600 * 1000) } },
  383. (hourSec * 3), 60
  384. ],
  385. [
  386. { period: null, namespace: 'AWS/EC2' },
  387. { range: { from: new Date(start), to: new Date(start + 3600 * 1000) } },
  388. (hourSec * 3), 300
  389. ],
  390. [
  391. { period: 60, namespace: 'AWS/ELB' },
  392. { range: { from: new Date(start), to: new Date(start + 3600 * 1000) } },
  393. (hourSec * 3), 60
  394. ],
  395. [
  396. { period: null, namespace: 'AWS/ELB' },
  397. { range: { from: new Date(start), to: new Date(start + 3600 * 1000) } },
  398. (hourSec * 3), 60
  399. ],
  400. [
  401. { period: 1, namespace: 'CustomMetricsNamespace' },
  402. { range: { from: new Date(start), to: new Date(start + (1440 - 1) * 1000) } },
  403. (hourSec * 3 - 1), 1
  404. ],
  405. [
  406. { period: 1, namespace: 'CustomMetricsNamespace' },
  407. { range: { from: new Date(start), to: new Date(start + 3600 * 1000) } },
  408. (hourSec * 3 - 1), 60
  409. ],
  410. [
  411. { period: 60, namespace: 'CustomMetricsNamespace' },
  412. { range: { from: new Date(start), to: new Date(start + 3600 * 1000) } },
  413. (hourSec * 3), 60
  414. ],
  415. [
  416. { period: null, namespace: 'CustomMetricsNamespace' },
  417. { range: { from: new Date(start), to: new Date(start + 3600 * 1000) } },
  418. (hourSec * 3 - 1), 60
  419. ],
  420. [
  421. { period: null, namespace: 'CustomMetricsNamespace' },
  422. { range: { from: new Date(start), to: new Date(start + 3600 * 1000) } },
  423. (hourSec * 3), 60
  424. ],
  425. [
  426. { period: null, namespace: 'CustomMetricsNamespace' },
  427. { range: { from: new Date(start), to: new Date(start + 3600 * 1000) } },
  428. (daySec * 15), 60
  429. ],
  430. [
  431. { period: null, namespace: 'CustomMetricsNamespace' },
  432. { range: { from: new Date(start), to: new Date(start + 3600 * 1000) } },
  433. (daySec * 63), 300
  434. ],
  435. [
  436. { period: null, namespace: 'CustomMetricsNamespace' },
  437. { range: { from: new Date(start), to: new Date(start + 3600 * 1000) } },
  438. (daySec * 455), 3600
  439. ]
  440. ];
  441. for (let t of testData) {
  442. let target = t[0];
  443. let options = t[1];
  444. let now = new Date(options.range.from.valueOf() + t[2] * 1000);
  445. let expected = t[3];
  446. let actual = ctx.ds.getPeriod(target, options, now);
  447. expect(actual).to.be(expected);
  448. }
  449. });
  450. });