datasource.test.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. import { GraphiteDatasource } from '../datasource';
  2. import _ from 'lodash';
  3. import $q from 'q';
  4. import { TemplateSrvStub } from 'test/specs/helpers';
  5. import { dateTime } from '@grafana/ui/src/utils/moment_wrapper';
  6. describe('graphiteDatasource', () => {
  7. const ctx: any = {
  8. backendSrv: {},
  9. $q: $q,
  10. templateSrv: new TemplateSrvStub(),
  11. instanceSettings: { url: 'url', name: 'graphiteProd', jsonData: {} },
  12. };
  13. beforeEach(() => {
  14. ctx.instanceSettings.url = '/api/datasources/proxy/1';
  15. ctx.ds = new GraphiteDatasource(ctx.instanceSettings, ctx.$q, ctx.backendSrv, ctx.templateSrv);
  16. });
  17. describe('When querying graphite with one target using query editor target spec', () => {
  18. const query = {
  19. panelId: 3,
  20. dashboardId: 5,
  21. rangeRaw: { from: 'now-1h', to: 'now' },
  22. targets: [{ target: 'prod1.count' }, { target: 'prod2.count' }],
  23. maxDataPoints: 500,
  24. };
  25. let results;
  26. let requestOptions;
  27. beforeEach(async () => {
  28. ctx.backendSrv.datasourceRequest = options => {
  29. requestOptions = options;
  30. return ctx.$q.when({
  31. data: [{ target: 'prod1.count', datapoints: [[10, 1], [12, 1]] }],
  32. });
  33. };
  34. await ctx.ds.query(query).then(data => {
  35. results = data;
  36. });
  37. });
  38. it('X-Dashboard and X-Panel headers to be set!', () => {
  39. expect(requestOptions.headers['X-Dashboard-Id']).toBe(5);
  40. expect(requestOptions.headers['X-Panel-Id']).toBe(3);
  41. });
  42. it('should generate the correct query', () => {
  43. expect(requestOptions.url).toBe('/api/datasources/proxy/1/render');
  44. });
  45. it('should set unique requestId', () => {
  46. expect(requestOptions.requestId).toBe('graphiteProd.panelId.3');
  47. });
  48. it('should query correctly', () => {
  49. const params = requestOptions.data.split('&');
  50. expect(params).toContain('target=prod1.count');
  51. expect(params).toContain('target=prod2.count');
  52. expect(params).toContain('from=-1h');
  53. expect(params).toContain('until=now');
  54. });
  55. it('should exclude undefined params', () => {
  56. const params = requestOptions.data.split('&');
  57. expect(params).not.toContain('cacheTimeout=undefined');
  58. });
  59. it('should return series list', () => {
  60. expect(results.data.length).toBe(1);
  61. expect(results.data[0].target).toBe('prod1.count');
  62. });
  63. it('should convert to millisecond resolution', () => {
  64. expect(results.data[0].datapoints[0][0]).toBe(10);
  65. });
  66. });
  67. describe('when fetching Graphite Events as annotations', () => {
  68. let results;
  69. const options = {
  70. annotation: {
  71. tags: 'tag1',
  72. },
  73. range: {
  74. from: dateTime(1432288354),
  75. to: dateTime(1432288401),
  76. },
  77. rangeRaw: { from: 'now-24h', to: 'now' },
  78. };
  79. describe('and tags are returned as string', () => {
  80. const response = {
  81. data: [
  82. {
  83. when: 1507222850,
  84. tags: 'tag1 tag2',
  85. data: 'some text',
  86. id: 2,
  87. what: 'Event - deploy',
  88. },
  89. ],
  90. };
  91. beforeEach(async () => {
  92. ctx.backendSrv.datasourceRequest = options => {
  93. return ctx.$q.when(response);
  94. };
  95. await ctx.ds.annotationQuery(options).then(data => {
  96. results = data;
  97. });
  98. });
  99. it('should parse the tags string into an array', () => {
  100. expect(_.isArray(results[0].tags)).toEqual(true);
  101. expect(results[0].tags.length).toEqual(2);
  102. expect(results[0].tags[0]).toEqual('tag1');
  103. expect(results[0].tags[1]).toEqual('tag2');
  104. });
  105. });
  106. describe('and tags are returned as an array', () => {
  107. const response = {
  108. data: [
  109. {
  110. when: 1507222850,
  111. tags: ['tag1', 'tag2'],
  112. data: 'some text',
  113. id: 2,
  114. what: 'Event - deploy',
  115. },
  116. ],
  117. };
  118. beforeEach(() => {
  119. ctx.backendSrv.datasourceRequest = options => {
  120. return ctx.$q.when(response);
  121. };
  122. ctx.ds.annotationQuery(options).then(data => {
  123. results = data;
  124. });
  125. // ctx.$rootScope.$apply();
  126. });
  127. it('should parse the tags string into an array', () => {
  128. expect(_.isArray(results[0].tags)).toEqual(true);
  129. expect(results[0].tags.length).toEqual(2);
  130. expect(results[0].tags[0]).toEqual('tag1');
  131. expect(results[0].tags[1]).toEqual('tag2');
  132. });
  133. });
  134. });
  135. describe('building graphite params', () => {
  136. it('should return empty array if no targets', () => {
  137. const results = ctx.ds.buildGraphiteParams({
  138. targets: [{}],
  139. });
  140. expect(results.length).toBe(0);
  141. });
  142. it('should uri escape targets', () => {
  143. const results = ctx.ds.buildGraphiteParams({
  144. targets: [{ target: 'prod1.{test,test2}' }, { target: 'prod2.count' }],
  145. });
  146. expect(results).toContain('target=prod1.%7Btest%2Ctest2%7D');
  147. });
  148. it('should replace target placeholder', () => {
  149. const results = ctx.ds.buildGraphiteParams({
  150. targets: [{ target: 'series1' }, { target: 'series2' }, { target: 'asPercent(#A,#B)' }],
  151. });
  152. expect(results[2]).toBe('target=asPercent(series1%2Cseries2)');
  153. });
  154. it('should replace target placeholder for hidden series', () => {
  155. const results = ctx.ds.buildGraphiteParams({
  156. targets: [
  157. { target: 'series1', hide: true },
  158. { target: 'sumSeries(#A)', hide: true },
  159. { target: 'asPercent(#A,#B)' },
  160. ],
  161. });
  162. expect(results[0]).toBe('target=' + encodeURIComponent('asPercent(series1,sumSeries(series1))'));
  163. });
  164. it('should replace target placeholder when nesting query references', () => {
  165. const results = ctx.ds.buildGraphiteParams({
  166. targets: [{ target: 'series1' }, { target: 'sumSeries(#A)' }, { target: 'asPercent(#A,#B)' }],
  167. });
  168. expect(results[2]).toBe('target=' + encodeURIComponent('asPercent(series1,sumSeries(series1))'));
  169. });
  170. it('should fix wrong minute interval parameters', () => {
  171. const results = ctx.ds.buildGraphiteParams({
  172. targets: [{ target: "summarize(prod.25m.count, '25m', 'sum')" }],
  173. });
  174. expect(results[0]).toBe('target=' + encodeURIComponent("summarize(prod.25m.count, '25min', 'sum')"));
  175. });
  176. it('should fix wrong month interval parameters', () => {
  177. const results = ctx.ds.buildGraphiteParams({
  178. targets: [{ target: "summarize(prod.5M.count, '5M', 'sum')" }],
  179. });
  180. expect(results[0]).toBe('target=' + encodeURIComponent("summarize(prod.5M.count, '5mon', 'sum')"));
  181. });
  182. it('should ignore empty targets', () => {
  183. const results = ctx.ds.buildGraphiteParams({
  184. targets: [{ target: 'series1' }, { target: '' }],
  185. });
  186. expect(results.length).toBe(2);
  187. });
  188. });
  189. describe('querying for template variables', () => {
  190. let results;
  191. let requestOptions;
  192. beforeEach(() => {
  193. ctx.backendSrv.datasourceRequest = options => {
  194. requestOptions = options;
  195. return ctx.$q.when({
  196. data: ['backend_01', 'backend_02'],
  197. });
  198. };
  199. });
  200. it('should generate tags query', () => {
  201. ctx.ds.metricFindQuery('tags()').then(data => {
  202. results = data;
  203. });
  204. expect(requestOptions.url).toBe('/api/datasources/proxy/1/tags/autoComplete/tags');
  205. expect(requestOptions.params.expr).toEqual([]);
  206. expect(results).not.toBe(null);
  207. });
  208. it('should generate tags query with a filter expression', () => {
  209. ctx.ds.metricFindQuery('tags(server=backend_01)').then(data => {
  210. results = data;
  211. });
  212. expect(requestOptions.url).toBe('/api/datasources/proxy/1/tags/autoComplete/tags');
  213. expect(requestOptions.params.expr).toEqual(['server=backend_01']);
  214. expect(results).not.toBe(null);
  215. });
  216. it('should generate tags query for an expression with whitespace after', () => {
  217. ctx.ds.metricFindQuery('tags(server=backend_01 )').then(data => {
  218. results = data;
  219. });
  220. expect(requestOptions.url).toBe('/api/datasources/proxy/1/tags/autoComplete/tags');
  221. expect(requestOptions.params.expr).toEqual(['server=backend_01']);
  222. expect(results).not.toBe(null);
  223. });
  224. it('should generate tag values query for one tag', () => {
  225. ctx.ds.metricFindQuery('tag_values(server)').then(data => {
  226. results = data;
  227. });
  228. expect(requestOptions.url).toBe('/api/datasources/proxy/1/tags/autoComplete/values');
  229. expect(requestOptions.params.tag).toBe('server');
  230. expect(requestOptions.params.expr).toEqual([]);
  231. expect(results).not.toBe(null);
  232. });
  233. it('should generate tag values query for a tag and expression', () => {
  234. ctx.ds.metricFindQuery('tag_values(server,server=~backend*)').then(data => {
  235. results = data;
  236. });
  237. expect(requestOptions.url).toBe('/api/datasources/proxy/1/tags/autoComplete/values');
  238. expect(requestOptions.params.tag).toBe('server');
  239. expect(requestOptions.params.expr).toEqual(['server=~backend*']);
  240. expect(results).not.toBe(null);
  241. });
  242. it('should generate tag values query for a tag with whitespace after', () => {
  243. ctx.ds.metricFindQuery('tag_values(server )').then(data => {
  244. results = data;
  245. });
  246. expect(requestOptions.url).toBe('/api/datasources/proxy/1/tags/autoComplete/values');
  247. expect(requestOptions.params.tag).toBe('server');
  248. expect(requestOptions.params.expr).toEqual([]);
  249. expect(results).not.toBe(null);
  250. });
  251. it('should generate tag values query for a tag and expression with whitespace after', () => {
  252. ctx.ds.metricFindQuery('tag_values(server , server=~backend* )').then(data => {
  253. results = data;
  254. });
  255. expect(requestOptions.url).toBe('/api/datasources/proxy/1/tags/autoComplete/values');
  256. expect(requestOptions.params.tag).toBe('server');
  257. expect(requestOptions.params.expr).toEqual(['server=~backend*']);
  258. expect(results).not.toBe(null);
  259. });
  260. });
  261. });
  262. function accessScenario(name, url, fn) {
  263. describe('access scenario ' + name, () => {
  264. const ctx: any = {
  265. backendSrv: {},
  266. $q: $q,
  267. templateSrv: new TemplateSrvStub(),
  268. instanceSettings: { url: 'url', name: 'graphiteProd', jsonData: {} },
  269. };
  270. const httpOptions = {
  271. headers: {},
  272. };
  273. describe('when using proxy mode', () => {
  274. const options = { dashboardId: 1, panelId: 2 };
  275. it('tracing headers should be added', () => {
  276. ctx.instanceSettings.url = url;
  277. const ds = new GraphiteDatasource(ctx.instanceSettings, ctx.$q, ctx.backendSrv, ctx.templateSrv);
  278. ds.addTracingHeaders(httpOptions, options);
  279. fn(httpOptions);
  280. });
  281. });
  282. });
  283. }
  284. accessScenario('with proxy access', '/api/datasources/proxy/1', httpOptions => {
  285. expect(httpOptions.headers['X-Dashboard-Id']).toBe(1);
  286. expect(httpOptions.headers['X-Panel-Id']).toBe(2);
  287. });
  288. accessScenario('with direct access', 'http://localhost:8080', httpOptions => {
  289. expect(httpOptions.headers['X-Dashboard-Id']).toBe(undefined);
  290. expect(httpOptions.headers['X-Panel-Id']).toBe(undefined);
  291. });