1 import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
2 import { fakeAsync, TestBed, tick } from '@angular/core/testing';
4 import { configureTestBed } from '../../../testing/unit-test-helper';
5 import { RbdMirroringService } from './rbd-mirroring.service';
7 describe('RbdMirroringService', () => {
8 let service: RbdMirroringService;
9 let httpTesting: HttpTestingController;
11 const summary: Record<string, any> = {
25 providers: [RbdMirroringService],
26 imports: [HttpClientTestingModule]
32 service = TestBed.get(RbdMirroringService);
33 httpTesting = TestBed.get(HttpTestingController);
35 const req = httpTesting.expectOne('api/block/mirroring/summary');
36 expect(req.request.method).toBe('GET');
44 it('should be created', () => {
45 expect(service).toBeTruthy();
48 it('should periodically poll summary', fakeAsync(() => {
49 const calledWith: any[] = [];
50 service.subscribeSummary((data) => {
51 calledWith.push(data);
53 service.refreshAndSchedule();
55 // In order to not trigger setTimeout again,
56 // which would raise 'Error: 1 timer(s) still in the queue.'
57 spyOn(service, 'refreshAndSchedule').and.callFake(() => true);
60 const calls = httpTesting.match((request) => {
61 return request.url.match(/api\/block\/mirroring\/summary/) && request.method === 'GET';
64 expect(calls.length).toEqual(2);
65 calls.forEach((call) => call.flush(summary));
67 expect(calledWith).toEqual([summary, summary, summary]);
70 it('should get current summary', () => {
71 expect(service.getCurrentSummary()).toEqual(summary);
74 it('should get pool config', () => {
75 service.getPool('poolName').subscribe();
77 const req = httpTesting.expectOne('api/block/mirroring/pool/poolName');
78 expect(req.request.method).toBe('GET');
81 it('should update pool config', () => {
85 service.updatePool('poolName', request).subscribe();
87 const req = httpTesting.expectOne('api/block/mirroring/pool/poolName');
88 expect(req.request.method).toBe('PUT');
89 expect(req.request.body).toEqual(request);
92 it('should get site name', () => {
93 service.getSiteName().subscribe();
95 const req = httpTesting.expectOne('api/block/mirroring/site_name');
96 expect(req.request.method).toBe('GET');
99 it('should set site name', () => {
100 service.setSiteName('site-a').subscribe();
102 const req = httpTesting.expectOne('api/block/mirroring/site_name');
103 expect(req.request.method).toBe('PUT');
104 expect(req.request.body).toEqual({ site_name: 'site-a' });
107 it('should create bootstrap token', () => {
108 service.createBootstrapToken('poolName').subscribe();
110 const req = httpTesting.expectOne('api/block/mirroring/pool/poolName/bootstrap/token');
111 expect(req.request.method).toBe('POST');
114 it('should import bootstrap token', () => {
115 service.importBootstrapToken('poolName', 'rx', 'token-1234').subscribe();
117 const req = httpTesting.expectOne('api/block/mirroring/pool/poolName/bootstrap/peer');
118 expect(req.request.method).toBe('POST');
119 expect(req.request.body).toEqual({
125 it('should get peer config', () => {
126 service.getPeer('poolName', 'peerUUID').subscribe();
128 const req = httpTesting.expectOne('api/block/mirroring/pool/poolName/peer/peerUUID');
129 expect(req.request.method).toBe('GET');
132 it('should add peer config', () => {
134 cluster_name: 'remote',
136 mon_host: 'localhost',
139 service.addPeer('poolName', request).subscribe();
141 const req = httpTesting.expectOne('api/block/mirroring/pool/poolName/peer');
142 expect(req.request.method).toBe('POST');
143 expect(req.request.body).toEqual(request);
146 it('should update peer config', () => {
148 cluster_name: 'remote'
150 service.updatePeer('poolName', 'peerUUID', request).subscribe();
152 const req = httpTesting.expectOne('api/block/mirroring/pool/poolName/peer/peerUUID');
153 expect(req.request.method).toBe('PUT');
154 expect(req.request.body).toEqual(request);
157 it('should delete peer config', () => {
158 service.deletePeer('poolName', 'peerUUID').subscribe();
160 const req = httpTesting.expectOne('api/block/mirroring/pool/poolName/peer/peerUUID');
161 expect(req.request.method).toBe('DELETE');