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 { NgbModalModule, NgbNavModule } from '@ng-bootstrap/ng-bootstrap';
7 import { MockComponent } from 'ng-mocks';
8 import { ToastrModule } from 'ngx-toastr';
9 import { Subject, throwError as observableThrowError } from 'rxjs';
15 } from '../../../../testing/unit-test-helper';
16 import { RbdService } from '../../../shared/api/rbd.service';
17 import { ComponentsModule } from '../../../shared/components/components.module';
18 import { CriticalConfirmationModalComponent } from '../../../shared/components/critical-confirmation-modal/critical-confirmation-modal.component';
19 import { ActionLabelsI18n } from '../../../shared/constants/app.constants';
20 import { DataTableModule } from '../../../shared/datatable/datatable.module';
21 import { TableActionsComponent } from '../../../shared/datatable/table-actions/table-actions.component';
22 import { CdTableSelection } from '../../../shared/models/cd-table-selection';
23 import { ExecutingTask } from '../../../shared/models/executing-task';
24 import { Permissions } from '../../../shared/models/permissions';
25 import { PipesModule } from '../../../shared/pipes/pipes.module';
26 import { AuthStorageService } from '../../../shared/services/auth-storage.service';
27 import { ModalService } from '../../../shared/services/modal.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 { RbdSnapshotActionsModel } from './rbd-snapshot-actions.model';
34 import { RbdSnapshotListComponent } from './rbd-snapshot-list.component';
35 import { RbdSnapshotModel } from './rbd-snapshot.model';
37 describe('RbdSnapshotListComponent', () => {
38 let component: RbdSnapshotListComponent;
39 let fixture: ComponentFixture<RbdSnapshotListComponent>;
40 let summaryService: SummaryService;
42 const fakeAuthStorageService = {
46 getPermissions: () => {
47 return new Permissions({ 'rbd-image': ['read', 'update', 'create', 'delete'] });
54 RbdSnapshotListComponent,
56 MockComponent(RbdSnapshotFormModalComponent)
59 BrowserAnimationsModule,
62 HttpClientTestingModule,
66 ToastrModule.forRoot(),
70 { provide: AuthStorageService, useValue: fakeAuthStorageService },
74 [CriticalConfirmationModalComponent]
78 fixture = TestBed.createComponent(RbdSnapshotListComponent);
79 component = fixture.componentInstance;
80 component.ngOnChanges();
81 summaryService = TestBed.inject(SummaryService);
84 it('should create', () => {
85 fixture.detectChanges();
86 expect(component).toBeTruthy();
89 describe('api delete request', () => {
91 let rbdService: RbdService;
92 let notificationService: NotificationService;
93 let authStorageService: AuthStorageService;
96 fixture.detectChanges();
97 const modalService = TestBed.inject(ModalService);
98 const actionLabelsI18n = TestBed.inject(ActionLabelsI18n);
100 rbdService = new RbdService(null, null);
101 notificationService = new NotificationService(null, null, null);
102 authStorageService = new AuthStorageService();
103 authStorageService.set('user', '', { 'rbd-image': ['create', 'read', 'update', 'delete'] });
104 component = new RbdSnapshotListComponent(
116 spyOn(rbdService, 'deleteSnapshot').and.returnValue(observableThrowError({ status: 500 }));
117 spyOn(notificationService, 'notifyTask').and.stub();
120 it('should call stopLoadingSpinner if the request fails', fakeAsync(() => {
121 component.updateSelection(new CdTableSelection([{ name: 'someName' }]));
122 expect(called).toBe(false);
123 component.deleteSnapshotModal();
124 spyOn(component.modalRef.componentInstance, 'stopLoadingSpinner').and.callFake(() => {
127 component.modalRef.componentInstance.submitAction();
129 expect(called).toBe(true);
133 describe('handling of executing tasks', () => {
134 let snapshots: RbdSnapshotModel[];
136 const addSnapshot = (name: string) => {
137 const model = new RbdSnapshotModel();
140 snapshots.push(model);
143 const addTask = (task_name: string, snapshot_name: string) => {
144 const task = new ExecutingTask();
145 task.name = task_name;
147 image_spec: 'rbd/foo',
148 snapshot_name: snapshot_name
150 summaryService.addRunningTask(task);
153 const refresh = (data: any) => {
154 summaryService['summaryDataSource'].next(data);
158 fixture.detectChanges();
163 component.snapshots = snapshots;
164 component.poolName = 'rbd';
165 component.rbdName = 'foo';
166 refresh({ executing_tasks: [], finished_tasks: [] });
167 component.ngOnChanges();
168 fixture.detectChanges();
171 it('should gets all snapshots without tasks', () => {
172 expect(component.snapshots.length).toBe(3);
173 expect(component.snapshots.every((image) => !image.cdExecuting)).toBeTruthy();
176 it('should add a new image from a task', () => {
177 addTask('rbd/snap/create', 'd');
178 expect(component.snapshots.length).toBe(4);
179 expectItemTasks(component.snapshots[0], undefined);
180 expectItemTasks(component.snapshots[1], undefined);
181 expectItemTasks(component.snapshots[2], undefined);
182 expectItemTasks(component.snapshots[3], 'Creating');
185 it('should show when an existing image is being modified', () => {
186 addTask('rbd/snap/edit', 'a');
187 addTask('rbd/snap/delete', 'b');
188 addTask('rbd/snap/rollback', 'c');
189 expect(component.snapshots.length).toBe(3);
190 expectItemTasks(component.snapshots[0], 'Updating');
191 expectItemTasks(component.snapshots[1], 'Deleting');
192 expectItemTasks(component.snapshots[2], 'Rolling back');
196 describe('snapshot modal dialog', () => {
198 component.poolName = 'pool01';
199 component.rbdName = 'image01';
200 spyOn(TestBed.inject(ModalService), 'show').and.callFake(() => {
202 ref.componentInstance = new RbdSnapshotFormModalComponent(
207 TestBed.inject(ActionLabelsI18n)
209 ref.componentInstance.onSubmit = new Subject();
214 it('should display old snapshot name', () => {
215 component.selection.selected = [{ name: 'oldname' }];
216 component.openEditSnapshotModal();
217 expect(component.modalRef.componentInstance.snapName).toBe('oldname');
218 expect(component.modalRef.componentInstance.editing).toBeTruthy();
221 it('should display suggested snapshot name', () => {
222 component.openCreateSnapshotModal();
223 expect(component.modalRef.componentInstance.snapName).toMatch(
224 RegExp(`^${component.rbdName}_[\\d-]+T[\\d.:]+[\\+-][\\d:]+$`)
229 it('should test all TableActions combinations', () => {
230 const permissionHelper: PermissionHelper = new PermissionHelper(component.permission);
231 const tableActions: TableActionsComponent = permissionHelper.setPermissionsAndGetActions(
232 component.tableActions
235 expect(tableActions).toEqual({
236 'create,update,delete': {
247 primary: { multiple: 'Create', executing: 'Rename', single: 'Rename', no: 'Create' }
250 actions: ['Create', 'Rename', 'Protect', 'Unprotect', 'Clone', 'Copy', 'Rollback'],
251 primary: { multiple: 'Create', executing: 'Rename', single: 'Rename', no: 'Create' }
254 actions: ['Create', 'Clone', 'Copy', 'Delete'],
255 primary: { multiple: 'Create', executing: 'Clone', single: 'Clone', no: 'Create' }
258 actions: ['Create', 'Clone', 'Copy'],
259 primary: { multiple: 'Create', executing: 'Clone', single: 'Clone', no: 'Create' }
262 actions: ['Rename', 'Protect', 'Unprotect', 'Rollback', 'Delete'],
263 primary: { multiple: 'Rename', executing: 'Rename', single: 'Rename', no: 'Rename' }
266 actions: ['Rename', 'Protect', 'Unprotect', 'Rollback'],
267 primary: { multiple: 'Rename', executing: 'Rename', single: 'Rename', no: 'Rename' }
271 primary: { multiple: 'Delete', executing: 'Delete', single: 'Delete', no: 'Delete' }
275 primary: { multiple: '', executing: '', single: '', no: '' }
280 describe('clone button disable state', () => {
281 let actions: RbdSnapshotActionsModel;
284 fixture.detectChanges();
285 const rbdService = TestBed.inject(RbdService);
286 const actionLabelsI18n = TestBed.inject(ActionLabelsI18n);
287 actions = new RbdSnapshotActionsModel(actionLabelsI18n, [], rbdService);
290 it('should be disabled with version 1 and protected false', () => {
291 const selection = new CdTableSelection([{ name: 'someName', is_protected: false }]);
292 const disableDesc = actions.getCloneDisableDesc(selection, ['layering']);
293 expect(disableDesc).toBe('Snapshot must be protected in order to clone.');
300 ])('should be enabled with version %d and protected %s', (version, is_protected) => {
301 actions.cloneFormatVersion = version;
302 const selection = new CdTableSelection([{ name: 'someName', is_protected: is_protected }]);
303 const disableDesc = actions.getCloneDisableDesc(selection, ['layering']);
304 expect(disableDesc).toBe(false);