datasource.test.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. import StackdriverDataSource from '../datasource';
  2. import { metricDescriptors } from './testData';
  3. import { TemplateSrv } from 'app/features/templating/template_srv';
  4. import { CustomVariable } from 'app/features/templating/all';
  5. import { toUtc } from '@grafana/data';
  6. import { DataSourceInstanceSettings } from '@grafana/ui';
  7. import { StackdriverOptions } from '../types';
  8. import { BackendSrv } from 'app/core/services/backend_srv';
  9. import { TimeSrv } from 'app/features/dashboard/services/TimeSrv';
  10. interface Result {
  11. status: any;
  12. message?: any;
  13. }
  14. describe('StackdriverDataSource', () => {
  15. const instanceSettings = ({
  16. jsonData: {
  17. defaultProject: 'testproject',
  18. },
  19. } as unknown) as DataSourceInstanceSettings<StackdriverOptions>;
  20. const templateSrv = new TemplateSrv();
  21. const timeSrv = {} as TimeSrv;
  22. describe('when performing testDataSource', () => {
  23. describe('and call to stackdriver api succeeds', () => {
  24. let ds;
  25. let result: Result;
  26. beforeEach(async () => {
  27. const backendSrv = ({
  28. async datasourceRequest() {
  29. return Promise.resolve({ status: 200 });
  30. },
  31. } as unknown) as BackendSrv;
  32. ds = new StackdriverDataSource(instanceSettings, backendSrv, templateSrv, timeSrv);
  33. result = await ds.testDatasource();
  34. });
  35. it('should return successfully', () => {
  36. expect(result.status).toBe('success');
  37. });
  38. });
  39. describe('and a list of metricDescriptors are returned', () => {
  40. let ds;
  41. let result: Result;
  42. beforeEach(async () => {
  43. const backendSrv = ({
  44. datasourceRequest: async () => Promise.resolve({ status: 200, data: metricDescriptors }),
  45. } as unknown) as BackendSrv;
  46. ds = new StackdriverDataSource(instanceSettings, backendSrv, templateSrv, timeSrv);
  47. result = await ds.testDatasource();
  48. });
  49. it('should return status success', () => {
  50. expect(result.status).toBe('success');
  51. });
  52. });
  53. describe('and call to stackdriver api fails with 400 error', () => {
  54. let ds;
  55. let result: Result;
  56. beforeEach(async () => {
  57. const backendSrv = ({
  58. datasourceRequest: async () =>
  59. Promise.reject({
  60. statusText: 'Bad Request',
  61. data: {
  62. error: { code: 400, message: 'Field interval.endTime had an invalid value' },
  63. },
  64. }),
  65. } as unknown) as BackendSrv;
  66. ds = new StackdriverDataSource(instanceSettings, backendSrv, templateSrv, timeSrv);
  67. result = await ds.testDatasource();
  68. });
  69. it('should return error status and a detailed error message', () => {
  70. expect(result.status).toEqual('error');
  71. expect(result.message).toBe('Stackdriver: Bad Request: 400. Field interval.endTime had an invalid value');
  72. });
  73. });
  74. });
  75. describe('When performing query', () => {
  76. const options = {
  77. range: {
  78. from: toUtc('2017-08-22T20:00:00Z'),
  79. to: toUtc('2017-08-22T23:59:00Z'),
  80. },
  81. rangeRaw: {
  82. from: 'now-4h',
  83. to: 'now',
  84. },
  85. targets: [
  86. {
  87. refId: 'A',
  88. },
  89. ],
  90. };
  91. describe('and no time series data is returned', () => {
  92. let ds: StackdriverDataSource;
  93. const response: any = {
  94. results: {
  95. A: {
  96. refId: 'A',
  97. meta: {
  98. rawQuery: 'arawquerystring',
  99. },
  100. series: null,
  101. tables: null,
  102. },
  103. },
  104. };
  105. beforeEach(() => {
  106. const backendSrv = ({
  107. datasourceRequest: async () => Promise.resolve({ status: 200, data: response }),
  108. } as unknown) as BackendSrv;
  109. ds = new StackdriverDataSource(instanceSettings, backendSrv, templateSrv, timeSrv);
  110. });
  111. it('should return a list of datapoints', () => {
  112. return ds.query(options as any).then(results => {
  113. expect(results.data.length).toBe(0);
  114. });
  115. });
  116. });
  117. });
  118. describe('when performing getMetricTypes', () => {
  119. describe('and call to stackdriver api succeeds', () => {});
  120. let ds;
  121. let result: any;
  122. beforeEach(async () => {
  123. const backendSrv = ({
  124. async datasourceRequest() {
  125. return Promise.resolve({
  126. data: {
  127. metricDescriptors: [
  128. {
  129. displayName: 'test metric name 1',
  130. type: 'compute.googleapis.com/instance/cpu/test-metric-type-1',
  131. description: 'A description',
  132. },
  133. {
  134. type: 'logging.googleapis.com/user/logbased-metric-with-no-display-name',
  135. },
  136. ],
  137. },
  138. });
  139. },
  140. } as unknown) as BackendSrv;
  141. ds = new StackdriverDataSource(instanceSettings, backendSrv, templateSrv, timeSrv);
  142. // @ts-ignore
  143. result = await ds.getMetricTypes();
  144. });
  145. it('should return successfully', () => {
  146. expect(result.length).toBe(2);
  147. expect(result[0].service).toBe('compute.googleapis.com');
  148. expect(result[0].serviceShortName).toBe('compute');
  149. expect(result[0].type).toBe('compute.googleapis.com/instance/cpu/test-metric-type-1');
  150. expect(result[0].displayName).toBe('test metric name 1');
  151. expect(result[0].description).toBe('A description');
  152. expect(result[1].type).toBe('logging.googleapis.com/user/logbased-metric-with-no-display-name');
  153. expect(result[1].displayName).toBe('logging.googleapis.com/user/logbased-metric-with-no-display-name');
  154. });
  155. });
  156. const noopBackendSrv = ({} as unknown) as BackendSrv;
  157. describe('when interpolating a template variable for the filter', () => {
  158. let interpolated: any[];
  159. describe('and is single value variable', () => {
  160. beforeEach(() => {
  161. const filterTemplateSrv = initTemplateSrv('filtervalue1');
  162. const ds = new StackdriverDataSource(instanceSettings, noopBackendSrv, filterTemplateSrv, timeSrv);
  163. interpolated = ds.interpolateFilters(['resource.label.zone', '=~', '${test}'], {});
  164. });
  165. it('should replace the variable with the value', () => {
  166. expect(interpolated.length).toBe(3);
  167. expect(interpolated[2]).toBe('filtervalue1');
  168. });
  169. });
  170. describe('and is multi value variable', () => {
  171. beforeEach(() => {
  172. const filterTemplateSrv = initTemplateSrv(['filtervalue1', 'filtervalue2'], true);
  173. const ds = new StackdriverDataSource(instanceSettings, noopBackendSrv, filterTemplateSrv, timeSrv);
  174. interpolated = ds.interpolateFilters(['resource.label.zone', '=~', '[[test]]'], {});
  175. });
  176. it('should replace the variable with a regex expression', () => {
  177. expect(interpolated[2]).toBe('(filtervalue1|filtervalue2)');
  178. });
  179. });
  180. });
  181. describe('when interpolating a template variable for group bys', () => {
  182. let interpolated: any[];
  183. describe('and is single value variable', () => {
  184. beforeEach(() => {
  185. const groupByTemplateSrv = initTemplateSrv('groupby1');
  186. const ds = new StackdriverDataSource(instanceSettings, noopBackendSrv, groupByTemplateSrv, timeSrv);
  187. interpolated = ds.interpolateGroupBys(['[[test]]'], {});
  188. });
  189. it('should replace the variable with the value', () => {
  190. expect(interpolated.length).toBe(1);
  191. expect(interpolated[0]).toBe('groupby1');
  192. });
  193. });
  194. describe('and is multi value variable', () => {
  195. beforeEach(() => {
  196. const groupByTemplateSrv = initTemplateSrv(['groupby1', 'groupby2'], true);
  197. const ds = new StackdriverDataSource(instanceSettings, noopBackendSrv, groupByTemplateSrv, timeSrv);
  198. interpolated = ds.interpolateGroupBys(['[[test]]'], {});
  199. });
  200. it('should replace the variable with an array of group bys', () => {
  201. expect(interpolated.length).toBe(2);
  202. expect(interpolated[0]).toBe('groupby1');
  203. expect(interpolated[1]).toBe('groupby2');
  204. });
  205. });
  206. });
  207. describe('unit parsing', () => {
  208. let ds: StackdriverDataSource, res: any;
  209. beforeEach(() => {
  210. ds = new StackdriverDataSource(instanceSettings, noopBackendSrv, templateSrv, timeSrv);
  211. });
  212. describe('when theres only one target', () => {
  213. describe('and the stackdriver unit doesnt have a corresponding grafana unit', () => {
  214. beforeEach(() => {
  215. res = ds.resolvePanelUnitFromTargets([{ unit: 'megaseconds' }]);
  216. });
  217. it('should return undefined', () => {
  218. expect(res).toBeUndefined();
  219. });
  220. });
  221. describe('and the stackdriver unit has a corresponding grafana unit', () => {
  222. beforeEach(() => {
  223. res = ds.resolvePanelUnitFromTargets([{ unit: 'bit' }]);
  224. });
  225. it('should return bits', () => {
  226. expect(res).toEqual('bits');
  227. });
  228. });
  229. });
  230. describe('when theres more than one target', () => {
  231. describe('and all target units are the same', () => {
  232. beforeEach(() => {
  233. res = ds.resolvePanelUnitFromTargets([{ unit: 'bit' }, { unit: 'bit' }]);
  234. });
  235. it('should return bits', () => {
  236. expect(res).toEqual('bits');
  237. });
  238. });
  239. describe('and all target units are the same but doesnt have grafana mappings', () => {
  240. beforeEach(() => {
  241. res = ds.resolvePanelUnitFromTargets([{ unit: 'megaseconds' }, { unit: 'megaseconds' }]);
  242. });
  243. it('should return the default value of undefined', () => {
  244. expect(res).toBeUndefined();
  245. });
  246. });
  247. describe('and all target units are not the same', () => {
  248. beforeEach(() => {
  249. res = ds.resolvePanelUnitFromTargets([{ unit: 'bit' }, { unit: 'min' }]);
  250. });
  251. it('should return the default value of undefined', () => {
  252. expect(res).toBeUndefined();
  253. });
  254. });
  255. });
  256. });
  257. });
  258. function initTemplateSrv(values: any, multi = false) {
  259. const templateSrv = new TemplateSrv();
  260. templateSrv.init([
  261. new CustomVariable(
  262. {
  263. name: 'test',
  264. current: {
  265. value: values,
  266. },
  267. multi: multi,
  268. },
  269. {} as any
  270. ),
  271. ]);
  272. return templateSrv;
  273. }