]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/blob
efee6abe23d136a319fed0d8dd37c8731d9bc7d7
[ceph-ci.git] /
1 import { HttpClientTestingModule } from '@angular/common/http/testing';
2 import { ComponentFixture, TestBed } from '@angular/core/testing';
3 import { ReactiveFormsModule } from '@angular/forms';
4
5 import * as _ from 'lodash';
6 import { ToastModule } from 'ng2-toastr';
7 import { BsModalRef, ModalModule } from 'ngx-bootstrap/modal';
8
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';
12
13 describe('OsdRecvSpeedModalComponent', () => {
14   let component: OsdRecvSpeedModalComponent;
15   let fixture: ComponentFixture<OsdRecvSpeedModalComponent>;
16
17   configureTestBed({
18     imports: [
19       HttpClientTestingModule,
20       ModalModule.forRoot(),
21       ReactiveFormsModule,
22       SharedModule,
23       ToastModule.forRoot()
24     ],
25     declarations: [OsdRecvSpeedModalComponent],
26     providers: [BsModalRef, i18nProviders]
27   });
28
29   let configOptions = [];
30
31   beforeEach(() => {
32     fixture = TestBed.createComponent(OsdRecvSpeedModalComponent);
33     component = fixture.componentInstance;
34     fixture.detectChanges();
35
36     configOptions = [
37       {
38         name: 'osd_max_backfills',
39         desc: '',
40         type: 'uint',
41         default: 1
42       },
43       {
44         name: 'osd_recovery_max_active',
45         desc: '',
46         type: 'uint',
47         default: 3
48       },
49       {
50         name: 'osd_recovery_max_single_start',
51         desc: '',
52         type: 'uint',
53         default: 1
54       },
55       {
56         name: 'osd_recovery_sleep',
57         desc: 'Time in seconds to sleep before next recovery or backfill op',
58         type: 'float',
59         default: 0
60       }
61     ];
62   });
63
64   it('should create', () => {
65     expect(component).toBeTruthy();
66   });
67
68   describe('setPriority', () => {
69     it('should prepare the form for a custom priority', () => {
70       const customPriority = {
71         name: 'custom',
72         text: 'Custom',
73         values: {
74           osd_max_backfills: 1,
75           osd_recovery_max_active: 4,
76           osd_recovery_max_single_start: 1,
77           osd_recovery_sleep: 1
78         }
79       };
80
81       component.setPriority(customPriority);
82
83       const customInPriorities = _.find(component.priorities, (p) => {
84         return p.name === 'custom';
85       });
86
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);
93     });
94
95     it('should prepare the form for a none custom priority', () => {
96       const lowPriority = {
97         name: 'low',
98         text: 'Low',
99         values: {
100           osd_max_backfills: 1,
101           osd_recovery_max_active: 1,
102           osd_recovery_max_single_start: 1,
103           osd_recovery_sleep: 0.5
104         }
105       };
106
107       component.setPriority(lowPriority);
108
109       const customInPriorities = _.find(component.priorities, (p) => {
110         return p.name === 'custom';
111       });
112
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);
119     });
120   });
121
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
128     };
129
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
135     };
136
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
142     };
143
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
149     };
150
151     const configOptionsIncomplete = {
152       osd_max_backfills: 1,
153       osd_recovery_max_single_start: 1,
154       osd_recovery_sleep: 0
155     };
156
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');
160       });
161       expect(component.osdRecvSpeedForm.getValue('customizePriority')).toBeFalsy();
162     });
163
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');
167       });
168       expect(component.osdRecvSpeedForm.getValue('customizePriority')).toBeFalsy();
169     });
170
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');
174       });
175       expect(component.osdRecvSpeedForm.getValue('customizePriority')).toBeFalsy();
176     });
177
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');
181       });
182       expect(component.osdRecvSpeedForm.getValue('customizePriority')).toBeTruthy();
183     });
184
185     it('should return no priority if the config option values are incomplete', () => {
186       component.detectPriority(configOptionsIncomplete, (priority) => {
187         expect(priority.name).toBeNull();
188       });
189       expect(component.osdRecvSpeedForm.getValue('customizePriority')).toBeFalsy();
190     });
191   });
192
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);
199       });
200     });
201
202     it('should return the values set by the user if they exist', () => {
203       configOptions.forEach((configOption) => {
204         configOption['value'] = [{ section: 'osd', value: 7 }];
205       });
206
207       const currentValues = component.getCurrentValues(configOptions);
208       Object.values(currentValues.values).forEach((configValue) => {
209         expect(configValue).toBe(7);
210       });
211     });
212
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 }];
216       }
217
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);
222         } else {
223           expect(configValue).toBe(7);
224         }
225       });
226     });
227
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');
232     });
233   });
234
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'
242           );
243         } else {
244           expect(component.priorityAttrs[configOptionName].desc).toBe('');
245         }
246       });
247     });
248   });
249
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];
255
256         if (configOption.type === 'float') {
257           expect(component.priorityAttrs[configOption.name].patternHelpText).toBe(
258             'The entered value needs to be a number or decimal.'
259           );
260         } else {
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.'
264           );
265
266           control.setValue(-1);
267           expect(control.hasError('min')).toBeTruthy();
268         }
269
270         control.setValue(null);
271         expect(control.hasError('required')).toBeTruthy();
272         control.setValue('E');
273         expect(control.hasError('pattern')).toBeTruthy();
274         control.setValue(3);
275         expect(control.hasError('required')).toBeFalsy();
276         expect(control.hasError('min')).toBeFalsy();
277         expect(control.hasError('pattern')).toBeFalsy();
278       });
279     });
280   });
281 });