]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/frontend/src/app/shared/services/summary.service.spec.ts
import 15.2.5
[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, Subscription } from 'rxjs';
6
7 import { configureTestBed } from '../../../testing/unit-test-helper';
8 import { ExecutingTask } from '../models/executing-task';
9 import { Summary } from '../models/summary.model';
10 import { AuthStorageService } from './auth-storage.service';
11 import { SummaryService } from './summary.service';
12
13 describe('SummaryService', () => {
14 let summaryService: SummaryService;
15 let authStorageService: AuthStorageService;
16 let subs: Subscription;
17
18 const summary: Summary = {
19 executing_tasks: [],
20 health_status: 'HEALTH_OK',
21 mgr_id: 'x',
22 rbd_mirroring: { errors: 0, warnings: 0 },
23 rbd_pools: [],
24 have_mon_connection: true,
25 finished_tasks: [],
26 filesystems: [{ id: 1, name: 'cephfs_a' }]
27 };
28
29 const httpClientSpy = {
30 get: () => observableOf(summary)
31 };
32
33 const nextSummary = (newData: any) => summaryService['summaryDataSource'].next(newData);
34
35 configureTestBed({
36 imports: [RouterTestingModule],
37 providers: [
38 SummaryService,
39 AuthStorageService,
40 { provide: HttpClient, useValue: httpClientSpy }
41 ]
42 });
43
44 beforeEach(() => {
45 summaryService = TestBed.get(SummaryService);
46 authStorageService = TestBed.get(AuthStorageService);
47 });
48
49 it('should be created', () => {
50 expect(summaryService).toBeTruthy();
51 });
52
53 it('should call refresh', fakeAsync(() => {
54 authStorageService.set('foobar', undefined, undefined);
55 const calledWith: any[] = [];
56 subs = new Subscription();
57 subs.add(summaryService.startPolling());
58 tick();
59 subs.add(
60 summaryService.subscribe((data) => {
61 calledWith.push(data);
62 })
63 );
64 expect(calledWith).toEqual([summary]);
65 subs.add(summaryService.refresh());
66 expect(calledWith).toEqual([summary, summary]);
67 tick(summaryService.REFRESH_INTERVAL * 2);
68 expect(calledWith.length).toEqual(4);
69 subs.unsubscribe();
70 }));
71
72 describe('Should test subscribe without initial value', () => {
73 let result: Summary;
74 let i: number;
75
76 const callback = (response: Summary) => {
77 i++;
78 result = response;
79 };
80
81 beforeEach(() => {
82 i = 0;
83 result = undefined;
84 nextSummary(undefined);
85 });
86
87 it('should call subscribeOnce', () => {
88 const subscriber = summaryService.subscribeOnce(callback);
89
90 expect(subscriber).toEqual(jasmine.any(Subscriber));
91 expect(i).toBe(0);
92 expect(result).toEqual(undefined);
93
94 nextSummary(undefined);
95 expect(i).toBe(0);
96 expect(result).toEqual(undefined);
97 expect(subscriber.closed).toBe(false);
98
99 nextSummary(summary);
100 expect(result).toEqual(summary);
101 expect(i).toBe(1);
102 expect(subscriber.closed).toBe(true);
103
104 nextSummary(summary);
105 expect(result).toEqual(summary);
106 expect(i).toBe(1);
107 });
108
109 it('should call subscribe', () => {
110 const subscriber = summaryService.subscribe(callback);
111
112 expect(subscriber).toEqual(jasmine.any(Subscriber));
113 expect(i).toBe(0);
114 expect(result).toEqual(undefined);
115
116 nextSummary(undefined);
117 expect(i).toBe(0);
118 expect(result).toEqual(undefined);
119 expect(subscriber.closed).toBe(false);
120
121 nextSummary(summary);
122 expect(result).toEqual(summary);
123 expect(i).toBe(1);
124 expect(subscriber.closed).toBe(false);
125
126 nextSummary(summary);
127 expect(result).toEqual(summary);
128 expect(i).toBe(2);
129 expect(subscriber.closed).toBe(false);
130 });
131 });
132
133 describe('Should test methods after first refresh', () => {
134 beforeEach(() => {
135 authStorageService.set('foobar', undefined, undefined);
136 summaryService.refresh();
137 });
138
139 it('should call addRunningTask', () => {
140 summaryService.addRunningTask(
141 new ExecutingTask('rbd/delete', {
142 pool_name: 'somePool',
143 image_name: 'someImage'
144 })
145 );
146 let result: any;
147 summaryService.subscribeOnce((response) => {
148 result = response;
149 });
150
151 expect(result.executing_tasks.length).toBe(1);
152 expect(result.executing_tasks[0]).toEqual({
153 metadata: { image_name: 'someImage', pool_name: 'somePool' },
154 name: 'rbd/delete'
155 });
156 });
157
158 it('should call addRunningTask with duplicate task', () => {
159 let result: any;
160 summaryService.subscribe((response) => {
161 result = response;
162 });
163
164 const exec_task = new ExecutingTask('rbd/delete', {
165 pool_name: 'somePool',
166 image_name: 'someImage'
167 });
168
169 result.executing_tasks = [exec_task];
170 nextSummary(result);
171
172 expect(result.executing_tasks.length).toBe(1);
173
174 summaryService.addRunningTask(exec_task);
175
176 expect(result.executing_tasks.length).toBe(1);
177 });
178 });
179 });