]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/frontend/src/app/ceph/cluster/osd/osd-list/osd-list.component.spec.ts
501943515e1a92f3fc453bbcd962a3621672c680
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / src / app / ceph / cluster / osd / osd-list / osd-list.component.spec.ts
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';
6
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';
12
13 import {
14 configureTestBed,
15 i18nProviders,
16 PermissionHelper
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';
33
34 describe('OsdListComponent', () => {
35 let component: OsdListComponent;
36 let fixture: ComponentFixture<OsdListComponent>;
37 let modalServiceShowSpy: jasmine.Spy;
38 let osdService: OsdService;
39
40 const fakeAuthStorageService = {
41 getPermissions: () => {
42 return new Permissions({
43 'config-opt': ['read', 'update', 'create', 'delete'],
44 osd: ['read', 'update', 'create', 'delete']
45 });
46 }
47 };
48
49 const getTableAction = (name: string) =>
50 component.tableActions.find((action) => action.name === name);
51
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' } }];
56
57 // Table data and selection
58 component.selection = new CdTableSelection();
59 component.selection.selected = selection;
60 component.osds = data;
61 component.permissions = fakeAuthStorageService.getPermissions();
62 };
63
64 const openActionModal = (actionName: string) => {
65 setFakeSelection();
66 getTableAction(actionName).click();
67 };
68
69 /**
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.
73 */
74 const mockSafeToDestroy = () => {
75 spyOn(TestBed.get(OsdService), 'safeToDestroy').and.callFake(() =>
76 of({ is_safe_to_destroy: true })
77 );
78 };
79
80 const mockSafeToDelete = () => {
81 spyOn(TestBed.get(OsdService), 'safeToDelete').and.callFake(() =>
82 of({ is_safe_to_delete: true })
83 );
84 };
85
86 const mockOrchestratorStatus = () => {
87 spyOn(TestBed.get(OrchestratorService), 'status').and.callFake(() => of({ available: true }));
88 };
89
90 configureTestBed({
91 imports: [
92 HttpClientTestingModule,
93 PerformanceCounterModule,
94 TabsModule.forRoot(),
95 ToastrModule.forRoot(),
96 CephModule,
97 ReactiveFormsModule,
98 RouterTestingModule,
99 CoreModule,
100 RouterTestingModule
101 ],
102 declarations: [],
103 providers: [
104 { provide: AuthStorageService, useValue: fakeAuthStorageService },
105 TableActionsComponent,
106 BsModalService,
107 i18nProviders
108 ]
109 });
110
111 beforeEach(() => {
112 fixture = TestBed.createComponent(OsdListComponent);
113 component = fixture.componentInstance;
114 osdService = TestBed.get(OsdService);
115 modalServiceShowSpy = spyOn(TestBed.get(BsModalService), 'show').and.stub();
116 });
117
118 it('should create', () => {
119 fixture.detectChanges();
120 expect(component).toBeTruthy();
121 });
122
123 it('should have columns that are sortable', () => {
124 fixture.detectChanges();
125 expect(
126 component.columns
127 .filter((column) => !column.checkboxable)
128 .every((column) => Boolean(column.prop))
129 ).toBeTruthy();
130 });
131
132 describe('getOsdList', () => {
133 let osds: any[];
134
135 const createOsd = (n: number) =>
136 <Record<string, any>>{
137 in: 'in',
138 up: 'up',
139 tree: {
140 device_class: 'ssd'
141 },
142 stats_history: {
143 op_out_bytes: [[n, n], [n * 2, n * 2]],
144 op_in_bytes: [[n * 3, n * 3], [n * 4, n * 4]]
145 },
146 stats: {
147 stat_bytes_used: n * n,
148 stat_bytes: n * n * n
149 },
150 state: []
151 };
152
153 const expectAttributeOnEveryOsd = (attr: string) =>
154 expect(component.osds.every((osd) => Boolean(_.get(osd, attr)))).toBeTruthy();
155
156 beforeEach(() => {
157 spyOn(osdService, 'getList').and.callFake(() => of(osds));
158 osds = [createOsd(1), createOsd(2), createOsd(3)];
159 component.getOsdList();
160 });
161
162 it('should replace "this.osds" with new data', () => {
163 expect(component.osds.length).toBe(3);
164 expect(osdService.getList).toHaveBeenCalledTimes(1);
165
166 osds = [createOsd(4)];
167 component.getOsdList();
168 expect(component.osds.length).toBe(1);
169 expect(osdService.getList).toHaveBeenCalledTimes(2);
170 });
171
172 it('should have custom attribute "collectedStates"', () => {
173 expectAttributeOnEveryOsd('collectedStates');
174 expect(component.osds[0].collectedStates).toEqual(['in', 'up']);
175 });
176
177 it('should have "destroyed" state in "collectedStates"', () => {
178 osds[0].state.push('destroyed');
179 osds[0].up = 0;
180 component.getOsdList();
181
182 expectAttributeOnEveryOsd('collectedStates');
183 expect(component.osds[0].collectedStates).toEqual(['in', 'destroyed']);
184 });
185
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]);
189 });
190
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]);
194 });
195
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);
201 });
202
203 it('should have custom attribute "cdIsBinary" to be true', () => {
204 expectAttributeOnEveryOsd('cdIsBinary');
205 expect(component.osds[0].cdIsBinary).toBe(true);
206 });
207 });
208
209 describe('show osd actions as defined', () => {
210 const getOsdActions = () => {
211 fixture.detectChanges();
212 return fixture.debugElement.query(By.css('#cluster-wide-actions')).componentInstance
213 .dropDownActions;
214 };
215
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]
220 );
221 });
222
223 it('shows both osd actions', () => {
224 const osdActions = getOsdActions();
225 expect(osdActions).toEqual(component.clusterWideActions);
226 expect(osdActions.length).toBe(3);
227 });
228
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);
234 });
235
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);
242 });
243
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([]);
249 });
250 });
251
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
256 );
257
258 expect(tableActions).toEqual({
259 'create,update,delete': {
260 actions: [
261 'Create',
262 'Edit',
263 'Scrub',
264 'Deep Scrub',
265 'Reweight',
266 'Mark Out',
267 'Mark In',
268 'Mark Down',
269 'Mark Lost',
270 'Purge',
271 'Destroy',
272 'Delete'
273 ],
274 primary: { multiple: 'Scrub', executing: 'Edit', single: 'Edit', no: 'Create' }
275 },
276 'create,update': {
277 actions: [
278 'Create',
279 'Edit',
280 'Scrub',
281 'Deep Scrub',
282 'Reweight',
283 'Mark Out',
284 'Mark In',
285 'Mark Down'
286 ],
287 primary: { multiple: 'Scrub', executing: 'Edit', single: 'Edit', no: 'Create' }
288 },
289 'create,delete': {
290 actions: ['Create', 'Mark Lost', 'Purge', 'Destroy', 'Delete'],
291 primary: {
292 multiple: 'Create',
293 executing: 'Mark Lost',
294 single: 'Mark Lost',
295 no: 'Create'
296 }
297 },
298 create: {
299 actions: ['Create'],
300 primary: { multiple: 'Create', executing: 'Create', single: 'Create', no: 'Create' }
301 },
302 'update,delete': {
303 actions: [
304 'Edit',
305 'Scrub',
306 'Deep Scrub',
307 'Reweight',
308 'Mark Out',
309 'Mark In',
310 'Mark Down',
311 'Mark Lost',
312 'Purge',
313 'Destroy',
314 'Delete'
315 ],
316 primary: { multiple: 'Scrub', executing: 'Edit', single: 'Edit', no: 'Edit' }
317 },
318 update: {
319 actions: ['Edit', 'Scrub', 'Deep Scrub', 'Reweight', 'Mark Out', 'Mark In', 'Mark Down'],
320 primary: { multiple: 'Scrub', executing: 'Edit', single: 'Edit', no: 'Edit' }
321 },
322 delete: {
323 actions: ['Mark Lost', 'Purge', 'Destroy', 'Delete'],
324 primary: {
325 multiple: 'Mark Lost',
326 executing: 'Mark Lost',
327 single: 'Mark Lost',
328 no: 'Mark Lost'
329 }
330 },
331 'no-permissions': {
332 actions: [],
333 primary: { multiple: '', executing: '', single: '', no: '' }
334 }
335 });
336 });
337
338 describe('test table actions in submenu', () => {
339 beforeEach(() => {
340 fixture.detectChanges();
341 });
342
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);
347 tick();
348 fixture.detectChanges();
349 }));
350
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;
356
357 component.tableActions.forEach((action) => {
358 if (action.name === 'Create') {
359 return;
360 }
361 expect(getActionClasses(action).disabled).toBe(true);
362 });
363 });
364 });
365
366 describe('tests if all modals are opened correctly', () => {
367 /**
368 * Helper function to check if a function opens a modal
369 *
370 * @param modalClass - The expected class of the modal
371 */
372 const expectOpensModal = (actionName: string, modalClass: any): void => {
373 openActionModal(actionName);
374
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);
378
379 modalServiceShowSpy.calls.reset();
380 };
381
382 it('opens the reweight modal', () => {
383 expectOpensModal('Reweight', OsdReweightModalComponent);
384 });
385
386 it('opens the form modal', () => {
387 expectOpensModal('Edit', FormModalComponent);
388 });
389
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);
395 });
396
397 it('opens all critical confirmation modals', () => {
398 const modalClass = CriticalConfirmationModalComponent;
399 mockSafeToDestroy();
400 expectOpensModal('Mark Lost', modalClass);
401 expectOpensModal('Purge', modalClass);
402 expectOpensModal('Destroy', modalClass);
403 mockOrchestratorStatus();
404 mockSafeToDelete();
405 expectOpensModal('Delete', modalClass);
406 });
407 });
408
409 describe('tests if the correct methods are called on confirmation', () => {
410 const expectOsdServiceMethodCalled = (
411 actionName: string,
412 osdServiceMethodName:
413 | 'markOut'
414 | 'markIn'
415 | 'markDown'
416 | 'markLost'
417 | 'purge'
418 | 'destroy'
419 | 'delete'
420 ): void => {
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);
426
427 expect(osdServiceSpy.calls.count()).toBe(1);
428 expect(osdServiceSpy.calls.first().args[0]).toBe(1);
429
430 // Reset spies to be able to recreate them
431 osdServiceSpy.calls.reset();
432 modalServiceShowSpy.calls.reset();
433 };
434
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');
439 });
440
441 it('calls the corresponding service methods in critical confirmation modals', () => {
442 mockSafeToDestroy();
443 expectOsdServiceMethodCalled('Mark Lost', 'markLost');
444 expectOsdServiceMethodCalled('Purge', 'purge');
445 expectOsdServiceMethodCalled('Destroy', 'destroy');
446 mockOrchestratorStatus();
447 mockSafeToDelete();
448 expectOsdServiceMethodCalled('Delete', 'delete');
449 });
450 });
451 });