datasource.test.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. import StackdriverDataSource from '../datasource';
  2. import { metricDescriptors } from './testData';
  3. import moment from 'moment';
  4. describe('StackdriverDataSource', () => {
  5. const instanceSettings = {
  6. jsonData: {
  7. projectName: 'testproject',
  8. },
  9. };
  10. describe('when performing testDataSource', () => {
  11. describe('and call to stackdriver api succeeds', () => {
  12. let ds;
  13. let result;
  14. beforeEach(async () => {
  15. const backendSrv = {
  16. async datasourceRequest() {
  17. return Promise.resolve({ status: 200 });
  18. },
  19. };
  20. ds = new StackdriverDataSource(instanceSettings, backendSrv);
  21. result = await ds.testDatasource();
  22. });
  23. it('should return successfully', () => {
  24. expect(result.status).toBe('success');
  25. });
  26. });
  27. describe('and a list of metricDescriptors are returned', () => {
  28. let ds;
  29. let result;
  30. beforeEach(async () => {
  31. const backendSrv = {
  32. datasourceRequest: async () => Promise.resolve({ status: 200, data: metricDescriptors }),
  33. };
  34. ds = new StackdriverDataSource(instanceSettings, backendSrv);
  35. result = await ds.testDatasource();
  36. });
  37. it('should return status success', () => {
  38. expect(result.status).toBe('success');
  39. });
  40. });
  41. describe('and call to stackdriver api fails with 400 error', () => {
  42. let ds;
  43. let result;
  44. beforeEach(async () => {
  45. const backendSrv = {
  46. datasourceRequest: async () =>
  47. Promise.reject({
  48. statusText: 'Bad Request',
  49. data: { error: { code: 400, message: 'Field interval.endTime had an invalid value' } },
  50. }),
  51. };
  52. ds = new StackdriverDataSource(instanceSettings, backendSrv);
  53. result = await ds.testDatasource();
  54. });
  55. it('should return error status and a detailed error message', () => {
  56. expect(result.status).toEqual('error');
  57. expect(result.message).toBe('Stackdriver: Bad Request: 400. Field interval.endTime had an invalid value');
  58. });
  59. });
  60. });
  61. describe('when performing getProjects', () => {
  62. describe('and call to resource manager api succeeds', () => {
  63. let ds;
  64. let result;
  65. beforeEach(async () => {
  66. const response = {
  67. projects: [
  68. {
  69. projectNumber: '853996325002',
  70. projectId: 'test-project',
  71. lifecycleState: 'ACTIVE',
  72. name: 'Test Project',
  73. createTime: '2015-06-02T14:16:08.520Z',
  74. parent: {
  75. type: 'organization',
  76. id: '853996325002',
  77. },
  78. },
  79. ],
  80. };
  81. const backendSrv = {
  82. async datasourceRequest() {
  83. return Promise.resolve({ status: 200, data: response });
  84. },
  85. };
  86. ds = new StackdriverDataSource(instanceSettings, backendSrv);
  87. result = await ds.getProjects();
  88. });
  89. it('should return successfully', () => {
  90. expect(result.length).toBe(1);
  91. expect(result[0].id).toBe('test-project');
  92. expect(result[0].name).toBe('Test Project');
  93. });
  94. });
  95. });
  96. describe('When performing query', () => {
  97. const options = {
  98. range: {
  99. from: moment.utc('2017-08-22T20:00:00Z'),
  100. to: moment.utc('2017-08-22T23:59:00Z'),
  101. },
  102. rangeRaw: {
  103. from: 'now-4h',
  104. to: 'now',
  105. },
  106. targets: [
  107. {
  108. refId: 'A',
  109. aggregation: {},
  110. },
  111. ],
  112. };
  113. describe('and no time series data is returned', () => {
  114. let ds;
  115. const response = {
  116. results: {
  117. A: {
  118. refId: 'A',
  119. meta: {
  120. rawQuery: 'arawquerystring',
  121. },
  122. series: null,
  123. tables: null,
  124. },
  125. },
  126. };
  127. beforeEach(() => {
  128. const backendSrv = {
  129. datasourceRequest: async () => Promise.resolve({ status: 200, data: response }),
  130. };
  131. ds = new StackdriverDataSource(instanceSettings, backendSrv);
  132. });
  133. it('should return a list of datapoints', () => {
  134. return ds.query(options).then(results => {
  135. expect(results.data.length).toBe(0);
  136. });
  137. });
  138. });
  139. });
  140. describe('when performing getMetricTypes', () => {
  141. describe('and call to stackdriver api succeeds', () => {});
  142. let ds;
  143. let result;
  144. beforeEach(async () => {
  145. const backendSrv = {
  146. async datasourceRequest() {
  147. return Promise.resolve({
  148. data: {
  149. metricDescriptors: [
  150. {
  151. displayName: 'test metric name 1',
  152. type: 'test metric type 1',
  153. },
  154. {
  155. displayName: 'test metric name 2',
  156. type: 'test metric type 2',
  157. },
  158. ],
  159. },
  160. });
  161. },
  162. };
  163. ds = new StackdriverDataSource(instanceSettings, backendSrv);
  164. result = await ds.getMetricTypes();
  165. });
  166. it('should return successfully', () => {
  167. expect(result.length).toBe(2);
  168. expect(result[0].id).toBe('test metric type 1');
  169. expect(result[0].name).toBe('test metric name 1');
  170. });
  171. });
  172. });