]>
Commit | Line | Data |
---|---|---|
494da23a TL |
1 | import { TestBed } from '@angular/core/testing'; |
2 | ||
f67539c2 | 3 | import { configureTestBed, PrometheusHelper } from '~/testing/unit-test-helper'; |
494da23a TL |
4 | import { PrometheusRule } from '../models/prometheus-alerts'; |
5 | import { SharedModule } from '../shared.module'; | |
6 | import { PrometheusSilenceMatcherService } from './prometheus-silence-matcher.service'; | |
7 | ||
8 | describe('PrometheusSilenceMatcherService', () => { | |
9 | let service: PrometheusSilenceMatcherService; | |
10 | let prometheus: PrometheusHelper; | |
11 | let rules: PrometheusRule[]; | |
12 | ||
13 | configureTestBed({ | |
f67539c2 | 14 | imports: [SharedModule] |
494da23a TL |
15 | }); |
16 | ||
9f95a23c | 17 | const addMatcher = (name: string, value: any) => ({ |
494da23a TL |
18 | name: name, |
19 | value: value, | |
20 | isRegex: false | |
21 | }); | |
22 | ||
23 | beforeEach(() => { | |
24 | prometheus = new PrometheusHelper(); | |
f67539c2 | 25 | service = TestBed.inject(PrometheusSilenceMatcherService); |
494da23a TL |
26 | rules = [ |
27 | prometheus.createRule('alert0', 'someSeverity', [prometheus.createAlert('alert0')]), | |
28 | prometheus.createRule('alert1', 'someSeverity', []), | |
29 | prometheus.createRule('alert2', 'someOtherSeverity', [prometheus.createAlert('alert2')]) | |
30 | ]; | |
31 | }); | |
32 | ||
33 | it('should create', () => { | |
34 | expect(service).toBeTruthy(); | |
35 | }); | |
36 | ||
37 | describe('test rule matching with one matcher', () => { | |
9f95a23c TL |
38 | const expectSingleMatch = ( |
39 | name: string, | |
40 | value: any, | |
41 | helpText: string, | |
42 | successClass: boolean | |
43 | ) => { | |
494da23a TL |
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', () => { | |
9f95a23c | 88 | const expectMultiMatch = (matchers: any[], helpText: string, successClass: boolean) => { |
494da23a TL |
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 | }); |