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 { RouterTestingModule } from '@angular/router/testing';
7 import * as _ from 'lodash';
8 import { BsModalService } from 'ngx-bootstrap/modal';
9 import { TabsModule } from 'ngx-bootstrap/tabs';
10 import { ToastrModule } from 'ngx-toastr';
11 import { EMPTY, of } from 'rxjs';
17 } from '../../../../../testing/unit-test-helper';
18 import { CoreModule } from '../../../../core/core.module';
19 import { OrchestratorService } from '../../../../shared/api/orchestrator.service';
20 import { OsdService } from '../../../../shared/api/osd.service';
21 import { ConfirmationModalComponent } from '../../../../shared/components/confirmation-modal/confirmation-modal.component';
22 import { CriticalConfirmationModalComponent } from '../../../../shared/components/critical-confirmation-modal/critical-confirmation-modal.component';
23 import { FormModalComponent } from '../../../../shared/components/form-modal/form-modal.component';
24 import { TableActionsComponent } from '../../../../shared/datatable/table-actions/table-actions.component';
25 import { CdTableAction } from '../../../../shared/models/cd-table-action';
26 import { CdTableSelection } from '../../../../shared/models/cd-table-selection';
27 import { Permissions } from '../../../../shared/models/permissions';
28 import { AuthStorageService } from '../../../../shared/services/auth-storage.service';
29 import { CephModule } from '../../../ceph.module';
30 import { PerformanceCounterModule } from '../../../performance-counter/performance-counter.module';
31 import { OsdReweightModalComponent } from '../osd-reweight-modal/osd-reweight-modal.component';
32 import { OsdListComponent } from './osd-list.component';
34 describe('OsdListComponent', () => {
35 let component: OsdListComponent;
36 let fixture: ComponentFixture<OsdListComponent>;
37 let modalServiceShowSpy: jasmine.Spy;
38 let osdService: OsdService;
40 const fakeAuthStorageService = {
41 getPermissions: () => {
42 return new Permissions({
43 'config-opt': ['read', 'update', 'create', 'delete'],
44 osd: ['read', 'update', 'create', 'delete']
49 const getTableAction = (name: string) =>
50 component.tableActions.find((action) => action.name === name);
52 const setFakeSelection = () => {
53 // Default data and selection
54 const selection = [{ id: 1, tree: { device_class: 'ssd' } }];
55 const data = [{ id: 1, tree: { device_class: 'ssd' } }];
57 // Table data and selection
58 component.selection = new CdTableSelection();
59 component.selection.selected = selection;
60 component.osds = data;
61 component.permissions = fakeAuthStorageService.getPermissions();
64 const openActionModal = (actionName: string) => {
66 getTableAction(actionName).click();
70 * The following modals are called after the information about their
71 * safety to destroy/remove/mark them lost has been retrieved, hence
72 * we will have to fake its request to be able to open those modals.
74 const mockSafeToDestroy = () => {
75 spyOn(TestBed.get(OsdService), 'safeToDestroy').and.callFake(() =>
76 of({ is_safe_to_destroy: true })
80 const mockSafeToDelete = () => {
81 spyOn(TestBed.get(OsdService), 'safeToDelete').and.callFake(() =>
82 of({ is_safe_to_delete: true })
86 const mockOrchestratorStatus = () => {
87 spyOn(TestBed.get(OrchestratorService), 'status').and.callFake(() => of({ available: true }));
92 HttpClientTestingModule,
93 PerformanceCounterModule,
95 ToastrModule.forRoot(),
104 { provide: AuthStorageService, useValue: fakeAuthStorageService },
105 TableActionsComponent,
112 fixture = TestBed.createComponent(OsdListComponent);
113 component = fixture.componentInstance;
114 osdService = TestBed.get(OsdService);
115 modalServiceShowSpy = spyOn(TestBed.get(BsModalService), 'show').and.stub();
118 it('should create', () => {
119 fixture.detectChanges();
120 expect(component).toBeTruthy();
123 it('should have columns that are sortable', () => {
124 fixture.detectChanges();
127 .filter((column) => !column.checkboxable)
128 .every((column) => Boolean(column.prop))
132 describe('getOsdList', () => {
135 const createOsd = (n: number) =>
136 <Record<string, any>>{
143 op_out_bytes: [[n, n], [n * 2, n * 2]],
144 op_in_bytes: [[n * 3, n * 3], [n * 4, n * 4]]
147 stat_bytes_used: n * n,
148 stat_bytes: n * n * n
153 const expectAttributeOnEveryOsd = (attr: string) =>
154 expect(component.osds.every((osd) => Boolean(_.get(osd, attr)))).toBeTruthy();
157 spyOn(osdService, 'getList').and.callFake(() => of(osds));
158 osds = [createOsd(1), createOsd(2), createOsd(3)];
159 component.getOsdList();
162 it('should replace "this.osds" with new data', () => {
163 expect(component.osds.length).toBe(3);
164 expect(osdService.getList).toHaveBeenCalledTimes(1);
166 osds = [createOsd(4)];
167 component.getOsdList();
168 expect(component.osds.length).toBe(1);
169 expect(osdService.getList).toHaveBeenCalledTimes(2);
172 it('should have custom attribute "collectedStates"', () => {
173 expectAttributeOnEveryOsd('collectedStates');
174 expect(component.osds[0].collectedStates).toEqual(['in', 'up']);
177 it('should have "destroyed" state in "collectedStates"', () => {
178 osds[0].state.push('destroyed');
180 component.getOsdList();
182 expectAttributeOnEveryOsd('collectedStates');
183 expect(component.osds[0].collectedStates).toEqual(['in', 'destroyed']);
186 it('should have custom attribute "stats_history.out_bytes"', () => {
187 expectAttributeOnEveryOsd('stats_history.out_bytes');
188 expect(component.osds[0].stats_history.out_bytes).toEqual([1, 2]);
191 it('should have custom attribute "stats_history.in_bytes"', () => {
192 expectAttributeOnEveryOsd('stats_history.in_bytes');
193 expect(component.osds[0].stats_history.in_bytes).toEqual([3, 4]);
196 it('should have custom attribute "stats.usage"', () => {
197 expectAttributeOnEveryOsd('stats.usage');
198 expect(component.osds[0].stats.usage).toBe(1);
199 expect(component.osds[1].stats.usage).toBe(0.5);
200 expect(component.osds[2].stats.usage).toBe(3 / 9);
203 it('should have custom attribute "cdIsBinary" to be true', () => {
204 expectAttributeOnEveryOsd('cdIsBinary');
205 expect(component.osds[0].cdIsBinary).toBe(true);
209 describe('show osd actions as defined', () => {
210 const getOsdActions = () => {
211 fixture.detectChanges();
212 return fixture.debugElement.query(By.css('#cluster-wide-actions')).componentInstance
216 it('shows osd actions after osd-actions', () => {
217 fixture.detectChanges();
218 expect(fixture.debugElement.query(By.css('#cluster-wide-actions'))).toBe(
219 fixture.debugElement.queryAll(By.directive(TableActionsComponent))[1]
223 it('shows both osd actions', () => {
224 const osdActions = getOsdActions();
225 expect(osdActions).toEqual(component.clusterWideActions);
226 expect(osdActions.length).toBe(3);
229 it('shows only "Flags" action', () => {
230 component.permissions.configOpt.read = false;
231 const osdActions = getOsdActions();
232 expect(osdActions[0].name).toBe('Flags');
233 expect(osdActions.length).toBe(1);
236 it('shows only "Recovery Priority" action', () => {
237 component.permissions.osd.read = false;
238 const osdActions = getOsdActions();
239 expect(osdActions[0].name).toBe('Recovery Priority');
240 expect(osdActions[1].name).toBe('PG scrub');
241 expect(osdActions.length).toBe(2);
244 it('shows no osd actions', () => {
245 component.permissions.configOpt.read = false;
246 component.permissions.osd.read = false;
247 const osdActions = getOsdActions();
248 expect(osdActions).toEqual([]);
252 it('should test all TableActions combinations', () => {
253 const permissionHelper: PermissionHelper = new PermissionHelper(component.permissions.osd);
254 const tableActions: TableActionsComponent = permissionHelper.setPermissionsAndGetActions(
255 component.tableActions
258 expect(tableActions).toEqual({
259 'create,update,delete': {
274 primary: { multiple: 'Scrub', executing: 'Edit', single: 'Edit', no: 'Create' }
287 primary: { multiple: 'Scrub', executing: 'Edit', single: 'Edit', no: 'Create' }
290 actions: ['Create', 'Mark Lost', 'Purge', 'Destroy', 'Delete'],
293 executing: 'Mark Lost',
300 primary: { multiple: 'Create', executing: 'Create', single: 'Create', no: 'Create' }
316 primary: { multiple: 'Scrub', executing: 'Edit', single: 'Edit', no: 'Edit' }
319 actions: ['Edit', 'Scrub', 'Deep Scrub', 'Reweight', 'Mark Out', 'Mark In', 'Mark Down'],
320 primary: { multiple: 'Scrub', executing: 'Edit', single: 'Edit', no: 'Edit' }
323 actions: ['Mark Lost', 'Purge', 'Destroy', 'Delete'],
325 multiple: 'Mark Lost',
326 executing: 'Mark Lost',
333 primary: { multiple: '', executing: '', single: '', no: '' }
338 describe('test table actions in submenu', () => {
340 fixture.detectChanges();
343 beforeEach(fakeAsync(() => {
344 // The menu needs a click to render the dropdown!
345 const dropDownToggle = fixture.debugElement.query(By.css('.dropdown-toggle'));
346 dropDownToggle.triggerEventHandler('click', null);
348 fixture.detectChanges();
351 it('has all menu entries disabled except create', () => {
352 const tableActionElement = fixture.debugElement.query(By.directive(TableActionsComponent));
353 const toClassName = TestBed.get(TableActionsComponent).toClassName;
354 const getActionClasses = (action: CdTableAction) =>
355 tableActionElement.query(By.css(`.${toClassName(action.name)} .dropdown-item`)).classes;
357 component.tableActions.forEach((action) => {
358 if (action.name === 'Create') {
361 expect(getActionClasses(action).disabled).toBe(true);
366 describe('tests if all modals are opened correctly', () => {
368 * Helper function to check if a function opens a modal
370 * @param modalClass - The expected class of the modal
372 const expectOpensModal = (actionName: string, modalClass: any): void => {
373 openActionModal(actionName);
375 // @TODO: check why tsc is complaining when passing 'expectationFailOutput' param.
376 expect(modalServiceShowSpy.calls.any()).toBeTruthy();
377 expect(modalServiceShowSpy.calls.first().args[0]).toBe(modalClass);
379 modalServiceShowSpy.calls.reset();
382 it('opens the reweight modal', () => {
383 expectOpensModal('Reweight', OsdReweightModalComponent);
386 it('opens the form modal', () => {
387 expectOpensModal('Edit', FormModalComponent);
390 it('opens all confirmation modals', () => {
391 const modalClass = ConfirmationModalComponent;
392 expectOpensModal('Mark Out', modalClass);
393 expectOpensModal('Mark In', modalClass);
394 expectOpensModal('Mark Down', modalClass);
397 it('opens all critical confirmation modals', () => {
398 const modalClass = CriticalConfirmationModalComponent;
400 expectOpensModal('Mark Lost', modalClass);
401 expectOpensModal('Purge', modalClass);
402 expectOpensModal('Destroy', modalClass);
403 mockOrchestratorStatus();
405 expectOpensModal('Delete', modalClass);
409 describe('tests if the correct methods are called on confirmation', () => {
410 const expectOsdServiceMethodCalled = (
412 osdServiceMethodName:
421 const osdServiceSpy = spyOn(osdService, osdServiceMethodName).and.callFake(() => EMPTY);
422 openActionModal(actionName);
423 const initialState = modalServiceShowSpy.calls.first().args[1].initialState;
424 const submit = initialState.onSubmit || initialState.submitAction;
425 submit.call(component);
427 expect(osdServiceSpy.calls.count()).toBe(1);
428 expect(osdServiceSpy.calls.first().args[0]).toBe(1);
430 // Reset spies to be able to recreate them
431 osdServiceSpy.calls.reset();
432 modalServiceShowSpy.calls.reset();
435 it('calls the corresponding service methods in confirmation modals', () => {
436 expectOsdServiceMethodCalled('Mark Out', 'markOut');
437 expectOsdServiceMethodCalled('Mark In', 'markIn');
438 expectOsdServiceMethodCalled('Mark Down', 'markDown');
441 it('calls the corresponding service methods in critical confirmation modals', () => {
443 expectOsdServiceMethodCalled('Mark Lost', 'markLost');
444 expectOsdServiceMethodCalled('Purge', 'purge');
445 expectOsdServiceMethodCalled('Destroy', 'destroy');
446 mockOrchestratorStatus();
448 expectOsdServiceMethodCalled('Delete', 'delete');