]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/blob
112626ff6e928788889cadba18ed1089b3f08699
[ceph-ci.git] /
1 import { HttpClientTestingModule } from '@angular/common/http/testing';
2 import { ComponentFixture, TestBed } from '@angular/core/testing';
3 import { ReactiveFormsModule } from '@angular/forms';
4 import { RouterTestingModule } from '@angular/router/testing';
5
6 import { NgbTypeaheadModule } from '@ng-bootstrap/ng-bootstrap';
7 import { BsModalRef } from 'ngx-bootstrap/modal';
8
9 import {
10   configureTestBed,
11   FixtureHelper,
12   FormHelper,
13   i18nProviders,
14   PrometheusHelper
15 } from '../../../../../testing/unit-test-helper';
16 import { SharedModule } from '../../../../shared/shared.module';
17 import { SilenceMatcherModalComponent } from './silence-matcher-modal.component';
18
19 describe('SilenceMatcherModalComponent', () => {
20   let component: SilenceMatcherModalComponent;
21   let fixture: ComponentFixture<SilenceMatcherModalComponent>;
22
23   let formH: FormHelper;
24   let fixtureH: FixtureHelper;
25   let prometheus: PrometheusHelper;
26
27   configureTestBed({
28     declarations: [SilenceMatcherModalComponent],
29     imports: [
30       HttpClientTestingModule,
31       SharedModule,
32       NgbTypeaheadModule,
33       RouterTestingModule,
34       ReactiveFormsModule
35     ],
36     providers: [BsModalRef, i18nProviders]
37   });
38
39   beforeEach(() => {
40     fixture = TestBed.createComponent(SilenceMatcherModalComponent);
41     component = fixture.componentInstance;
42
43     fixtureH = new FixtureHelper(fixture);
44     formH = new FormHelper(component.form);
45     prometheus = new PrometheusHelper();
46
47     component.rules = [
48       prometheus.createRule('alert0', 'someSeverity', [prometheus.createAlert('alert0')]),
49       prometheus.createRule('alert1', 'someSeverity', [])
50     ];
51     fixture.detectChanges();
52   });
53
54   it('should create', () => {
55     expect(component).toBeTruthy();
56   });
57
58   it('should have a name field', () => {
59     formH.expectError('name', 'required');
60     formH.expectValidChange('name', 'alertname');
61   });
62
63   it('should only allow a specific set of name attributes', () => {
64     expect(component.nameAttributes).toEqual(['alertname', 'instance', 'job', 'severity']);
65   });
66
67   it('should autocomplete a list based on the set name', () => {
68     const expectations = {
69       alertname: ['alert0', 'alert1'],
70       instance: ['someInstance'],
71       job: ['someJob'],
72       severity: ['someSeverity']
73     };
74     Object.keys(expectations).forEach((key) => {
75       formH.setValue('name', key);
76       expect(component.possibleValues).toEqual(expectations[key]);
77     });
78   });
79
80   describe('test rule matching', () => {
81     const expectMatch = (name: string, value: string, helpText: string) => {
82       component.preFillControls({
83         name: name,
84         value: value,
85         isRegex: false
86       });
87       expect(fixtureH.getText('#match-state')).toBe(helpText);
88     };
89
90     it('should match no rule and no alert', () => {
91       expectMatch(
92         'alertname',
93         'alert',
94         'Your matcher seems to match no currently defined rule or active alert.'
95       );
96     });
97
98     it('should match a rule with no alert', () => {
99       expectMatch('alertname', 'alert1', 'Matches 1 rule with no active alerts.');
100     });
101
102     it('should match a rule and an alert', () => {
103       expectMatch('alertname', 'alert0', 'Matches 1 rule with 1 active alert.');
104     });
105
106     it('should match multiple rules and an alert', () => {
107       expectMatch('severity', 'someSeverity', 'Matches 2 rules with 1 active alert.');
108     });
109
110     it('should match multiple rules and multiple alerts', () => {
111       component.rules[1].alerts.push(null);
112       expectMatch('severity', 'someSeverity', 'Matches 2 rules with 2 active alerts.');
113     });
114
115     it('should not show match-state if regex is checked', () => {
116       fixtureH.expectElementVisible('#match-state', false);
117       formH.setValue('name', 'severity');
118       formH.setValue('value', 'someSeverity');
119       fixtureH.expectElementVisible('#match-state', true);
120       formH.setValue('isRegex', true);
121       fixtureH.expectElementVisible('#match-state', false);
122     });
123   });
124
125   it('should only enable value field if name was set', () => {
126     const value = component.form.get('value');
127     expect(value.disabled).toBeTruthy();
128     formH.setValue('name', component.nameAttributes[0]);
129     expect(value.enabled).toBeTruthy();
130     formH.setValue('name', null);
131     expect(value.disabled).toBeTruthy();
132   });
133
134   it('should have a value field', () => {
135     formH.setValue('name', component.nameAttributes[0]);
136     formH.expectError('value', 'required');
137     formH.expectValidChange('value', 'alert0');
138   });
139
140   it('should test preFillControls', () => {
141     const controlValues = {
142       name: 'alertname',
143       value: 'alert0',
144       isRegex: false
145     };
146     component.preFillControls(controlValues);
147     expect(component.form.value).toEqual(controlValues);
148   });
149
150   it('should test submit', (done) => {
151     const controlValues = {
152       name: 'alertname',
153       value: 'alert0',
154       isRegex: false
155     };
156     component.preFillControls(controlValues);
157     component.submitAction.subscribe((resp: object) => {
158       expect(resp).toEqual(controlValues);
159       done();
160     });
161     component.onSubmit();
162   });
163 });