1 import { HttpClientTestingModule } from '@angular/common/http/testing';
2 import { ComponentFixture, fakeAsync, TestBed, tick } from '@angular/core/testing';
3 import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
4 import { RouterTestingModule } from '@angular/router/testing';
6 import { NgbNavModule } from '@ng-bootstrap/ng-bootstrap';
7 import { I18n } from '@ngx-translate/i18n-polyfill';
8 import { BsModalRef, BsModalService } from 'ngx-bootstrap/modal';
9 import { ToastrModule } from 'ngx-toastr';
10 import { Subject, throwError as observableThrowError } from 'rxjs';
17 } from '../../../../testing/unit-test-helper';
18 import { ApiModule } from '../../../shared/api/api.module';
19 import { RbdService } from '../../../shared/api/rbd.service';
20 import { ComponentsModule } from '../../../shared/components/components.module';
21 import { ActionLabelsI18n } from '../../../shared/constants/app.constants';
22 import { DataTableModule } from '../../../shared/datatable/datatable.module';
23 import { TableActionsComponent } from '../../../shared/datatable/table-actions/table-actions.component';
24 import { ExecutingTask } from '../../../shared/models/executing-task';
25 import { Permissions } from '../../../shared/models/permissions';
26 import { PipesModule } from '../../../shared/pipes/pipes.module';
27 import { AuthStorageService } from '../../../shared/services/auth-storage.service';
28 import { NotificationService } from '../../../shared/services/notification.service';
29 import { SummaryService } from '../../../shared/services/summary.service';
30 import { TaskListService } from '../../../shared/services/task-list.service';
31 import { RbdSnapshotFormModalComponent } from '../rbd-snapshot-form/rbd-snapshot-form-modal.component';
32 import { RbdTabsComponent } from '../rbd-tabs/rbd-tabs.component';
33 import { RbdSnapshotListComponent } from './rbd-snapshot-list.component';
34 import { RbdSnapshotModel } from './rbd-snapshot.model';
36 describe('RbdSnapshotListComponent', () => {
37 let component: RbdSnapshotListComponent;
38 let fixture: ComponentFixture<RbdSnapshotListComponent>;
39 let summaryService: SummaryService;
41 const fakeAuthStorageService = {
45 getPermissions: () => {
46 return new Permissions({ 'rbd-image': ['read', 'update', 'create', 'delete'] });
51 declarations: [RbdSnapshotListComponent, RbdTabsComponent],
54 BrowserAnimationsModule,
57 HttpClientTestingModule,
61 ToastrModule.forRoot()
64 { provide: AuthStorageService, useValue: fakeAuthStorageService },
71 fixture = TestBed.createComponent(RbdSnapshotListComponent);
72 component = fixture.componentInstance;
73 component.ngOnChanges();
74 summaryService = TestBed.inject(SummaryService);
77 it('should create', () => {
78 fixture.detectChanges();
79 expect(component).toBeTruthy();
82 describe('api delete request', () => {
84 let rbdService: RbdService;
85 let notificationService: NotificationService;
86 let authStorageService: AuthStorageService;
89 fixture.detectChanges();
90 const i18n = TestBed.inject(I18n);
91 const actionLabelsI18n = TestBed.inject(ActionLabelsI18n);
93 rbdService = new RbdService(null, null);
94 notificationService = new NotificationService(null, null, null);
95 authStorageService = new AuthStorageService();
96 authStorageService.set('user', '', { 'rbd-image': ['create', 'read', 'update', 'delete'] });
97 component = new RbdSnapshotListComponent(
110 spyOn(rbdService, 'deleteSnapshot').and.returnValue(observableThrowError({ status: 500 }));
111 spyOn(notificationService, 'notifyTask').and.stub();
112 component.modalRef = new BsModalRef();
113 component.modalRef.content = {
114 stopLoadingSpinner: () => (called = true)
118 it('should call stopLoadingSpinner if the request fails', fakeAsync(() => {
119 expect(called).toBe(false);
120 component._asyncTask('deleteSnapshot', 'rbd/snap/delete', 'someName');
122 expect(called).toBe(true);
126 describe('handling of executing tasks', () => {
127 let snapshots: RbdSnapshotModel[];
129 const addSnapshot = (name: string) => {
130 const model = new RbdSnapshotModel();
133 snapshots.push(model);
136 const addTask = (task_name: string, snapshot_name: string) => {
137 const task = new ExecutingTask();
138 task.name = task_name;
140 image_spec: 'rbd/foo',
141 snapshot_name: snapshot_name
143 summaryService.addRunningTask(task);
146 const refresh = (data: any) => {
147 summaryService['summaryDataSource'].next(data);
151 fixture.detectChanges();
156 component.snapshots = snapshots;
157 component.poolName = 'rbd';
158 component.rbdName = 'foo';
159 refresh({ executing_tasks: [], finished_tasks: [] });
160 component.ngOnChanges();
161 fixture.detectChanges();
164 it('should gets all snapshots without tasks', () => {
165 expect(component.snapshots.length).toBe(3);
166 expect(component.snapshots.every((image) => !image.cdExecuting)).toBeTruthy();
169 it('should add a new image from a task', () => {
170 addTask('rbd/snap/create', 'd');
171 expect(component.snapshots.length).toBe(4);
172 expectItemTasks(component.snapshots[0], undefined);
173 expectItemTasks(component.snapshots[1], undefined);
174 expectItemTasks(component.snapshots[2], undefined);
175 expectItemTasks(component.snapshots[3], 'Creating');
178 it('should show when an existing image is being modified', () => {
179 addTask('rbd/snap/edit', 'a');
180 addTask('rbd/snap/delete', 'b');
181 addTask('rbd/snap/rollback', 'c');
182 expect(component.snapshots.length).toBe(3);
183 expectItemTasks(component.snapshots[0], 'Updating');
184 expectItemTasks(component.snapshots[1], 'Deleting');
185 expectItemTasks(component.snapshots[2], 'Rolling back');
189 describe('snapshot modal dialog', () => {
191 component.poolName = 'pool01';
192 component.rbdName = 'image01';
193 spyOn(TestBed.inject(BsModalService), 'show').and.callFake(() => {
194 const ref = new BsModalRef();
195 ref.content = new RbdSnapshotFormModalComponent(
200 TestBed.inject(I18n),
201 TestBed.inject(ActionLabelsI18n)
203 ref.content.onSubmit = new Subject();
208 it('should display old snapshot name', () => {
209 component.selection.selected = [{ name: 'oldname' }];
210 component.openEditSnapshotModal();
211 expect(component.modalRef.content.snapName).toBe('oldname');
212 expect(component.modalRef.content.editing).toBeTruthy();
215 it('should display suggested snapshot name', () => {
216 component.openCreateSnapshotModal();
217 expect(component.modalRef.content.snapName).toMatch(
218 RegExp(`^${component.rbdName}_[\\d-]+T[\\d.:]+[\\+-][\\d:]+$`)
223 it('should test all TableActions combinations', () => {
224 const permissionHelper: PermissionHelper = new PermissionHelper(component.permission);
225 const tableActions: TableActionsComponent = permissionHelper.setPermissionsAndGetActions(
226 component.tableActions
229 expect(tableActions).toEqual({
230 'create,update,delete': {
241 primary: { multiple: 'Create', executing: 'Rename', single: 'Rename', no: 'Create' }
244 actions: ['Create', 'Rename', 'Protect', 'Unprotect', 'Clone', 'Copy', 'Rollback'],
245 primary: { multiple: 'Create', executing: 'Rename', single: 'Rename', no: 'Create' }
248 actions: ['Create', 'Clone', 'Copy', 'Delete'],
249 primary: { multiple: 'Create', executing: 'Clone', single: 'Clone', no: 'Create' }
252 actions: ['Create', 'Clone', 'Copy'],
253 primary: { multiple: 'Create', executing: 'Clone', single: 'Clone', no: 'Create' }
256 actions: ['Rename', 'Protect', 'Unprotect', 'Rollback', 'Delete'],
257 primary: { multiple: 'Rename', executing: 'Rename', single: 'Rename', no: 'Rename' }
260 actions: ['Rename', 'Protect', 'Unprotect', 'Rollback'],
261 primary: { multiple: 'Rename', executing: 'Rename', single: 'Rename', no: 'Rename' }
265 primary: { multiple: 'Delete', executing: 'Delete', single: 'Delete', no: 'Delete' }
269 primary: { multiple: '', executing: '', single: '', no: '' }