1 import { HttpClientTestingModule } from '@angular/common/http/testing';
2 import { ComponentFixture, fakeAsync, TestBed, tick } from '@angular/core/testing';
3 import { ReactiveFormsModule } from '@angular/forms';
4 import { By } from '@angular/platform-browser';
5 import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
6 import { RouterTestingModule } from '@angular/router/testing';
8 import * as _ from 'lodash';
9 import { BsModalService } from 'ngx-bootstrap/modal';
10 import { TabsModule } from 'ngx-bootstrap/tabs';
11 import { ToastrModule } from 'ngx-toastr';
12 import { EMPTY, of } from 'rxjs';
18 } from '../../../../../testing/unit-test-helper';
19 import { CoreModule } from '../../../../core/core.module';
20 import { OrchestratorService } from '../../../../shared/api/orchestrator.service';
21 import { OsdService } from '../../../../shared/api/osd.service';
22 import { ConfirmationModalComponent } from '../../../../shared/components/confirmation-modal/confirmation-modal.component';
23 import { CriticalConfirmationModalComponent } from '../../../../shared/components/critical-confirmation-modal/critical-confirmation-modal.component';
24 import { FormModalComponent } from '../../../../shared/components/form-modal/form-modal.component';
25 import { TableActionsComponent } from '../../../../shared/datatable/table-actions/table-actions.component';
26 import { CdTableAction } from '../../../../shared/models/cd-table-action';
27 import { CdTableSelection } from '../../../../shared/models/cd-table-selection';
28 import { Permissions } from '../../../../shared/models/permissions';
29 import { AuthStorageService } from '../../../../shared/services/auth-storage.service';
30 import { CephModule } from '../../../ceph.module';
31 import { PerformanceCounterModule } from '../../../performance-counter/performance-counter.module';
32 import { OsdReweightModalComponent } from '../osd-reweight-modal/osd-reweight-modal.component';
33 import { OsdListComponent } from './osd-list.component';
35 describe('OsdListComponent', () => {
36 let component: OsdListComponent;
37 let fixture: ComponentFixture<OsdListComponent>;
38 let modalServiceShowSpy: jasmine.Spy;
39 let osdService: OsdService;
41 const fakeAuthStorageService = {
42 getPermissions: () => {
43 return new Permissions({
44 'config-opt': ['read', 'update', 'create', 'delete'],
45 osd: ['read', 'update', 'create', 'delete']
50 const getTableAction = (name: string) =>
51 component.tableActions.find((action) => action.name === name);
53 const setFakeSelection = () => {
54 // Default data and selection
55 const selection = [{ id: 1, tree: { device_class: 'ssd' } }];
56 const data = [{ id: 1, tree: { device_class: 'ssd' } }];
58 // Table data and selection
59 component.selection = new CdTableSelection();
60 component.selection.selected = selection;
61 component.osds = data;
62 component.permissions = fakeAuthStorageService.getPermissions();
65 const openActionModal = (actionName: string) => {
67 getTableAction(actionName).click();
71 * The following modals are called after the information about their
72 * safety to destroy/remove/mark them lost has been retrieved, hence
73 * we will have to fake its request to be able to open those modals.
75 const mockSafeToDestroy = () => {
76 spyOn(TestBed.get(OsdService), 'safeToDestroy').and.callFake(() =>
77 of({ is_safe_to_destroy: true })
81 const mockSafeToDelete = () => {
82 spyOn(TestBed.get(OsdService), 'safeToDelete').and.callFake(() =>
83 of({ is_safe_to_delete: true })
87 const mockOrchestratorStatus = () => {
88 spyOn(TestBed.get(OrchestratorService), 'status').and.callFake(() => of({ available: true }));
93 BrowserAnimationsModule,
94 HttpClientTestingModule,
95 PerformanceCounterModule,
97 ToastrModule.forRoot(),
106 { provide: AuthStorageService, useValue: fakeAuthStorageService },
107 TableActionsComponent,
114 fixture = TestBed.createComponent(OsdListComponent);
115 component = fixture.componentInstance;
116 osdService = TestBed.get(OsdService);
117 modalServiceShowSpy = spyOn(TestBed.get(BsModalService), 'show').and.stub();
120 it('should create', () => {
121 fixture.detectChanges();
122 expect(component).toBeTruthy();
125 it('should have columns that are sortable', () => {
126 fixture.detectChanges();
129 .filter((column) => !(column.prop === undefined))
130 .every((column) => Boolean(column.prop))
134 describe('getOsdList', () => {
137 const createOsd = (n: number) =>
138 <Record<string, any>>{
155 stat_bytes_used: n * n,
156 stat_bytes: n * n * n
161 const expectAttributeOnEveryOsd = (attr: string) =>
162 expect(component.osds.every((osd) => Boolean(_.get(osd, attr)))).toBeTruthy();
165 spyOn(osdService, 'getList').and.callFake(() => of(osds));
166 osds = [createOsd(1), createOsd(2), createOsd(3)];
167 component.getOsdList();
170 it('should replace "this.osds" with new data', () => {
171 expect(component.osds.length).toBe(3);
172 expect(osdService.getList).toHaveBeenCalledTimes(1);
174 osds = [createOsd(4)];
175 component.getOsdList();
176 expect(component.osds.length).toBe(1);
177 expect(osdService.getList).toHaveBeenCalledTimes(2);
180 it('should have custom attribute "collectedStates"', () => {
181 expectAttributeOnEveryOsd('collectedStates');
182 expect(component.osds[0].collectedStates).toEqual(['in', 'up']);
185 it('should have "destroyed" state in "collectedStates"', () => {
186 osds[0].state.push('destroyed');
188 component.getOsdList();
190 expectAttributeOnEveryOsd('collectedStates');
191 expect(component.osds[0].collectedStates).toEqual(['in', 'destroyed']);
194 it('should have custom attribute "stats_history.out_bytes"', () => {
195 expectAttributeOnEveryOsd('stats_history.out_bytes');
196 expect(component.osds[0].stats_history.out_bytes).toEqual([1, 2]);
199 it('should have custom attribute "stats_history.in_bytes"', () => {
200 expectAttributeOnEveryOsd('stats_history.in_bytes');
201 expect(component.osds[0].stats_history.in_bytes).toEqual([3, 4]);
204 it('should have custom attribute "stats.usage"', () => {
205 expectAttributeOnEveryOsd('stats.usage');
206 expect(component.osds[0].stats.usage).toBe(1);
207 expect(component.osds[1].stats.usage).toBe(0.5);
208 expect(component.osds[2].stats.usage).toBe(3 / 9);
211 it('should have custom attribute "cdIsBinary" to be true', () => {
212 expectAttributeOnEveryOsd('cdIsBinary');
213 expect(component.osds[0].cdIsBinary).toBe(true);
217 describe('show osd actions as defined', () => {
218 const getOsdActions = () => {
219 fixture.detectChanges();
220 return fixture.debugElement.query(By.css('#cluster-wide-actions')).componentInstance
224 it('shows osd actions after osd-actions', () => {
225 fixture.detectChanges();
226 expect(fixture.debugElement.query(By.css('#cluster-wide-actions'))).toBe(
227 fixture.debugElement.queryAll(By.directive(TableActionsComponent))[1]
231 it('shows both osd actions', () => {
232 const osdActions = getOsdActions();
233 expect(osdActions).toEqual(component.clusterWideActions);
234 expect(osdActions.length).toBe(3);
237 it('shows only "Flags" action', () => {
238 component.permissions.configOpt.read = false;
239 const osdActions = getOsdActions();
240 expect(osdActions[0].name).toBe('Flags');
241 expect(osdActions.length).toBe(1);
244 it('shows only "Recovery Priority" action', () => {
245 component.permissions.osd.read = false;
246 const osdActions = getOsdActions();
247 expect(osdActions[0].name).toBe('Recovery Priority');
248 expect(osdActions[1].name).toBe('PG scrub');
249 expect(osdActions.length).toBe(2);
252 it('shows no osd actions', () => {
253 component.permissions.configOpt.read = false;
254 component.permissions.osd.read = false;
255 const osdActions = getOsdActions();
256 expect(osdActions).toEqual([]);
260 it('should test all TableActions combinations', () => {
261 const permissionHelper: PermissionHelper = new PermissionHelper(component.permissions.osd);
262 const tableActions: TableActionsComponent = permissionHelper.setPermissionsAndGetActions(
263 component.tableActions
266 expect(tableActions).toEqual({
267 'create,update,delete': {
282 primary: { multiple: 'Scrub', executing: 'Edit', single: 'Edit', no: 'Create' }
295 primary: { multiple: 'Scrub', executing: 'Edit', single: 'Edit', no: 'Create' }
298 actions: ['Create', 'Mark Lost', 'Purge', 'Destroy', 'Delete'],
301 executing: 'Mark Lost',
308 primary: { multiple: 'Create', executing: 'Create', single: 'Create', no: 'Create' }
324 primary: { multiple: 'Scrub', executing: 'Edit', single: 'Edit', no: 'Edit' }
327 actions: ['Edit', 'Scrub', 'Deep Scrub', 'Reweight', 'Mark Out', 'Mark In', 'Mark Down'],
328 primary: { multiple: 'Scrub', executing: 'Edit', single: 'Edit', no: 'Edit' }
331 actions: ['Mark Lost', 'Purge', 'Destroy', 'Delete'],
333 multiple: 'Mark Lost',
334 executing: 'Mark Lost',
341 primary: { multiple: '', executing: '', single: '', no: '' }
346 describe('test table actions in submenu', () => {
348 fixture.detectChanges();
351 beforeEach(fakeAsync(() => {
352 // The menu needs a click to render the dropdown!
353 const dropDownToggle = fixture.debugElement.query(By.css('.dropdown-toggle'));
354 dropDownToggle.triggerEventHandler('click', null);
356 fixture.detectChanges();
359 it('has all menu entries disabled except create', () => {
360 const tableActionElement = fixture.debugElement.query(By.directive(TableActionsComponent));
361 const toClassName = TestBed.get(TableActionsComponent).toClassName;
362 const getActionClasses = (action: CdTableAction) =>
363 tableActionElement.query(By.css(`.${toClassName(action.name)} .dropdown-item`)).classes;
365 component.tableActions.forEach((action) => {
366 if (action.name === 'Create') {
369 expect(getActionClasses(action).disabled).toBe(true);
374 describe('tests if all modals are opened correctly', () => {
376 * Helper function to check if a function opens a modal
378 * @param modalClass - The expected class of the modal
380 const expectOpensModal = (actionName: string, modalClass: any): void => {
381 openActionModal(actionName);
383 // @TODO: check why tsc is complaining when passing 'expectationFailOutput' param.
384 expect(modalServiceShowSpy.calls.any()).toBeTruthy();
385 expect(modalServiceShowSpy.calls.first().args[0]).toBe(modalClass);
387 modalServiceShowSpy.calls.reset();
390 it('opens the reweight modal', () => {
391 expectOpensModal('Reweight', OsdReweightModalComponent);
394 it('opens the form modal', () => {
395 expectOpensModal('Edit', FormModalComponent);
398 it('opens all confirmation modals', () => {
399 const modalClass = ConfirmationModalComponent;
400 expectOpensModal('Mark Out', modalClass);
401 expectOpensModal('Mark In', modalClass);
402 expectOpensModal('Mark Down', modalClass);
405 it('opens all critical confirmation modals', () => {
406 const modalClass = CriticalConfirmationModalComponent;
408 expectOpensModal('Mark Lost', modalClass);
409 expectOpensModal('Purge', modalClass);
410 expectOpensModal('Destroy', modalClass);
411 mockOrchestratorStatus();
413 expectOpensModal('Delete', modalClass);
417 describe('tests if the correct methods are called on confirmation', () => {
418 const expectOsdServiceMethodCalled = (
420 osdServiceMethodName:
429 const osdServiceSpy = spyOn(osdService, osdServiceMethodName).and.callFake(() => EMPTY);
430 openActionModal(actionName);
431 const initialState = modalServiceShowSpy.calls.first().args[1].initialState;
432 const submit = initialState.onSubmit || initialState.submitAction;
433 submit.call(component);
435 expect(osdServiceSpy.calls.count()).toBe(1);
436 expect(osdServiceSpy.calls.first().args[0]).toBe(1);
438 // Reset spies to be able to recreate them
439 osdServiceSpy.calls.reset();
440 modalServiceShowSpy.calls.reset();
443 it('calls the corresponding service methods in confirmation modals', () => {
444 expectOsdServiceMethodCalled('Mark Out', 'markOut');
445 expectOsdServiceMethodCalled('Mark In', 'markIn');
446 expectOsdServiceMethodCalled('Mark Down', 'markDown');
449 it('calls the corresponding service methods in critical confirmation modals', () => {
451 expectOsdServiceMethodCalled('Mark Lost', 'markLost');
452 expectOsdServiceMethodCalled('Purge', 'purge');
453 expectOsdServiceMethodCalled('Destroy', 'destroy');
454 mockOrchestratorStatus();
456 expectOsdServiceMethodCalled('Delete', 'delete');