datasource_specs.ts 15 KB

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