]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
mgr/dashboard: refactor test of Prometheus alert service
authorVolker Theile <vtheile@suse.com>
Thu, 2 Apr 2020 08:50:09 +0000 (10:50 +0200)
committerVolker Theile <vtheile@suse.com>
Thu, 2 Apr 2020 11:18:50 +0000 (13:18 +0200)
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 <pseidensal@suse.com>
(cherry picked from commit b85b24b2d085fd775298625b546b9fd5abe4d741)

src/pybind/mgr/dashboard/frontend/src/app/shared/services/prometheus-alert.service.spec.ts

index de3ecf10298cd098b3ab022792340ac0e4fa584e..100c8299d12116a44d925b346e1f00e603045078 100644 (file)
@@ -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);
     });
   });