]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/frontend/src/app/shared/services/summary.service.spec.ts
9741084b2a885fb4091f0f73d75fd6991452ebc5
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / src / app / shared / services / summary.service.spec.ts
1 import { HttpClient } from '@angular/common/http';
2 import { fakeAsync, TestBed, tick } from '@angular/core/testing';
3 import { RouterTestingModule } from '@angular/router/testing';
4
5 import { of as observableOf, Subscriber } from 'rxjs';
6
7 import { configureTestBed } from '../../../testing/unit-test-helper';
8 import { ExecutingTask } from '../models/executing-task';
9 import { AuthStorageService } from './auth-storage.service';
10 import { SummaryService } from './summary.service';
11
12 describe('SummaryService', () => {
13 let summaryService: SummaryService;
14 let authStorageService: AuthStorageService;
15
16 const summary: Record<string, any> = {
17 executing_tasks: [],
18 health_status: 'HEALTH_OK',
19 mgr_id: 'x',
20 rbd_mirroring: { errors: 0, warnings: 0 },
21 rbd_pools: [],
22 have_mon_connection: true,
23 finished_tasks: [],
24 filesystems: [{ id: 1, name: 'cephfs_a' }]
25 };
26
27 const httpClientSpy = {
28 get: () => observableOf(summary)
29 };
30
31 configureTestBed({
32 imports: [RouterTestingModule],
33 providers: [
34 SummaryService,
35 AuthStorageService,
36 { provide: HttpClient, useValue: httpClientSpy }
37 ]
38 });
39
40 beforeEach(() => {
41 summaryService = TestBed.get(SummaryService);
42 authStorageService = TestBed.get(AuthStorageService);
43 });
44
45 it('should be created', () => {
46 expect(summaryService).toBeTruthy();
47 });
48
49 it('should call refresh', fakeAsync(() => {
50 summaryService.enablePolling();
51 authStorageService.set('foobar', undefined, undefined);
52 const calledWith: any[] = [];
53 summaryService.subscribe((data) => {
54 calledWith.push(data);
55 });
56 expect(calledWith).toEqual([summary]);
57 summaryService.refresh();
58 expect(calledWith).toEqual([summary, summary]);
59 tick(10000);
60 expect(calledWith.length).toEqual(4);
61 // In order to not trigger setInterval again,
62 // which would raise 'Error: 1 timer(s) still in the queue.'
63 window.clearInterval(summaryService.polling);
64 }));
65
66 describe('Should test methods after first refresh', () => {
67 beforeEach(() => {
68 authStorageService.set('foobar', undefined, undefined);
69 summaryService.refresh();
70 });
71
72 it('should call getCurrentSummary', () => {
73 expect(summaryService.getCurrentSummary()).toEqual(summary);
74 });
75
76 it('should call subscribe', () => {
77 let result;
78 const subscriber = summaryService.subscribe((data) => {
79 result = data;
80 });
81 expect(subscriber).toEqual(jasmine.any(Subscriber));
82 expect(result).toEqual(summary);
83 });
84
85 it('should call addRunningTask', () => {
86 summaryService.addRunningTask(
87 new ExecutingTask('rbd/delete', {
88 pool_name: 'somePool',
89 image_name: 'someImage'
90 })
91 );
92 const result = summaryService.getCurrentSummary();
93 expect(result.executing_tasks.length).toBe(1);
94 expect(result.executing_tasks[0]).toEqual({
95 metadata: { image_name: 'someImage', pool_name: 'somePool' },
96 name: 'rbd/delete'
97 });
98 });
99
100 it('should call addRunningTask with duplicate task', () => {
101 let result = summaryService.getCurrentSummary();
102 const exec_task = new ExecutingTask('rbd/delete', {
103 pool_name: 'somePool',
104 image_name: 'someImage'
105 });
106
107 result.executing_tasks = [exec_task];
108 summaryService['summaryDataSource'].next(result);
109 result = summaryService.getCurrentSummary();
110 expect(result.executing_tasks.length).toBe(1);
111
112 summaryService.addRunningTask(exec_task);
113 result = summaryService.getCurrentSummary();
114 expect(result.executing_tasks.length).toBe(1);
115 });
116 });
117 });