1 import { HttpClientTestingModule } from '@angular/common/http/testing';
2 import { ComponentFixture, TestBed } from '@angular/core/testing';
3 import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
4 import { RouterTestingModule } from '@angular/router/testing';
6 import { NgbNavModule, NgbTooltipModule } from '@ng-bootstrap/ng-bootstrap';
7 import { ToastrModule } from 'ngx-toastr';
8 import { BehaviorSubject, of } from 'rxjs';
10 import { RbdService } from '~/app/shared/api/rbd.service';
11 import { TableStatusViewCache } from '~/app/shared/classes/table-status-view-cache';
12 import { TableActionsComponent } from '~/app/shared/datatable/table-actions/table-actions.component';
13 import { ViewCacheStatus } from '~/app/shared/enum/view-cache-status.enum';
14 import { ExecutingTask } from '~/app/shared/models/executing-task';
15 import { SummaryService } from '~/app/shared/services/summary.service';
16 import { TaskListService } from '~/app/shared/services/task-list.service';
17 import { SharedModule } from '~/app/shared/shared.module';
18 import { configureTestBed, expectItemTasks, PermissionHelper } from '~/testing/unit-test-helper';
19 import { RbdConfigurationListComponent } from '../rbd-configuration-list/rbd-configuration-list.component';
20 import { RbdDetailsComponent } from '../rbd-details/rbd-details.component';
21 import { RbdSnapshotListComponent } from '../rbd-snapshot-list/rbd-snapshot-list.component';
22 import { RbdTabsComponent } from '../rbd-tabs/rbd-tabs.component';
23 import { RbdListComponent } from './rbd-list.component';
24 import { RbdModel } from './rbd-model';
26 describe('RbdListComponent', () => {
27 let fixture: ComponentFixture<RbdListComponent>;
28 let component: RbdListComponent;
29 let summaryService: SummaryService;
30 let rbdService: RbdService;
32 const refresh = (data: any) => {
33 summaryService['summaryDataSource'].next(data);
38 BrowserAnimationsModule,
42 ToastrModule.forRoot(),
44 HttpClientTestingModule
49 RbdSnapshotListComponent,
50 RbdConfigurationListComponent,
53 providers: [TaskListService]
57 fixture = TestBed.createComponent(RbdListComponent);
58 component = fixture.componentInstance;
59 summaryService = TestBed.inject(SummaryService);
60 rbdService = TestBed.inject(RbdService);
62 // this is needed because summaryService isn't being reset after each test.
63 summaryService['summaryDataSource'] = new BehaviorSubject(null);
64 summaryService['summaryData$'] = summaryService['summaryDataSource'].asObservable();
67 it('should create', () => {
68 expect(component).toBeTruthy();
71 describe('after ngOnInit', () => {
73 fixture.detectChanges();
74 spyOn(rbdService, 'list').and.callThrough();
77 it('should load images on init', () => {
79 expect(rbdService.list).toHaveBeenCalled();
82 it('should not load images on init because no data', () => {
84 expect(rbdService.list).not.toHaveBeenCalled();
87 it('should call error function on init when summary service fails', () => {
88 spyOn(component.table, 'reset');
89 summaryService['summaryDataSource'].error(undefined);
90 expect(component.table.reset).toHaveBeenCalled();
91 expect(component.tableStatus).toEqual(
92 new TableStatusViewCache(ViewCacheStatus.ValueException)
97 describe('handling of provisioned columns', () => {
98 let rbdServiceListSpy: jasmine.Spy;
104 features_name: ['layering', 'exclusive-lock'],
106 total_disk_usage: null
111 features_name: ['layering', 'exclusive-lock', 'object-map', 'fast-diff'],
113 total_disk_usage: 1024
118 component.images = images;
119 refresh({ executing_tasks: [], finished_tasks: [] });
120 rbdServiceListSpy = spyOn(rbdService, 'list');
123 it('should display N/A for Provisioned & Total Provisioned columns if disk usage is null', () => {
124 rbdServiceListSpy.and.callFake(() => of([{ pool_name: 'rbd', status: 1, value: images }]));
125 fixture.detectChanges();
126 const spanWithoutFastDiff = fixture.debugElement.nativeElement.querySelectorAll(
127 '.datatable-body-cell-label span'
129 // check image with disk usage = null & fast-diff disabled
130 expect(spanWithoutFastDiff[6].textContent).toBe('N/A');
132 images[0]['features_name'] = ['layering', 'exclusive-lock', 'object-map', 'fast-diff'];
133 component.images = images;
134 refresh({ executing_tasks: [], finished_tasks: [] });
136 rbdServiceListSpy.and.callFake(() => of([{ pool_name: 'rbd', status: 1, value: images }]));
137 fixture.detectChanges();
139 const spanWithFastDiff = fixture.debugElement.nativeElement.querySelectorAll(
140 '.datatable-body-cell-label span'
142 // check image with disk usage = null & fast-diff changed to enabled
143 expect(spanWithFastDiff[6].textContent).toBe('-');
147 describe('handling of deletion', () => {
149 fixture.detectChanges();
152 it('should check if there are no snapshots', () => {
153 component.selection.add({
158 expect(component.hasSnapshots()).toBeFalsy();
161 it('should check if there are snapshots', () => {
162 component.selection.add({
168 expect(component.hasSnapshots()).toBeTruthy();
171 it('should get delete disable description', () => {
172 component.selection.add({
182 expect(component.getDeleteDisableDesc(component.selection)).toBe(
183 'This RBD has cloned snapshots. Please delete related RBDs before deleting this RBD.'
187 it('should list all protected snapshots', () => {
188 component.selection.add({
204 expect(component.listProtectedSnapshots()).toEqual(['snap2']);
208 describe('handling of executing tasks', () => {
209 let images: RbdModel[];
211 const addImage = (name: string) => {
212 const model = new RbdModel();
215 model.pool_name = 'rbd';
219 const addTask = (name: string, image_name: string) => {
220 const task = new ExecutingTask();
225 dest_pool_name: 'rbd',
226 dest_namespace: null,
232 child_pool_name: 'rbd',
233 child_namespace: null,
234 child_image_name: 'd'
241 image_name: image_name
246 image_spec: `rbd/${image_name}`
250 summaryService.addRunningTask(task);
258 component.images = images;
259 refresh({ executing_tasks: [], finished_tasks: [] });
260 spyOn(rbdService, 'list').and.callFake(() =>
261 of([{ pool_name: 'rbd', status: 1, value: images }])
263 fixture.detectChanges();
266 it('should gets all images without tasks', () => {
267 expect(component.images.length).toBe(3);
268 expect(component.images.every((image: any) => !image.cdExecuting)).toBeTruthy();
271 it('should add a new image from a task', () => {
272 addTask('rbd/create', 'd');
273 expect(component.images.length).toBe(4);
274 expectItemTasks(component.images[0], undefined);
275 expectItemTasks(component.images[1], undefined);
276 expectItemTasks(component.images[2], undefined);
277 expectItemTasks(component.images[3], 'Creating');
280 it('should show when a image is being cloned', () => {
281 addTask('rbd/clone', 'd');
282 expect(component.images.length).toBe(4);
283 expectItemTasks(component.images[0], undefined);
284 expectItemTasks(component.images[1], undefined);
285 expectItemTasks(component.images[2], undefined);
286 expectItemTasks(component.images[3], 'Cloning');
289 it('should show when a image is being copied', () => {
290 addTask('rbd/copy', 'd');
291 expect(component.images.length).toBe(4);
292 expectItemTasks(component.images[0], undefined);
293 expectItemTasks(component.images[1], undefined);
294 expectItemTasks(component.images[2], undefined);
295 expectItemTasks(component.images[3], 'Copying');
298 it('should show when an existing image is being modified', () => {
299 addTask('rbd/edit', 'a');
300 addTask('rbd/delete', 'b');
301 addTask('rbd/flatten', 'c');
302 expect(component.images.length).toBe(3);
303 expectItemTasks(component.images[0], 'Updating');
304 expectItemTasks(component.images[1], 'Deleting');
305 expectItemTasks(component.images[2], 'Flattening');
309 it('should test all TableActions combinations', () => {
310 const permissionHelper: PermissionHelper = new PermissionHelper(component.permission);
311 const tableActions: TableActionsComponent = permissionHelper.setPermissionsAndGetActions(
312 component.tableActions
315 expect(tableActions).toEqual({
316 'create,update,delete': {
317 actions: ['Create', 'Edit', 'Copy', 'Flatten', 'Delete', 'Move to Trash'],
318 primary: { multiple: 'Create', executing: 'Edit', single: 'Edit', no: 'Create' }
321 actions: ['Create', 'Edit', 'Copy', 'Flatten'],
322 primary: { multiple: 'Create', executing: 'Edit', single: 'Edit', no: 'Create' }
325 actions: ['Create', 'Copy', 'Delete', 'Move to Trash'],
326 primary: { multiple: 'Create', executing: 'Copy', single: 'Copy', no: 'Create' }
329 actions: ['Create', 'Copy'],
330 primary: { multiple: 'Create', executing: 'Copy', single: 'Copy', no: 'Create' }
333 actions: ['Edit', 'Flatten', 'Delete', 'Move to Trash'],
334 primary: { multiple: 'Edit', executing: 'Edit', single: 'Edit', no: 'Edit' }
337 actions: ['Edit', 'Flatten'],
338 primary: { multiple: 'Edit', executing: 'Edit', single: 'Edit', no: 'Edit' }
341 actions: ['Delete', 'Move to Trash'],
342 primary: { multiple: 'Delete', executing: 'Delete', single: 'Delete', no: 'Delete' }
346 primary: { multiple: '', executing: '', single: '', no: '' }
351 const getActionDisable = (name: string) =>
352 component.tableActions.find((o) => o.name === name).disable;
354 const testActions = (selection: any, expected: { [action: string]: string | boolean }) => {
355 expect(getActionDisable('Edit')(selection)).toBe(expected.edit || false);
356 expect(getActionDisable('Delete')(selection)).toBe(expected.delete || false);
357 expect(getActionDisable('Copy')(selection)).toBe(expected.copy || false);
358 expect(getActionDisable('Flatten')(selection)).toBeTruthy();
359 expect(getActionDisable('Move to Trash')(selection)).toBe(expected.moveTrash || false);
362 it('should test TableActions with valid/invalid image name', () => {
363 component.selection.selected = [
370 testActions(component.selection, {});
372 component.selection.selected = [
379 const message = `This RBD image has an invalid name and can't be managed by ceph.`;
386 testActions(component.selection, expected);
389 it('should disable edit, copy, flatten and move action if RBD is in status `Removing`', () => {
390 component.selection.selected = [
399 const message = `Action not possible for an RBD in status 'Removing'`;
405 testActions(component.selection, expected);