]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/blob
6ac93140243142a97677ca9632a119bef7bb131d
[ceph-ci.git] /
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: string, value: any) => ({
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 = (
44       name: string,
45       value: any,
46       helpText: string,
47       successClass: boolean
48     ) => {
49       const match = service.singleMatch(addMatcher(name, value), rules);
50       expect(match.status).toBe(helpText);
51       expect(match.cssClass).toBe(successClass ? 'has-success' : 'has-warning');
52     };
53
54     it('should match no rule and no alert', () => {
55       expectSingleMatch(
56         'alertname',
57         'alert',
58         'Your matcher seems to match no currently defined rule or active alert.',
59         false
60       );
61     });
62
63     it('should match a rule with no alert', () => {
64       expectSingleMatch('alertname', 'alert1', 'Matches 1 rule with no active alerts.', false);
65     });
66
67     it('should match a rule and an alert', () => {
68       expectSingleMatch('alertname', 'alert0', 'Matches 1 rule with 1 active alert.', true);
69     });
70
71     it('should match multiple rules and an alert', () => {
72       expectSingleMatch('severity', 'someSeverity', 'Matches 2 rules with 1 active alert.', true);
73     });
74
75     it('should match multiple rules and multiple alerts', () => {
76       expectSingleMatch('job', 'someJob', 'Matches 2 rules with 2 active alerts.', true);
77     });
78
79     it('should return any match if regex is checked', () => {
80       const match = service.singleMatch(
81         {
82           name: 'severity',
83           value: 'someSeverity',
84           isRegex: true
85         },
86         rules
87       );
88       expect(match).toBeFalsy();
89     });
90   });
91
92   describe('test rule matching with multiple matcher', () => {
93     const expectMultiMatch = (matchers: any[], helpText: string, successClass: boolean) => {
94       const match = service.multiMatch(matchers, rules);
95       expect(match.status).toBe(helpText);
96       expect(match.cssClass).toBe(successClass ? 'has-success' : 'has-warning');
97     };
98
99     it('should match no rule and no alert', () => {
100       expectMultiMatch(
101         [addMatcher('alertname', 'alert0'), addMatcher('job', 'ceph')],
102         'Your matcher seems to match no currently defined rule or active alert.',
103         false
104       );
105     });
106
107     it('should match a rule with no alert', () => {
108       expectMultiMatch(
109         [addMatcher('severity', 'someSeverity'), addMatcher('alertname', 'alert1')],
110         'Matches 1 rule with no active alerts.',
111         false
112       );
113     });
114
115     it('should match a rule and an alert', () => {
116       expectMultiMatch(
117         [addMatcher('instance', 'someInstance'), addMatcher('alertname', 'alert0')],
118         'Matches 1 rule with 1 active alert.',
119         true
120       );
121     });
122
123     it('should return any match if regex is checked', () => {
124       const match = service.multiMatch(
125         [
126           addMatcher('instance', 'someInstance'),
127           {
128             name: 'severity',
129             value: 'someSeverity',
130             isRegex: true
131           }
132         ],
133         rules
134       );
135       expect(match).toBeFalsy();
136     });
137   });
138 });