1 import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
2 import { TestBed } from '@angular/core/testing';
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';
9 describe('PrometheusService', () => {
10 let service: PrometheusService;
11 let httpTesting: HttpTestingController;
14 providers: [PrometheusService, SettingsService],
15 imports: [HttpClientTestingModule]
19 service = TestBed.get(PrometheusService);
20 httpTesting = TestBed.get(HttpTestingController);
27 it('should be created', () => {
28 expect(service).toBeTruthy();
31 it('should get alerts', () => {
32 service.getAlerts().subscribe();
33 const req = httpTesting.expectOne('api/prometheus');
34 expect(req.request.method).toBe('GET');
37 it('should get silences', () => {
38 service.getSilences().subscribe();
39 const req = httpTesting.expectOne('api/prometheus/silences');
40 expect(req.request.method).toBe('GET');
43 it('should set a silence', () => {
53 startsAt: '2019-01-25T14:32:46.646300974Z',
54 endsAt: '2019-01-25T18:32:46.646300974Z',
55 createdBy: 'someCreator',
56 comment: 'for testing purpose'
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);
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');
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');
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');
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');
88 describe('ifAlertmanagerConfigured', () => {
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 });
100 TestBed.get(SettingsService)['settings'] = {};
101 service.ifAlertmanagerConfigured((v) => (x = v), () => (x = []));
102 host = 'http://localhost:9093';
105 it('changes x in a valid case', () => {
106 expect(x).toBe(false);
108 expect(x).toBe(host);
111 it('does changes x an empty array in a invalid case', () => {
114 expect(x).toEqual([]);
117 it('disables the set setting', () => {
119 service.disableAlertmanagerConfig();
121 service.ifAlertmanagerConfigured((v) => (x = v));
122 expect(x).toBe(false);
126 describe('ifPrometheusConfigured', () => {
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 });
138 TestBed.get(SettingsService)['settings'] = {};
139 service.ifPrometheusConfigured((v) => (x = v), () => (x = []));
140 host = 'http://localhost:9090';
143 it('changes x in a valid case', () => {
144 expect(x).toBe(false);
146 expect(x).toBe(host);
149 it('does changes x an empty array in a invalid case', () => {
152 expect(x).toEqual([]);
155 it('disables the set setting', () => {
157 service.disablePrometheusConfig();
159 service.ifPrometheusConfigured((v) => (x = v));
160 expect(x).toBe(false);