datasource.test.ts 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. import StackdriverDataSource from '../datasource';
  2. import { metricDescriptors } from './testData';
  3. import moment from 'moment';
  4. import { TemplateSrvStub } from 'test/specs/helpers';
  5. describe('StackdriverDataSource', () => {
  6. const instanceSettings = {
  7. jsonData: {
  8. projectName: 'testproject',
  9. },
  10. };
  11. const templateSrv = new TemplateSrvStub();
  12. const timeSrv = {};
  13. describe('when performing testDataSource', () => {
  14. describe('and call to stackdriver api succeeds', () => {
  15. let ds;
  16. let result;
  17. beforeEach(async () => {
  18. const backendSrv = {
  19. async datasourceRequest() {
  20. return Promise.resolve({ status: 200 });
  21. },
  22. };
  23. ds = new StackdriverDataSource(instanceSettings, backendSrv, templateSrv, timeSrv);
  24. result = await ds.testDatasource();
  25. });
  26. it('should return successfully', () => {
  27. expect(result.status).toBe('success');
  28. });
  29. });
  30. describe('and a list of metricDescriptors are returned', () => {
  31. let ds;
  32. let result;
  33. beforeEach(async () => {
  34. const backendSrv = {
  35. datasourceRequest: async () => Promise.resolve({ status: 200, data: metricDescriptors }),
  36. };
  37. ds = new StackdriverDataSource(instanceSettings, backendSrv, templateSrv, timeSrv);
  38. result = await ds.testDatasource();
  39. });
  40. it('should return status success', () => {
  41. expect(result.status).toBe('success');
  42. });
  43. });
  44. describe('and call to stackdriver api fails with 400 error', () => {
  45. let ds;
  46. let result;
  47. beforeEach(async () => {
  48. const backendSrv = {
  49. datasourceRequest: async () =>
  50. Promise.reject({
  51. statusText: 'Bad Request',
  52. data: {
  53. error: JSON.stringify({ error: { code: 400, message: 'Field interval.endTime had an invalid value' } }),
  54. },
  55. }),
  56. };
  57. ds = new StackdriverDataSource(instanceSettings, backendSrv, templateSrv, timeSrv);
  58. result = await ds.testDatasource();
  59. });
  60. it('should return error status and a detailed error message', () => {
  61. expect(result.status).toEqual('error');
  62. expect(result.message).toBe('Stackdriver: Bad Request: 400. Field interval.endTime had an invalid value');
  63. });
  64. });
  65. });
  66. describe('When performing query', () => {
  67. const options = {
  68. range: {
  69. from: moment.utc('2017-08-22T20:00:00Z'),
  70. to: moment.utc('2017-08-22T23:59:00Z'),
  71. },
  72. rangeRaw: {
  73. from: 'now-4h',
  74. to: 'now',
  75. },
  76. targets: [
  77. {
  78. refId: 'A',
  79. aggregation: {},
  80. },
  81. ],
  82. };
  83. describe('and no time series data is returned', () => {
  84. let ds;
  85. const response = {
  86. results: {
  87. A: {
  88. refId: 'A',
  89. meta: {
  90. rawQuery: 'arawquerystring',
  91. },
  92. series: null,
  93. tables: null,
  94. },
  95. },
  96. };
  97. beforeEach(() => {
  98. const backendSrv = {
  99. datasourceRequest: async () => Promise.resolve({ status: 200, data: response }),
  100. };
  101. ds = new StackdriverDataSource(instanceSettings, backendSrv, templateSrv, timeSrv);
  102. });
  103. it('should return a list of datapoints', () => {
  104. return ds.query(options).then(results => {
  105. expect(results.data.length).toBe(0);
  106. });
  107. });
  108. });
  109. });
  110. describe('when performing getMetricTypes', () => {
  111. describe('and call to stackdriver api succeeds', () => {});
  112. let ds;
  113. let result;
  114. beforeEach(async () => {
  115. const backendSrv = {
  116. async datasourceRequest() {
  117. return Promise.resolve({
  118. data: {
  119. metricDescriptors: [
  120. {
  121. displayName: 'test metric name 1',
  122. type: 'compute.googleapis.com/instance/cpu/test-metric-type-1',
  123. description: 'A description',
  124. },
  125. {
  126. type: 'logging.googleapis.com/user/logbased-metric-with-no-display-name',
  127. },
  128. ],
  129. },
  130. });
  131. },
  132. };
  133. ds = new StackdriverDataSource(instanceSettings, backendSrv, templateSrv, timeSrv);
  134. result = await ds.getMetricTypes();
  135. });
  136. it('should return successfully', () => {
  137. expect(result.length).toBe(2);
  138. expect(result[0].service).toBe('compute.googleapis.com');
  139. expect(result[0].serviceShortName).toBe('compute');
  140. expect(result[0].type).toBe('compute.googleapis.com/instance/cpu/test-metric-type-1');
  141. expect(result[0].displayName).toBe('test metric name 1');
  142. expect(result[0].description).toBe('A description');
  143. expect(result[1].type).toBe('logging.googleapis.com/user/logbased-metric-with-no-display-name');
  144. expect(result[1].displayName).toBe('logging.googleapis.com/user/logbased-metric-with-no-display-name');
  145. });
  146. });
  147. describe('when interpolating a template variable for group bys', () => {
  148. let interpolated;
  149. describe('and is single value variable', () => {
  150. beforeEach(() => {
  151. templateSrv.data = {
  152. test: 'groupby1',
  153. };
  154. const ds = new StackdriverDataSource(instanceSettings, {}, templateSrv, timeSrv);
  155. interpolated = ds.interpolateGroupBys(['[[test]]'], {});
  156. });
  157. it('should replace the variable with the value', () => {
  158. expect(interpolated.length).toBe(1);
  159. expect(interpolated[0]).toBe('groupby1');
  160. });
  161. });
  162. describe('and is multi value variable', () => {
  163. beforeEach(() => {
  164. templateSrv.data = {
  165. test: 'groupby1,groupby2',
  166. };
  167. const ds = new StackdriverDataSource(instanceSettings, {}, templateSrv, timeSrv);
  168. interpolated = ds.interpolateGroupBys(['[[test]]'], {});
  169. });
  170. it('should replace the variable with an array of group bys', () => {
  171. expect(interpolated.length).toBe(2);
  172. expect(interpolated[0]).toBe('groupby1');
  173. expect(interpolated[1]).toBe('groupby2');
  174. });
  175. });
  176. });
  177. describe('unit parsing', () => {
  178. let ds, res;
  179. beforeEach(() => {
  180. ds = new StackdriverDataSource(instanceSettings, {}, templateSrv, timeSrv);
  181. });
  182. describe('when theres only one target', () => {
  183. describe('and the stackdriver unit doesnt have a corresponding grafana unit', () => {
  184. beforeEach(() => {
  185. res = ds.resolvePanelUnitFromTargets([{ unit: 'megaseconds' }]);
  186. });
  187. it('should return undefined', () => {
  188. expect(res).toBeUndefined();
  189. });
  190. });
  191. describe('and the stackdriver unit has a corresponding grafana unit', () => {
  192. beforeEach(() => {
  193. res = ds.resolvePanelUnitFromTargets([{ unit: 'bit' }]);
  194. });
  195. it('should return bits', () => {
  196. expect(res).toEqual('bits');
  197. });
  198. });
  199. });
  200. describe('when theres more than one target', () => {
  201. describe('and all target units are the same', () => {
  202. beforeEach(() => {
  203. res = ds.resolvePanelUnitFromTargets([{ unit: 'bit' }, { unit: 'bit' }]);
  204. });
  205. it('should return bits', () => {
  206. expect(res).toEqual('bits');
  207. });
  208. });
  209. describe('and all target units are the same but doesnt have grafana mappings', () => {
  210. beforeEach(() => {
  211. res = ds.resolvePanelUnitFromTargets([{ unit: 'megaseconds' }, { unit: 'megaseconds' }]);
  212. });
  213. it('should return the default value of undefined', () => {
  214. expect(res).toBeUndefined();
  215. });
  216. });
  217. describe('and all target units are not the same', () => {
  218. beforeEach(() => {
  219. res = ds.resolvePanelUnitFromTargets([{ unit: 'bit' }, { unit: 'min' }]);
  220. });
  221. it('should return the default value of undefined', () => {
  222. expect(res).toBeUndefined();
  223. });
  224. });
  225. });
  226. });
  227. });