datasource.test.ts 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. defaultProject: '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: { 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. },
  80. ],
  81. };
  82. describe('and no time series data is returned', () => {
  83. let ds;
  84. const response = {
  85. results: {
  86. A: {
  87. refId: 'A',
  88. meta: {
  89. rawQuery: 'arawquerystring',
  90. },
  91. series: null,
  92. tables: null,
  93. },
  94. },
  95. };
  96. beforeEach(() => {
  97. const backendSrv = {
  98. datasourceRequest: async () => Promise.resolve({ status: 200, data: response }),
  99. };
  100. ds = new StackdriverDataSource(instanceSettings, backendSrv, templateSrv, timeSrv);
  101. });
  102. it('should return a list of datapoints', () => {
  103. return ds.query(options).then(results => {
  104. expect(results.data.length).toBe(0);
  105. });
  106. });
  107. });
  108. });
  109. describe('when performing getMetricTypes', () => {
  110. describe('and call to stackdriver api succeeds', () => {});
  111. let ds;
  112. let result;
  113. beforeEach(async () => {
  114. const backendSrv = {
  115. async datasourceRequest() {
  116. return Promise.resolve({
  117. data: {
  118. metricDescriptors: [
  119. {
  120. displayName: 'test metric name 1',
  121. type: 'compute.googleapis.com/instance/cpu/test-metric-type-1',
  122. description: 'A description',
  123. },
  124. {
  125. type: 'logging.googleapis.com/user/logbased-metric-with-no-display-name',
  126. },
  127. ],
  128. },
  129. });
  130. },
  131. };
  132. ds = new StackdriverDataSource(instanceSettings, backendSrv, templateSrv, timeSrv);
  133. result = await ds.getMetricTypes();
  134. });
  135. it('should return successfully', () => {
  136. expect(result.length).toBe(2);
  137. expect(result[0].service).toBe('compute.googleapis.com');
  138. expect(result[0].serviceShortName).toBe('compute');
  139. expect(result[0].type).toBe('compute.googleapis.com/instance/cpu/test-metric-type-1');
  140. expect(result[0].displayName).toBe('test metric name 1');
  141. expect(result[0].description).toBe('A description');
  142. expect(result[1].type).toBe('logging.googleapis.com/user/logbased-metric-with-no-display-name');
  143. expect(result[1].displayName).toBe('logging.googleapis.com/user/logbased-metric-with-no-display-name');
  144. });
  145. });
  146. describe('when interpolating a template variable for group bys', () => {
  147. let interpolated;
  148. describe('and is single value variable', () => {
  149. beforeEach(() => {
  150. templateSrv.data = {
  151. test: 'groupby1',
  152. };
  153. const ds = new StackdriverDataSource(instanceSettings, {}, templateSrv, timeSrv);
  154. interpolated = ds.interpolateGroupBys(['[[test]]'], {});
  155. });
  156. it('should replace the variable with the value', () => {
  157. expect(interpolated.length).toBe(1);
  158. expect(interpolated[0]).toBe('groupby1');
  159. });
  160. });
  161. describe('and is multi value variable', () => {
  162. beforeEach(() => {
  163. templateSrv.data = {
  164. test: 'groupby1,groupby2',
  165. };
  166. const ds = new StackdriverDataSource(instanceSettings, {}, templateSrv, timeSrv);
  167. interpolated = ds.interpolateGroupBys(['[[test]]'], {});
  168. });
  169. it('should replace the variable with an array of group bys', () => {
  170. expect(interpolated.length).toBe(2);
  171. expect(interpolated[0]).toBe('groupby1');
  172. expect(interpolated[1]).toBe('groupby2');
  173. });
  174. });
  175. });
  176. describe('unit parsing', () => {
  177. let ds, res;
  178. beforeEach(() => {
  179. ds = new StackdriverDataSource(instanceSettings, {}, templateSrv, timeSrv);
  180. });
  181. describe('when theres only one target', () => {
  182. describe('and the stackdriver unit doesnt have a corresponding grafana unit', () => {
  183. beforeEach(() => {
  184. res = ds.resolvePanelUnitFromTargets([{ unit: 'megaseconds' }]);
  185. });
  186. it('should return undefined', () => {
  187. expect(res).toBeUndefined();
  188. });
  189. });
  190. describe('and the stackdriver unit has a corresponding grafana unit', () => {
  191. beforeEach(() => {
  192. res = ds.resolvePanelUnitFromTargets([{ unit: 'bit' }]);
  193. });
  194. it('should return bits', () => {
  195. expect(res).toEqual('bits');
  196. });
  197. });
  198. });
  199. describe('when theres more than one target', () => {
  200. describe('and all target units are the same', () => {
  201. beforeEach(() => {
  202. res = ds.resolvePanelUnitFromTargets([{ unit: 'bit' }, { unit: 'bit' }]);
  203. });
  204. it('should return bits', () => {
  205. expect(res).toEqual('bits');
  206. });
  207. });
  208. describe('and all target units are the same but doesnt have grafana mappings', () => {
  209. beforeEach(() => {
  210. res = ds.resolvePanelUnitFromTargets([{ unit: 'megaseconds' }, { unit: 'megaseconds' }]);
  211. });
  212. it('should return the default value of undefined', () => {
  213. expect(res).toBeUndefined();
  214. });
  215. });
  216. describe('and all target units are not the same', () => {
  217. beforeEach(() => {
  218. res = ds.resolvePanelUnitFromTargets([{ unit: 'bit' }, { unit: 'min' }]);
  219. });
  220. it('should return the default value of undefined', () => {
  221. expect(res).toBeUndefined();
  222. });
  223. });
  224. });
  225. });
  226. });