datasource_specs.ts 16 KB

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