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 { AlertModule } from 'ngx-bootstrap/alert';
7 import { BsDropdownModule } from 'ngx-bootstrap/dropdown';
8 import { ModalModule } from 'ngx-bootstrap/modal';
9 import { TabsModule } from 'ngx-bootstrap/tabs';
10 import { TooltipModule } from 'ngx-bootstrap/tooltip';
11 import { ToastrModule } from 'ngx-toastr';
12 import { BehaviorSubject, of } from 'rxjs';
19 } from '../../../../testing/unit-test-helper';
20 import { RbdService } from '../../../shared/api/rbd.service';
21 import { TableActionsComponent } from '../../../shared/datatable/table-actions/table-actions.component';
22 import { ViewCacheStatus } from '../../../shared/enum/view-cache-status.enum';
23 import { ExecutingTask } from '../../../shared/models/executing-task';
24 import { SummaryService } from '../../../shared/services/summary.service';
25 import { TaskListService } from '../../../shared/services/task-list.service';
26 import { SharedModule } from '../../../shared/shared.module';
27 import { RbdConfigurationListComponent } from '../rbd-configuration-list/rbd-configuration-list.component';
28 import { RbdDetailsComponent } from '../rbd-details/rbd-details.component';
29 import { RbdSnapshotListComponent } from '../rbd-snapshot-list/rbd-snapshot-list.component';
30 import { RbdTabsComponent } from '../rbd-tabs/rbd-tabs.component';
31 import { RbdListComponent } from './rbd-list.component';
32 import { RbdModel } from './rbd-model';
34 describe('RbdListComponent', () => {
35 let fixture: ComponentFixture<RbdListComponent>;
36 let component: RbdListComponent;
37 let summaryService: SummaryService;
38 let rbdService: RbdService;
40 const refresh = (data: any) => {
41 summaryService['summaryDataSource'].next(data);
46 BrowserAnimationsModule,
48 BsDropdownModule.forRoot(),
50 ModalModule.forRoot(),
51 TooltipModule.forRoot(),
52 ToastrModule.forRoot(),
53 AlertModule.forRoot(),
55 HttpClientTestingModule
60 RbdSnapshotListComponent,
61 RbdConfigurationListComponent,
64 providers: [TaskListService, i18nProviders]
68 fixture = TestBed.createComponent(RbdListComponent);
69 component = fixture.componentInstance;
70 summaryService = TestBed.get(SummaryService);
71 rbdService = TestBed.get(RbdService);
73 // this is needed because summaryService isn't being reset after each test.
74 summaryService['summaryDataSource'] = new BehaviorSubject(null);
75 summaryService['summaryData$'] = summaryService['summaryDataSource'].asObservable();
78 it('should create', () => {
79 expect(component).toBeTruthy();
82 describe('after ngOnInit', () => {
84 fixture.detectChanges();
85 spyOn(rbdService, 'list').and.callThrough();
88 it('should load images on init', () => {
90 expect(rbdService.list).toHaveBeenCalled();
93 it('should not load images on init because no data', () => {
95 expect(rbdService.list).not.toHaveBeenCalled();
98 it('should call error function on init when summary service fails', () => {
99 spyOn(component.table, 'reset');
100 summaryService['summaryDataSource'].error(undefined);
101 expect(component.table.reset).toHaveBeenCalled();
102 expect(component.viewCacheStatusList).toEqual([{ status: ViewCacheStatus.ValueException }]);
106 describe('handling of deletion', () => {
108 fixture.detectChanges();
111 it('should check if there are no snapshots', () => {
112 component.selection.add({
117 expect(component.hasSnapshots()).toBeFalsy();
120 it('should check if there are snapshots', () => {
121 component.selection.add({
127 expect(component.hasSnapshots()).toBeTruthy();
130 it('should get delete disable description', () => {
131 component.selection.add({
141 expect(component.getDeleteDisableDesc(component.selection)).toBe(
142 'This RBD has cloned snapshots. Please delete related RBDs before deleting this RBD.'
146 it('should list all protected snapshots', () => {
147 component.selection.add({
163 expect(component.listProtectedSnapshots()).toEqual(['snap2']);
167 describe('handling of executing tasks', () => {
168 let images: RbdModel[];
170 const addImage = (name: string) => {
171 const model = new RbdModel();
174 model.pool_name = 'rbd';
178 const addTask = (name: string, image_name: string) => {
179 const task = new ExecutingTask();
184 dest_pool_name: 'rbd',
185 dest_namespace: null,
191 child_pool_name: 'rbd',
192 child_namespace: null,
193 child_image_name: 'd'
200 image_name: image_name
205 image_spec: `rbd/${image_name}`
209 summaryService.addRunningTask(task);
217 component.images = images;
218 refresh({ executing_tasks: [], finished_tasks: [] });
219 spyOn(rbdService, 'list').and.callFake(() =>
220 of([{ pool_name: 'rbd', status: 1, value: images }])
222 fixture.detectChanges();
225 it('should gets all images without tasks', () => {
226 expect(component.images.length).toBe(3);
227 expect(component.images.every((image: any) => !image.cdExecuting)).toBeTruthy();
230 it('should add a new image from a task', () => {
231 addTask('rbd/create', '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], 'Creating');
239 it('should show when a image is being cloned', () => {
240 addTask('rbd/clone', '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], 'Cloning');
248 it('should show when a image is being copied', () => {
249 addTask('rbd/copy', 'd');
250 expect(component.images.length).toBe(4);
251 expectItemTasks(component.images[0], undefined);
252 expectItemTasks(component.images[1], undefined);
253 expectItemTasks(component.images[2], undefined);
254 expectItemTasks(component.images[3], 'Copying');
257 it('should show when an existing image is being modified', () => {
258 addTask('rbd/edit', 'a');
259 addTask('rbd/delete', 'b');
260 addTask('rbd/flatten', 'c');
261 expect(component.images.length).toBe(3);
262 expectItemTasks(component.images[0], 'Updating');
263 expectItemTasks(component.images[1], 'Deleting');
264 expectItemTasks(component.images[2], 'Flattening');
268 it('should test all TableActions combinations', () => {
269 const permissionHelper: PermissionHelper = new PermissionHelper(component.permission);
270 const tableActions: TableActionsComponent = permissionHelper.setPermissionsAndGetActions(
271 component.tableActions
274 expect(tableActions).toEqual({
275 'create,update,delete': {
276 actions: ['Create', 'Edit', 'Copy', 'Flatten', 'Delete', 'Move to Trash'],
277 primary: { multiple: 'Create', executing: 'Edit', single: 'Edit', no: 'Create' }
280 actions: ['Create', 'Edit', 'Copy', 'Flatten'],
281 primary: { multiple: 'Create', executing: 'Edit', single: 'Edit', no: 'Create' }
284 actions: ['Create', 'Copy', 'Delete', 'Move to Trash'],
285 primary: { multiple: 'Create', executing: 'Copy', single: 'Copy', no: 'Create' }
288 actions: ['Create', 'Copy'],
289 primary: { multiple: 'Create', executing: 'Copy', single: 'Copy', no: 'Create' }
292 actions: ['Edit', 'Flatten', 'Delete', 'Move to Trash'],
293 primary: { multiple: 'Edit', executing: 'Edit', single: 'Edit', no: 'Edit' }
296 actions: ['Edit', 'Flatten'],
297 primary: { multiple: 'Edit', executing: 'Edit', single: 'Edit', no: 'Edit' }
300 actions: ['Delete', 'Move to Trash'],
301 primary: { multiple: 'Delete', executing: 'Delete', single: 'Delete', no: 'Delete' }
305 primary: { multiple: '', executing: '', single: '', no: '' }
310 const getActionDisable = (name: string) =>
311 component.tableActions.find((o) => o.name === name).disable;
313 const testActions = (selection: any, expected: { [action: string]: string | boolean }) => {
314 expect(getActionDisable('Edit')(selection)).toBe(expected.edit || false);
315 expect(getActionDisable('Delete')(selection)).toBe(expected.delete || false);
316 expect(getActionDisable('Copy')(selection)).toBe(expected.copy || false);
317 expect(getActionDisable('Flatten')(selection)).toBeTruthy();
318 expect(getActionDisable('Move to Trash')(selection)).toBe(expected.moveTrash || false);
321 it('should disable edit, copy, flatten and move action if RBD is in status `Removing`', () => {
322 component.selection.selected = [
331 const message = `Action not possible for an RBD in status 'Removing'`;
337 testActions(component.selection, expected);