]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/blob
08d408fbac1f7b37c14c5ec2a55b6bd8ae0c1e98
[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 { NgbActiveModal, NgbTypeaheadModule } from '@ng-bootstrap/ng-bootstrap';
7 import * as _ from 'lodash';
8 import { of } from 'rxjs';
9
10 import {
11   configureTestBed,
12   FixtureHelper,
13   FormHelper,
14   i18nProviders,
15   PrometheusHelper
16 } from '../../../../../testing/unit-test-helper';
17 import { SharedModule } from '../../../../shared/shared.module';
18 import { SilenceMatcherModalComponent } from './silence-matcher-modal.component';
19
20 describe('SilenceMatcherModalComponent', () => {
21   let component: SilenceMatcherModalComponent;
22   let fixture: ComponentFixture<SilenceMatcherModalComponent>;
23
24   let formH: FormHelper;
25   let fixtureH: FixtureHelper;
26   let prometheus: PrometheusHelper;
27
28   configureTestBed({
29     declarations: [SilenceMatcherModalComponent],
30     imports: [
31       HttpClientTestingModule,
32       SharedModule,
33       NgbTypeaheadModule,
34       RouterTestingModule,
35       ReactiveFormsModule
36     ],
37     providers: [NgbActiveModal, i18nProviders]
38   });
39
40   beforeEach(() => {
41     fixture = TestBed.createComponent(SilenceMatcherModalComponent);
42     component = fixture.componentInstance;
43
44     fixtureH = new FixtureHelper(fixture);
45     formH = new FormHelper(component.form);
46     prometheus = new PrometheusHelper();
47
48     component.rules = [
49       prometheus.createRule('alert0', 'someSeverity', [prometheus.createAlert('alert0')]),
50       prometheus.createRule('alert1', 'someSeverity', [])
51     ];
52     fixture.detectChanges();
53   });
54
55   it('should create', () => {
56     expect(component).toBeTruthy();
57   });
58
59   it('should have a name field', () => {
60     formH.expectError('name', 'required');
61     formH.expectValidChange('name', 'alertname');
62   });
63
64   it('should only allow a specific set of name attributes', () => {
65     expect(component.nameAttributes).toEqual(['alertname', 'instance', 'job', 'severity']);
66   });
67
68   it('should autocomplete a list based on the set name', () => {
69     const expectations = {
70       alertname: ['alert0', 'alert1'],
71       instance: ['someInstance'],
72       job: ['someJob'],
73       severity: ['someSeverity']
74     };
75     Object.keys(expectations).forEach((key) => {
76       formH.setValue('name', key);
77       expect(component.possibleValues).toEqual(expectations[key]);
78     });
79   });
80
81   describe('test rule matching', () => {
82     const expectMatch = (name: string, value: string, helpText: string) => {
83       component.preFillControls({
84         name: name,
85         value: value,
86         isRegex: false
87       });
88       expect(fixtureH.getText('#match-state')).toBe(helpText);
89     };
90
91     it('should match no rule and no alert', () => {
92       expectMatch(
93         'alertname',
94         'alert',
95         'Your matcher seems to match no currently defined rule or active alert.'
96       );
97     });
98
99     it('should match a rule with no alert', () => {
100       expectMatch('alertname', 'alert1', 'Matches 1 rule with no active alerts.');
101     });
102
103     it('should match a rule and an alert', () => {
104       expectMatch('alertname', 'alert0', 'Matches 1 rule with 1 active alert.');
105     });
106
107     it('should match multiple rules and an alert', () => {
108       expectMatch('severity', 'someSeverity', 'Matches 2 rules with 1 active alert.');
109     });
110
111     it('should match multiple rules and multiple alerts', () => {
112       component.rules[1].alerts.push(null);
113       expectMatch('severity', 'someSeverity', 'Matches 2 rules with 2 active alerts.');
114     });
115
116     it('should not show match-state if regex is checked', () => {
117       fixtureH.expectElementVisible('#match-state', false);
118       formH.setValue('name', 'severity');
119       formH.setValue('value', 'someSeverity');
120       fixtureH.expectElementVisible('#match-state', true);
121       formH.setValue('isRegex', true);
122       fixtureH.expectElementVisible('#match-state', false);
123     });
124   });
125
126   it('should only enable value field if name was set', () => {
127     const value = component.form.get('value');
128     expect(value.disabled).toBeTruthy();
129     formH.setValue('name', component.nameAttributes[0]);
130     expect(value.enabled).toBeTruthy();
131     formH.setValue('name', null);
132     expect(value.disabled).toBeTruthy();
133   });
134
135   it('should have a value field', () => {
136     formH.setValue('name', component.nameAttributes[0]);
137     formH.expectError('value', 'required');
138     formH.expectValidChange('value', 'alert0');
139   });
140
141   it('should test preFillControls', () => {
142     const controlValues = {
143       name: 'alertname',
144       value: 'alert0',
145       isRegex: false
146     };
147     component.preFillControls(controlValues);
148     expect(component.form.value).toEqual(controlValues);
149   });
150
151   it('should test submit', (done) => {
152     const controlValues = {
153       name: 'alertname',
154       value: 'alert0',
155       isRegex: false
156     };
157     component.preFillControls(controlValues);
158     component.submitAction.subscribe((resp: object) => {
159       expect(resp).toEqual(controlValues);
160       done();
161     });
162     component.onSubmit();
163   });
164
165   describe('typeahead', () => {
166     let equality: { [key: string]: boolean };
167     let expectations: { [key: string]: string[] };
168
169     const search = (s: string) => {
170       Object.keys(expectations).forEach((key) => {
171         formH.setValue('name', key);
172         component.search(of(s)).subscribe((result) => {
173           // Expect won't fail the test inside subscribe
174           equality[key] = _.isEqual(result, expectations[key]);
175         });
176         expect(equality[key]).toBeTruthy();
177       });
178     };
179
180     beforeEach(() => {
181       equality = {
182         alertname: false,
183         instance: false,
184         job: false,
185         severity: false
186       };
187       expectations = {
188         alertname: ['alert0', 'alert1'],
189         instance: ['someInstance'],
190         job: ['someJob'],
191         severity: ['someSeverity']
192       };
193     });
194
195     it('should show all values on name switch', () => {
196       search('');
197     });
198
199     it('should search for "some"', () => {
200       expectations['alertname'] = [];
201       search('some');
202     });
203
204     it('should search for "er"', () => {
205       expectations['instance'] = [];
206       expectations['job'] = [];
207       search('er');
208     });
209   });
210 });