From b85b24b2d085fd775298625b546b9fd5abe4d741 Mon Sep 17 00:00:00 2001 From: Patrick Seidensal Date: Wed, 27 Nov 2019 09:14:13 +0100 Subject: [PATCH] mgr/dashboard: refactor test of Prometheus alert service Mocking the test the way it was removed the asynchronous nature of the test. By using an Observable the test can stay asynchronous and be tested as well. Signed-off-by: Patrick Seidensal --- .../services/prometheus-alert.service.spec.ts | 42 +++++++++++-------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/services/prometheus-alert.service.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/services/prometheus-alert.service.spec.ts index de3ecf10298..100c8299d12 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/shared/services/prometheus-alert.service.spec.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/services/prometheus-alert.service.spec.ts @@ -2,7 +2,7 @@ import { HttpClientTestingModule } from '@angular/common/http/testing'; import { TestBed } from '@angular/core/testing'; import { ToastrModule } from 'ngx-toastr'; -import { of } from 'rxjs'; +import { Observable, of } from 'rxjs'; import { configureTestBed, @@ -38,29 +38,37 @@ describe('PrometheusAlertService', () => { expect(TestBed.get(PrometheusAlertService)).toBeTruthy(); }); - describe('test error cases', () => { - const expectDisabling = (status, expectation) => { - let disabledSetting = false; - const resp = { status: status, error: {} }; - service = new PrometheusAlertService(null, ({ - ifAlertmanagerConfigured: (fn) => fn(), - getAlerts: () => ({ subscribe: (_fn, err) => err(resp) }), - disableAlertmanagerConfig: () => (disabledSetting = true) - } as object) as PrometheusService); + describe('test failing status codes and verify disabling of the alertmanager', () => { + const isDisabledByStatusCode = (statusCode: number, expectedStatus: boolean, done) => { + service = TestBed.get(PrometheusAlertService); + prometheusService = TestBed.get(PrometheusService); + spyOn(prometheusService, 'ifAlertmanagerConfigured').and.callFake((fn) => fn()); + spyOn(prometheusService, 'getAlerts').and.returnValue( + Observable.create((observer) => observer.error({ status: statusCode, error: {} })) + ); + const disableFn = spyOn(prometheusService, 'disableAlertmanagerConfig').and.callFake(() => { + expect(expectedStatus).toBe(true); + done(); + }); + + if (!expectedStatus) { + expect(disableFn).not.toHaveBeenCalled(); + done(); + } + service.getAlerts(); - expect(disabledSetting).toBe(expectation); }; - it('disables on 504 error which is thrown if the mgr failed', () => { - expectDisabling(504, true); + it('disables on 504 error which is thrown if the mgr failed', (done) => { + isDisabledByStatusCode(504, true, done); }); - it('disables on 404 error which is thrown if the external api cannot be reached', () => { - expectDisabling(404, true); + it('disables on 404 error which is thrown if the external api cannot be reached', (done) => { + isDisabledByStatusCode(404, true, done); }); - it('does not disable on 400 error which is thrown if the external api receives unexpected data', () => { - expectDisabling(400, false); + it('does not disable on 400 error which is thrown if the external api receives unexpected data', (done) => { + isDisabledByStatusCode(400, false, done); }); }); -- 2.47.3