1 import { HttpClientTestingModule } from '@angular/common/http/testing';
2 import { ComponentFixture, TestBed } from '@angular/core/testing';
3 import { ReactiveFormsModule } from '@angular/forms';
5 import * as _ from 'lodash';
6 import { ToastModule } from 'ng2-toastr';
7 import { BsModalRef, ModalModule } from 'ngx-bootstrap/modal';
9 import { configureTestBed, i18nProviders } from '../../../../../testing/unit-test-helper';
10 import { SharedModule } from '../../../../shared/shared.module';
11 import { OsdRecvSpeedModalComponent } from './osd-recv-speed-modal.component';
13 describe('OsdRecvSpeedModalComponent', () => {
14 let component: OsdRecvSpeedModalComponent;
15 let fixture: ComponentFixture<OsdRecvSpeedModalComponent>;
19 HttpClientTestingModule,
20 ModalModule.forRoot(),
25 declarations: [OsdRecvSpeedModalComponent],
26 providers: [BsModalRef, i18nProviders]
29 let configOptions = [];
32 fixture = TestBed.createComponent(OsdRecvSpeedModalComponent);
33 component = fixture.componentInstance;
34 fixture.detectChanges();
38 name: 'osd_max_backfills',
44 name: 'osd_recovery_max_active',
50 name: 'osd_recovery_max_single_start',
56 name: 'osd_recovery_sleep',
57 desc: 'Time in seconds to sleep before next recovery or backfill op',
64 it('should create', () => {
65 expect(component).toBeTruthy();
68 describe('setPriority', () => {
69 it('should prepare the form for a custom priority', () => {
70 const customPriority = {
75 osd_recovery_max_active: 4,
76 osd_recovery_max_single_start: 1,
81 component.setPriority(customPriority);
83 const customInPriorities = _.find(component.priorities, (p) => {
84 return p.name === 'custom';
87 expect(customInPriorities).not.toBeNull();
88 expect(component.osdRecvSpeedForm.getValue('priority')).toBe('custom');
89 expect(component.osdRecvSpeedForm.getValue('osd_max_backfills')).toBe(1);
90 expect(component.osdRecvSpeedForm.getValue('osd_recovery_max_active')).toBe(4);
91 expect(component.osdRecvSpeedForm.getValue('osd_recovery_max_single_start')).toBe(1);
92 expect(component.osdRecvSpeedForm.getValue('osd_recovery_sleep')).toBe(1);
95 it('should prepare the form for a none custom priority', () => {
100 osd_max_backfills: 1,
101 osd_recovery_max_active: 1,
102 osd_recovery_max_single_start: 1,
103 osd_recovery_sleep: 0.5
107 component.setPriority(lowPriority);
109 const customInPriorities = _.find(component.priorities, (p) => {
110 return p.name === 'custom';
113 expect(customInPriorities).toBeUndefined();
114 expect(component.osdRecvSpeedForm.getValue('priority')).toBe('low');
115 expect(component.osdRecvSpeedForm.getValue('osd_max_backfills')).toBe(1);
116 expect(component.osdRecvSpeedForm.getValue('osd_recovery_max_active')).toBe(1);
117 expect(component.osdRecvSpeedForm.getValue('osd_recovery_max_single_start')).toBe(1);
118 expect(component.osdRecvSpeedForm.getValue('osd_recovery_sleep')).toBe(0.5);
122 describe('detectPriority', () => {
123 const configOptionsLow = {
124 osd_max_backfills: 1,
125 osd_recovery_max_active: 1,
126 osd_recovery_max_single_start: 1,
127 osd_recovery_sleep: 0.5
130 const configOptionsDefault = {
131 osd_max_backfills: 1,
132 osd_recovery_max_active: 3,
133 osd_recovery_max_single_start: 1,
134 osd_recovery_sleep: 0
137 const configOptionsHigh = {
138 osd_max_backfills: 4,
139 osd_recovery_max_active: 4,
140 osd_recovery_max_single_start: 4,
141 osd_recovery_sleep: 0
144 const configOptionsCustom = {
145 osd_max_backfills: 1,
146 osd_recovery_max_active: 2,
147 osd_recovery_max_single_start: 1,
148 osd_recovery_sleep: 0
151 const configOptionsIncomplete = {
152 osd_max_backfills: 1,
153 osd_recovery_max_single_start: 1,
154 osd_recovery_sleep: 0
157 it('should return priority "low" if the config option values have been set accordingly', () => {
158 component.detectPriority(configOptionsLow, (priority) => {
159 expect(priority.name).toBe('low');
161 expect(component.osdRecvSpeedForm.getValue('customizePriority')).toBeFalsy();
164 it('should return priority "default" if the config option values have been set accordingly', () => {
165 component.detectPriority(configOptionsDefault, (priority) => {
166 expect(priority.name).toBe('default');
168 expect(component.osdRecvSpeedForm.getValue('customizePriority')).toBeFalsy();
171 it('should return priority "high" if the config option values have been set accordingly', () => {
172 component.detectPriority(configOptionsHigh, (priority) => {
173 expect(priority.name).toBe('high');
175 expect(component.osdRecvSpeedForm.getValue('customizePriority')).toBeFalsy();
178 it('should return priority "custom" if the config option values do not match any priority', () => {
179 component.detectPriority(configOptionsCustom, (priority) => {
180 expect(priority.name).toBe('custom');
182 expect(component.osdRecvSpeedForm.getValue('customizePriority')).toBeTruthy();
185 it('should return no priority if the config option values are incomplete', () => {
186 component.detectPriority(configOptionsIncomplete, (priority) => {
187 expect(priority.name).toBeNull();
189 expect(component.osdRecvSpeedForm.getValue('customizePriority')).toBeFalsy();
193 describe('getCurrentValues', () => {
194 it('should return default values if no value has been set by the user', () => {
195 const currentValues = component.getCurrentValues(configOptions);
196 configOptions.forEach((configOption) => {
197 const configOptionValue = currentValues.values[configOption.name];
198 expect(configOptionValue).toBe(configOption.default);
202 it('should return the values set by the user if they exist', () => {
203 configOptions.forEach((configOption) => {
204 configOption['value'] = [{ section: 'osd', value: 7 }];
207 const currentValues = component.getCurrentValues(configOptions);
208 Object.values(currentValues.values).forEach((configValue) => {
209 expect(configValue).toBe(7);
213 it('should return the default value if one is missing', () => {
214 for (let i = 1; i < configOptions.length; i++) {
215 configOptions[i]['value'] = [{ section: 'osd', value: 7 }];
218 const currentValues = component.getCurrentValues(configOptions);
219 Object.entries(currentValues.values).forEach(([configName, configValue]) => {
220 if (configName === 'osd_max_backfills') {
221 expect(configValue).toBe(1);
223 expect(configValue).toBe(7);
228 it('should return nothing if neither value nor default value is given', () => {
229 configOptions[0].default = null;
230 const currentValues = component.getCurrentValues(configOptions);
231 expect(currentValues.values).not.toContain('osd_max_backfills');
235 describe('setDescription', () => {
236 it('should set the description if one is given', () => {
237 component.setDescription(configOptions);
238 Object.keys(component.priorityAttrs).forEach((configOptionName) => {
239 if (configOptionName === 'osd_recovery_sleep') {
240 expect(component.priorityAttrs[configOptionName].desc).toBe(
241 'Time in seconds to sleep before next recovery or backfill op'
244 expect(component.priorityAttrs[configOptionName].desc).toBe('');
250 describe('setValidators', () => {
251 it('should set needed validators for config option', () => {
252 component.setValidators(configOptions);
253 configOptions.forEach((configOption) => {
254 const control = component.osdRecvSpeedForm.controls[configOption.name];
256 if (configOption.type === 'float') {
257 expect(component.priorityAttrs[configOption.name].patternHelpText).toBe(
258 'The entered value needs to be a number or decimal.'
261 expect(component.priorityAttrs[configOption.name].minValue).toBe(0);
262 expect(component.priorityAttrs[configOption.name].patternHelpText).toBe(
263 'The entered value needs to be an unsigned number.'
266 control.setValue(-1);
267 expect(control.hasError('min')).toBeTruthy();
270 control.setValue(null);
271 expect(control.hasError('required')).toBeTruthy();
272 control.setValue('E');
273 expect(control.hasError('pattern')).toBeTruthy();
275 expect(control.hasError('required')).toBeFalsy();
276 expect(control.hasError('min')).toBeFalsy();
277 expect(control.hasError('pattern')).toBeFalsy();