datasource_specs.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. import {describe, beforeEach, it, sinon, expect, angularMocks} from 'test/lib/common';
  2. import moment from 'moment';
  3. import angular from 'angular';
  4. import helpers from 'test/specs/helpers';
  5. import {ElasticDatasource} from "../datasource";
  6. describe('ElasticDatasource', function() {
  7. var ctx = new helpers.ServiceTestContext();
  8. var instanceSettings: any = {jsonData: {}};
  9. beforeEach(angularMocks.module('grafana.core'));
  10. beforeEach(angularMocks.module('grafana.services'));
  11. beforeEach(ctx.providePhase(['templateSrv', 'backendSrv','timeSrv']));
  12. beforeEach(angularMocks.inject(function($q, $rootScope, $httpBackend, $injector) {
  13. ctx.$q = $q;
  14. ctx.$httpBackend = $httpBackend;
  15. ctx.$rootScope = $rootScope;
  16. ctx.$injector = $injector;
  17. $httpBackend.when('GET', /\.html$/).respond('');
  18. }));
  19. function createDatasource(instanceSettings) {
  20. instanceSettings.jsonData = instanceSettings.jsonData || {};
  21. ctx.ds = ctx.$injector.instantiate(ElasticDatasource, {instanceSettings: instanceSettings});
  22. }
  23. describe('When testing datasource with index pattern', function() {
  24. beforeEach(function() {
  25. createDatasource({url: 'http://es.com', index: '[asd-]YYYY.MM.DD', jsonData: {interval: 'Daily'}});
  26. });
  27. it('should translate index pattern to current day', function() {
  28. var requestOptions;
  29. ctx.backendSrv.datasourceRequest = function(options) {
  30. requestOptions = options;
  31. return ctx.$q.when({data: {}});
  32. };
  33. ctx.ds.testDatasource();
  34. ctx.$rootScope.$apply();
  35. var today = moment.utc().format("YYYY.MM.DD");
  36. expect(requestOptions.url).to.be("http://es.com/asd-" + today + '/_stats');
  37. });
  38. });
  39. describe('When issueing metric query with interval pattern', function() {
  40. var requestOptions, parts, header;
  41. beforeEach(function() {
  42. createDatasource({url: 'http://es.com', index: '[asd-]YYYY.MM.DD', jsonData: {interval: 'Daily'}});
  43. ctx.backendSrv.datasourceRequest = function(options) {
  44. requestOptions = options;
  45. return ctx.$q.when({data: {responses: []}});
  46. };
  47. ctx.ds.query({
  48. range: {
  49. from: moment([2015, 4, 30, 10]),
  50. to: moment([2015, 5, 1, 10])
  51. },
  52. targets: [{ bucketAggs: [], metrics: [], query: 'escape\\:test' }]
  53. });
  54. ctx.$rootScope.$apply();
  55. parts = requestOptions.data.split('\n');
  56. header = angular.fromJson(parts[0]);
  57. });
  58. it('should translate index pattern to current day', function() {
  59. expect(header.index).to.eql(['asd-2015.05.30', 'asd-2015.05.31', 'asd-2015.06.01']);
  60. });
  61. it('should json escape lucene query', function() {
  62. var body = angular.fromJson(parts[1]);
  63. expect(body.query.filtered.query.query_string.query).to.be('escape\\:test');
  64. });
  65. });
  66. describe('When issueing document query', function() {
  67. var requestOptions, parts, header;
  68. beforeEach(function() {
  69. createDatasource({url: 'http://es.com', index: 'test'});
  70. ctx.backendSrv.datasourceRequest = function(options) {
  71. requestOptions = options;
  72. return ctx.$q.when({data: {responses: []}});
  73. };
  74. ctx.ds.query({
  75. range: { from: moment([2015, 4, 30, 10]), to: moment([2015, 5, 1, 10]) },
  76. targets: [{ bucketAggs: [], metrics: [{type: 'raw_document'}], query: 'test' }]
  77. });
  78. ctx.$rootScope.$apply();
  79. parts = requestOptions.data.split('\n');
  80. header = angular.fromJson(parts[0]);
  81. });
  82. it('should set search type to query_then_fetch', function() {
  83. expect(header.search_type).to.eql('query_then_fetch');
  84. });
  85. it('should set size', function() {
  86. var body = angular.fromJson(parts[1]);
  87. expect(body.size).to.be(500);
  88. });
  89. });
  90. describe('When issuing aggregation query on es5.x', function() {
  91. var requestOptions, parts, header;
  92. beforeEach(function() {
  93. createDatasource({url: 'http://es.com', index: 'test', jsonData: {esVersion: '5'}});
  94. ctx.backendSrv.datasourceRequest = function(options) {
  95. requestOptions = options;
  96. return ctx.$q.when({data: {responses: []}});
  97. };
  98. ctx.ds.query({
  99. range: { from: moment([2015, 4, 30, 10]), to: moment([2015, 5, 1, 10]) },
  100. targets: [{
  101. bucketAggs: [
  102. {type: 'date_histogram', field: '@timestamp', id: '2'}
  103. ],
  104. metrics: [
  105. {type: 'count'}], query: 'test' }
  106. ]
  107. });
  108. ctx.$rootScope.$apply();
  109. parts = requestOptions.data.split('\n');
  110. header = angular.fromJson(parts[0]);
  111. });
  112. it('should not set search type to count', function() {
  113. expect(header.search_type).to.not.eql('count');
  114. });
  115. it('should set size to 0', function() {
  116. var body = angular.fromJson(parts[1]);
  117. expect(body.size).to.be(0);
  118. });
  119. });
  120. describe('When issuing metricFind query on es5.x', function() {
  121. var requestOptions, parts, header;
  122. beforeEach(function() {
  123. createDatasource({url: 'http://es.com', index: 'test', jsonData: {esVersion: '5'}});
  124. ctx.backendSrv.datasourceRequest = function(options) {
  125. requestOptions = options;
  126. return ctx.$q.when({
  127. data: {
  128. responses: [{aggregations: {"1": [{buckets: {text: 'test', value: '1'}}]}}]
  129. }
  130. });
  131. };
  132. ctx.ds.metricFindQuery('{"find": "terms", "field": "test"}');
  133. ctx.$rootScope.$apply();
  134. parts = requestOptions.data.split('\n');
  135. header = angular.fromJson(parts[0]);
  136. });
  137. it('should not set search type to count', function() {
  138. expect(header.search_type).to.not.eql('count');
  139. });
  140. it('should set size to 0', function() {
  141. var body = angular.fromJson(parts[1]);
  142. expect(body.size).to.be(0);
  143. });
  144. });
  145. });