]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/frontend/src/app/shared/services/summary.service.spec.ts
import 15.2.2 octopus source
[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 { AuthStorageService } from './auth-storage.service';
10 import { SummaryService } from './summary.service';
11
12 describe('SummaryService', () => {
13 let summaryService: SummaryService;
14 let authStorageService: AuthStorageService;
15 let subs: Subscription;
16
17 const summary: Record<string, any> = {
18 executing_tasks: [],
19 health_status: 'HEALTH_OK',
20 mgr_id: 'x',
21 rbd_mirroring: { errors: 0, warnings: 0 },
22 rbd_pools: [],
23 have_mon_connection: true,
24 finished_tasks: [],
25 filesystems: [{ id: 1, name: 'cephfs_a' }]
26 };
27
28 const httpClientSpy = {
29 get: () => observableOf(summary)
30 };
31
32 configureTestBed({
33 imports: [RouterTestingModule],
34 providers: [
35 SummaryService,
36 AuthStorageService,
37 { provide: HttpClient, useValue: httpClientSpy }
38 ]
39 });
40
41 beforeEach(() => {
42 summaryService = TestBed.get(SummaryService);
43 authStorageService = TestBed.get(AuthStorageService);
44 });
45
46 it('should be created', () => {
47 expect(summaryService).toBeTruthy();
48 });
49
50 it('should call refresh', fakeAsync(() => {
51 authStorageService.set('foobar', undefined, undefined);
52 const calledWith: any[] = [];
53 subs = new Subscription();
54 subs.add(summaryService.startPolling());
55 tick();
56 subs.add(
57 summaryService.subscribe((data) => {
58 calledWith.push(data);
59 })
60 );
61 expect(calledWith).toEqual([summary]);
62 subs.add(summaryService.refresh());
63 expect(calledWith).toEqual([summary, summary]);
64 tick(summaryService.REFRESH_INTERVAL * 2);
65 expect(calledWith.length).toEqual(4);
66 subs.unsubscribe();
67 }));
68
69 describe('Should test methods after first refresh', () => {
70 beforeEach(() => {
71 authStorageService.set('foobar', undefined, undefined);
72 summaryService.refresh();
73 });
74
75 it('should call getCurrentSummary', () => {
76 expect(summaryService.getCurrentSummary()).toEqual(summary);
77 });
78
79 it('should call subscribe', () => {
80 let result;
81 const subscriber = summaryService.subscribe((data) => {
82 result = data;
83 });
84 expect(subscriber).toEqual(jasmine.any(Subscriber));
85 expect(result).toEqual(summary);
86 });
87
88 it('should call addRunningTask', () => {
89 summaryService.addRunningTask(
90 new ExecutingTask('rbd/delete', {
91 pool_name: 'somePool',
92 image_name: 'someImage'
93 })
94 );
95 const result = summaryService.getCurrentSummary();
96 expect(result.executing_tasks.length).toBe(1);
97 expect(result.executing_tasks[0]).toEqual({
98 metadata: { image_name: 'someImage', pool_name: 'somePool' },
99 name: 'rbd/delete'
100 });
101 });
102
103 it('should call addRunningTask with duplicate task', () => {
104 let result = summaryService.getCurrentSummary();
105 const exec_task = new ExecutingTask('rbd/delete', {
106 pool_name: 'somePool',
107 image_name: 'someImage'
108 });
109
110 result.executing_tasks = [exec_task];
111 summaryService['summaryDataSource'].next(result);
112 result = summaryService.getCurrentSummary();
113 expect(result.executing_tasks.length).toBe(1);
114
115 summaryService.addRunningTask(exec_task);
116 result = summaryService.getCurrentSummary();
117 expect(result.executing_tasks.length).toBe(1);
118 });
119 });
120 });