1 import { TestBed } from '@angular/core/testing';
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';
12 describe('PrometheusSilenceMatcherService', () => {
13 let service: PrometheusSilenceMatcherService;
14 let prometheus: PrometheusHelper;
15 let rules: PrometheusRule[];
18 imports: [SharedModule],
19 providers: [i18nProviders]
22 const addMatcher = (name, value) => ({
29 prometheus = new PrometheusHelper();
30 service = TestBed.get(PrometheusSilenceMatcherService);
32 prometheus.createRule('alert0', 'someSeverity', [prometheus.createAlert('alert0')]),
33 prometheus.createRule('alert1', 'someSeverity', []),
34 prometheus.createRule('alert2', 'someOtherSeverity', [prometheus.createAlert('alert2')])
38 it('should create', () => {
39 expect(service).toBeTruthy();
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');
49 it('should match no rule and no alert', () => {
53 'Your matcher seems to match no currently defined rule or active alert.',
58 it('should match a rule with no alert', () => {
59 expectSingleMatch('alertname', 'alert1', 'Matches 1 rule with no active alerts.', false);
62 it('should match a rule and an alert', () => {
63 expectSingleMatch('alertname', 'alert0', 'Matches 1 rule with 1 active alert.', true);
66 it('should match multiple rules and an alert', () => {
67 expectSingleMatch('severity', 'someSeverity', 'Matches 2 rules with 1 active alert.', true);
70 it('should match multiple rules and multiple alerts', () => {
71 expectSingleMatch('job', 'someJob', 'Matches 2 rules with 2 active alerts.', true);
74 it('should return any match if regex is checked', () => {
75 const match = service.singleMatch(
78 value: 'someSeverity',
83 expect(match).toBeFalsy();
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');
94 it('should match no rule and no alert', () => {
96 [addMatcher('alertname', 'alert0'), addMatcher('job', 'ceph')],
97 'Your matcher seems to match no currently defined rule or active alert.',
102 it('should match a rule with no alert', () => {
104 [addMatcher('severity', 'someSeverity'), addMatcher('alertname', 'alert1')],
105 'Matches 1 rule with no active alerts.',
110 it('should match a rule and an alert', () => {
112 [addMatcher('instance', 'someInstance'), addMatcher('alertname', 'alert0')],
113 'Matches 1 rule with 1 active alert.',
118 it('should return any match if regex is checked', () => {
119 const match = service.multiMatch(
121 addMatcher('instance', 'someInstance'),
124 value: 'someSeverity',
130 expect(match).toBeFalsy();