]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/frontend/src/app/shared/api/prometheus.service.spec.ts
import 14.2.4 nautilus point release
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / src / app / shared / api / prometheus.service.spec.ts
1 import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
2 import { TestBed } from '@angular/core/testing';
3
4 import { configureTestBed } from '../../../testing/unit-test-helper';
5 import { AlertmanagerNotification } from '../models/prometheus-alerts';
6 import { PrometheusService } from './prometheus.service';
7 import { SettingsService } from './settings.service';
8
9 describe('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
31 it('should get alerts', () => {
32 service.getAlerts().subscribe();
33 const req = httpTesting.expectOne('api/prometheus');
34 expect(req.request.method).toBe('GET');
35 });
36
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
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', () => {
77 service.getNotifications({ id: '42' } as AlertmanagerNotification).subscribe();
78 const req = httpTesting.expectOne('api/prometheus/notifications?from=42');
79 expect(req.request.method).toBe('GET');
80 });
81
82 it('should get prometheus rules', () => {
83 service.getRules({}).subscribe();
84 const req = httpTesting.expectOne('api/prometheus/rules');
85 expect(req.request.method).toBe('GET');
86 });
87
88 describe('ifAlertmanagerConfigured', () => {
89 let x: any;
90 let host;
91
92 const receiveConfig = () => {
93 const req = httpTesting.expectOne('api/settings/alertmanager-api-host');
94 expect(req.request.method).toBe('GET');
95 req.flush({ value: host });
96 };
97
98 beforeEach(() => {
99 x = false;
100 TestBed.get(SettingsService)['settings'] = {};
101 service.ifAlertmanagerConfigured((v) => (x = v), () => (x = []));
102 host = 'http://localhost:9093';
103 });
104
105 it('changes x in a valid case', () => {
106 expect(x).toBe(false);
107 receiveConfig();
108 expect(x).toBe(host);
109 });
110
111 it('does changes x an empty array in a invalid case', () => {
112 host = '';
113 receiveConfig();
114 expect(x).toEqual([]);
115 });
116
117 it('disables the set setting', () => {
118 receiveConfig();
119 service.disableAlertmanagerConfig();
120 x = false;
121 service.ifAlertmanagerConfigured((v) => (x = v));
122 expect(x).toBe(false);
123 });
124 });
125
126 describe('ifPrometheusConfigured', () => {
127 let x: any;
128 let host;
129
130 const receiveConfig = () => {
131 const req = httpTesting.expectOne('api/settings/prometheus-api-host');
132 expect(req.request.method).toBe('GET');
133 req.flush({ value: host });
134 };
135
136 beforeEach(() => {
137 x = false;
138 TestBed.get(SettingsService)['settings'] = {};
139 service.ifPrometheusConfigured((v) => (x = v), () => (x = []));
140 host = 'http://localhost:9090';
141 });
142
143 it('changes x in a valid case', () => {
144 expect(x).toBe(false);
145 receiveConfig();
146 expect(x).toBe(host);
147 });
148
149 it('does changes x an empty array in a invalid case', () => {
150 host = '';
151 receiveConfig();
152 expect(x).toEqual([]);
153 });
154
155 it('disables the set setting', () => {
156 receiveConfig();
157 service.disablePrometheusConfig();
158 x = false;
159 service.ifPrometheusConfigured((v) => (x = v));
160 expect(x).toBe(false);
161 });
162 });
163 });