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 deletion', () => {
99 fixture.detectChanges();
102 it('should check if there are no snapshots', () => {
103 component.selection.add({
108 expect(component.hasSnapshots()).toBeFalsy();
111 it('should check if there are snapshots', () => {
112 component.selection.add({
118 expect(component.hasSnapshots()).toBeTruthy();
121 it('should get delete disable description', () => {
122 component.selection.add({
132 expect(component.getDeleteDisableDesc(component.selection)).toBe(
133 'This RBD has cloned snapshots. Please delete related RBDs before deleting this RBD.'
137 it('should list all protected snapshots', () => {
138 component.selection.add({
154 expect(component.listProtectedSnapshots()).toEqual(['snap2']);
158 describe('handling of executing tasks', () => {
159 let images: RbdModel[];
161 const addImage = (name: string) => {
162 const model = new RbdModel();
165 model.pool_name = 'rbd';
169 const addTask = (name: string, image_name: string) => {
170 const task = new ExecutingTask();
175 dest_pool_name: 'rbd',
176 dest_namespace: null,
182 child_pool_name: 'rbd',
183 child_namespace: null,
184 child_image_name: 'd'
191 image_name: image_name
196 image_spec: `rbd/${image_name}`
200 summaryService.addRunningTask(task);
208 component.images = images;
209 refresh({ executing_tasks: [], finished_tasks: [] });
210 spyOn(rbdService, 'list').and.callFake(() =>
211 of([{ pool_name: 'rbd', status: 1, value: images }])
213 fixture.detectChanges();
216 it('should gets all images without tasks', () => {
217 expect(component.images.length).toBe(3);
218 expect(component.images.every((image: any) => !image.cdExecuting)).toBeTruthy();
221 it('should add a new image from a task', () => {
222 addTask('rbd/create', 'd');
223 expect(component.images.length).toBe(4);
224 expectItemTasks(component.images[0], undefined);
225 expectItemTasks(component.images[1], undefined);
226 expectItemTasks(component.images[2], undefined);
227 expectItemTasks(component.images[3], 'Creating');
230 it('should show when a image is being cloned', () => {
231 addTask('rbd/clone', 'd');
232 expect(component.images.length).toBe(4);
233 expectItemTasks(component.images[0], undefined);
234 expectItemTasks(component.images[1], undefined);
235 expectItemTasks(component.images[2], undefined);
236 expectItemTasks(component.images[3], 'Cloning');
239 it('should show when a image is being copied', () => {
240 addTask('rbd/copy', 'd');
241 expect(component.images.length).toBe(4);
242 expectItemTasks(component.images[0], undefined);
243 expectItemTasks(component.images[1], undefined);
244 expectItemTasks(component.images[2], undefined);
245 expectItemTasks(component.images[3], 'Copying');
248 it('should show when an existing image is being modified', () => {
249 addTask('rbd/edit', 'a');
250 addTask('rbd/delete', 'b');
251 addTask('rbd/flatten', 'c');
252 expect(component.images.length).toBe(3);
253 expectItemTasks(component.images[0], 'Updating');
254 expectItemTasks(component.images[1], 'Deleting');
255 expectItemTasks(component.images[2], 'Flattening');
259 it('should test all TableActions combinations', () => {
260 const permissionHelper: PermissionHelper = new PermissionHelper(component.permission);
261 const tableActions: TableActionsComponent = permissionHelper.setPermissionsAndGetActions(
262 component.tableActions
265 expect(tableActions).toEqual({
266 'create,update,delete': {
267 actions: ['Create', 'Edit', 'Copy', 'Flatten', 'Delete', 'Move to Trash'],
268 primary: { multiple: 'Create', executing: 'Edit', single: 'Edit', no: 'Create' }
271 actions: ['Create', 'Edit', 'Copy', 'Flatten'],
272 primary: { multiple: 'Create', executing: 'Edit', single: 'Edit', no: 'Create' }
275 actions: ['Create', 'Copy', 'Delete', 'Move to Trash'],
276 primary: { multiple: 'Create', executing: 'Copy', single: 'Copy', no: 'Create' }
279 actions: ['Create', 'Copy'],
280 primary: { multiple: 'Create', executing: 'Copy', single: 'Copy', no: 'Create' }
283 actions: ['Edit', 'Flatten', 'Delete', 'Move to Trash'],
284 primary: { multiple: 'Edit', executing: 'Edit', single: 'Edit', no: 'Edit' }
287 actions: ['Edit', 'Flatten'],
288 primary: { multiple: 'Edit', executing: 'Edit', single: 'Edit', no: 'Edit' }
291 actions: ['Delete', 'Move to Trash'],
292 primary: { multiple: 'Delete', executing: 'Delete', single: 'Delete', no: 'Delete' }
296 primary: { multiple: '', executing: '', single: '', no: '' }
301 const getActionDisable = (name: string) =>
302 component.tableActions.find((o) => o.name === name).disable;
304 const testActions = (selection: any, expected: { [action: string]: string | boolean }) => {
305 expect(getActionDisable('Edit')(selection)).toBe(expected.edit || false);
306 expect(getActionDisable('Delete')(selection)).toBe(expected.delete || false);
307 expect(getActionDisable('Copy')(selection)).toBe(expected.copy || false);
308 expect(getActionDisable('Flatten')(selection)).toBeTruthy();
309 expect(getActionDisable('Move to Trash')(selection)).toBe(expected.moveTrash || false);
312 it('should test TableActions with valid/invalid image name', () => {
313 component.selection.selected = [
320 testActions(component.selection, {});
322 component.selection.selected = [
329 const message = `This RBD image has an invalid name and can't be managed by ceph.`;
336 testActions(component.selection, expected);
339 it('should disable edit, copy, flatten and move action if RBD is in status `Removing`', () => {
340 component.selection.selected = [
349 const message = `Action not possible for an RBD in status 'Removing'`;
355 testActions(component.selection, expected);