datasource.test.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import { PrometheusDatasource } from './datasource';
  2. import { DataSourceInstanceSettings } from '@grafana/ui';
  3. import { PromOptions } from './types';
  4. import { dateTime, LoadingState } from '@grafana/data';
  5. const defaultInstanceSettings: DataSourceInstanceSettings<PromOptions> = {
  6. url: 'test_prom',
  7. jsonData: {},
  8. } as any;
  9. const backendSrvMock: any = {
  10. datasourceRequest: jest.fn(),
  11. };
  12. const templateSrvMock: any = {
  13. replace(): null {
  14. return null;
  15. },
  16. getAdhocFilters(): any[] {
  17. return [];
  18. },
  19. };
  20. const timeSrvMock: any = {
  21. timeRange(): any {
  22. return {
  23. from: dateTime(),
  24. to: dateTime(),
  25. };
  26. },
  27. };
  28. describe('datasource', () => {
  29. describe('query', () => {
  30. const ds = new PrometheusDatasource(
  31. defaultInstanceSettings,
  32. {} as any,
  33. backendSrvMock,
  34. templateSrvMock,
  35. timeSrvMock
  36. );
  37. it('returns empty array when no queries', done => {
  38. expect.assertions(2);
  39. ds.query(makeQuery([])).subscribe({
  40. next(next) {
  41. expect(next.data).toEqual([]);
  42. expect(next.state).toBe(LoadingState.Done);
  43. },
  44. complete() {
  45. done();
  46. },
  47. });
  48. });
  49. it('performs time series queries', done => {
  50. expect.assertions(2);
  51. backendSrvMock.datasourceRequest.mockReturnValueOnce(Promise.resolve(makePromResponse()));
  52. ds.query(makeQuery([{}])).subscribe({
  53. next(next) {
  54. expect(next.data.length).not.toBe(0);
  55. expect(next.state).toBe(LoadingState.Done);
  56. },
  57. complete() {
  58. done();
  59. },
  60. });
  61. });
  62. it('with 2 queries, waits for all to finish until sending Done status', done => {
  63. expect.assertions(4);
  64. backendSrvMock.datasourceRequest.mockReturnValue(Promise.resolve(makePromResponse()));
  65. const responseStatus = [LoadingState.Loading, LoadingState.Done];
  66. ds.query(makeQuery([{}, {}])).subscribe({
  67. next(next) {
  68. expect(next.data.length).not.toBe(0);
  69. expect(next.state).toBe(responseStatus.shift());
  70. },
  71. complete() {
  72. done();
  73. },
  74. });
  75. });
  76. });
  77. });
  78. function makeQuery(targets: any[]): any {
  79. return {
  80. targets: targets.map(t => {
  81. return {
  82. instant: false,
  83. start: dateTime().subtract(5, 'minutes'),
  84. end: dateTime(),
  85. expr: 'test',
  86. ...t,
  87. };
  88. }),
  89. range: {
  90. from: dateTime(),
  91. to: dateTime(),
  92. },
  93. interval: '15s',
  94. };
  95. }
  96. /**
  97. * Creates a pretty bogus prom response. Definitelly needs more work but right now we do not test the contents of the
  98. * messages anyway.
  99. */
  100. function makePromResponse() {
  101. return {
  102. data: {
  103. data: {
  104. result: [
  105. {
  106. metric: {
  107. __name__: 'test_metric',
  108. },
  109. values: [[1568369640, 1]],
  110. },
  111. ],
  112. resultType: 'matrix',
  113. },
  114. },
  115. };
  116. }