datasource_specs.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import {describe, beforeEach, it, sinon, expect, angularMocks} from 'test/lib/common';
  2. import moment from 'moment';
  3. import helpers from 'test/specs/helpers';
  4. import {PrometheusDatasource} from '../datasource';
  5. describe('PrometheusDatasource', function() {
  6. var ctx = new helpers.ServiceTestContext();
  7. var instanceSettings = {url: 'proxied', directUrl: 'direct', user: 'test', password: 'mupp' };
  8. beforeEach(angularMocks.module('grafana.core'));
  9. beforeEach(angularMocks.module('grafana.services'));
  10. beforeEach(angularMocks.inject(function($q, $rootScope, $httpBackend, $injector) {
  11. ctx.$q = $q;
  12. ctx.$httpBackend = $httpBackend;
  13. ctx.$rootScope = $rootScope;
  14. ctx.ds = $injector.instantiate(PrometheusDatasource, {instanceSettings: instanceSettings});
  15. }));
  16. describe('When querying prometheus with one target using query editor target spec', function() {
  17. var results;
  18. var urlExpected = 'proxied/api/v1/query_range?query=' +
  19. encodeURIComponent('test{job="testjob"}') +
  20. '&start=1443438675&end=1443460275&step=60';
  21. var query = {
  22. range: { from: moment(1443438674760), to: moment(1443460274760) },
  23. targets: [{ expr: 'test{job="testjob"}' }],
  24. interval: '60s'
  25. };
  26. var response = {
  27. status: "success",
  28. data: {
  29. resultType: "matrix",
  30. result: [{
  31. metric: {"__name__": "test", job: "testjob"},
  32. values: [[1443454528, "3846"]]
  33. }]
  34. }
  35. };
  36. beforeEach(function() {
  37. ctx.$httpBackend.expect('GET', urlExpected).respond(response);
  38. ctx.ds.query(query).then(function(data) { results = data; });
  39. ctx.$httpBackend.flush();
  40. });
  41. it('should generate the correct query', function() {
  42. ctx.$httpBackend.verifyNoOutstandingExpectation();
  43. });
  44. it('should return series list', function() {
  45. expect(results.data.length).to.be(1);
  46. expect(results.data[0].target).to.be('test{job="testjob"}');
  47. });
  48. });
  49. describe('When querying prometheus with one target which return multiple series', function() {
  50. var results;
  51. var start = 1443438675;
  52. var end = 1443460275;
  53. var step = 60;
  54. var urlExpected = 'proxied/api/v1/query_range?query=' +
  55. encodeURIComponent('test{job="testjob"}') +
  56. '&start=' + start + '&end=' + end + '&step=' + step;
  57. var query = {
  58. range: { from: moment(1443438674760), to: moment(1443460274760) },
  59. targets: [{ expr: 'test{job="testjob"}' }],
  60. interval: '60s'
  61. };
  62. var response = {
  63. status: "success",
  64. data: {
  65. resultType: "matrix",
  66. result: [
  67. {
  68. metric: {"__name__": "test", job: "testjob", series: 'series 1'},
  69. values: [
  70. [start + step * 1, "3846"],
  71. [start + step * 3, "3847"],
  72. [end - step * 1, "3848"],
  73. ]
  74. },
  75. {
  76. metric: {"__name__": "test", job: "testjob", series: 'series 2'},
  77. values: [
  78. [start + step * 2, "4846"]
  79. ]
  80. },
  81. ]
  82. }
  83. };
  84. beforeEach(function() {
  85. ctx.$httpBackend.expect('GET', urlExpected).respond(response);
  86. ctx.ds.query(query).then(function(data) { results = data; });
  87. ctx.$httpBackend.flush();
  88. });
  89. it('should be same length', function() {
  90. expect(results.data.length).to.be(2);
  91. expect(results.data[0].datapoints.length).to.be((end - start) / step + 1);
  92. expect(results.data[1].datapoints.length).to.be((end - start) / step + 1);
  93. });
  94. it('should fill null until first datapoint in response', function() {
  95. expect(results.data[0].datapoints[0][1]).to.be(start * 1000);
  96. expect(results.data[0].datapoints[0][0]).to.be(null);
  97. expect(results.data[0].datapoints[1][1]).to.be((start + step * 1) * 1000);
  98. expect(results.data[0].datapoints[1][0]).to.be(3846);
  99. });
  100. it('should fill null after last datapoint in response', function() {
  101. var length = (end - start) / step + 1;
  102. expect(results.data[0].datapoints[length-2][1]).to.be((end - step * 1) * 1000);
  103. expect(results.data[0].datapoints[length-2][0]).to.be(3848);
  104. expect(results.data[0].datapoints[length-1][1]).to.be(end * 1000);
  105. expect(results.data[0].datapoints[length-1][0]).to.be(null);
  106. });
  107. it('should fill null at gap between series', function() {
  108. expect(results.data[0].datapoints[2][1]).to.be((start + step * 2) * 1000);
  109. expect(results.data[0].datapoints[2][0]).to.be(null);
  110. expect(results.data[1].datapoints[1][1]).to.be((start + step * 1) * 1000);
  111. expect(results.data[1].datapoints[1][0]).to.be(null);
  112. expect(results.data[1].datapoints[3][1]).to.be((start + step * 3) * 1000);
  113. expect(results.data[1].datapoints[3][0]).to.be(null);
  114. });
  115. });
  116. describe('When performing annotationQuery', function() {
  117. var results;
  118. var urlExpected = 'proxied/api/v1/query_range?query=' +
  119. encodeURIComponent('ALERTS{alertstate="firing"}') +
  120. '&start=1443438675&end=1443460275&step=60s';
  121. var options = {
  122. annotation: {
  123. expr: 'ALERTS{alertstate="firing"}',
  124. tagKeys: 'job',
  125. titleFormat: '{{alertname}}',
  126. textFormat: '{{instance}}'
  127. },
  128. range: {
  129. from: moment(1443438674760),
  130. to: moment(1443460274760)
  131. }
  132. };
  133. var response = {
  134. status: "success",
  135. data: {
  136. resultType: "matrix",
  137. result: [{
  138. metric: {"__name__": "ALERTS", alertname: "InstanceDown", alertstate: "firing", instance: "testinstance", job: "testjob"},
  139. values: [[1443454528, "1"]]
  140. }]
  141. }
  142. };
  143. beforeEach(function() {
  144. ctx.$httpBackend.expect('GET', urlExpected).respond(response);
  145. ctx.ds.annotationQuery(options).then(function(data) { results = data; });
  146. ctx.$httpBackend.flush();
  147. });
  148. it('should return annotation list', function() {
  149. ctx.$rootScope.$apply();
  150. expect(results.length).to.be(1);
  151. expect(results[0].tags).to.contain('testjob');
  152. expect(results[0].title).to.be('InstanceDown');
  153. expect(results[0].text).to.be('testinstance');
  154. expect(results[0].time).to.be(1443454528 * 1000);
  155. });
  156. });
  157. });