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