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: string, value: any) => ({
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 = (
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');
54 it('should match no rule and no alert', () => {
58 'Your matcher seems to match no currently defined rule or active alert.',
63 it('should match a rule with no alert', () => {
64 expectSingleMatch('alertname', 'alert1', 'Matches 1 rule with no active alerts.', false);
67 it('should match a rule and an alert', () => {
68 expectSingleMatch('alertname', 'alert0', 'Matches 1 rule with 1 active alert.', true);
71 it('should match multiple rules and an alert', () => {
72 expectSingleMatch('severity', 'someSeverity', 'Matches 2 rules with 1 active alert.', true);
75 it('should match multiple rules and multiple alerts', () => {
76 expectSingleMatch('job', 'someJob', 'Matches 2 rules with 2 active alerts.', true);
79 it('should return any match if regex is checked', () => {
80 const match = service.singleMatch(
83 value: 'someSeverity',
88 expect(match).toBeFalsy();
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');
99 it('should match no rule and no alert', () => {
101 [addMatcher('alertname', 'alert0'), addMatcher('job', 'ceph')],
102 'Your matcher seems to match no currently defined rule or active alert.',
107 it('should match a rule with no alert', () => {
109 [addMatcher('severity', 'someSeverity'), addMatcher('alertname', 'alert1')],
110 'Matches 1 rule with no active alerts.',
115 it('should match a rule and an alert', () => {
117 [addMatcher('instance', 'someInstance'), addMatcher('alertname', 'alert0')],
118 'Matches 1 rule with 1 active alert.',
123 it('should return any match if regex is checked', () => {
124 const match = service.multiMatch(
126 addMatcher('instance', 'someInstance'),
129 value: 'someSeverity',
135 expect(match).toBeFalsy();