matchers.test.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import { MatcherOperator } from 'app/plugins/datasource/alertmanager/types';
  2. import { mockPromAlert } from '../mocks';
  3. import { getMatcherQueryParams, findAlertInstancesWithMatchers, parseQueryParamMatchers } from './matchers';
  4. describe('Unified Alerting matchers', () => {
  5. describe('getMatcherQueryParams tests', () => {
  6. it('Should create an entry for each label', () => {
  7. const params = getMatcherQueryParams({ foo: 'bar', alertname: 'TestData - No data', rule_uid: 'YNZBpGJnk' });
  8. const matcherParams = params.getAll('matcher');
  9. expect(matcherParams).toHaveLength(3);
  10. expect(matcherParams).toContain('foo=bar');
  11. expect(matcherParams).toContain('alertname=TestData - No data');
  12. expect(matcherParams).toContain('rule_uid=YNZBpGJnk');
  13. });
  14. });
  15. describe('parseQueryParamMatchers tests', () => {
  16. it('Should create a matcher for each unique label-expression pair', () => {
  17. const matchers = parseQueryParamMatchers(['alertname=TestData 1', 'rule_uid=YNZBpGJnk']);
  18. expect(matchers).toHaveLength(2);
  19. expect(matchers[0].name).toBe('alertname');
  20. expect(matchers[0].value).toBe('TestData 1');
  21. expect(matchers[1].name).toBe('rule_uid');
  22. expect(matchers[1].value).toBe('YNZBpGJnk');
  23. });
  24. it('Should create one matcher, using the first occurrence when duplicated labels exists', () => {
  25. const matchers = parseQueryParamMatchers(['alertname=TestData 1', 'alertname=TestData 2']);
  26. expect(matchers).toHaveLength(1);
  27. expect(matchers[0].name).toBe('alertname');
  28. expect(matchers[0].value).toBe('TestData 1');
  29. });
  30. });
  31. describe('matchLabelsToMatchers', () => {
  32. it('should match for equal', () => {
  33. const matchers = [{ name: 'foo', value: 'bar', operator: MatcherOperator.equal }];
  34. const alerts = [mockPromAlert({ labels: { foo: 'bar' } }), mockPromAlert({ labels: { foo: 'baz' } })];
  35. const matchedAlerts = findAlertInstancesWithMatchers(alerts, matchers);
  36. expect(matchedAlerts).toHaveLength(1);
  37. });
  38. it('should match for not equal', () => {
  39. const matchers = [{ name: 'foo', value: 'bar', operator: MatcherOperator.notEqual }];
  40. const alerts = [mockPromAlert({ labels: { foo: 'bar' } }), mockPromAlert({ labels: { foo: 'baz' } })];
  41. const matchedAlerts = findAlertInstancesWithMatchers(alerts, matchers);
  42. expect(matchedAlerts).toHaveLength(1);
  43. });
  44. it('should match for regex', () => {
  45. const matchers = [{ name: 'foo', value: 'b{1}a.*', operator: MatcherOperator.regex }];
  46. const alerts = [
  47. mockPromAlert({ labels: { foo: 'bbr' } }),
  48. mockPromAlert({ labels: { foo: 'aba' } }), // This does not match because the regex is implicitly anchored.
  49. mockPromAlert({ labels: { foo: 'ba' } }),
  50. mockPromAlert({ labels: { foo: 'bar' } }),
  51. mockPromAlert({ labels: { foo: 'baz' } }),
  52. mockPromAlert({ labels: { foo: 'bas' } }),
  53. ];
  54. const matchedAlerts = findAlertInstancesWithMatchers(alerts, matchers);
  55. expect(matchedAlerts).toHaveLength(4);
  56. expect(matchedAlerts.map((instance) => instance.data.matchedInstance.labels.foo)).toEqual([
  57. 'ba',
  58. 'bar',
  59. 'baz',
  60. 'bas',
  61. ]);
  62. });
  63. it('should not match regex', () => {
  64. const matchers = [{ name: 'foo', value: 'ba{3}', operator: MatcherOperator.notRegex }];
  65. const alerts = [
  66. mockPromAlert({ labels: { foo: 'bar' } }),
  67. mockPromAlert({ labels: { foo: 'baz' } }),
  68. mockPromAlert({ labels: { foo: 'baaa' } }),
  69. mockPromAlert({ labels: { foo: 'bas' } }),
  70. ];
  71. const matchedAlerts = findAlertInstancesWithMatchers(alerts, matchers);
  72. expect(matchedAlerts).toHaveLength(3);
  73. expect(matchedAlerts.map((instance) => instance.data.matchedInstance.labels.foo)).toEqual(['bar', 'baz', 'bas']);
  74. });
  75. });
  76. });