1 import { HttpRequest } from '@angular/common/http';
3 HttpClientTestingModule,
6 } from '@angular/common/http/testing';
7 import { fakeAsync, TestBed, tick } from '@angular/core/testing';
9 import { configureTestBed } from '~/testing/unit-test-helper';
10 import { RbdMirroringService } from './rbd-mirroring.service';
12 describe('RbdMirroringService', () => {
13 let service: RbdMirroringService;
14 let httpTesting: HttpTestingController;
15 let getMirroringSummaryCalls: () => TestRequest[];
16 let flushCalls: (call: TestRequest) => void;
18 const summary: Record<string, any> = {
31 providers: [RbdMirroringService],
32 imports: [HttpClientTestingModule]
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';
43 flushCalls = (call: TestRequest) => {
44 if (!call.cancelled) {
54 it('should be created', () => {
55 expect(service).toBeTruthy();
58 it('should periodically poll summary', fakeAsync(() => {
59 const subs = service.startPolling();
61 const calledWith: any[] = [];
62 service.subscribeSummary((data) => {
63 calledWith.push(data);
65 tick(service.REFRESH_INTERVAL * 2);
66 const calls = getMirroringSummaryCalls();
68 expect(calls.length).toEqual(3);
69 calls.forEach((call: TestRequest) => flushCalls(call));
70 expect(calledWith).toEqual([summary]);
75 it('should get pool config', () => {
76 service.getPool('poolName').subscribe();
78 const req = httpTesting.expectOne('api/block/mirroring/pool/poolName');
79 expect(req.request.method).toBe('GET');
82 it('should update pool config', () => {
86 service.updatePool('poolName', request).subscribe();
88 const req = httpTesting.expectOne('api/block/mirroring/pool/poolName');
89 expect(req.request.method).toBe('PUT');
90 expect(req.request.body).toEqual(request);
93 it('should get site name', () => {
94 service.getSiteName().subscribe();
96 const req = httpTesting.expectOne('api/block/mirroring/site_name');
97 expect(req.request.method).toBe('GET');
100 it('should set site name', () => {
101 service.setSiteName('site-a').subscribe();
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' });
108 it('should create bootstrap token', () => {
109 service.createBootstrapToken('poolName').subscribe();
111 const req = httpTesting.expectOne('api/block/mirroring/pool/poolName/bootstrap/token');
112 expect(req.request.method).toBe('POST');
115 it('should import bootstrap token', () => {
116 service.importBootstrapToken('poolName', 'rx', 'token-1234').subscribe();
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({
126 it('should get peer config', () => {
127 service.getPeer('poolName', 'peerUUID').subscribe();
129 const req = httpTesting.expectOne('api/block/mirroring/pool/poolName/peer/peerUUID');
130 expect(req.request.method).toBe('GET');
133 it('should add peer config', () => {
135 cluster_name: 'remote',
137 mon_host: 'localhost',
140 service.addPeer('poolName', request).subscribe();
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);
147 it('should update peer config', () => {
149 cluster_name: 'remote'
151 service.updatePeer('poolName', 'peerUUID', request).subscribe();
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);
158 it('should delete peer config', () => {
159 service.deletePeer('poolName', 'peerUUID').subscribe();
161 const req = httpTesting.expectOne('api/block/mirroring/pool/poolName/peer/peerUUID');
162 expect(req.request.method).toBe('DELETE');