datasource.test.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import StackdriverDataSource from '../datasource';
  2. import { metricDescriptors } from './testData';
  3. describe('StackdriverDataSource', () => {
  4. describe('when performing testDataSource', () => {
  5. describe('and call to stackdriver api succeeds', () => {
  6. let ds;
  7. let result;
  8. beforeEach(async () => {
  9. const backendSrv = {
  10. async datasourceRequest() {
  11. return Promise.resolve({ status: 200 });
  12. },
  13. };
  14. ds = new StackdriverDataSource({}, backendSrv);
  15. result = await ds.testDatasource();
  16. });
  17. it('should return successfully', () => {
  18. expect(result.status).toBe('success');
  19. });
  20. });
  21. describe('and a list of metricDescriptors are returned', () => {
  22. let ds;
  23. let result;
  24. beforeEach(async () => {
  25. const backendSrv = {
  26. datasourceRequest: async () => Promise.resolve({ status: 200, data: metricDescriptors }),
  27. };
  28. ds = new StackdriverDataSource({}, backendSrv);
  29. result = await ds.testDatasource();
  30. });
  31. it('should return status success', () => {
  32. expect(result.status).toBe('success');
  33. });
  34. });
  35. describe('and call to stackdriver api fails with 400 error', () => {
  36. let ds;
  37. let result;
  38. beforeEach(async () => {
  39. const backendSrv = {
  40. datasourceRequest: async () =>
  41. Promise.reject({
  42. statusText: 'Bad Request',
  43. data: { error: { code: 400, message: 'Field interval.endTime had an invalid value' } },
  44. }),
  45. };
  46. ds = new StackdriverDataSource({}, backendSrv);
  47. result = await ds.testDatasource();
  48. });
  49. it('should return error status and a detailed error message', () => {
  50. expect(result.status).toEqual('error');
  51. expect(result.message).toBe('Stackdriver: Bad Request: 400. Field interval.endTime had an invalid value');
  52. });
  53. });
  54. });
  55. describe('when performing getProjects', () => {
  56. describe('and call to resource manager api succeeds', () => {
  57. let ds;
  58. let result;
  59. beforeEach(async () => {
  60. const response = {
  61. projects: [
  62. {
  63. projectNumber: '853996325002',
  64. projectId: 'test-project',
  65. lifecycleState: 'ACTIVE',
  66. name: 'Test Project',
  67. createTime: '2015-06-02T14:16:08.520Z',
  68. parent: {
  69. type: 'organization',
  70. id: '853996325002',
  71. },
  72. },
  73. ],
  74. };
  75. const backendSrv = {
  76. async datasourceRequest() {
  77. return Promise.resolve({ status: 200, data: response });
  78. },
  79. };
  80. ds = new StackdriverDataSource({}, backendSrv);
  81. result = await ds.getProjects();
  82. });
  83. it('should return successfully', () => {
  84. expect(result.length).toBe(1);
  85. expect(result[0].id).toBe('test-project');
  86. expect(result[0].name).toBe('Test Project');
  87. });
  88. });
  89. });
  90. });