]> git.proxmox.com Git - ceph.git/blame - ceph/src/pybind/mgr/dashboard/frontend/src/app/shared/api/prometheus.service.spec.ts
import 15.2.0 Octopus source
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / src / app / shared / api / prometheus.service.spec.ts
CommitLineData
11fdf7f2
TL
1import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
2import { TestBed } from '@angular/core/testing';
3
4import { configureTestBed } from '../../../testing/unit-test-helper';
494da23a 5import { AlertmanagerNotification } from '../models/prometheus-alerts';
11fdf7f2
TL
6import { PrometheusService } from './prometheus.service';
7import { SettingsService } from './settings.service';
8
9describe('PrometheusService', () => {
10 let service: PrometheusService;
11 let httpTesting: HttpTestingController;
12
13 configureTestBed({
14 providers: [PrometheusService, SettingsService],
15 imports: [HttpClientTestingModule]
16 });
17
18 beforeEach(() => {
19 service = TestBed.get(PrometheusService);
20 httpTesting = TestBed.get(HttpTestingController);
21 });
22
23 afterEach(() => {
24 httpTesting.verify();
25 });
26
27 it('should be created', () => {
28 expect(service).toBeTruthy();
29 });
30
494da23a
TL
31 it('should get alerts', () => {
32 service.getAlerts().subscribe();
11fdf7f2
TL
33 const req = httpTesting.expectOne('api/prometheus');
34 expect(req.request.method).toBe('GET');
35 });
36
494da23a
TL
37 it('should get silences', () => {
38 service.getSilences().subscribe();
39 const req = httpTesting.expectOne('api/prometheus/silences');
40 expect(req.request.method).toBe('GET');
41 });
42
43 it('should set a silence', () => {
44 const silence = {
45 id: 'someId',
46 matchers: [
47 {
48 name: 'getZero',
49 value: 0,
50 isRegex: false
51 }
52 ],
53 startsAt: '2019-01-25T14:32:46.646300974Z',
54 endsAt: '2019-01-25T18:32:46.646300974Z',
55 createdBy: 'someCreator',
56 comment: 'for testing purpose'
57 };
58 service.setSilence(silence).subscribe();
59 const req = httpTesting.expectOne('api/prometheus/silence');
60 expect(req.request.method).toBe('POST');
61 expect(req.request.body).toEqual(silence);
62 });
63
64 it('should expire a silence', () => {
65 service.expireSilence('someId').subscribe();
66 const req = httpTesting.expectOne('api/prometheus/silence/someId');
67 expect(req.request.method).toBe('DELETE');
68 });
69
11fdf7f2
TL
70 it('should call getNotificationSince without a notification', () => {
71 service.getNotifications().subscribe();
72 const req = httpTesting.expectOne('api/prometheus/notifications?from=last');
73 expect(req.request.method).toBe('GET');
74 });
75
76 it('should call getNotificationSince with notification', () => {
494da23a 77 service.getNotifications({ id: '42' } as AlertmanagerNotification).subscribe();
11fdf7f2
TL
78 const req = httpTesting.expectOne('api/prometheus/notifications?from=42');
79 expect(req.request.method).toBe('GET');
80 });
81
9f95a23c
TL
82 describe('test getRules()', () => {
83 let data: {}; // Subset of PrometheusRuleGroup to keep the tests concise.
84
85 beforeEach(() => {
86 data = {
87 groups: [
88 {
89 name: 'test',
90 rules: [
91 {
92 name: 'load_0',
93 type: 'alerting'
94 },
95 {
96 name: 'load_1',
97 type: 'alerting'
98 },
99 {
100 name: 'load_2',
101 type: 'alerting'
102 }
103 ]
104 },
105 {
106 name: 'recording_rule',
107 rules: [
108 {
109 name: 'node_memory_MemUsed_percent',
110 type: 'recording'
111 }
112 ]
113 }
114 ]
115 };
116 });
117
118 it('should get rules without applying filters', () => {
119 service.getRules().subscribe((rules) => {
120 expect(rules).toEqual(data);
121 });
122
123 const req = httpTesting.expectOne('api/prometheus/rules');
124 expect(req.request.method).toBe('GET');
125 req.flush(data);
126 });
127
128 it('should get rewrite rules only', () => {
129 service.getRules('rewrites').subscribe((rules) => {
130 expect(rules).toEqual({
131 groups: [{ name: 'test', rules: [] }, { name: 'recording_rule', rules: [] }]
132 });
133 });
134
135 const req = httpTesting.expectOne('api/prometheus/rules');
136 expect(req.request.method).toBe('GET');
137 req.flush(data);
138 });
139
140 it('should get alerting rules only', () => {
141 service.getRules('alerting').subscribe((rules) => {
142 expect(rules).toEqual({
143 groups: [
144 {
145 name: 'test',
146 rules: [
147 { name: 'load_0', type: 'alerting' },
148 { name: 'load_1', type: 'alerting' },
149 { name: 'load_2', type: 'alerting' }
150 ]
151 },
152 { name: 'recording_rule', rules: [] }
153 ]
154 });
155 });
156
157 const req = httpTesting.expectOne('api/prometheus/rules');
158 expect(req.request.method).toBe('GET');
159 req.flush(data);
160 });
494da23a
TL
161 });
162
11fdf7f2
TL
163 describe('ifAlertmanagerConfigured', () => {
164 let x: any;
9f95a23c 165 let host: string;
11fdf7f2 166
494da23a 167 const receiveConfig = () => {
11fdf7f2
TL
168 const req = httpTesting.expectOne('api/settings/alertmanager-api-host');
169 expect(req.request.method).toBe('GET');
494da23a 170 req.flush({ value: host });
11fdf7f2
TL
171 };
172
173 beforeEach(() => {
174 x = false;
175 TestBed.get(SettingsService)['settings'] = {};
494da23a
TL
176 service.ifAlertmanagerConfigured((v) => (x = v), () => (x = []));
177 host = 'http://localhost:9093';
11fdf7f2
TL
178 });
179
180 it('changes x in a valid case', () => {
11fdf7f2 181 expect(x).toBe(false);
494da23a 182 receiveConfig();
11fdf7f2
TL
183 expect(x).toBe(host);
184 });
185
494da23a
TL
186 it('does changes x an empty array in a invalid case', () => {
187 host = '';
188 receiveConfig();
189 expect(x).toEqual([]);
190 });
191
192 it('disables the set setting', () => {
193 receiveConfig();
194 service.disableAlertmanagerConfig();
195 x = false;
11fdf7f2 196 service.ifAlertmanagerConfigured((v) => (x = v));
494da23a
TL
197 expect(x).toBe(false);
198 });
199 });
200
201 describe('ifPrometheusConfigured', () => {
202 let x: any;
9f95a23c 203 let host: string;
494da23a
TL
204
205 const receiveConfig = () => {
206 const req = httpTesting.expectOne('api/settings/prometheus-api-host');
207 expect(req.request.method).toBe('GET');
208 req.flush({ value: host });
209 };
210
211 beforeEach(() => {
212 x = false;
213 TestBed.get(SettingsService)['settings'] = {};
214 service.ifPrometheusConfigured((v) => (x = v), () => (x = []));
215 host = 'http://localhost:9090';
216 });
217
218 it('changes x in a valid case', () => {
219 expect(x).toBe(false);
220 receiveConfig();
221 expect(x).toBe(host);
222 });
223
224 it('does changes x an empty array in a invalid case', () => {
225 host = '';
226 receiveConfig();
227 expect(x).toEqual([]);
228 });
229
230 it('disables the set setting', () => {
231 receiveConfig();
232 service.disablePrometheusConfig();
233 x = false;
234 service.ifPrometheusConfigured((v) => (x = v));
11fdf7f2
TL
235 expect(x).toBe(false);
236 });
237 });
238});