1 import { HttpClientTestingModule } from '@angular/common/http/testing';
2 import { ComponentFixture, TestBed } from '@angular/core/testing';
3 import { ReactiveFormsModule } from '@angular/forms';
4 import { By } from '@angular/platform-browser';
5 import { RouterTestingModule } from '@angular/router/testing';
7 import { NgbActiveModal, NgbTypeaheadModule } from '@ng-bootstrap/ng-bootstrap';
8 import _ from 'lodash';
9 import { ToastrModule } from 'ngx-toastr';
11 import { CephServiceService } from '~/app/shared/api/ceph-service.service';
12 import { CdFormGroup } from '~/app/shared/forms/cd-form-group';
13 import { SharedModule } from '~/app/shared/shared.module';
14 import { configureTestBed, FormHelper } from '~/testing/unit-test-helper';
15 import { ServiceFormComponent } from './service-form.component';
17 describe('ServiceFormComponent', () => {
18 let component: ServiceFormComponent;
19 let fixture: ComponentFixture<ServiceFormComponent>;
20 let cephServiceService: CephServiceService;
21 let form: CdFormGroup;
22 let formHelper: FormHelper;
25 declarations: [ServiceFormComponent],
26 providers: [NgbActiveModal],
28 HttpClientTestingModule,
33 ToastrModule.forRoot()
38 fixture = TestBed.createComponent(ServiceFormComponent);
39 component = fixture.componentInstance;
41 form = component.serviceForm;
42 formHelper = new FormHelper(form);
43 fixture.detectChanges();
46 it('should create', () => {
47 expect(component).toBeTruthy();
50 describe('should test form', () => {
52 cephServiceService = TestBed.inject(CephServiceService);
53 spyOn(cephServiceService, 'create').and.stub();
56 it('should test placement (host)', () => {
57 formHelper.setValue('service_type', 'crash');
58 formHelper.setValue('placement', 'hosts');
59 formHelper.setValue('hosts', ['mgr0', 'mon0', 'osd0']);
60 formHelper.setValue('count', 2);
62 expect(cephServiceService.create).toHaveBeenCalledWith({
63 service_type: 'crash',
65 hosts: ['mgr0', 'mon0', 'osd0'],
72 it('should test placement (label)', () => {
73 formHelper.setValue('service_type', 'mgr');
74 formHelper.setValue('placement', 'label');
75 formHelper.setValue('label', 'foo');
77 expect(cephServiceService.create).toHaveBeenCalledWith({
86 it('should submit valid count', () => {
87 formHelper.setValue('count', 1);
89 formHelper.expectValid('count');
92 it('should submit invalid count (1)', () => {
93 formHelper.setValue('count', 0);
95 formHelper.expectError('count', 'min');
98 it('should submit invalid count (2)', () => {
99 formHelper.setValue('count', 'abc');
100 component.onSubmit();
101 formHelper.expectError('count', 'pattern');
104 it('should test unmanaged', () => {
105 formHelper.setValue('service_type', 'mgr');
106 formHelper.setValue('service_id', 'svc');
107 formHelper.setValue('placement', 'label');
108 formHelper.setValue('label', 'bar');
109 formHelper.setValue('unmanaged', true);
110 component.onSubmit();
111 expect(cephServiceService.create).toHaveBeenCalledWith({
119 it('should test various services', () => {
133 formHelper.setValue('service_type', serviceType);
134 component.onSubmit();
135 expect(cephServiceService.create).toHaveBeenCalledWith({
136 service_type: serviceType,
144 describe('should test service nfs', () => {
146 formHelper.setValue('service_type', 'nfs');
149 it('should submit nfs', () => {
150 component.onSubmit();
151 expect(cephServiceService.create).toHaveBeenCalledWith({
159 describe('should test service rgw', () => {
161 formHelper.setValue('service_type', 'rgw');
162 formHelper.setValue('service_id', 'svc');
165 it('should test rgw valid service id', () => {
166 formHelper.setValue('service_id', 'svc.realm.zone');
167 formHelper.expectValid('service_id');
168 formHelper.setValue('service_id', 'svc');
169 formHelper.expectValid('service_id');
172 it('should test rgw invalid service id', () => {
173 formHelper.setValue('service_id', '.');
174 formHelper.expectError('service_id', 'rgwPattern');
175 formHelper.setValue('service_id', 'svc.');
176 formHelper.expectError('service_id', 'rgwPattern');
177 formHelper.setValue('service_id', 'svc.realm');
178 formHelper.expectError('service_id', 'rgwPattern');
179 formHelper.setValue('service_id', 'svc.realm.');
180 formHelper.expectError('service_id', 'rgwPattern');
181 formHelper.setValue('service_id', '.svc.realm');
182 formHelper.expectError('service_id', 'rgwPattern');
183 formHelper.setValue('service_id', 'svc.realm.zone.');
184 formHelper.expectError('service_id', 'rgwPattern');
187 it('should submit rgw with realm and zone', () => {
188 formHelper.setValue('service_id', 'svc.my-realm.my-zone');
189 component.onSubmit();
190 expect(cephServiceService.create).toHaveBeenCalledWith({
193 rgw_realm: 'my-realm',
201 it('should submit rgw with port and ssl enabled', () => {
202 formHelper.setValue('rgw_frontend_port', 1234);
203 formHelper.setValue('ssl', true);
204 component.onSubmit();
205 expect(cephServiceService.create).toHaveBeenCalledWith({
210 rgw_frontend_port: 1234,
211 rgw_frontend_ssl_certificate: '',
216 it('should submit valid rgw port (1)', () => {
217 formHelper.setValue('rgw_frontend_port', 1);
218 component.onSubmit();
219 formHelper.expectValid('rgw_frontend_port');
222 it('should submit valid rgw port (2)', () => {
223 formHelper.setValue('rgw_frontend_port', 65535);
224 component.onSubmit();
225 formHelper.expectValid('rgw_frontend_port');
228 it('should submit invalid rgw port (1)', () => {
229 formHelper.setValue('rgw_frontend_port', 0);
230 fixture.detectChanges();
231 formHelper.expectError('rgw_frontend_port', 'min');
234 it('should submit invalid rgw port (2)', () => {
235 formHelper.setValue('rgw_frontend_port', 65536);
236 fixture.detectChanges();
237 formHelper.expectError('rgw_frontend_port', 'max');
240 it('should submit invalid rgw port (3)', () => {
241 formHelper.setValue('rgw_frontend_port', 'abc');
242 component.onSubmit();
243 formHelper.expectError('rgw_frontend_port', 'pattern');
246 it('should submit rgw w/o port', () => {
247 formHelper.setValue('ssl', false);
248 component.onSubmit();
249 expect(cephServiceService.create).toHaveBeenCalledWith({
258 it('should not show private key field', () => {
259 formHelper.setValue('ssl', true);
260 fixture.detectChanges();
261 const ssl_key = fixture.debugElement.query(By.css('#ssl_key'));
262 expect(ssl_key).toBeNull();
265 it('should test .pem file', () => {
267 -----BEGIN CERTIFICATE-----
268 iJ5IbgzlKPssdYwuAEI3yPZxX/g5vKBrgcyD3LttLL/DlElq/1xCnwVrv7WROSNu
269 -----END CERTIFICATE-----
270 -----BEGIN CERTIFICATE-----
271 mn/S7BNBEC7AGe5ajmN+8hBTGdACUXe8rwMNrtTy/MwBZ0VpJsAAjJh+aptZh5yB
272 -----END CERTIFICATE-----
273 -----BEGIN RSA PRIVATE KEY-----
274 x4Ea7kGVgx9kWh5XjWz9wjZvY49UKIT5ppIAWPMbLl3UpfckiuNhTA==
275 -----END RSA PRIVATE KEY-----`;
276 formHelper.setValue('ssl', true);
277 formHelper.setValue('ssl_cert', pemCert);
278 fixture.detectChanges();
279 formHelper.expectValid('ssl_cert');
283 describe('should test service iscsi', () => {
285 formHelper.setValue('service_type', 'iscsi');
286 formHelper.setValue('pool', 'xyz');
287 formHelper.setValue('api_user', 'user');
288 formHelper.setValue('api_password', 'password');
289 formHelper.setValue('ssl', false);
292 it('should submit iscsi', () => {
293 component.onSubmit();
294 expect(cephServiceService.create).toHaveBeenCalledWith({
295 service_type: 'iscsi',
300 api_password: 'password',
305 it('should submit iscsi with trusted ips', () => {
306 formHelper.setValue('ssl', true);
307 formHelper.setValue('trusted_ip_list', ' 172.16.0.5, 192.1.1.10 ');
308 component.onSubmit();
309 expect(cephServiceService.create).toHaveBeenCalledWith({
310 service_type: 'iscsi',
315 api_password: 'password',
319 trusted_ip_list: '172.16.0.5, 192.1.1.10'
323 it('should submit iscsi with port', () => {
324 formHelper.setValue('api_port', 456);
325 component.onSubmit();
326 expect(cephServiceService.create).toHaveBeenCalledWith({
327 service_type: 'iscsi',
332 api_password: 'password',
338 it('should submit valid iscsi port (1)', () => {
339 formHelper.setValue('api_port', 1);
340 component.onSubmit();
341 formHelper.expectValid('api_port');
344 it('should submit valid iscsi port (2)', () => {
345 formHelper.setValue('api_port', 65535);
346 component.onSubmit();
347 formHelper.expectValid('api_port');
350 it('should submit invalid iscsi port (1)', () => {
351 formHelper.setValue('api_port', 0);
352 fixture.detectChanges();
353 formHelper.expectError('api_port', 'min');
356 it('should submit invalid iscsi port (2)', () => {
357 formHelper.setValue('api_port', 65536);
358 fixture.detectChanges();
359 formHelper.expectError('api_port', 'max');
362 it('should submit invalid iscsi port (3)', () => {
363 formHelper.setValue('api_port', 'abc');
364 component.onSubmit();
365 formHelper.expectError('api_port', 'pattern');
368 it('should throw error when there is no pool', () => {
369 formHelper.expectErrorChange('pool', '', 'required');
373 describe('should test service ingress', () => {
375 formHelper.setValue('service_type', 'ingress');
376 formHelper.setValue('backend_service', 'rgw.foo');
377 formHelper.setValue('virtual_ip', '192.168.20.1/24');
378 formHelper.setValue('ssl', false);
381 it('should submit ingress', () => {
382 component.onSubmit();
383 expect(cephServiceService.create).toHaveBeenCalledWith({
384 service_type: 'ingress',
387 backend_service: 'rgw.foo',
388 service_id: 'rgw.foo',
389 virtual_ip: '192.168.20.1/24',
390 virtual_interface_networks: null,
395 it('should pre-populate the service id', () => {
396 component.prePopulateId();
397 const prePopulatedID = component.serviceForm.getValue('service_id');
398 expect(prePopulatedID).toBe('rgw.foo');
401 it('should submit valid frontend and monitor port', () => {
403 formHelper.setValue('frontend_port', 1);
404 formHelper.setValue('monitor_port', 1);
405 fixture.detectChanges();
406 formHelper.expectValid('frontend_port');
407 formHelper.expectValid('monitor_port');
410 formHelper.setValue('frontend_port', 65535);
411 formHelper.setValue('monitor_port', 65535);
412 fixture.detectChanges();
413 formHelper.expectValid('frontend_port');
414 formHelper.expectValid('monitor_port');
417 it('should submit invalid frontend and monitor port', () => {
419 formHelper.setValue('frontend_port', 0);
420 formHelper.setValue('monitor_port', 0);
421 fixture.detectChanges();
422 formHelper.expectError('frontend_port', 'min');
423 formHelper.expectError('monitor_port', 'min');
426 formHelper.setValue('frontend_port', 65536);
427 formHelper.setValue('monitor_port', 65536);
428 fixture.detectChanges();
429 formHelper.expectError('frontend_port', 'max');
430 formHelper.expectError('monitor_port', 'max');
433 formHelper.setValue('frontend_port', 'abc');
434 formHelper.setValue('monitor_port', 'abc');
435 component.onSubmit();
436 formHelper.expectError('frontend_port', 'pattern');
437 formHelper.expectError('monitor_port', 'pattern');
441 describe('should test service snmp-gateway', () => {
443 formHelper.setValue('service_type', 'snmp-gateway');
444 formHelper.setValue('snmp_destination', '192.168.20.1:8443');
447 it('should test snmp-gateway service with V2c', () => {
448 formHelper.setValue('snmp_version', 'V2c');
449 formHelper.setValue('snmp_community', 'public');
450 component.onSubmit();
451 expect(cephServiceService.create).toHaveBeenCalledWith({
452 service_type: 'snmp-gateway',
456 snmp_destination: '192.168.20.1:8443',
458 snmp_community: 'public'
463 it('should test snmp-gateway service with V3', () => {
464 formHelper.setValue('snmp_version', 'V3');
465 formHelper.setValue('engine_id', '800C53F00000');
466 formHelper.setValue('auth_protocol', 'SHA');
467 formHelper.setValue('privacy_protocol', 'DES');
468 formHelper.setValue('snmp_v3_auth_username', 'testuser');
469 formHelper.setValue('snmp_v3_auth_password', 'testpass');
470 formHelper.setValue('snmp_v3_priv_password', 'testencrypt');
471 component.onSubmit();
472 expect(cephServiceService.create).toHaveBeenCalledWith({
473 service_type: 'snmp-gateway',
477 snmp_destination: '192.168.20.1:8443',
478 engine_id: '800C53F00000',
479 auth_protocol: 'SHA',
480 privacy_protocol: 'DES',
482 snmp_v3_auth_username: 'testuser',
483 snmp_v3_auth_password: 'testpass',
484 snmp_v3_priv_password: 'testencrypt'
489 it('should submit invalid snmp destination', () => {
490 formHelper.setValue('snmp_version', 'V2c');
491 formHelper.setValue('snmp_destination', '192.168.20.1');
492 formHelper.setValue('snmp_community', 'public');
493 formHelper.expectError('snmp_destination', 'snmpDestinationPattern');
496 it('should submit invalid snmp engine id', () => {
497 formHelper.setValue('snmp_version', 'V3');
498 formHelper.setValue('snmp_destination', '192.168.20.1');
499 formHelper.setValue('engine_id', 'AABBCCDDE');
500 formHelper.setValue('auth_protocol', 'SHA');
501 formHelper.setValue('privacy_protocol', 'DES');
502 formHelper.setValue('snmp_v3_auth_username', 'testuser');
503 formHelper.setValue('snmp_v3_auth_password', 'testpass');
505 formHelper.expectError('engine_id', 'snmpEngineIdPattern');
509 describe('check edit fields', () => {
511 component.editing = true;
514 it('should check whether edit field is correctly loaded', () => {
515 const cephServiceSpy = spyOn(cephServiceService, 'list').and.callThrough();
516 component.ngOnInit();
517 expect(cephServiceSpy).toBeCalledTimes(2);
518 expect(component.action).toBe('Edit');
519 const serviceType = fixture.debugElement.query(By.css('#service_type')).nativeElement;
520 const serviceId = fixture.debugElement.query(By.css('#service_id')).nativeElement;
521 expect(serviceType.disabled).toBeTruthy();
522 expect(serviceId.disabled).toBeTruthy();