]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/frontend/src/app/shared/services/prometheus-silence-matcher.service.spec.ts
import 14.2.4 nautilus point release
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / src / app / shared / services / prometheus-silence-matcher.service.spec.ts
1 import { TestBed } from '@angular/core/testing';
2
3 import {
4 configureTestBed,
5 i18nProviders,
6 PrometheusHelper
7 } from '../../../testing/unit-test-helper';
8 import { PrometheusRule } from '../models/prometheus-alerts';
9 import { SharedModule } from '../shared.module';
10 import { PrometheusSilenceMatcherService } from './prometheus-silence-matcher.service';
11
12 describe('PrometheusSilenceMatcherService', () => {
13 let service: PrometheusSilenceMatcherService;
14 let prometheus: PrometheusHelper;
15 let rules: PrometheusRule[];
16
17 configureTestBed({
18 imports: [SharedModule],
19 providers: [i18nProviders]
20 });
21
22 const addMatcher = (name, value) => ({
23 name: name,
24 value: value,
25 isRegex: false
26 });
27
28 beforeEach(() => {
29 prometheus = new PrometheusHelper();
30 service = TestBed.get(PrometheusSilenceMatcherService);
31 rules = [
32 prometheus.createRule('alert0', 'someSeverity', [prometheus.createAlert('alert0')]),
33 prometheus.createRule('alert1', 'someSeverity', []),
34 prometheus.createRule('alert2', 'someOtherSeverity', [prometheus.createAlert('alert2')])
35 ];
36 });
37
38 it('should create', () => {
39 expect(service).toBeTruthy();
40 });
41
42 describe('test rule matching with one matcher', () => {
43 const expectSingleMatch = (name, value, helpText, successClass: boolean) => {
44 const match = service.singleMatch(addMatcher(name, value), rules);
45 expect(match.status).toBe(helpText);
46 expect(match.cssClass).toBe(successClass ? 'has-success' : 'has-warning');
47 };
48
49 it('should match no rule and no alert', () => {
50 expectSingleMatch(
51 'alertname',
52 'alert',
53 'Your matcher seems to match no currently defined rule or active alert.',
54 false
55 );
56 });
57
58 it('should match a rule with no alert', () => {
59 expectSingleMatch('alertname', 'alert1', 'Matches 1 rule with no active alerts.', false);
60 });
61
62 it('should match a rule and an alert', () => {
63 expectSingleMatch('alertname', 'alert0', 'Matches 1 rule with 1 active alert.', true);
64 });
65
66 it('should match multiple rules and an alert', () => {
67 expectSingleMatch('severity', 'someSeverity', 'Matches 2 rules with 1 active alert.', true);
68 });
69
70 it('should match multiple rules and multiple alerts', () => {
71 expectSingleMatch('job', 'someJob', 'Matches 2 rules with 2 active alerts.', true);
72 });
73
74 it('should return any match if regex is checked', () => {
75 const match = service.singleMatch(
76 {
77 name: 'severity',
78 value: 'someSeverity',
79 isRegex: true
80 },
81 rules
82 );
83 expect(match).toBeFalsy();
84 });
85 });
86
87 describe('test rule matching with multiple matcher', () => {
88 const expectMultiMatch = (matchers, helpText, successClass: boolean) => {
89 const match = service.multiMatch(matchers, rules);
90 expect(match.status).toBe(helpText);
91 expect(match.cssClass).toBe(successClass ? 'has-success' : 'has-warning');
92 };
93
94 it('should match no rule and no alert', () => {
95 expectMultiMatch(
96 [addMatcher('alertname', 'alert0'), addMatcher('job', 'ceph')],
97 'Your matcher seems to match no currently defined rule or active alert.',
98 false
99 );
100 });
101
102 it('should match a rule with no alert', () => {
103 expectMultiMatch(
104 [addMatcher('severity', 'someSeverity'), addMatcher('alertname', 'alert1')],
105 'Matches 1 rule with no active alerts.',
106 false
107 );
108 });
109
110 it('should match a rule and an alert', () => {
111 expectMultiMatch(
112 [addMatcher('instance', 'someInstance'), addMatcher('alertname', 'alert0')],
113 'Matches 1 rule with 1 active alert.',
114 true
115 );
116 });
117
118 it('should return any match if regex is checked', () => {
119 const match = service.multiMatch(
120 [
121 addMatcher('instance', 'someInstance'),
122 {
123 name: 'severity',
124 value: 'someSeverity',
125 isRegex: true
126 }
127 ],
128 rules
129 );
130 expect(match).toBeFalsy();
131 });
132 });
133 });