]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/frontend/src/app/ceph/cluster/prometheus/silence-matcher-modal/silence-matcher-modal.component.spec.ts
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / src / app / ceph / cluster / prometheus / silence-matcher-modal / silence-matcher-modal.component.spec.ts
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 _ from 'lodash';
8 import { of } from 'rxjs';
9
10 import { SharedModule } from '~/app/shared/shared.module';
11 import {
12 configureTestBed,
13 FixtureHelper,
14 FormHelper,
15 PrometheusHelper
16 } from '~/testing/unit-test-helper';
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: [NgbActiveModal]
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
164 describe('typeahead', () => {
165 let equality: { [key: string]: boolean };
166 let expectations: { [key: string]: string[] };
167
168 const search = (s: string) => {
169 Object.keys(expectations).forEach((key) => {
170 formH.setValue('name', key);
171 component.search(of(s)).subscribe((result) => {
172 // Expect won't fail the test inside subscribe
173 equality[key] = _.isEqual(result, expectations[key]);
174 });
175 expect(equality[key]).toBeTruthy();
176 });
177 };
178
179 beforeEach(() => {
180 equality = {
181 alertname: false,
182 instance: false,
183 job: false,
184 severity: false
185 };
186 expectations = {
187 alertname: ['alert0', 'alert1'],
188 instance: ['someInstance'],
189 job: ['someJob'],
190 severity: ['someSeverity']
191 };
192 });
193
194 it('should show all values on name switch', () => {
195 search('');
196 });
197
198 it('should search for "some"', () => {
199 expectations['alertname'] = [];
200 search('some');
201 });
202
203 it('should search for "er"', () => {
204 expectations['instance'] = [];
205 expectations['job'] = [];
206 search('er');
207 });
208 });
209 });