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 * as _ from 'lodash';
6 import { BsModalService } from 'ngx-bootstrap/modal';
7 import { TabsModule } from 'ngx-bootstrap/tabs';
8 import { ToastrModule } from 'ngx-toastr';
9 import { of } from 'rxjs';
15 } from '../../../../testing/unit-test-helper';
16 import { ConfigurationService } from '../../../shared/api/configuration.service';
17 import { PoolService } from '../../../shared/api/pool.service';
18 import { CriticalConfirmationModalComponent } from '../../../shared/components/critical-confirmation-modal/critical-confirmation-modal.component';
19 import { ExecutingTask } from '../../../shared/models/executing-task';
20 import { AuthStorageService } from '../../../shared/services/auth-storage.service';
21 import { SummaryService } from '../../../shared/services/summary.service';
22 import { TaskWrapperService } from '../../../shared/services/task-wrapper.service';
23 import { SharedModule } from '../../../shared/shared.module';
24 import { RbdConfigurationListComponent } from '../../block/rbd-configuration-list/rbd-configuration-list.component';
25 import { PgCategoryService } from '../../shared/pg-category.service';
26 import { Pool } from '../pool';
27 import { PoolDetailsComponent } from '../pool-details/pool-details.component';
28 import { PoolListComponent } from './pool-list.component';
30 describe('PoolListComponent', () => {
31 let component: PoolListComponent;
32 let fixture: ComponentFixture<PoolListComponent>;
33 let poolService: PoolService;
35 const createPool = (name: string, id: number): Pool => {
36 return _.merge(new Pool(name), {
39 pg_placement_num: 256,
41 pg_placement_num_target: 256
45 const getPoolList = (): Pool[] => {
46 return [createPool('a', 0), createPool('b', 1), createPool('c', 2)];
50 declarations: [PoolListComponent, PoolDetailsComponent, RbdConfigurationListComponent],
53 ToastrModule.forRoot(),
56 HttpClientTestingModule
58 providers: [i18nProviders, PgCategoryService]
62 fixture = TestBed.createComponent(PoolListComponent);
63 component = fixture.componentInstance;
64 component.permissions.pool.read = true;
65 poolService = TestBed.get(PoolService);
66 spyOn(poolService, 'getList').and.callFake(() => of(getPoolList()));
67 fixture.detectChanges();
70 it('should create', () => {
71 expect(component).toBeTruthy();
74 it('should have columns that are sortable', () => {
75 expect(component.columns.every((column) => Boolean(column.prop))).toBeTruthy();
78 describe('monAllowPoolDelete', () => {
79 let configOptRead: boolean;
80 let configurationService: ConfigurationService;
84 spyOn(TestBed.get(AuthStorageService), 'getPermissions').and.callFake(() => ({
85 configOpt: { read: configOptRead }
87 configurationService = TestBed.get(ConfigurationService);
90 it('should set value correctly if mon_allow_pool_delete flag is set to true', () => {
91 const configOption = {
92 name: 'mon_allow_pool_delete',
100 spyOn(configurationService, 'get').and.returnValue(of(configOption));
101 fixture = TestBed.createComponent(PoolListComponent);
102 component = fixture.componentInstance;
103 expect(component.monAllowPoolDelete).toBe(true);
106 it('should set value correctly if mon_allow_pool_delete flag is set to false', () => {
107 const configOption = {
108 name: 'mon_allow_pool_delete',
116 spyOn(configurationService, 'get').and.returnValue(of(configOption));
117 fixture = TestBed.createComponent(PoolListComponent);
118 component = fixture.componentInstance;
119 expect(component.monAllowPoolDelete).toBe(false);
122 it('should set value correctly if mon_allow_pool_delete flag is not set', () => {
123 const configOption = {
124 name: 'mon_allow_pool_delete'
126 spyOn(configurationService, 'get').and.returnValue(of(configOption));
127 fixture = TestBed.createComponent(PoolListComponent);
128 component = fixture.componentInstance;
129 expect(component.monAllowPoolDelete).toBe(false);
132 it('should set value correctly w/o config-opt read privileges', () => {
133 configOptRead = false;
134 fixture = TestBed.createComponent(PoolListComponent);
135 component = fixture.componentInstance;
136 expect(component.monAllowPoolDelete).toBe(false);
140 describe('pool deletion', () => {
141 let taskWrapper: TaskWrapperService;
143 const setSelectedPool = (poolName: string) =>
144 (component.selection.selected = [{ pool_name: poolName }]);
146 const callDeletion = () => {
147 component.deletePoolModal();
148 const deletion: CriticalConfirmationModalComponent = component.modalRef.content;
149 deletion.submitActionObservable();
152 const testPoolDeletion = (poolName: string) => {
153 setSelectedPool(poolName);
155 expect(poolService.delete).toHaveBeenCalledWith(poolName);
156 expect(taskWrapper.wrapTaskAroundCall).toHaveBeenCalledWith({
163 call: undefined // because of stub
168 spyOn(TestBed.get(BsModalService), 'show').and.callFake((deletionClass, config) => {
170 content: Object.assign(new deletionClass(), config.initialState)
173 spyOn(poolService, 'delete').and.stub();
174 taskWrapper = TestBed.get(TaskWrapperService);
175 spyOn(taskWrapper, 'wrapTaskAroundCall').and.callThrough();
178 it('should pool deletion with two different pools', () => {
179 testPoolDeletion('somePoolName');
180 testPoolDeletion('aDifferentPoolName');
184 describe('handling of executing tasks', () => {
185 let summaryService: SummaryService;
187 const addTask = (name: string, pool: string) => {
188 const task = new ExecutingTask();
190 task.metadata = { pool_name: pool };
191 summaryService.addRunningTask(task);
195 summaryService = TestBed.get(SummaryService);
196 summaryService['summaryDataSource'].next({ executing_tasks: [], finished_tasks: [] });
199 it('gets all pools without executing pools', () => {
200 expect(component.pools.length).toBe(3);
201 expect(component.pools.every((pool) => !pool.executingTasks)).toBeTruthy();
204 it('gets a pool from a task during creation', () => {
205 addTask('pool/create', 'd');
206 expect(component.pools.length).toBe(4);
207 expectItemTasks(component.pools[3], 'Creating');
210 it('gets all pools with one executing pools', () => {
211 addTask('pool/create', 'a');
212 expect(component.pools.length).toBe(3);
213 expectItemTasks(component.pools[0], 'Creating');
214 expect(component.pools[1].cdExecuting).toBeFalsy();
215 expect(component.pools[2].cdExecuting).toBeFalsy();
218 it('gets all pools with multiple executing pools', () => {
219 addTask('pool/create', 'a');
220 addTask('pool/edit', 'a');
221 addTask('pool/delete', 'a');
222 addTask('pool/edit', 'b');
223 addTask('pool/delete', 'b');
224 addTask('pool/delete', 'c');
225 expect(component.pools.length).toBe(3);
226 expectItemTasks(component.pools[0], 'Creating..., Updating..., Deleting');
227 expectItemTasks(component.pools[1], 'Updating..., Deleting');
228 expectItemTasks(component.pools[2], 'Deleting');
231 it('gets all pools with multiple executing tasks (not only pool tasks)', () => {
232 addTask('rbd/create', 'a');
233 addTask('rbd/edit', 'a');
234 addTask('pool/delete', 'a');
235 addTask('pool/edit', 'b');
236 addTask('rbd/delete', 'b');
237 addTask('rbd/delete', 'c');
238 expect(component.pools.length).toBe(3);
239 expectItemTasks(component.pools[0], 'Deleting');
240 expectItemTasks(component.pools[1], 'Updating');
241 expect(component.pools[2].cdExecuting).toBeFalsy();
245 describe('getPgStatusCellClass', () => {
246 const testMethod = (value: string, expected: string) =>
247 expect(component.getPgStatusCellClass('', '', value)).toEqual({
252 it('pg-clean', () => {
253 testMethod('8 active+clean', 'pg-clean');
256 it('pg-working', () => {
257 testMethod(' 8 active+clean+scrubbing+deep, 255 active+clean ', 'pg-working');
260 it('pg-warning', () => {
261 testMethod('8 active+clean+scrubbing+down', 'pg-warning');
262 testMethod('8 active+clean+scrubbing+down+nonMappedState', 'pg-warning');
265 it('pg-unknown', () => {
266 testMethod('8 active+clean+scrubbing+nonMappedState', 'pg-unknown');
267 testMethod('8 ', 'pg-unknown');
268 testMethod('', 'pg-unknown');
272 describe('custom row comparators', () => {
273 const expectCorrectComparator = (statsAttribute: string) => {
274 const mockPool = (v: number) => ({ stats: { [statsAttribute]: { latest: v } } });
275 const columnDefinition = _.find(
277 (column) => column.prop === `stats.${statsAttribute}.rates`
279 expect(columnDefinition.comparator(undefined, undefined, mockPool(2), mockPool(1))).toBe(1);
280 expect(columnDefinition.comparator(undefined, undefined, mockPool(1), mockPool(2))).toBe(-1);
283 it('compares read bytes correctly', () => {
284 expectCorrectComparator('rd_bytes');
287 it('compares write bytes correctly', () => {
288 expectCorrectComparator('wr_bytes');
292 describe('transformPoolsData', () => {
295 const getPoolData = (o: object) => [
297 _.merge(createPool('a', 0), {
301 bytes_used: { latest: 0, rate: 0, rates: [] },
302 max_avail: { latest: 0, rate: 0, rates: [] },
303 rd: { latest: 0, rate: 0, rates: [] },
304 rd_bytes: { latest: 0, rate: 0, rates: [] },
305 wr: { latest: 0, rate: 0, rates: [] },
306 wr_bytes: { latest: 0, rate: 0, rates: [] }
315 pool = createPool('a', 0);
318 it('transforms pools data correctly', () => {
319 pool = _.merge(pool, {
321 bytes_used: { latest: 5, rate: 0, rates: [] },
322 max_avail: { latest: 15, rate: 0, rates: [] },
332 pg_status: { 'active+clean': 8, down: 2 }
334 expect(component.transformPoolsData([pool])).toEqual(
336 pg_status: '8 active+clean, 2 down',
338 bytes_used: { latest: 5, rate: 0, rates: [] },
339 max_avail: { latest: 15, rate: 0, rates: [] },
340 rd_bytes: { latest: 6, rate: 4, rates: [2, 6] }
347 it('transforms pools data correctly if stats are missing', () => {
348 expect(component.transformPoolsData([pool])).toEqual(getPoolData({}));
351 it('transforms empty pools data correctly', () => {
352 expect(component.transformPoolsData(undefined)).toEqual(undefined);
353 expect(component.transformPoolsData([])).toEqual([]);
356 it('shows not marked pools in progress if pg_num does not match pg_num_target', () => {
361 pg_placement_num: 32,
362 pg_placement_num_target: 16
365 expect(component.transformPoolsData(pools)).toEqual(
367 cdExecuting: 'Updating',
370 pg_placement_num: 32,
371 pg_placement_num_target: 16
376 it('shows marked pools in progress as defined by task', () => {
381 pg_placement_num: 32,
382 pg_placement_num_target: 16,
383 cdExecuting: 'Updating... 50%'
386 expect(component.transformPoolsData(pools)).toEqual(
388 cdExecuting: 'Updating... 50%',
391 pg_placement_num: 32,
392 pg_placement_num_target: 16
398 describe('transformPgStatus', () => {
399 it('returns status groups correctly', () => {
400 const pgStatus = { 'active+clean': 8 };
401 const expected = '8 active+clean';
403 expect(component.transformPgStatus(pgStatus)).toEqual(expected);
406 it('returns separated status groups', () => {
407 const pgStatus = { 'active+clean': 8, down: 2 };
408 const expected = '8 active+clean, 2 down';
410 expect(component.transformPgStatus(pgStatus)).toEqual(expected);
413 it('returns separated statuses correctly', () => {
414 const pgStatus = { active: 8, down: 2 };
415 const expected = '8 active, 2 down';
417 expect(component.transformPgStatus(pgStatus)).toEqual(expected);
420 it('returns empty string', () => {
421 const pgStatus: any = undefined;
424 expect(component.transformPgStatus(pgStatus)).toEqual(expected);
428 describe('getSelectionTiers', () => {
429 const setSelectionTiers = (tiers: number[]) => {
430 component.selection.selected = [{ tiers }];
431 component.getSelectionTiers();
435 component.pools = getPoolList();
438 it('should select multiple existing cache tiers', () => {
439 setSelectionTiers([0, 1, 2]);
440 expect(component.selectionCacheTiers).toEqual(getPoolList());
443 it('should select correct existing cache tier', () => {
444 setSelectionTiers([0]);
445 expect(component.selectionCacheTiers).toEqual([createPool('a', 0)]);
448 it('should not select cache tier if id is invalid', () => {
449 setSelectionTiers([-1]);
450 expect(component.selectionCacheTiers).toEqual([]);
453 it('should not select cache tier if empty', () => {
454 setSelectionTiers([]);
455 expect(component.selectionCacheTiers).toEqual([]);
458 it('should be able to selected one pool with multiple tiers, than with a single tier, than with no tiers', () => {
459 setSelectionTiers([0, 1, 2]);
460 expect(component.selectionCacheTiers).toEqual(getPoolList());
461 setSelectionTiers([0]);
462 expect(component.selectionCacheTiers).toEqual([createPool('a', 0)]);
463 setSelectionTiers([]);
464 expect(component.selectionCacheTiers).toEqual([]);
468 describe('getDisableDesc', () => {
469 it('should return message if mon_allow_pool_delete flag is set to false', () => {
470 component.monAllowPoolDelete = false;
471 expect(component.getDisableDesc()).toBe(
472 'Pool deletion is disabled by the mon_allow_pool_delete configuration setting.'
476 it('should return undefined if mon_allow_pool_delete flag is set to true', () => {
477 component.monAllowPoolDelete = true;
478 expect(component.getDisableDesc()).toBeUndefined();