1 import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
2 import { TestBed } from '@angular/core/testing';
4 import { CdTableFetchDataContext } from '~/app/shared/models/cd-table-fetch-data-context';
5 import { configureTestBed } from '~/testing/unit-test-helper';
6 import { ImageSpec } from '../models/image-spec';
7 import { RbdConfigurationService } from '../services/rbd-configuration.service';
8 import { RbdService } from './rbd.service';
10 describe('RbdService', () => {
11 let service: RbdService;
12 let httpTesting: HttpTestingController;
15 providers: [RbdService, RbdConfigurationService],
16 imports: [HttpClientTestingModule]
20 service = TestBed.inject(RbdService);
21 httpTesting = TestBed.inject(HttpTestingController);
28 it('should be created', () => {
29 expect(service).toBeTruthy();
32 it('should call create', () => {
33 service.create('foo').subscribe();
34 const req = httpTesting.expectOne('api/block/image');
35 expect(req.request.method).toBe('POST');
36 expect(req.request.body).toEqual('foo');
39 it('should call delete', () => {
40 service.delete(new ImageSpec('poolName', null, 'rbdName')).subscribe();
41 const req = httpTesting.expectOne('api/block/image/poolName%2FrbdName');
42 expect(req.request.method).toBe('DELETE');
45 it('should call update', () => {
46 service.update(new ImageSpec('poolName', null, 'rbdName'), 'foo').subscribe();
47 const req = httpTesting.expectOne('api/block/image/poolName%2FrbdName');
48 expect(req.request.body).toEqual('foo');
49 expect(req.request.method).toBe('PUT');
52 it('should call get', () => {
53 service.get(new ImageSpec('poolName', null, 'rbdName')).subscribe();
54 const req = httpTesting.expectOne('api/block/image/poolName%2FrbdName');
55 expect(req.request.method).toBe('GET');
58 it('should call list', () => {
59 /* tslint:disable:no-empty */
60 const context = new CdTableFetchDataContext(() => {});
61 service.list(context.toParams()).subscribe();
62 const req = httpTesting.expectOne((req) => {
63 return 'api/block/image?offset=0&limit=-1&search=&sort=+name' && req.method === 'GET';
65 expect(req.request.method).toBe('GET');
68 it('should call copy', () => {
69 service.copy(new ImageSpec('poolName', null, 'rbdName'), 'foo').subscribe();
70 const req = httpTesting.expectOne('api/block/image/poolName%2FrbdName/copy');
71 expect(req.request.body).toEqual('foo');
72 expect(req.request.method).toBe('POST');
75 it('should call flatten', () => {
76 service.flatten(new ImageSpec('poolName', null, 'rbdName')).subscribe();
77 const req = httpTesting.expectOne('api/block/image/poolName%2FrbdName/flatten');
78 expect(req.request.body).toEqual(null);
79 expect(req.request.method).toBe('POST');
82 it('should call defaultFeatures', () => {
83 service.defaultFeatures().subscribe();
84 const req = httpTesting.expectOne('api/block/image/default_features');
85 expect(req.request.method).toBe('GET');
88 it('should call cloneFormatVersion', () => {
89 service.cloneFormatVersion().subscribe();
90 const req = httpTesting.expectOne('api/block/image/clone_format_version');
91 expect(req.request.method).toBe('GET');
94 it('should call createSnapshot', () => {
96 .createSnapshot(new ImageSpec('poolName', null, 'rbdName'), 'snapshotName', false)
98 const req = httpTesting.expectOne('api/block/image/poolName%2FrbdName/snap');
99 expect(req.request.body).toEqual({
100 snapshot_name: 'snapshotName',
101 mirrorImageSnapshot: false
103 expect(req.request.method).toBe('POST');
106 it('should call renameSnapshot', () => {
108 .renameSnapshot(new ImageSpec('poolName', null, 'rbdName'), 'snapshotName', 'foo')
110 const req = httpTesting.expectOne('api/block/image/poolName%2FrbdName/snap/snapshotName');
111 expect(req.request.body).toEqual({
114 expect(req.request.method).toBe('PUT');
117 it('should call protectSnapshot', () => {
119 .protectSnapshot(new ImageSpec('poolName', null, 'rbdName'), 'snapshotName', true)
121 const req = httpTesting.expectOne('api/block/image/poolName%2FrbdName/snap/snapshotName');
122 expect(req.request.body).toEqual({
125 expect(req.request.method).toBe('PUT');
128 it('should call rollbackSnapshot', () => {
130 .rollbackSnapshot(new ImageSpec('poolName', null, 'rbdName'), 'snapshotName')
132 const req = httpTesting.expectOne(
133 'api/block/image/poolName%2FrbdName/snap/snapshotName/rollback'
135 expect(req.request.body).toEqual(null);
136 expect(req.request.method).toBe('POST');
139 it('should call cloneSnapshot', () => {
141 .cloneSnapshot(new ImageSpec('poolName', null, 'rbdName'), 'snapshotName', null)
143 const req = httpTesting.expectOne('api/block/image/poolName%2FrbdName/snap/snapshotName/clone');
144 expect(req.request.body).toEqual(null);
145 expect(req.request.method).toBe('POST');
148 it('should call deleteSnapshot', () => {
149 service.deleteSnapshot(new ImageSpec('poolName', null, 'rbdName'), 'snapshotName').subscribe();
150 const req = httpTesting.expectOne('api/block/image/poolName%2FrbdName/snap/snapshotName');
151 expect(req.request.method).toBe('DELETE');
154 it('should call moveTrash', () => {
155 service.moveTrash(new ImageSpec('poolName', null, 'rbdName'), 1).subscribe();
156 const req = httpTesting.expectOne('api/block/image/poolName%2FrbdName/move_trash');
157 expect(req.request.method).toBe('POST');
158 expect(req.request.body).toEqual({ delay: 1 });
161 describe('should compose image spec', () => {
162 it('with namespace', () => {
163 expect(new ImageSpec('mypool', 'myns', 'myimage').toString()).toBe('mypool/myns/myimage');
166 it('without namespace', () => {
167 expect(new ImageSpec('mypool', null, 'myimage').toString()).toBe('mypool/myimage');
171 describe('should parse image spec', () => {
172 it('with namespace', () => {
173 const imageSpec = ImageSpec.fromString('mypool/myns/myimage');
174 expect(imageSpec.poolName).toBe('mypool');
175 expect(imageSpec.namespace).toBe('myns');
176 expect(imageSpec.imageName).toBe('myimage');
179 it('without namespace', () => {
180 const imageSpec = ImageSpec.fromString('mypool/myimage');
181 expect(imageSpec.poolName).toBe('mypool');
182 expect(imageSpec.namespace).toBeNull();
183 expect(imageSpec.imageName).toBe('myimage');