summaryrefslogtreecommitdiffstats
path: root/src/pybind/mgr/dashboard/frontend/src/app/ceph/cluster/osd/osd-recv-speed-modal/osd-recv-speed-modal.component.spec.ts
blob: f8b72940b5a375889ba782a5c9b083ef5e7fa32d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { ReactiveFormsModule } from '@angular/forms';
import { RouterTestingModule } from '@angular/router/testing';

import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
import _ from 'lodash';
import { ToastrModule } from 'ngx-toastr';
import { of as observableOf } from 'rxjs';

import { ConfigurationService } from '~/app/shared/api/configuration.service';
import { SharedModule } from '~/app/shared/shared.module';
import { configureTestBed } from '~/testing/unit-test-helper';
import { OsdRecvSpeedModalComponent } from './osd-recv-speed-modal.component';

describe('OsdRecvSpeedModalComponent', () => {
  let component: OsdRecvSpeedModalComponent;
  let fixture: ComponentFixture<OsdRecvSpeedModalComponent>;
  let configurationService: ConfigurationService;

  configureTestBed({
    imports: [
      HttpClientTestingModule,
      ReactiveFormsModule,
      RouterTestingModule,
      SharedModule,
      ToastrModule.forRoot()
    ],
    declarations: [OsdRecvSpeedModalComponent],
    providers: [NgbActiveModal]
  });

  let configOptions: any[] = [];

  beforeEach(() => {
    fixture = TestBed.createComponent(OsdRecvSpeedModalComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
    configurationService = TestBed.inject(ConfigurationService);
    configOptions = [
      {
        name: 'osd_max_backfills',
        desc: '',
        type: 'uint',
        default: 1
      },
      {
        name: 'osd_recovery_max_active',
        desc: '',
        type: 'uint',
        default: 3
      },
      {
        name: 'osd_recovery_max_single_start',
        desc: '',
        type: 'uint',
        default: 1
      },
      {
        name: 'osd_recovery_sleep',
        desc: 'Time in seconds to sleep before next recovery or backfill op',
        type: 'float',
        default: 0
      }
    ];
    spyOn(configurationService, 'filter').and.returnValue(observableOf(configOptions));
  });

  it('should create', () => {
    expect(component).toBeTruthy();
  });

  describe('ngOnInit', () => {
    let setPriority: jasmine.Spy;
    let setValidators: jasmine.Spy;

    beforeEach(() => {
      setPriority = spyOn(component, 'setPriority').and.callThrough();
      setValidators = spyOn(component, 'setValidators').and.callThrough();
      component.ngOnInit();
    });

    it('should call setValidators', () => {
      expect(setValidators).toHaveBeenCalled();
    });

    it('should get and set priority correctly', () => {
      const defaultPriority = _.find(component.priorities, (p) => {
        return _.isEqual(p.name, 'default');
      });
      expect(setPriority).toHaveBeenCalledWith(defaultPriority);
    });

    it('should set descriptions correctly', () => {
      expect(component.priorityAttrs['osd_max_backfills'].desc).toBe('');
      expect(component.priorityAttrs['osd_recovery_max_active'].desc).toBe('');
      expect(component.priorityAttrs['osd_recovery_max_single_start'].desc).toBe('');
      expect(component.priorityAttrs['osd_recovery_sleep'].desc).toBe(
        'Time in seconds to sleep before next recovery or backfill op'
      );
    });
  });

  describe('setPriority', () => {
    it('should prepare the form for a custom priority', () => {
      const customPriority = {
        name: 'custom',
        text: 'Custom',
        values: {
          osd_max_backfills: 1,
          osd_recovery_max_active: 4,
          osd_recovery_max_single_start: 1,
          osd_recovery_sleep: 1
        }
      };

      component.setPriority(customPriority);

      const customInPriorities = _.find(component.priorities, (p) => {
        return p.name === 'custom';
      });

      expect(customInPriorities).not.toBeNull();
      expect(component.osdRecvSpeedForm.getValue('priority')).toBe('custom');
      expect(component.osdRecvSpeedForm.getValue('osd_max_backfills')).toBe(1);
      expect(component.osdRecvSpeedForm.getValue('osd_recovery_max_active')).toBe(4);
      expect(component.osdRecvSpeedForm.getValue('osd_recovery_max_single_start')).toBe(1);
      expect(component.osdRecvSpeedForm.getValue('osd_recovery_sleep')).toBe(1);
    });

    it('should prepare the form for a none custom priority', () => {
      const lowPriority = {
        name: 'low',
        text: 'Low',
        values: {
          osd_max_backfills: 1,
          osd_recovery_max_active: 1,
          osd_recovery_max_single_start: 1,
          osd_recovery_sleep: 0.5
        }
      };

      component.setPriority(lowPriority);

      const customInPriorities = _.find(component.priorities, (p) => {
        return p.name === 'custom';
      });

      expect(customInPriorities).toBeUndefined();
      expect(component.osdRecvSpeedForm.getValue('priority')).toBe('low');
      expect(component.osdRecvSpeedForm.getValue('osd_max_backfills')).toBe(1);
      expect(component.osdRecvSpeedForm.getValue('osd_recovery_max_active')).toBe(1);
      expect(component.osdRecvSpeedForm.getValue('osd_recovery_max_single_start')).toBe(1);
      expect(component.osdRecvSpeedForm.getValue('osd_recovery_sleep')).toBe(0.5);
    });
  });

  describe('detectPriority', () => {
    const configOptionsLow = {
      osd_max_backfills: 1,
      osd_recovery_max_active: 1,
      osd_recovery_max_single_start: 1,
      osd_recovery_sleep: 0.5
    };

    const configOptionsDefault = {
      osd_max_backfills: 1,
      osd_recovery_max_active: 3,
      osd_recovery_max_single_start: 1,
      osd_recovery_sleep: 0
    };

    const configOptionsHigh = {
      osd_max_backfills: 4,
      osd_recovery_max_active: 4,
      osd_recovery_max_single_start: 4,
      osd_recovery_sleep: 0
    };

    const configOptionsCustom = {
      osd_max_backfills: 1,
      osd_recovery_max_active: 2,
      osd_recovery_max_single_start: 1,
      osd_recovery_sleep: 0
    };

    const configOptionsIncomplete = {
      osd_max_backfills: 1,
      osd_recovery_max_single_start: 1,
      osd_recovery_sleep: 0
    };

    it('should return priority "low" if the config option values have been set accordingly', () => {
      component.detectPriority(configOptionsLow, (priority: Record<string, any>) => {
        expect(priority.name).toBe('low');
      });
      expect(component.osdRecvSpeedForm.getValue('customizePriority')).toBeFalsy();
    });

    it('should return priority "default" if the config option values have been set accordingly', () => {
      component.detectPriority(configOptionsDefault, (priority: Record<string, any>) => {
        expect(priority.name).toBe('default');
      });
      expect(component.osdRecvSpeedForm.getValue('customizePriority')).toBeFalsy();
    });

    it('should return priority "high" if the config option values have been set accordingly', () => {
      component.detectPriority(configOptionsHigh, (priority: Record<string, any>) => {
        expect(priority.name).toBe('high');
      });
      expect(component.osdRecvSpeedForm.getValue('customizePriority')).toBeFalsy();
    });

    it('should return priority "custom" if the config option values do not match any priority', () => {
      component.detectPriority(configOptionsCustom, (priority: Record<string, any>) => {
        expect(priority.name).toBe('custom');
      });
      expect(component.osdRecvSpeedForm.getValue('customizePriority')).toBeTruthy();
    });

    it('should return no priority if the config option values are incomplete', () => {
      component.detectPriority(configOptionsIncomplete, (priority: Record<string, any>) => {
        expect(priority.name).toBeNull();
      });
      expect(component.osdRecvSpeedForm.getValue('customizePriority')).toBeFalsy();
    });
  });

  describe('getCurrentValues', () => {
    it('should return default values if no value has been set by the user', () => {
      const currentValues = component.getCurrentValues(configOptions);
      configOptions.forEach((configOption) => {
        const configOptionValue = currentValues.values[configOption.name];
        expect(configOptionValue).toBe(configOption.default);
      });
    });

    it('should return the values set by the user if they exist', () => {
      configOptions.forEach((configOption) => {
        configOption['value'] = [{ section: 'osd', value: 7 }];
      });

      const currentValues = component.getCurrentValues(configOptions);
      Object.values(currentValues.values).forEach((configValue) => {
        expect(configValue).toBe(7);
      });
    });

    it('should return the default value if one is missing', () => {
      for (let i = 1; i < configOptions.length; i++) {
        configOptions[i]['value'] = [{ section: 'osd', value: 7 }];
      }

      const currentValues = component.getCurrentValues(configOptions);
      Object.entries(currentValues.values).forEach(([configName, configValue]) => {
        if (configName === 'osd_max_backfills') {
          expect(configValue).toBe(1);
        } else {
          expect(configValue).toBe(7);
        }
      });
    });

    it('should return nothing if neither value nor default value is given', () => {
      configOptions[0].default = null;
      const currentValues = component.getCurrentValues(configOptions);
      expect(currentValues.values).not.toContain('osd_max_backfills');
    });
  });

  describe('setDescription', () => {
    it('should set the description if one is given', () => {
      component.setDescription(configOptions);
      Object.keys(component.priorityAttrs).forEach((configOptionName) => {
        if (configOptionName === 'osd_recovery_sleep') {
          expect(component.priorityAttrs[configOptionName].desc).toBe(
            'Time in seconds to sleep before next recovery or backfill op'
          );
        } else {
          expect(component.priorityAttrs[configOptionName].desc).toBe('');
        }
      });
    });
  });

  describe('setValidators', () => {
    it('should set needed validators for config option', () => {
      component.setValidators(configOptions);
      configOptions.forEach((configOption) => {
        const control = component.osdRecvSpeedForm.controls[configOption.name];

        if (configOption.type === 'float') {
          expect(component.priorityAttrs[configOption.name].patternHelpText).toBe(
            'The entered value needs to be a number or decimal.'
          );
        } else {
          expect(component.priorityAttrs[configOption.name].minValue).toBe(0);
          expect(component.priorityAttrs[configOption.name].patternHelpText).toBe(
            'The entered value needs to be an unsigned number.'
          );

          control.setValue(-1);
          expect(control.hasError('min')).toBeTruthy();
        }

        control.setValue(null);
        expect(control.hasError('required')).toBeTruthy();
        control.setValue('E');
        expect(control.hasError('pattern')).toBeTruthy();
        control.setValue(3);
        expect(control.hasError('required')).toBeFalsy();
        expect(control.hasError('min')).toBeFalsy();
        expect(control.hasError('pattern')).toBeFalsy();
      });
    });
  });
});