datasource.test.ts 13 KB

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