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('api/block/image?offset=0&limit=10&search=&sort=+name');
63 expect(req.request.method).toBe('GET');
66 it('should call copy', () => {
67 service.copy(new ImageSpec('poolName', null, 'rbdName'), 'foo').subscribe();
68 const req = httpTesting.expectOne('api/block/image/poolName%2FrbdName/copy');
69 expect(req.request.body).toEqual('foo');
70 expect(req.request.method).toBe('POST');
73 it('should call flatten', () => {
74 service.flatten(new ImageSpec('poolName', null, 'rbdName')).subscribe();
75 const req = httpTesting.expectOne('api/block/image/poolName%2FrbdName/flatten');
76 expect(req.request.body).toEqual(null);
77 expect(req.request.method).toBe('POST');
80 it('should call defaultFeatures', () => {
81 service.defaultFeatures().subscribe();
82 const req = httpTesting.expectOne('api/block/image/default_features');
83 expect(req.request.method).toBe('GET');
86 it('should call cloneFormatVersion', () => {
87 service.cloneFormatVersion().subscribe();
88 const req = httpTesting.expectOne('api/block/image/clone_format_version');
89 expect(req.request.method).toBe('GET');
92 it('should call createSnapshot', () => {
94 .createSnapshot(new ImageSpec('poolName', null, 'rbdName'), 'snapshotName', false)
96 const req = httpTesting.expectOne('api/block/image/poolName%2FrbdName/snap');
97 expect(req.request.body).toEqual({
98 snapshot_name: 'snapshotName',
99 mirrorImageSnapshot: false
101 expect(req.request.method).toBe('POST');
104 it('should call renameSnapshot', () => {
106 .renameSnapshot(new ImageSpec('poolName', null, 'rbdName'), 'snapshotName', 'foo')
108 const req = httpTesting.expectOne('api/block/image/poolName%2FrbdName/snap/snapshotName');
109 expect(req.request.body).toEqual({
112 expect(req.request.method).toBe('PUT');
115 it('should call protectSnapshot', () => {
117 .protectSnapshot(new ImageSpec('poolName', null, 'rbdName'), 'snapshotName', true)
119 const req = httpTesting.expectOne('api/block/image/poolName%2FrbdName/snap/snapshotName');
120 expect(req.request.body).toEqual({
123 expect(req.request.method).toBe('PUT');
126 it('should call rollbackSnapshot', () => {
128 .rollbackSnapshot(new ImageSpec('poolName', null, 'rbdName'), 'snapshotName')
130 const req = httpTesting.expectOne(
131 'api/block/image/poolName%2FrbdName/snap/snapshotName/rollback'
133 expect(req.request.body).toEqual(null);
134 expect(req.request.method).toBe('POST');
137 it('should call cloneSnapshot', () => {
139 .cloneSnapshot(new ImageSpec('poolName', null, 'rbdName'), 'snapshotName', null)
141 const req = httpTesting.expectOne('api/block/image/poolName%2FrbdName/snap/snapshotName/clone');
142 expect(req.request.body).toEqual(null);
143 expect(req.request.method).toBe('POST');
146 it('should call deleteSnapshot', () => {
147 service.deleteSnapshot(new ImageSpec('poolName', null, 'rbdName'), 'snapshotName').subscribe();
148 const req = httpTesting.expectOne('api/block/image/poolName%2FrbdName/snap/snapshotName');
149 expect(req.request.method).toBe('DELETE');
152 it('should call moveTrash', () => {
153 service.moveTrash(new ImageSpec('poolName', null, 'rbdName'), 1).subscribe();
154 const req = httpTesting.expectOne('api/block/image/poolName%2FrbdName/move_trash');
155 expect(req.request.method).toBe('POST');
156 expect(req.request.body).toEqual({ delay: 1 });
159 describe('should compose image spec', () => {
160 it('with namespace', () => {
161 expect(new ImageSpec('mypool', 'myns', 'myimage').toString()).toBe('mypool/myns/myimage');
164 it('without namespace', () => {
165 expect(new ImageSpec('mypool', null, 'myimage').toString()).toBe('mypool/myimage');
169 describe('should parse image spec', () => {
170 it('with namespace', () => {
171 const imageSpec = ImageSpec.fromString('mypool/myns/myimage');
172 expect(imageSpec.poolName).toBe('mypool');
173 expect(imageSpec.namespace).toBe('myns');
174 expect(imageSpec.imageName).toBe('myimage');
177 it('without namespace', () => {
178 const imageSpec = ImageSpec.fromString('mypool/myimage');
179 expect(imageSpec.poolName).toBe('mypool');
180 expect(imageSpec.namespace).toBeNull();
181 expect(imageSpec.imageName).toBe('myimage');