datasource_specs.ts 15 KB

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