datasource.test.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. import { GraphiteDatasource } from '../datasource';
  2. import _ from 'lodash';
  3. // @ts-ignore
  4. import $q from 'q';
  5. import { TemplateSrvStub } from 'test/specs/helpers';
  6. import { dateTime } from '@grafana/data';
  7. describe('graphiteDatasource', () => {
  8. const ctx: any = {
  9. backendSrv: {},
  10. $q,
  11. // @ts-ignore
  12. templateSrv: new TemplateSrvStub(),
  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. });
  192. describe('querying for template variables', () => {
  193. let results: any;
  194. let requestOptions: any;
  195. beforeEach(() => {
  196. ctx.backendSrv.datasourceRequest = (options: any) => {
  197. requestOptions = options;
  198. return ctx.$q.when({
  199. data: ['backend_01', 'backend_02'],
  200. });
  201. };
  202. });
  203. it('should generate tags query', () => {
  204. ctx.ds.metricFindQuery('tags()').then((data: any) => {
  205. results = data;
  206. });
  207. expect(requestOptions.url).toBe('/api/datasources/proxy/1/tags/autoComplete/tags');
  208. expect(requestOptions.params.expr).toEqual([]);
  209. expect(results).not.toBe(null);
  210. });
  211. it('should generate tags query with a filter expression', () => {
  212. ctx.ds.metricFindQuery('tags(server=backend_01)').then((data: any) => {
  213. results = data;
  214. });
  215. expect(requestOptions.url).toBe('/api/datasources/proxy/1/tags/autoComplete/tags');
  216. expect(requestOptions.params.expr).toEqual(['server=backend_01']);
  217. expect(results).not.toBe(null);
  218. });
  219. it('should generate tags query for an expression with whitespace after', () => {
  220. ctx.ds.metricFindQuery('tags(server=backend_01 )').then((data: any) => {
  221. results = data;
  222. });
  223. expect(requestOptions.url).toBe('/api/datasources/proxy/1/tags/autoComplete/tags');
  224. expect(requestOptions.params.expr).toEqual(['server=backend_01']);
  225. expect(results).not.toBe(null);
  226. });
  227. it('should generate tag values query for one tag', () => {
  228. ctx.ds.metricFindQuery('tag_values(server)').then((data: any) => {
  229. results = data;
  230. });
  231. expect(requestOptions.url).toBe('/api/datasources/proxy/1/tags/autoComplete/values');
  232. expect(requestOptions.params.tag).toBe('server');
  233. expect(requestOptions.params.expr).toEqual([]);
  234. expect(results).not.toBe(null);
  235. });
  236. it('should generate tag values query for a tag and expression', () => {
  237. ctx.ds.metricFindQuery('tag_values(server,server=~backend*)').then((data: any) => {
  238. results = data;
  239. });
  240. expect(requestOptions.url).toBe('/api/datasources/proxy/1/tags/autoComplete/values');
  241. expect(requestOptions.params.tag).toBe('server');
  242. expect(requestOptions.params.expr).toEqual(['server=~backend*']);
  243. expect(results).not.toBe(null);
  244. });
  245. it('should generate tag values query for a tag with whitespace after', () => {
  246. ctx.ds.metricFindQuery('tag_values(server )').then((data: any) => {
  247. results = data;
  248. });
  249. expect(requestOptions.url).toBe('/api/datasources/proxy/1/tags/autoComplete/values');
  250. expect(requestOptions.params.tag).toBe('server');
  251. expect(requestOptions.params.expr).toEqual([]);
  252. expect(results).not.toBe(null);
  253. });
  254. it('should generate tag values query for a tag and expression with whitespace after', () => {
  255. ctx.ds.metricFindQuery('tag_values(server , server=~backend* )').then((data: any) => {
  256. results = data;
  257. });
  258. expect(requestOptions.url).toBe('/api/datasources/proxy/1/tags/autoComplete/values');
  259. expect(requestOptions.params.tag).toBe('server');
  260. expect(requestOptions.params.expr).toEqual(['server=~backend*']);
  261. expect(results).not.toBe(null);
  262. });
  263. it('/metrics/find should be POST', () => {
  264. ctx.templateSrv.setGrafanaVariable('foo', 'bar');
  265. ctx.ds.metricFindQuery('[[foo]]').then((data: any) => {
  266. results = data;
  267. });
  268. expect(requestOptions.url).toBe('/api/datasources/proxy/1/metrics/find');
  269. expect(requestOptions.method).toEqual('POST');
  270. expect(requestOptions.headers).toHaveProperty('Content-Type', 'application/x-www-form-urlencoded');
  271. expect(requestOptions.data).toMatch(`query=bar`);
  272. expect(requestOptions).toHaveProperty('params');
  273. });
  274. });
  275. });
  276. function accessScenario(name: string, url: string, fn: any) {
  277. describe('access scenario ' + name, () => {
  278. const ctx: any = {
  279. backendSrv: {},
  280. $q,
  281. // @ts-ignore
  282. templateSrv: new TemplateSrvStub(),
  283. instanceSettings: { url: 'url', name: 'graphiteProd', jsonData: {} },
  284. };
  285. const httpOptions = {
  286. headers: {},
  287. };
  288. describe('when using proxy mode', () => {
  289. const options = { dashboardId: 1, panelId: 2 };
  290. it('tracing headers should be added', () => {
  291. ctx.instanceSettings.url = url;
  292. // @ts-ignore
  293. const ds = new GraphiteDatasource(ctx.instanceSettings, ctx.$q, ctx.backendSrv, ctx.templateSrv);
  294. ds.addTracingHeaders(httpOptions, options);
  295. fn(httpOptions);
  296. });
  297. });
  298. });
  299. }
  300. accessScenario('with proxy access', '/api/datasources/proxy/1', (httpOptions: any) => {
  301. expect(httpOptions.headers['X-Dashboard-Id']).toBe(1);
  302. expect(httpOptions.headers['X-Panel-Id']).toBe(2);
  303. });
  304. accessScenario('with direct access', 'http://localhost:8080', (httpOptions: any) => {
  305. expect(httpOptions.headers['X-Dashboard-Id']).toBe(undefined);
  306. expect(httpOptions.headers['X-Panel-Id']).toBe(undefined);
  307. });