ruler.test.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import { RulerDataSourceConfig } from 'app/types/unified-alerting';
  2. import { getDatasourceAPIUid } from '../utils/datasource';
  3. import { rulerUrlBuilder } from './ruler';
  4. jest.mock('../utils/datasource');
  5. const mocks = {
  6. getDatasourceAPIUId: jest.mocked(getDatasourceAPIUid),
  7. };
  8. describe('rulerUrlBuilder', () => {
  9. it('Should use /api/v1/rules endpoint with subtype = cortex param for legacy api version', () => {
  10. // Arrange
  11. const config: RulerDataSourceConfig = {
  12. dataSourceName: 'Cortex',
  13. apiVersion: 'legacy',
  14. };
  15. mocks.getDatasourceAPIUId.mockReturnValue('ds-uid');
  16. // Act
  17. const builder = rulerUrlBuilder(config);
  18. const rules = builder.rules();
  19. const namespace = builder.namespace('test-ns');
  20. const group = builder.namespaceGroup('test-ns', 'test-gr');
  21. // Assert
  22. expect(rules.path).toBe('/api/ruler/ds-uid/api/v1/rules');
  23. expect(rules.params).toMatchObject({ subtype: 'cortex' });
  24. expect(namespace.path).toBe('/api/ruler/ds-uid/api/v1/rules/test-ns');
  25. expect(namespace.params).toMatchObject({ subtype: 'cortex' });
  26. expect(group.path).toBe('/api/ruler/ds-uid/api/v1/rules/test-ns/test-gr');
  27. expect(group.params).toMatchObject({ subtype: 'cortex' });
  28. });
  29. it('Should use /api/v1/rules endpoint with subtype = mimir parameter for config api version', () => {
  30. // Arrange
  31. const config: RulerDataSourceConfig = {
  32. dataSourceName: 'Cortex v2',
  33. apiVersion: 'config',
  34. };
  35. mocks.getDatasourceAPIUId.mockReturnValue('ds-uid');
  36. // Act
  37. const builder = rulerUrlBuilder(config);
  38. const rules = builder.rules();
  39. const namespace = builder.namespace('test-ns');
  40. const group = builder.namespaceGroup('test-ns', 'test-gr');
  41. // Assert
  42. expect(rules.path).toBe('/api/ruler/ds-uid/api/v1/rules');
  43. expect(rules.params).toMatchObject({ subtype: 'mimir' });
  44. expect(namespace.path).toBe('/api/ruler/ds-uid/api/v1/rules/test-ns');
  45. expect(namespace.params).toMatchObject({ subtype: 'mimir' });
  46. expect(group.path).toBe('/api/ruler/ds-uid/api/v1/rules/test-ns/test-gr');
  47. expect(group.params).toMatchObject({ subtype: 'mimir' });
  48. });
  49. it('Should append source=rules parameter when custom ruler enabled', () => {
  50. // Arrange
  51. const config: RulerDataSourceConfig = {
  52. dataSourceName: 'Cortex v2',
  53. apiVersion: 'config',
  54. };
  55. mocks.getDatasourceAPIUId.mockReturnValue('ds-uid');
  56. // Act
  57. const builder = rulerUrlBuilder(config);
  58. const rules = builder.rules();
  59. const namespace = builder.namespace('test-ns');
  60. const group = builder.namespaceGroup('test-ns', 'test-gr');
  61. // Assert
  62. expect(rules.params).toMatchObject({ subtype: 'mimir' });
  63. expect(namespace.params).toMatchObject({ subtype: 'mimir' });
  64. expect(group.params).toMatchObject({ subtype: 'mimir' });
  65. });
  66. it('Should append dashboard_uid and panel_id for rules endpoint when specified', () => {
  67. // Arrange
  68. const config: RulerDataSourceConfig = {
  69. dataSourceName: 'Cortex v2',
  70. apiVersion: 'config',
  71. };
  72. mocks.getDatasourceAPIUId.mockReturnValue('ds-uid');
  73. // Act
  74. const builder = rulerUrlBuilder(config);
  75. const rules = builder.rules({ dashboardUID: 'dashboard-uid', panelId: 1234 });
  76. // Assert
  77. expect(rules.params).toMatchObject({ dashboard_uid: 'dashboard-uid', panel_id: '1234', subtype: 'mimir' });
  78. });
  79. });