From: Volker Theile Date: Thu, 2 Apr 2020 08:50:09 +0000 (+0200) Subject: mgr/dashboard: refactor test of Prometheus alert service X-Git-Tag: v14.2.10~149^2~4 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=28a3dc3368eeff91cebb01c95280998e84b9f0af;p=ceph.git 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 (cherry picked from commit b85b24b2d085fd775298625b546b9fd5abe4d741) --- 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 de3ecf10298c..100c8299d121 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); }); });