1 import { HttpClientTestingModule } from '@angular/common/http/testing';
2 import { ComponentFixture, TestBed } from '@angular/core/testing';
3 import { RouterTestingModule } from '@angular/router/testing';
5 import { AlertModule } from 'ngx-bootstrap/alert';
6 import { BsDropdownModule } from 'ngx-bootstrap/dropdown';
7 import { ModalModule } from 'ngx-bootstrap/modal';
8 import { TabsModule } from 'ngx-bootstrap/tabs';
9 import { TooltipModule } from 'ngx-bootstrap/tooltip';
10 import { ToastrModule } from 'ngx-toastr';
11 import { BehaviorSubject, of } from 'rxjs';
18 } from '../../../../testing/unit-test-helper';
19 import { RbdService } from '../../../shared/api/rbd.service';
20 import { TableActionsComponent } from '../../../shared/datatable/table-actions/table-actions.component';
21 import { ViewCacheStatus } from '../../../shared/enum/view-cache-status.enum';
22 import { ExecutingTask } from '../../../shared/models/executing-task';
23 import { SummaryService } from '../../../shared/services/summary.service';
24 import { TaskListService } from '../../../shared/services/task-list.service';
25 import { SharedModule } from '../../../shared/shared.module';
26 import { RbdConfigurationListComponent } from '../rbd-configuration-list/rbd-configuration-list.component';
27 import { RbdDetailsComponent } from '../rbd-details/rbd-details.component';
28 import { RbdSnapshotListComponent } from '../rbd-snapshot-list/rbd-snapshot-list.component';
29 import { RbdTabsComponent } from '../rbd-tabs/rbd-tabs.component';
30 import { RbdListComponent } from './rbd-list.component';
31 import { RbdModel } from './rbd-model';
33 describe('RbdListComponent', () => {
34 let fixture: ComponentFixture<RbdListComponent>;
35 let component: RbdListComponent;
36 let summaryService: SummaryService;
37 let rbdService: RbdService;
39 const refresh = (data: any) => {
40 summaryService['summaryDataSource'].next(data);
46 BsDropdownModule.forRoot(),
48 ModalModule.forRoot(),
49 TooltipModule.forRoot(),
50 ToastrModule.forRoot(),
51 AlertModule.forRoot(),
53 HttpClientTestingModule
58 RbdSnapshotListComponent,
59 RbdConfigurationListComponent,
62 providers: [TaskListService, i18nProviders]
66 fixture = TestBed.createComponent(RbdListComponent);
67 component = fixture.componentInstance;
68 summaryService = TestBed.get(SummaryService);
69 rbdService = TestBed.get(RbdService);
71 // this is needed because summaryService isn't being reset after each test.
72 summaryService['summaryDataSource'] = new BehaviorSubject(null);
73 summaryService['summaryData$'] = summaryService['summaryDataSource'].asObservable();
76 it('should create', () => {
77 expect(component).toBeTruthy();
80 describe('after ngOnInit', () => {
82 fixture.detectChanges();
83 spyOn(rbdService, 'list').and.callThrough();
86 it('should load images on init', () => {
88 expect(rbdService.list).toHaveBeenCalled();
91 it('should not load images on init because no data', () => {
93 expect(rbdService.list).not.toHaveBeenCalled();
96 it('should call error function on init when summary service fails', () => {
97 spyOn(component.table, 'reset');
98 summaryService['summaryDataSource'].error(undefined);
99 expect(component.table.reset).toHaveBeenCalled();
100 expect(component.viewCacheStatusList).toEqual([{ status: ViewCacheStatus.ValueException }]);
104 describe('handling of deletion', () => {
106 fixture.detectChanges();
109 it('should check if there are no snapshots', () => {
110 component.selection.add({
115 expect(component.hasSnapshots()).toBeFalsy();
118 it('should check if there are snapshots', () => {
119 component.selection.add({
125 expect(component.hasSnapshots()).toBeTruthy();
128 it('should get delete disable description', () => {
129 component.selection.add({
139 expect(component.getDeleteDisableDesc()).toBe(
140 'This RBD has cloned snapshots. Please delete related RBDs before deleting this RBD.'
144 it('should list all protected snapshots', () => {
145 component.selection.add({
161 expect(component.listProtectedSnapshots()).toEqual(['snap2']);
165 describe('handling of executing tasks', () => {
166 let images: RbdModel[];
168 const addImage = (name: string) => {
169 const model = new RbdModel();
172 model.pool_name = 'rbd';
176 const addTask = (name: string, image_name: string) => {
177 const task = new ExecutingTask();
182 dest_pool_name: 'rbd',
183 dest_namespace: null,
189 child_pool_name: 'rbd',
190 child_namespace: null,
191 child_image_name: 'd'
198 image_name: image_name
203 image_spec: `rbd/${image_name}`
207 summaryService.addRunningTask(task);
215 component.images = images;
216 refresh({ executing_tasks: [], finished_tasks: [] });
217 spyOn(rbdService, 'list').and.callFake(() =>
218 of([{ pool_name: 'rbd', status: 1, value: images }])
220 fixture.detectChanges();
223 it('should gets all images without tasks', () => {
224 expect(component.images.length).toBe(3);
225 expect(component.images.every((image: any) => !image.cdExecuting)).toBeTruthy();
228 it('should add a new image from a task', () => {
229 addTask('rbd/create', '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], 'Creating');
237 it('should show when a image is being cloned', () => {
238 addTask('rbd/clone', '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], 'Cloning');
246 it('should show when a image is being copied', () => {
247 addTask('rbd/copy', 'd');
248 expect(component.images.length).toBe(4);
249 expectItemTasks(component.images[0], undefined);
250 expectItemTasks(component.images[1], undefined);
251 expectItemTasks(component.images[2], undefined);
252 expectItemTasks(component.images[3], 'Copying');
255 it('should show when an existing image is being modified', () => {
256 addTask('rbd/edit', 'a');
257 addTask('rbd/delete', 'b');
258 addTask('rbd/flatten', 'c');
259 expect(component.images.length).toBe(3);
260 expectItemTasks(component.images[0], 'Updating');
261 expectItemTasks(component.images[1], 'Deleting');
262 expectItemTasks(component.images[2], 'Flattening');
266 it('should test all TableActions combinations', () => {
267 const permissionHelper: PermissionHelper = new PermissionHelper(component.permission);
268 const tableActions: TableActionsComponent = permissionHelper.setPermissionsAndGetActions(
269 component.tableActions
272 expect(tableActions).toEqual({
273 'create,update,delete': {
274 actions: ['Create', 'Edit', 'Copy', 'Flatten', 'Delete', 'Move to Trash'],
275 primary: { multiple: 'Create', executing: 'Edit', single: 'Edit', no: 'Create' }
278 actions: ['Create', 'Edit', 'Copy', 'Flatten'],
279 primary: { multiple: 'Create', executing: 'Edit', single: 'Edit', no: 'Create' }
282 actions: ['Create', 'Copy', 'Delete', 'Move to Trash'],
283 primary: { multiple: 'Create', executing: 'Copy', single: 'Copy', no: 'Create' }
286 actions: ['Create', 'Copy'],
287 primary: { multiple: 'Create', executing: 'Copy', single: 'Copy', no: 'Create' }
290 actions: ['Edit', 'Flatten', 'Delete', 'Move to Trash'],
291 primary: { multiple: 'Edit', executing: 'Edit', single: 'Edit', no: 'Edit' }
294 actions: ['Edit', 'Flatten'],
295 primary: { multiple: 'Edit', executing: 'Edit', single: 'Edit', no: 'Edit' }
298 actions: ['Delete', 'Move to Trash'],
299 primary: { multiple: 'Delete', executing: 'Delete', single: 'Delete', no: 'Delete' }
303 primary: { multiple: '', executing: '', single: '', no: '' }