]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/blob
a2afd1e58f18d3224324ff2fcd1c82acc08403fa
[ceph-ci.git] /
1 import { HttpClientTestingModule } from '@angular/common/http/testing';
2 import { ComponentFixture, fakeAsync, TestBed, tick } from '@angular/core/testing';
3 import { RouterTestingModule } from '@angular/router/testing';
4
5 import { ToastModule } from 'ng2-toastr';
6 import { BsModalRef, ModalModule } from 'ngx-bootstrap';
7 import { throwError as observableThrowError } from 'rxjs';
8
9 import { configureTestBed } from '../../../../testing/unit-test-helper';
10 import { ApiModule } from '../../../shared/api/api.module';
11 import { RbdService } from '../../../shared/api/rbd.service';
12 import { ComponentsModule } from '../../../shared/components/components.module';
13 import { DataTableModule } from '../../../shared/datatable/datatable.module';
14 import { ExecutingTask } from '../../../shared/models/executing-task';
15 import { Permissions } from '../../../shared/models/permissions';
16 import { PipesModule } from '../../../shared/pipes/pipes.module';
17 import { AuthStorageService } from '../../../shared/services/auth-storage.service';
18 import { NotificationService } from '../../../shared/services/notification.service';
19 import { ServicesModule } from '../../../shared/services/services.module';
20 import { SummaryService } from '../../../shared/services/summary.service';
21 import { TaskListService } from '../../../shared/services/task-list.service';
22 import { RbdSnapshotListComponent } from './rbd-snapshot-list.component';
23 import { RbdSnapshotModel } from './rbd-snapshot.model';
24
25 describe('RbdSnapshotListComponent', () => {
26   let component: RbdSnapshotListComponent;
27   let fixture: ComponentFixture<RbdSnapshotListComponent>;
28   let summaryService: SummaryService;
29
30   const fakeAuthStorageService = {
31     isLoggedIn: () => {
32       return true;
33     },
34     getPermissions: () => {
35       return new Permissions({ 'rbd-image': ['read', 'update', 'create', 'delete'] });
36     }
37   };
38
39   configureTestBed({
40     declarations: [RbdSnapshotListComponent],
41     imports: [
42       DataTableModule,
43       ComponentsModule,
44       ModalModule.forRoot(),
45       ToastModule.forRoot(),
46       ServicesModule,
47       ApiModule,
48       HttpClientTestingModule,
49       RouterTestingModule,
50       PipesModule
51     ],
52     providers: [
53       { provide: AuthStorageService, useValue: fakeAuthStorageService },
54       SummaryService,
55       TaskListService
56     ]
57   });
58
59   beforeEach(() => {
60     fixture = TestBed.createComponent(RbdSnapshotListComponent);
61     component = fixture.componentInstance;
62     summaryService = TestBed.get(SummaryService);
63     fixture.detectChanges();
64   });
65
66   it('should create', () => {
67     expect(component).toBeTruthy();
68   });
69
70   describe('api delete request', () => {
71     let called;
72     let rbdService: RbdService;
73     let notificationService: NotificationService;
74     let authStorageService: AuthStorageService;
75
76     beforeEach(() => {
77       called = false;
78       rbdService = new RbdService(null);
79       notificationService = new NotificationService(null, null);
80       authStorageService = new AuthStorageService();
81       authStorageService.set('user', { 'rbd-image': ['create', 'read', 'update', 'delete'] });
82       component = new RbdSnapshotListComponent(
83         authStorageService,
84         null,
85         null,
86         null,
87         rbdService,
88         null,
89         notificationService,
90         null,
91         null
92       );
93       spyOn(rbdService, 'deleteSnapshot').and.returnValue(observableThrowError({ status: 500 }));
94       spyOn(notificationService, 'notifyTask').and.stub();
95       component.modalRef = new BsModalRef();
96       component.modalRef.content = {
97         stopLoadingSpinner: () => (called = true)
98       };
99     });
100
101     it('should call stopLoadingSpinner if the request fails', <any>fakeAsync(() => {
102       expect(called).toBe(false);
103       component._asyncTask('deleteSnapshot', 'rbd/snap/delete', 'someName');
104       tick(500);
105       expect(called).toBe(true);
106     }));
107   });
108
109   describe('handling of executing tasks', () => {
110     let snapshots: RbdSnapshotModel[];
111
112     const addSnapshot = (name) => {
113       const model = new RbdSnapshotModel();
114       model.id = 1;
115       model.name = name;
116       snapshots.push(model);
117     };
118
119     const addTask = (task_name: string, snapshot_name: string) => {
120       const task = new ExecutingTask();
121       task.name = task_name;
122       task.metadata = {
123         pool_name: 'rbd',
124         image_name: 'foo',
125         snapshot_name: snapshot_name
126       };
127       summaryService.addRunningTask(task);
128     };
129
130     const expectImageTasks = (snapshot: RbdSnapshotModel, executing: string) => {
131       expect(snapshot.cdExecuting).toEqual(executing);
132     };
133
134     const refresh = (data) => {
135       summaryService['summaryDataSource'].next(data);
136     };
137
138     beforeEach(() => {
139       snapshots = [];
140       addSnapshot('a');
141       addSnapshot('b');
142       addSnapshot('c');
143       component.snapshots = snapshots;
144       component.poolName = 'rbd';
145       component.rbdName = 'foo';
146       refresh({ executing_tasks: [], finished_tasks: [] });
147       component.ngOnChanges();
148       fixture.detectChanges();
149     });
150
151     it('should gets all snapshots without tasks', () => {
152       expect(component.snapshots.length).toBe(3);
153       expect(component.snapshots.every((image) => !image.cdExecuting)).toBeTruthy();
154     });
155
156     it('should add a new image from a task', () => {
157       addTask('rbd/snap/create', 'd');
158       expect(component.snapshots.length).toBe(4);
159       expectImageTasks(component.snapshots[0], undefined);
160       expectImageTasks(component.snapshots[1], undefined);
161       expectImageTasks(component.snapshots[2], undefined);
162       expectImageTasks(component.snapshots[3], 'Creating');
163     });
164
165     it('should show when an existing image is being modified', () => {
166       addTask('rbd/snap/edit', 'a');
167       addTask('rbd/snap/delete', 'b');
168       addTask('rbd/snap/rollback', 'c');
169       expect(component.snapshots.length).toBe(3);
170       expectImageTasks(component.snapshots[0], 'Updating');
171       expectImageTasks(component.snapshots[1], 'Deleting');
172       expectImageTasks(component.snapshots[2], 'Rolling back');
173     });
174   });
175 });