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>>{
153 stat_bytes_used: n * n,
154 stat_bytes: n * n * n
159 const expectAttributeOnEveryOsd = (attr: string) =>
160 expect(component.osds.every((osd) => Boolean(_.get(osd, attr)))).toBeTruthy();
163 spyOn(osdService, 'getList').and.callFake(() => of(osds));
164 osds = [createOsd(1), createOsd(2), createOsd(3)];
165 component.getOsdList();
168 it('should replace "this.osds" with new data', () => {
169 expect(component.osds.length).toBe(3);
170 expect(osdService.getList).toHaveBeenCalledTimes(1);
172 osds = [createOsd(4)];
173 component.getOsdList();
174 expect(component.osds.length).toBe(1);
175 expect(osdService.getList).toHaveBeenCalledTimes(2);
178 it('should have custom attribute "collectedStates"', () => {
179 expectAttributeOnEveryOsd('collectedStates');
180 expect(component.osds[0].collectedStates).toEqual(['in', 'up']);
183 it('should have "destroyed" state in "collectedStates"', () => {
184 osds[0].state.push('destroyed');
186 component.getOsdList();
188 expectAttributeOnEveryOsd('collectedStates');
189 expect(component.osds[0].collectedStates).toEqual(['in', 'destroyed']);
192 it('should have custom attribute "stats_history.out_bytes"', () => {
193 expectAttributeOnEveryOsd('stats_history.out_bytes');
194 expect(component.osds[0].stats_history.out_bytes).toEqual([1, 2]);
197 it('should have custom attribute "stats_history.in_bytes"', () => {
198 expectAttributeOnEveryOsd('stats_history.in_bytes');
199 expect(component.osds[0].stats_history.in_bytes).toEqual([3, 4]);
202 it('should have custom attribute "stats.usage"', () => {
203 expectAttributeOnEveryOsd('stats.usage');
204 expect(component.osds[0].stats.usage).toBe(1);
205 expect(component.osds[1].stats.usage).toBe(0.5);
206 expect(component.osds[2].stats.usage).toBe(3 / 9);
209 it('should have custom attribute "cdIsBinary" to be true', () => {
210 expectAttributeOnEveryOsd('cdIsBinary');
211 expect(component.osds[0].cdIsBinary).toBe(true);
215 describe('show osd actions as defined', () => {
216 const getOsdActions = () => {
217 fixture.detectChanges();
218 return fixture.debugElement.query(By.css('#cluster-wide-actions')).componentInstance
222 it('shows osd actions after osd-actions', () => {
223 fixture.detectChanges();
224 expect(fixture.debugElement.query(By.css('#cluster-wide-actions'))).toBe(
225 fixture.debugElement.queryAll(By.directive(TableActionsComponent))[1]
229 it('shows both osd actions', () => {
230 const osdActions = getOsdActions();
231 expect(osdActions).toEqual(component.clusterWideActions);
232 expect(osdActions.length).toBe(3);
235 it('shows only "Flags" action', () => {
236 component.permissions.configOpt.read = false;
237 const osdActions = getOsdActions();
238 expect(osdActions[0].name).toBe('Flags');
239 expect(osdActions.length).toBe(1);
242 it('shows only "Recovery Priority" action', () => {
243 component.permissions.osd.read = false;
244 const osdActions = getOsdActions();
245 expect(osdActions[0].name).toBe('Recovery Priority');
246 expect(osdActions[1].name).toBe('PG scrub');
247 expect(osdActions.length).toBe(2);
250 it('shows no osd actions', () => {
251 component.permissions.configOpt.read = false;
252 component.permissions.osd.read = false;
253 const osdActions = getOsdActions();
254 expect(osdActions).toEqual([]);
258 it('should test all TableActions combinations', () => {
259 const permissionHelper: PermissionHelper = new PermissionHelper(component.permissions.osd);
260 const tableActions: TableActionsComponent = permissionHelper.setPermissionsAndGetActions(
261 component.tableActions
264 expect(tableActions).toEqual({
265 'create,update,delete': {
280 primary: { multiple: 'Scrub', executing: 'Edit', single: 'Edit', no: 'Create' }
293 primary: { multiple: 'Scrub', executing: 'Edit', single: 'Edit', no: 'Create' }
296 actions: ['Create', 'Mark Lost', 'Purge', 'Destroy', 'Delete'],
299 executing: 'Mark Lost',
306 primary: { multiple: 'Create', executing: 'Create', single: 'Create', no: 'Create' }
322 primary: { multiple: 'Scrub', executing: 'Edit', single: 'Edit', no: 'Edit' }
325 actions: ['Edit', 'Scrub', 'Deep Scrub', 'Reweight', 'Mark Out', 'Mark In', 'Mark Down'],
326 primary: { multiple: 'Scrub', executing: 'Edit', single: 'Edit', no: 'Edit' }
329 actions: ['Mark Lost', 'Purge', 'Destroy', 'Delete'],
331 multiple: 'Mark Lost',
332 executing: 'Mark Lost',
339 primary: { multiple: '', executing: '', single: '', no: '' }
344 describe('test table actions in submenu', () => {
346 fixture.detectChanges();
349 beforeEach(fakeAsync(() => {
350 // The menu needs a click to render the dropdown!
351 const dropDownToggle = fixture.debugElement.query(By.css('.dropdown-toggle'));
352 dropDownToggle.triggerEventHandler('click', null);
354 fixture.detectChanges();
357 it('has all menu entries disabled except create', () => {
358 const tableActionElement = fixture.debugElement.query(By.directive(TableActionsComponent));
359 const toClassName = TestBed.get(TableActionsComponent).toClassName;
360 const getActionClasses = (action: CdTableAction) =>
361 tableActionElement.query(By.css(`.${toClassName(action.name)} .dropdown-item`)).classes;
363 component.tableActions.forEach((action) => {
364 if (action.name === 'Create') {
367 expect(getActionClasses(action).disabled).toBe(true);
372 describe('tests if all modals are opened correctly', () => {
374 * Helper function to check if a function opens a modal
376 * @param modalClass - The expected class of the modal
378 const expectOpensModal = (actionName: string, modalClass: any): void => {
379 openActionModal(actionName);
381 // @TODO: check why tsc is complaining when passing 'expectationFailOutput' param.
382 expect(modalServiceShowSpy.calls.any()).toBeTruthy();
383 expect(modalServiceShowSpy.calls.first().args[0]).toBe(modalClass);
385 modalServiceShowSpy.calls.reset();
388 it('opens the reweight modal', () => {
389 expectOpensModal('Reweight', OsdReweightModalComponent);
392 it('opens the form modal', () => {
393 expectOpensModal('Edit', FormModalComponent);
396 it('opens all confirmation modals', () => {
397 const modalClass = ConfirmationModalComponent;
398 expectOpensModal('Mark Out', modalClass);
399 expectOpensModal('Mark In', modalClass);
400 expectOpensModal('Mark Down', modalClass);
403 it('opens all critical confirmation modals', () => {
404 const modalClass = CriticalConfirmationModalComponent;
406 expectOpensModal('Mark Lost', modalClass);
407 expectOpensModal('Purge', modalClass);
408 expectOpensModal('Destroy', modalClass);
409 mockOrchestratorStatus();
411 expectOpensModal('Delete', modalClass);
415 describe('tests if the correct methods are called on confirmation', () => {
416 const expectOsdServiceMethodCalled = (
418 osdServiceMethodName:
427 const osdServiceSpy = spyOn(osdService, osdServiceMethodName).and.callFake(() => EMPTY);
428 openActionModal(actionName);
429 const initialState = modalServiceShowSpy.calls.first().args[1].initialState;
430 const submit = initialState.onSubmit || initialState.submitAction;
431 submit.call(component);
433 expect(osdServiceSpy.calls.count()).toBe(1);
434 expect(osdServiceSpy.calls.first().args[0]).toBe(1);
436 // Reset spies to be able to recreate them
437 osdServiceSpy.calls.reset();
438 modalServiceShowSpy.calls.reset();
441 it('calls the corresponding service methods in confirmation modals', () => {
442 expectOsdServiceMethodCalled('Mark Out', 'markOut');
443 expectOsdServiceMethodCalled('Mark In', 'markIn');
444 expectOsdServiceMethodCalled('Mark Down', 'markDown');
447 it('calls the corresponding service methods in critical confirmation modals', () => {
449 expectOsdServiceMethodCalled('Mark Lost', 'markLost');
450 expectOsdServiceMethodCalled('Purge', 'purge');
451 expectOsdServiceMethodCalled('Destroy', 'destroy');
452 mockOrchestratorStatus();
454 expectOsdServiceMethodCalled('Delete', 'delete');