1 import { HttpClientTestingModule } from '@angular/common/http/testing';
2 import { ComponentFixture, TestBed } from '@angular/core/testing';
3 import { ReactiveFormsModule } from '@angular/forms';
4 import { RouterTestingModule } from '@angular/router/testing';
6 import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
7 import _ from 'lodash';
8 import { ToastrModule } from 'ngx-toastr';
9 import { of as observableOf } from 'rxjs';
11 import { ConfigurationService } from '~/app/shared/api/configuration.service';
12 import { SharedModule } from '~/app/shared/shared.module';
13 import { configureTestBed } from '~/testing/unit-test-helper';
14 import { OsdRecvSpeedModalComponent } from './osd-recv-speed-modal.component';
16 describe('OsdRecvSpeedModalComponent', () => {
17 let component: OsdRecvSpeedModalComponent;
18 let fixture: ComponentFixture<OsdRecvSpeedModalComponent>;
19 let configurationService: ConfigurationService;
23 HttpClientTestingModule,
27 ToastrModule.forRoot()
29 declarations: [OsdRecvSpeedModalComponent],
30 providers: [NgbActiveModal]
33 let configOptions: any[] = [];
36 fixture = TestBed.createComponent(OsdRecvSpeedModalComponent);
37 component = fixture.componentInstance;
38 fixture.detectChanges();
39 configurationService = TestBed.inject(ConfigurationService);
42 name: 'osd_max_backfills',
48 name: 'osd_recovery_max_active',
54 name: 'osd_recovery_max_single_start',
60 name: 'osd_recovery_sleep',
61 desc: 'Time in seconds to sleep before next recovery or backfill op',
66 spyOn(configurationService, 'filter').and.returnValue(observableOf(configOptions));
69 it('should create', () => {
70 expect(component).toBeTruthy();
73 describe('ngOnInit', () => {
74 let setPriority: jasmine.Spy;
75 let setValidators: jasmine.Spy;
78 setPriority = spyOn(component, 'setPriority').and.callThrough();
79 setValidators = spyOn(component, 'setValidators').and.callThrough();
83 it('should call setValidators', () => {
84 expect(setValidators).toHaveBeenCalled();
87 it('should get and set priority correctly', () => {
88 const defaultPriority = _.find(component.priorities, (p) => {
89 return _.isEqual(p.name, 'default');
91 expect(setPriority).toHaveBeenCalledWith(defaultPriority);
94 it('should set descriptions correctly', () => {
95 expect(component.priorityAttrs['osd_max_backfills'].desc).toBe('');
96 expect(component.priorityAttrs['osd_recovery_max_active'].desc).toBe('');
97 expect(component.priorityAttrs['osd_recovery_max_single_start'].desc).toBe('');
98 expect(component.priorityAttrs['osd_recovery_sleep'].desc).toBe(
99 'Time in seconds to sleep before next recovery or backfill op'
104 describe('setPriority', () => {
105 it('should prepare the form for a custom priority', () => {
106 const customPriority = {
110 osd_max_backfills: 1,
111 osd_recovery_max_active: 4,
112 osd_recovery_max_single_start: 1,
113 osd_recovery_sleep: 1
117 component.setPriority(customPriority);
119 const customInPriorities = _.find(component.priorities, (p) => {
120 return p.name === 'custom';
123 expect(customInPriorities).not.toBeNull();
124 expect(component.osdRecvSpeedForm.getValue('priority')).toBe('custom');
125 expect(component.osdRecvSpeedForm.getValue('osd_max_backfills')).toBe(1);
126 expect(component.osdRecvSpeedForm.getValue('osd_recovery_max_active')).toBe(4);
127 expect(component.osdRecvSpeedForm.getValue('osd_recovery_max_single_start')).toBe(1);
128 expect(component.osdRecvSpeedForm.getValue('osd_recovery_sleep')).toBe(1);
131 it('should prepare the form for a none custom priority', () => {
132 const lowPriority = {
136 osd_max_backfills: 1,
137 osd_recovery_max_active: 1,
138 osd_recovery_max_single_start: 1,
139 osd_recovery_sleep: 0.5
143 component.setPriority(lowPriority);
145 const customInPriorities = _.find(component.priorities, (p) => {
146 return p.name === 'custom';
149 expect(customInPriorities).toBeUndefined();
150 expect(component.osdRecvSpeedForm.getValue('priority')).toBe('low');
151 expect(component.osdRecvSpeedForm.getValue('osd_max_backfills')).toBe(1);
152 expect(component.osdRecvSpeedForm.getValue('osd_recovery_max_active')).toBe(1);
153 expect(component.osdRecvSpeedForm.getValue('osd_recovery_max_single_start')).toBe(1);
154 expect(component.osdRecvSpeedForm.getValue('osd_recovery_sleep')).toBe(0.5);
158 describe('detectPriority', () => {
159 const configOptionsLow = {
160 osd_max_backfills: 1,
161 osd_recovery_max_active: 1,
162 osd_recovery_max_single_start: 1,
163 osd_recovery_sleep: 0.5
166 const configOptionsDefault = {
167 osd_max_backfills: 1,
168 osd_recovery_max_active: 3,
169 osd_recovery_max_single_start: 1,
170 osd_recovery_sleep: 0
173 const configOptionsHigh = {
174 osd_max_backfills: 4,
175 osd_recovery_max_active: 4,
176 osd_recovery_max_single_start: 4,
177 osd_recovery_sleep: 0
180 const configOptionsCustom = {
181 osd_max_backfills: 1,
182 osd_recovery_max_active: 2,
183 osd_recovery_max_single_start: 1,
184 osd_recovery_sleep: 0
187 const configOptionsIncomplete = {
188 osd_max_backfills: 1,
189 osd_recovery_max_single_start: 1,
190 osd_recovery_sleep: 0
193 it('should return priority "low" if the config option values have been set accordingly', () => {
194 component.detectPriority(configOptionsLow, (priority: Record<string, any>) => {
195 expect(priority.name).toBe('low');
197 expect(component.osdRecvSpeedForm.getValue('customizePriority')).toBeFalsy();
200 it('should return priority "default" if the config option values have been set accordingly', () => {
201 component.detectPriority(configOptionsDefault, (priority: Record<string, any>) => {
202 expect(priority.name).toBe('default');
204 expect(component.osdRecvSpeedForm.getValue('customizePriority')).toBeFalsy();
207 it('should return priority "high" if the config option values have been set accordingly', () => {
208 component.detectPriority(configOptionsHigh, (priority: Record<string, any>) => {
209 expect(priority.name).toBe('high');
211 expect(component.osdRecvSpeedForm.getValue('customizePriority')).toBeFalsy();
214 it('should return priority "custom" if the config option values do not match any priority', () => {
215 component.detectPriority(configOptionsCustom, (priority: Record<string, any>) => {
216 expect(priority.name).toBe('custom');
218 expect(component.osdRecvSpeedForm.getValue('customizePriority')).toBeTruthy();
221 it('should return no priority if the config option values are incomplete', () => {
222 component.detectPriority(configOptionsIncomplete, (priority: Record<string, any>) => {
223 expect(priority.name).toBeNull();
225 expect(component.osdRecvSpeedForm.getValue('customizePriority')).toBeFalsy();
229 describe('getCurrentValues', () => {
230 it('should return default values if no value has been set by the user', () => {
231 const currentValues = component.getCurrentValues(configOptions);
232 configOptions.forEach((configOption) => {
233 const configOptionValue = currentValues.values[configOption.name];
234 expect(configOptionValue).toBe(configOption.default);
238 it('should return the values set by the user if they exist', () => {
239 configOptions.forEach((configOption) => {
240 configOption['value'] = [{ section: 'osd', value: 7 }];
243 const currentValues = component.getCurrentValues(configOptions);
244 Object.values(currentValues.values).forEach((configValue) => {
245 expect(configValue).toBe(7);
249 it('should return the default value if one is missing', () => {
250 for (let i = 1; i < configOptions.length; i++) {
251 configOptions[i]['value'] = [{ section: 'osd', value: 7 }];
254 const currentValues = component.getCurrentValues(configOptions);
255 Object.entries(currentValues.values).forEach(([configName, configValue]) => {
256 if (configName === 'osd_max_backfills') {
257 expect(configValue).toBe(1);
259 expect(configValue).toBe(7);
264 it('should return nothing if neither value nor default value is given', () => {
265 configOptions[0].default = null;
266 const currentValues = component.getCurrentValues(configOptions);
267 expect(currentValues.values).not.toContain('osd_max_backfills');
271 describe('setDescription', () => {
272 it('should set the description if one is given', () => {
273 component.setDescription(configOptions);
274 Object.keys(component.priorityAttrs).forEach((configOptionName) => {
275 if (configOptionName === 'osd_recovery_sleep') {
276 expect(component.priorityAttrs[configOptionName].desc).toBe(
277 'Time in seconds to sleep before next recovery or backfill op'
280 expect(component.priorityAttrs[configOptionName].desc).toBe('');
286 describe('setValidators', () => {
287 it('should set needed validators for config option', () => {
288 component.setValidators(configOptions);
289 configOptions.forEach((configOption) => {
290 const control = component.osdRecvSpeedForm.controls[configOption.name];
292 if (configOption.type === 'float') {
293 expect(component.priorityAttrs[configOption.name].patternHelpText).toBe(
294 'The entered value needs to be a number or decimal.'
297 expect(component.priorityAttrs[configOption.name].minValue).toBe(0);
298 expect(component.priorityAttrs[configOption.name].patternHelpText).toBe(
299 'The entered value needs to be an unsigned number.'
302 control.setValue(-1);
303 expect(control.hasError('min')).toBeTruthy();
306 control.setValue(null);
307 expect(control.hasError('required')).toBeTruthy();
308 control.setValue('E');
309 expect(control.hasError('pattern')).toBeTruthy();
311 expect(control.hasError('required')).toBeFalsy();
312 expect(control.hasError('min')).toBeFalsy();
313 expect(control.hasError('pattern')).toBeFalsy();