1 import { HttpHeaders } from '@angular/common/http';
2 import { HttpClientTestingModule } from '@angular/common/http/testing';
3 import { ComponentFixture, TestBed } from '@angular/core/testing';
4 import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
5 import { RouterTestingModule } from '@angular/router/testing';
7 import { NgbNavModule, NgbTooltipModule } from '@ng-bootstrap/ng-bootstrap';
8 import { ToastrModule } from 'ngx-toastr';
9 import { BehaviorSubject, of } from 'rxjs';
11 import { RbdService } from '~/app/shared/api/rbd.service';
12 import { TableActionsComponent } from '~/app/shared/datatable/table-actions/table-actions.component';
13 import { ExecutingTask } from '~/app/shared/models/executing-task';
14 import { SummaryService } from '~/app/shared/services/summary.service';
15 import { TaskListService } from '~/app/shared/services/task-list.service';
16 import { SharedModule } from '~/app/shared/shared.module';
17 import { configureTestBed, expectItemTasks, PermissionHelper } from '~/testing/unit-test-helper';
18 import { RbdConfigurationListComponent } from '../rbd-configuration-list/rbd-configuration-list.component';
19 import { RbdDetailsComponent } from '../rbd-details/rbd-details.component';
20 import { RbdSnapshotListComponent } from '../rbd-snapshot-list/rbd-snapshot-list.component';
21 import { RbdTabsComponent } from '../rbd-tabs/rbd-tabs.component';
22 import { RbdListComponent } from './rbd-list.component';
23 import { RbdModel } from './rbd-model';
25 describe('RbdListComponent', () => {
26 let fixture: ComponentFixture<RbdListComponent>;
27 let component: RbdListComponent;
28 let summaryService: SummaryService;
29 let rbdService: RbdService;
30 let headers: HttpHeaders;
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);
61 headers = new HttpHeaders().set('X-Total-Count', '10');
63 // this is needed because summaryService isn't being reset after each test.
64 summaryService['summaryDataSource'] = new BehaviorSubject(null);
65 summaryService['summaryData$'] = summaryService['summaryDataSource'].asObservable();
68 it('should create', () => {
69 expect(component).toBeTruthy();
72 describe('after ngOnInit', () => {
74 fixture.detectChanges();
75 spyOn(rbdService, 'list').and.callThrough();
78 it('should load images on init', () => {
80 expect(rbdService.list).toHaveBeenCalled();
83 it('should not load images on init because no data', () => {
85 expect(rbdService.list).not.toHaveBeenCalled();
88 it('should call error function on init when summary service fails', () => {
89 spyOn(component.table, 'reset');
90 summaryService['summaryDataSource'].error(undefined);
91 expect(component.table.reset).toHaveBeenCalled();
95 describe('handling of deletion', () => {
97 fixture.detectChanges();
100 it('should check if there are no snapshots', () => {
101 component.selection.add({
106 expect(component.hasSnapshots()).toBeFalsy();
109 it('should check if there are snapshots', () => {
110 component.selection.add({
116 expect(component.hasSnapshots()).toBeTruthy();
119 it('should get delete disable description', () => {
120 component.selection.add({
130 expect(component.getDeleteDisableDesc(component.selection)).toBe(
131 'This RBD has cloned snapshots. Please delete related RBDs before deleting this RBD.'
135 it('should list all protected snapshots', () => {
136 component.selection.add({
152 expect(component.listProtectedSnapshots()).toEqual(['snap2']);
156 describe('handling of executing tasks', () => {
157 let images: RbdModel[];
159 const addImage = (name: string) => {
160 const model = new RbdModel();
163 model.pool_name = 'rbd';
167 const addTask = (name: string, image_name: string) => {
168 const task = new ExecutingTask();
173 dest_pool_name: 'rbd',
174 dest_namespace: null,
180 child_pool_name: 'rbd',
181 child_namespace: null,
182 child_image_name: 'd'
189 image_name: image_name
194 image_spec: `rbd/${image_name}`
198 summaryService.addRunningTask(task);
206 component.images = images;
207 refresh({ executing_tasks: [], finished_tasks: [] });
208 spyOn(rbdService, 'list').and.callFake(() =>
209 of([{ pool_name: 'rbd', value: images, headers: headers }])
211 fixture.detectChanges();
214 it('should gets all images without tasks', () => {
215 expect(component.images.length).toBe(3);
216 expect(component.images.every((image: any) => !image.cdExecuting)).toBeTruthy();
219 it('should add a new image from a task', () => {
220 addTask('rbd/create', 'd');
221 expect(component.images.length).toBe(4);
222 expectItemTasks(component.images[0], undefined);
223 expectItemTasks(component.images[1], undefined);
224 expectItemTasks(component.images[2], undefined);
225 expectItemTasks(component.images[3], 'Creating');
228 it('should show when a image is being cloned', () => {
229 addTask('rbd/clone', 'd');
230 expect(component.images.length).toBe(4);
231 expectItemTasks(component.images[0], undefined);
232 expectItemTasks(component.images[1], undefined);
233 expectItemTasks(component.images[2], undefined);
234 expectItemTasks(component.images[3], 'Cloning');
237 it('should show when a image is being copied', () => {
238 addTask('rbd/copy', 'd');
239 expect(component.images.length).toBe(4);
240 expectItemTasks(component.images[0], undefined);
241 expectItemTasks(component.images[1], undefined);
242 expectItemTasks(component.images[2], undefined);
243 expectItemTasks(component.images[3], 'Copying');
246 it('should show when an existing image is being modified', () => {
247 addTask('rbd/edit', 'a');
248 expectItemTasks(component.images[0], 'Updating');
249 addTask('rbd/delete', 'b');
250 expectItemTasks(component.images[1], 'Deleting');
251 addTask('rbd/flatten', 'c');
252 expectItemTasks(component.images[2], 'Flattening');
253 expect(component.images.length).toBe(3);
257 it('should test all TableActions combinations', () => {
258 const permissionHelper: PermissionHelper = new PermissionHelper(component.permission);
259 const tableActions: TableActionsComponent = permissionHelper.setPermissionsAndGetActions(
260 component.tableActions
263 expect(tableActions).toEqual({
264 'create,update,delete': {
277 primary: { multiple: 'Create', executing: 'Edit', single: 'Edit', no: 'Create' }
290 primary: { multiple: 'Create', executing: 'Edit', single: 'Edit', no: 'Create' }
293 actions: ['Create', 'Copy', 'Delete', 'Move to Trash'],
294 primary: { multiple: 'Create', executing: 'Copy', single: 'Copy', no: 'Create' }
297 actions: ['Create', 'Copy'],
298 primary: { multiple: 'Create', executing: 'Copy', single: 'Copy', no: 'Create' }
311 primary: { multiple: 'Edit', executing: 'Edit', single: 'Edit', no: 'Edit' }
314 actions: ['Edit', 'Flatten', 'Resync', 'Remove Scheduling', 'Promote', 'Demote'],
315 primary: { multiple: 'Edit', executing: 'Edit', single: 'Edit', no: 'Edit' }
318 actions: ['Delete', 'Move to Trash'],
319 primary: { multiple: 'Delete', executing: 'Delete', single: 'Delete', no: 'Delete' }
323 primary: { multiple: '', executing: '', single: '', no: '' }
328 const getActionDisable = (name: string) =>
329 component.tableActions.find((o) => o.name === name).disable;
331 const testActions = (selection: any, expected: { [action: string]: string | boolean }) => {
332 expect(getActionDisable('Edit')(selection)).toBe(expected.edit || false);
333 expect(getActionDisable('Delete')(selection)).toBe(expected.delete || false);
334 expect(getActionDisable('Copy')(selection)).toBe(expected.copy || false);
335 expect(getActionDisable('Flatten')(selection)).toBeTruthy();
336 expect(getActionDisable('Move to Trash')(selection)).toBe(expected.moveTrash || false);
339 it('should test TableActions with valid/invalid image name', () => {
340 component.selection.selected = [
347 testActions(component.selection, {});
349 component.selection.selected = [
356 const message = `This RBD image has an invalid name and can't be managed by ceph.`;
363 testActions(component.selection, expected);
366 it('should disable edit, copy, flatten and move action if RBD is in status `Removing`', () => {
367 component.selection.selected = [
376 const message = `Action not possible for an RBD in status 'Removing'`;
382 testActions(component.selection, expected);