]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/frontend/src/app/shared/api/rbd-mirroring.service.spec.ts
import ceph 16.2.6
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / src / app / shared / api / rbd-mirroring.service.spec.ts
1 import { HttpRequest } from '@angular/common/http';
2 import {
3 HttpClientTestingModule,
4 HttpTestingController,
5 TestRequest
6 } from '@angular/common/http/testing';
7 import { fakeAsync, TestBed, tick } from '@angular/core/testing';
8
9 import { configureTestBed } from '~/testing/unit-test-helper';
10 import { RbdMirroringService } from './rbd-mirroring.service';
11
12 describe('RbdMirroringService', () => {
13 let service: RbdMirroringService;
14 let httpTesting: HttpTestingController;
15 let getMirroringSummaryCalls: () => TestRequest[];
16 let flushCalls: (call: TestRequest) => void;
17
18 const summary: Record<string, any> = {
19 status: 0,
20 content_data: {
21 daemons: [],
22 pools: [],
23 image_error: [],
24 image_syncing: [],
25 image_ready: []
26 },
27 executing_tasks: [{}]
28 };
29
30 configureTestBed({
31 providers: [RbdMirroringService],
32 imports: [HttpClientTestingModule]
33 });
34
35 beforeEach(() => {
36 service = TestBed.inject(RbdMirroringService);
37 httpTesting = TestBed.inject(HttpTestingController);
38 getMirroringSummaryCalls = () => {
39 return httpTesting.match((request: HttpRequest<any>) => {
40 return request.url.match(/api\/block\/mirroring\/summary/) && request.method === 'GET';
41 });
42 };
43 flushCalls = (call: TestRequest) => {
44 if (!call.cancelled) {
45 call.flush(summary);
46 }
47 };
48 });
49
50 afterEach(() => {
51 httpTesting.verify();
52 });
53
54 it('should be created', () => {
55 expect(service).toBeTruthy();
56 });
57
58 it('should periodically poll summary', fakeAsync(() => {
59 const subs = service.startPolling();
60 tick();
61 const calledWith: any[] = [];
62 service.subscribeSummary((data) => {
63 calledWith.push(data);
64 });
65 tick(service.REFRESH_INTERVAL * 2);
66 const calls = getMirroringSummaryCalls();
67
68 expect(calls.length).toEqual(3);
69 calls.forEach((call: TestRequest) => flushCalls(call));
70 expect(calledWith).toEqual([summary]);
71
72 subs.unsubscribe();
73 }));
74
75 it('should get pool config', () => {
76 service.getPool('poolName').subscribe();
77
78 const req = httpTesting.expectOne('api/block/mirroring/pool/poolName');
79 expect(req.request.method).toBe('GET');
80 });
81
82 it('should update pool config', () => {
83 const request = {
84 mirror_mode: 'pool'
85 };
86 service.updatePool('poolName', request).subscribe();
87
88 const req = httpTesting.expectOne('api/block/mirroring/pool/poolName');
89 expect(req.request.method).toBe('PUT');
90 expect(req.request.body).toEqual(request);
91 });
92
93 it('should get site name', () => {
94 service.getSiteName().subscribe();
95
96 const req = httpTesting.expectOne('api/block/mirroring/site_name');
97 expect(req.request.method).toBe('GET');
98 });
99
100 it('should set site name', () => {
101 service.setSiteName('site-a').subscribe();
102
103 const req = httpTesting.expectOne('api/block/mirroring/site_name');
104 expect(req.request.method).toBe('PUT');
105 expect(req.request.body).toEqual({ site_name: 'site-a' });
106 });
107
108 it('should create bootstrap token', () => {
109 service.createBootstrapToken('poolName').subscribe();
110
111 const req = httpTesting.expectOne('api/block/mirroring/pool/poolName/bootstrap/token');
112 expect(req.request.method).toBe('POST');
113 });
114
115 it('should import bootstrap token', () => {
116 service.importBootstrapToken('poolName', 'rx', 'token-1234').subscribe();
117
118 const req = httpTesting.expectOne('api/block/mirroring/pool/poolName/bootstrap/peer');
119 expect(req.request.method).toBe('POST');
120 expect(req.request.body).toEqual({
121 direction: 'rx',
122 token: 'token-1234'
123 });
124 });
125
126 it('should get peer config', () => {
127 service.getPeer('poolName', 'peerUUID').subscribe();
128
129 const req = httpTesting.expectOne('api/block/mirroring/pool/poolName/peer/peerUUID');
130 expect(req.request.method).toBe('GET');
131 });
132
133 it('should add peer config', () => {
134 const request = {
135 cluster_name: 'remote',
136 client_id: 'admin',
137 mon_host: 'localhost',
138 key: '1234'
139 };
140 service.addPeer('poolName', request).subscribe();
141
142 const req = httpTesting.expectOne('api/block/mirroring/pool/poolName/peer');
143 expect(req.request.method).toBe('POST');
144 expect(req.request.body).toEqual(request);
145 });
146
147 it('should update peer config', () => {
148 const request = {
149 cluster_name: 'remote'
150 };
151 service.updatePeer('poolName', 'peerUUID', request).subscribe();
152
153 const req = httpTesting.expectOne('api/block/mirroring/pool/poolName/peer/peerUUID');
154 expect(req.request.method).toBe('PUT');
155 expect(req.request.body).toEqual(request);
156 });
157
158 it('should delete peer config', () => {
159 service.deletePeer('poolName', 'peerUUID').subscribe();
160
161 const req = httpTesting.expectOne('api/block/mirroring/pool/poolName/peer/peerUUID');
162 expect(req.request.method).toBe('DELETE');
163 });
164 });