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';
5 import { ToastModule } from 'ng2-toastr';
6 import { BsModalRef, ModalModule } from 'ngx-bootstrap';
7 import { throwError as observableThrowError } from 'rxjs';
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';
25 describe('RbdSnapshotListComponent', () => {
26 let component: RbdSnapshotListComponent;
27 let fixture: ComponentFixture<RbdSnapshotListComponent>;
28 let summaryService: SummaryService;
30 const fakeAuthStorageService = {
34 getPermissions: () => {
35 return new Permissions({ 'rbd-image': ['read', 'update', 'create', 'delete'] });
40 declarations: [RbdSnapshotListComponent],
44 ModalModule.forRoot(),
45 ToastModule.forRoot(),
48 HttpClientTestingModule,
53 { provide: AuthStorageService, useValue: fakeAuthStorageService },
60 fixture = TestBed.createComponent(RbdSnapshotListComponent);
61 component = fixture.componentInstance;
62 summaryService = TestBed.get(SummaryService);
63 fixture.detectChanges();
66 it('should create', () => {
67 expect(component).toBeTruthy();
70 describe('api delete request', () => {
72 let rbdService: RbdService;
73 let notificationService: NotificationService;
74 let authStorageService: AuthStorageService;
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(
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)
101 it('should call stopLoadingSpinner if the request fails', <any>fakeAsync(() => {
102 expect(called).toBe(false);
103 component._asyncTask('deleteSnapshot', 'rbd/snap/delete', 'someName');
105 expect(called).toBe(true);
109 describe('handling of executing tasks', () => {
110 let snapshots: RbdSnapshotModel[];
112 const addSnapshot = (name) => {
113 const model = new RbdSnapshotModel();
116 snapshots.push(model);
119 const addTask = (task_name: string, snapshot_name: string) => {
120 const task = new ExecutingTask();
121 task.name = task_name;
125 snapshot_name: snapshot_name
127 summaryService.addRunningTask(task);
130 const expectImageTasks = (snapshot: RbdSnapshotModel, executing: string) => {
131 expect(snapshot.cdExecuting).toEqual(executing);
134 const refresh = (data) => {
135 summaryService['summaryDataSource'].next(data);
143 component.snapshots = snapshots;
144 component.poolName = 'rbd';
145 component.rbdName = 'foo';
146 refresh({ executing_tasks: [], finished_tasks: [] });
147 component.ngOnChanges();
148 fixture.detectChanges();
151 it('should gets all snapshots without tasks', () => {
152 expect(component.snapshots.length).toBe(3);
153 expect(component.snapshots.every((image) => !image.cdExecuting)).toBeTruthy();
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');
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');