]> git.apps.os.sepia.ceph.com Git - ceph.git/blob
322c60f45d0c277b559475252a91327945fe0256
[ceph.git] /
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';
6 import { NgbActiveModal, NgbTypeaheadModule } from '@ng-bootstrap/ng-bootstrap';
7 import _ from 'lodash';
8 import { ToastrModule } from 'ngx-toastr';
9 import { of } from 'rxjs';
10
11 import { CephServiceService } from '~/app/shared/api/ceph-service.service';
12 import { PaginateObservable } from '~/app/shared/api/paginate.model';
13 import { CdFormGroup } from '~/app/shared/forms/cd-form-group';
14 import { SharedModule } from '~/app/shared/shared.module';
15 import { configureTestBed, FormHelper, Mocks } from '~/testing/unit-test-helper';
16 import { ServiceFormComponent } from './service-form.component';
17 import { PoolService } from '~/app/shared/api/pool.service';
18 import { ButtonModule, CheckboxModule, ComboBoxModule, DropdownModule, FileUploaderModule, InputModule, ModalModule, NumberModule, ProgressIndicatorModule, SelectModule } from 'carbon-components-angular';
19 import { Router } from '@angular/router';
20 import { NO_ERRORS_SCHEMA } from '@angular/core';
21
22 // for 'nvmeof' service
23 const mockPools = [
24   Mocks.getPool('pool-1', 1, ['cephfs']),
25   Mocks.getPool('rbd', 2),
26   Mocks.getPool('pool-2', 3)
27 ];
28 class MockPoolService {
29   getList() {
30     return of(mockPools);
31   }
32 }
33
34 describe('ServiceFormComponent', () => {
35   let component: ServiceFormComponent;
36   let fixture: ComponentFixture<ServiceFormComponent>;
37   let cephServiceService: CephServiceService;
38   let form: CdFormGroup;
39   let formHelper: FormHelper;
40   let router: Router;
41   configureTestBed({
42     declarations: [ServiceFormComponent],
43     providers: [NgbActiveModal, { provide: PoolService, useClass: MockPoolService }
44     ],
45     imports: [
46       HttpClientTestingModule,
47       NgbTypeaheadModule,
48       ReactiveFormsModule,
49       RouterTestingModule,
50       SharedModule,
51       ModalModule,
52       InputModule,
53       FileUploaderModule,
54       NumberModule,
55       SelectModule,
56       ButtonModule,
57       ComboBoxModule,
58       DropdownModule,
59       CheckboxModule,
60       ProgressIndicatorModule,
61       ToastrModule.forRoot()
62     ],
63     schemas: [NO_ERRORS_SCHEMA]
64   });
65
66   beforeEach(() => {
67     fixture = TestBed.createComponent(ServiceFormComponent);
68     component = fixture.componentInstance;
69     component.ngOnInit();
70     form = component.serviceForm;
71     formHelper = new FormHelper(form);
72     router = TestBed.inject(Router);
73       Object.defineProperty(router, 'url', {
74         get: jasmine.createSpy('url').and.returnValue('services/(modal:create')
75       });
76     fixture.detectChanges();
77   });
78
79   it('should create', () => {
80     expect(component).toBeTruthy();
81   });
82
83   describe('should test form', () => {
84     beforeEach(() => {
85       cephServiceService = TestBed.inject(CephServiceService);
86       spyOn(cephServiceService, 'create').and.stub();
87     });
88    
89     it('should test placement (host)', () => {
90       formHelper.setValue('service_type', 'crash');
91       formHelper.setValue('placement', 'hosts');
92       formHelper.setValue('hosts', ['mgr0', 'mon0', 'osd0']);
93       formHelper.setValue('count', 2);
94       component.onSubmit();
95       expect(cephServiceService.create).toHaveBeenCalledWith({
96         service_type: 'crash',
97         placement: {
98           hosts: ['mgr0', 'mon0', 'osd0'],
99           count: 2
100         },
101         unmanaged: false
102       });
103     });
104
105     it('should test placement (label)', () => {
106       formHelper.setValue('service_type', 'mgr');
107       formHelper.setValue('placement', 'label');
108       formHelper.setValue('label', 'foo');
109       component.onSubmit();
110       expect(cephServiceService.create).toHaveBeenCalledWith({
111         service_type: 'mgr',
112         placement: {
113           label: 'foo'
114         },
115         unmanaged: false
116       });
117     });
118
119     it('should submit valid count', () => {
120       formHelper.setValue('count', 1);
121       component.onSubmit();
122       formHelper.expectValid('count');
123     });
124
125     // it('should submit invalid count (1)', () => {
126     //   formHelper.setValue('count', 0);
127     //   component.onSubmit();
128     //   formHelper.expectError('count', 'min');
129     // });
130
131     it('should submit invalid count (2)', () => {
132       formHelper.setValue('count', 'abc');
133       component.onSubmit();
134       formHelper.expectError('count', 'pattern');
135     });
136
137     it('should test unmanaged', () => {
138       formHelper.setValue('service_type', 'mgr');
139       formHelper.setValue('service_id', 'svc');
140       formHelper.setValue('placement', 'label');
141       formHelper.setValue('label', 'bar');
142       formHelper.setValue('unmanaged', true);
143       component.onSubmit();
144       expect(cephServiceService.create).toHaveBeenCalledWith({
145         service_type: 'mgr',
146         service_id: 'svc',
147         placement: {},
148         unmanaged: true
149       });
150     });
151
152     it('should test various services', () => {
153       _.forEach(
154         ['alertmanager', 'crash', 'mds', 'mgr', 'mon', 'node-exporter', 'prometheus', 'rbd-mirror'],
155         (serviceType) => {
156           formHelper.setValue('service_type', serviceType);
157           component.onSubmit();
158           expect(cephServiceService.create).toHaveBeenCalledWith({
159             service_type: serviceType,
160             placement: {},
161             unmanaged: false
162           });
163         }
164       );
165     });
166
167     describe('should test service grafana', () => {
168       beforeEach(() => {
169         formHelper.setValue('service_type', 'grafana');
170       });
171
172       it('should sumbit grafana', () => {
173         component.onSubmit();
174         expect(cephServiceService.create).toHaveBeenCalledWith({
175           service_type: 'grafana',
176           placement: {},
177           unmanaged: false,
178           initial_admin_password: null,
179           port: null
180         });
181       });
182
183       it('should sumbit grafana with custom port and initial password', () => {
184         formHelper.setValue('grafana_port', 1234);
185         formHelper.setValue('grafana_admin_password', 'foo');
186         component.onSubmit();
187         expect(cephServiceService.create).toHaveBeenCalledWith({
188           service_type: 'grafana',
189           placement: {},
190           unmanaged: false,
191           initial_admin_password: 'foo',
192           port: 1234
193         });
194       });
195     });
196
197     describe('should test service nfs', () => {
198       beforeEach(() => {
199         formHelper.setValue('service_type', 'nfs');
200       });
201
202       it('should submit nfs', () => {
203         component.onSubmit();
204         expect(cephServiceService.create).toHaveBeenCalledWith({
205           service_type: 'nfs',
206           placement: {},
207           unmanaged: false
208         });
209       });
210     });
211
212     describe('should test service rgw', () => {
213       beforeEach(() => {
214         formHelper.setValue('service_type', 'rgw');
215         formHelper.setValue('service_id', 'svc');
216       });
217
218       it('should test rgw valid service id', () => {
219         formHelper.setValue('service_id', 'svc.realm.zone');
220         formHelper.expectValid('service_id');
221         formHelper.setValue('service_id', 'svc');
222         formHelper.expectValid('service_id');
223       });
224
225       it('should submit rgw with realm, zonegroup and zone', () => {
226         formHelper.setValue('service_id', 'svc');
227         formHelper.setValue('realm_name', 'my-realm');
228         formHelper.setValue('zone_name', 'my-zone');
229         formHelper.setValue('zonegroup_name', 'my-zonegroup');
230         component.onSubmit();
231         expect(cephServiceService.create).toHaveBeenCalledWith({
232           service_type: 'rgw',
233           service_id: 'svc',
234           rgw_realm: 'my-realm',
235           rgw_zone: 'my-zone',
236           rgw_zonegroup: 'my-zonegroup',
237           placement: {},
238           unmanaged: false,
239           ssl: false
240         });
241       });
242
243       it('should submit rgw with port and ssl enabled', () => {
244         formHelper.setValue('rgw_frontend_port', 1234);
245         formHelper.setValue('ssl', true);
246         component.onSubmit();
247         expect(cephServiceService.create).toHaveBeenCalledWith({
248           service_type: 'rgw',
249           service_id: 'svc',
250           rgw_realm: null,
251           rgw_zone: null,
252           rgw_zonegroup: null,
253           placement: {},
254           unmanaged: false,
255           rgw_frontend_port: 1234,
256           rgw_frontend_ssl_certificate: '',
257           ssl: true
258         });
259       });
260
261       it('should submit valid rgw port (1)', () => {
262         formHelper.setValue('rgw_frontend_port', 1);
263         component.onSubmit();
264         formHelper.expectValid('rgw_frontend_port');
265       });
266
267       it('should submit valid rgw port (2)', () => {
268         formHelper.setValue('rgw_frontend_port', 65535);
269         component.onSubmit();
270         formHelper.expectValid('rgw_frontend_port');
271       });
272
273       // it('should submit invalid rgw port (1)', () => {
274       //   formHelper.setValue('rgw_frontend_port', 0);
275       //   fixture.detectChanges();
276       //   formHelper.expectError('rgw_frontend_port', 'min');
277       // });
278
279       // it('should submit invalid rgw port (2)', () => {
280       //   formHelper.setValue('rgw_frontend_port', 65536);
281       //   fixture.detectChanges();
282       //   formHelper.expectError('rgw_frontend_port', 'max');
283       // });
284
285       it('should submit invalid rgw port (3)', () => {
286         formHelper.setValue('rgw_frontend_port', 'abc');
287         component.onSubmit();
288         formHelper.expectError('rgw_frontend_port', 'pattern');
289       });
290
291       it('should submit rgw w/o port', () => {
292         formHelper.setValue('ssl', false);
293         component.onSubmit();
294         expect(cephServiceService.create).toHaveBeenCalledWith({
295           service_type: 'rgw',
296           service_id: 'svc',
297           rgw_realm: null,
298           rgw_zone: null,
299           rgw_zonegroup: null,
300           placement: {},
301           unmanaged: false,
302           ssl: false
303         });
304       });
305
306       it('should not show private key field', () => {
307         formHelper.setValue('ssl', true);
308         fixture.detectChanges();
309         const ssl_key = fixture.debugElement.query(By.css('#ssl_key'));
310         expect(ssl_key).toBeNull();
311       });
312
313       it('should test .pem file', () => {
314         const pemCert = `
315 -----BEGIN CERTIFICATE-----
316 iJ5IbgzlKPssdYwuAEI3yPZxX/g5vKBrgcyD3LttLL/DlElq/1xCnwVrv7WROSNu
317 -----END CERTIFICATE-----
318 -----BEGIN CERTIFICATE-----
319 mn/S7BNBEC7AGe5ajmN+8hBTGdACUXe8rwMNrtTy/MwBZ0VpJsAAjJh+aptZh5yB
320 -----END CERTIFICATE-----
321 -----BEGIN RSA PRIVATE KEY-----
322 x4Ea7kGVgx9kWh5XjWz9wjZvY49UKIT5ppIAWPMbLl3UpfckiuNhTA==
323 -----END RSA PRIVATE KEY-----`;
324         formHelper.setValue('ssl', true);
325         formHelper.setValue('ssl_cert', pemCert);
326         fixture.detectChanges();
327         formHelper.expectValid('ssl_cert');
328       });
329     });
330
331     describe('should test service iscsi', () => {
332       beforeEach(() => {
333         formHelper.setValue('service_type', 'iscsi');
334         formHelper.setValue('pool', 'xyz');
335         formHelper.setValue('api_user', 'user');
336         formHelper.setValue('api_password', 'password');
337         formHelper.setValue('ssl', false);
338       });
339
340       it('should submit iscsi', () => {
341         component.onSubmit();
342         expect(cephServiceService.create).toHaveBeenCalledWith({
343           service_type: 'iscsi',
344           placement: {},
345           unmanaged: false,
346           pool: 'xyz',
347           api_user: 'user',
348           api_password: 'password',
349           api_secure: false
350         });
351       });
352
353       it('should submit iscsi with trusted ips', () => {
354         formHelper.setValue('ssl', true);
355         formHelper.setValue('trusted_ip_list', ' 172.16.0.5, 192.1.1.10  ');
356         component.onSubmit();
357         expect(cephServiceService.create).toHaveBeenCalledWith({
358           service_type: 'iscsi',
359           placement: {},
360           unmanaged: false,
361           pool: 'xyz',
362           api_user: 'user',
363           api_password: 'password',
364           api_secure: true,
365           ssl_cert: '',
366           ssl_key: '',
367           trusted_ip_list: '172.16.0.5, 192.1.1.10'
368         });
369       });
370
371       it('should submit iscsi with port', () => {
372         formHelper.setValue('api_port', 456);
373         component.onSubmit();
374         expect(cephServiceService.create).toHaveBeenCalledWith({
375           service_type: 'iscsi',
376           placement: {},
377           unmanaged: false,
378           pool: 'xyz',
379           api_user: 'user',
380           api_password: 'password',
381           api_secure: false,
382           api_port: 456
383         });
384       });
385
386       it('should submit valid iscsi port (1)', () => {
387         formHelper.setValue('api_port', 1);
388         component.onSubmit();
389         formHelper.expectValid('api_port');
390       });
391
392       it('should submit valid iscsi port (2)', () => {
393         formHelper.setValue('api_port', 65535);
394         component.onSubmit();
395         formHelper.expectValid('api_port');
396       });
397
398       // it('should submit invalid iscsi port (1)', () => {
399       //   formHelper.setValue('api_port', 0);
400       //   fixture.detectChanges();
401       //   formHelper.expectError('api_port', 'min');
402       // });
403
404       // it('should submit invalid iscsi port (2)', () => {
405       //   formHelper.setValue('api_port', 65536);
406       //   fixture.detectChanges();
407       //   formHelper.expectError('api_port', 'max');
408       // });
409
410       // it('should submit invalid iscsi port (3)', () => {
411       //   formHelper.setValue('api_port', 'abc');
412       //   component.onSubmit();
413       //   formHelper.expectError('api_port', 'pattern');
414       // });
415
416       // it('should throw error when there is no pool', () => {
417       //   formHelper.expectErrorChange('pool', '', 'required');
418       // });
419     });
420
421     describe('should test service nvmeof', () => {
422       beforeEach(() => {
423         component.serviceType = 'nvmeof';
424         formHelper.setValue('service_type', 'nvmeof');
425         component.ngOnInit();
426         fixture.detectChanges();
427       });
428
429       it('should set rbd pools correctly onInit', () => {
430         expect(component.pools.length).toBe(3);
431         expect(component.rbdPools.length).toBe(2);
432       });
433
434       it('should set default values correctly onInit', () => {
435         expect(form.get('service_type').value).toBe('nvmeof');
436         expect(form.get('group').value).toBe('default');
437         expect(form.get('pool').value).toBe('rbd');
438         expect(form.get('service_id').value).toBe('rbd.default');
439       });
440
441       it('should reflect correct values on group change', () => {
442         // Initially the group value should be 'default'
443         expect(component.serviceForm.get('group')?.value).toBe('default');
444         const groupInput = fixture.debugElement.query(By.css('#group')).nativeElement;
445         // Simulate input value change
446         groupInput.value = 'foo';
447         // Trigger the input event
448         groupInput.dispatchEvent(new Event('input'));
449         // Trigger the change event
450         groupInput.dispatchEvent(new Event('change'));
451         fixture.detectChanges();
452         // Verify values after change
453         expect(form.get('group').value).toBe('foo');
454         expect(form.get('service_id').value).toBe('rbd.foo');
455       });
456
457       it('should reflect correct values on pool change', () => {
458         // Initially the pool value should be 'rbd'
459         expect(component.serviceForm.get('pool')?.value).toBe('rbd');
460         const poolInput = fixture.debugElement.query(By.css('#pool')).nativeElement;
461         // Simulate input value change
462         poolInput.value = 'pool-2';
463         // Trigger the input event
464         poolInput.dispatchEvent(new Event('input'));
465         // Trigger the change event
466         poolInput.dispatchEvent(new Event('change'));
467         fixture.detectChanges();
468         // Verify values after change
469         expect(component.serviceForm.getValue('pool').value).equals('pool-2');
470         expect(component.serviceForm.getValue('service_id')).equals('pool-2.default');
471       });
472
473       it('should throw error when there is no service id', () => {
474         formHelper.expectErrorChange('service_id', '', 'required');
475       });
476
477       it('should throw error when there is no pool', () => {
478         formHelper.expectErrorChange('pool', '', 'required');
479       });
480
481       it('should throw error when there is no group', () => {
482         formHelper.expectErrorChange('group', '', 'required');
483       });
484
485       it('should hide the count element when service_type is "nvmeof"', () => {
486         const countEl = fixture.debugElement.query(By.css('#count'));
487         expect(countEl).toBeNull();
488       });
489
490       it('should not show certs and keys field with mTLS disabled', () => {
491         formHelper.setValue('ssl', true);
492         fixture.detectChanges();
493         const root_ca_cert = fixture.debugElement.query(By.css('#root_ca_cert'));
494         const client_cert = fixture.debugElement.query(By.css('#client_cert'));
495         const client_key = fixture.debugElement.query(By.css('#client_key'));
496         const server_cert = fixture.debugElement.query(By.css('#server_cert'));
497         const server_key = fixture.debugElement.query(By.css('#server_key'));
498         expect(root_ca_cert).toBeNull();
499         expect(client_cert).toBeNull();
500         expect(client_key).toBeNull();
501         expect(server_cert).toBeNull();
502         expect(server_key).toBeNull();
503       });
504
505       it('should submit nvmeof without mTLS', () => {
506         component.onSubmit();
507         expect(cephServiceService.create).toHaveBeenCalledWith({
508           service_type: 'nvmeof',
509           service_id: 'rbd.default',
510           placement: {},
511           unmanaged: false,
512           pool: 'rbd',
513           group: 'default',
514           enable_auth: false
515         });
516       });
517
518       it('should submit nvmeof with mTLS', () => {
519         formHelper.setValue('enable_mtls', true);
520         formHelper.setValue('root_ca_cert', 'root_ca_cert');
521         formHelper.setValue('client_cert', 'client_cert');
522         formHelper.setValue('client_key', 'client_key');
523         formHelper.setValue('server_cert', 'server_cert');
524         formHelper.setValue('server_key', 'server_key');
525         component.onSubmit();
526         expect(cephServiceService.create).toHaveBeenCalledWith({
527           service_type: 'nvmeof',
528           service_id: 'rbd.default',
529           placement: {},
530           unmanaged: false,
531           pool: 'rbd',
532           group: 'default',
533           enable_auth: true,
534           root_ca_cert: 'root_ca_cert',
535           client_cert: 'client_cert',
536           client_key: 'client_key',
537           server_cert: 'server_cert',
538           server_key: 'server_key'
539         });
540       });
541     });
542
543     describe('should test service smb', () => {
544       beforeEach(() => {
545         formHelper.setValue('service_type', 'smb');
546         formHelper.setValue('service_id', 'foo');
547         formHelper.setValue('cluster_id', 'cluster_foo');
548         formHelper.setValue('config_uri', 'rados://.smb/foo/scc.toml');
549       });
550
551       it('should submit smb', () => {
552         component.onSubmit();
553         expect(cephServiceService.create).toHaveBeenCalledWith({
554           service_type: 'smb',
555           placement: {},
556           unmanaged: false,
557           service_id: 'foo',
558           cluster_id: 'cluster_foo',
559           config_uri: 'rados://.smb/foo/scc.toml'
560         });
561       });
562     });
563
564     describe('should test service ingress', () => {
565       beforeEach(() => {
566         formHelper.setValue('service_type', 'ingress');
567         formHelper.setValue('backend_service', 'rgw.foo');
568         formHelper.setValue('virtual_ip', '192.168.20.1/24');
569         formHelper.setValue('ssl', false);
570       });
571
572       it('should submit ingress', () => {
573         component.onSubmit();
574         expect(cephServiceService.create).toHaveBeenCalledWith({
575           service_type: 'ingress',
576           placement: {},
577           unmanaged: false,
578           backend_service: 'rgw.foo',
579           service_id: 'rgw.foo',
580           virtual_ip: '192.168.20.1/24',
581           virtual_interface_networks: null,
582           ssl: false
583         });
584       });
585
586       it('should pre-populate the service id', () => {
587         component.prePopulateId();
588         const prePopulatedID = component.serviceForm.getValue('service_id');
589         expect(prePopulatedID).toBe('rgw.foo');
590       });
591
592       it('should submit valid frontend and monitor port', () => {
593         // min value
594         formHelper.setValue('frontend_port', 1);
595         formHelper.setValue('monitor_port', 1);
596         fixture.detectChanges();
597         formHelper.expectValid('frontend_port');
598         formHelper.expectValid('monitor_port');
599
600         // max value
601         formHelper.setValue('frontend_port', 65535);
602         formHelper.setValue('monitor_port', 65535);
603         fixture.detectChanges();
604         formHelper.expectValid('frontend_port');
605         formHelper.expectValid('monitor_port');
606       });
607
608       // it('should submit invalid frontend and monitor port', () => {
609       //   // min
610       //   formHelper.setValue('frontend_port', 0);
611       //   formHelper.setValue('monitor_port', 0);
612       //   fixture.detectChanges();
613       //   formHelper.expectError('frontend_port', 'min');
614       //   formHelper.expectError('monitor_port', 'min');
615
616       //   // max
617       //   formHelper.setValue('frontend_port', 65536);
618       //   formHelper.setValue('monitor_port', 65536);
619       //   fixture.detectChanges();
620       //   formHelper.expectError('frontend_port', 'max');
621       //   formHelper.expectError('monitor_port', 'max');
622
623       //   // pattern
624       //   formHelper.setValue('frontend_port', 'abc');
625       //   formHelper.setValue('monitor_port', 'abc');
626       //   component.onSubmit();
627       //   formHelper.expectError('frontend_port', 'pattern');
628       //   formHelper.expectError('monitor_port', 'pattern');
629       // });
630
631       it('should not show private key field with ssl enabled', () => {
632         formHelper.setValue('ssl', true);
633         fixture.detectChanges();
634         const ssl_key = fixture.debugElement.query(By.css('#ssl_key'));
635         expect(ssl_key).toBeNull();
636       });
637
638       it('should test .pem file with ssl enabled', () => {
639         const pemCert = `
640 -----BEGIN CERTIFICATE-----
641 iJ5IbgzlKPssdYwuAEI3yPZxX/g5vKBrgcyD3LttLL/DlElq/1xCnwVrv7WROSNu
642 -----END CERTIFICATE-----
643 -----BEGIN CERTIFICATE-----
644 mn/S7BNBEC7AGe5ajmN+8hBTGdACUXe8rwMNrtTy/MwBZ0VpJsAAjJh+aptZh5yB
645 -----END CERTIFICATE-----
646 -----BEGIN RSA PRIVATE KEY-----
647 x4Ea7kGVgx9kWh5XjWz9wjZvY49UKIT5ppIAWPMbLl3UpfckiuNhTA==
648 -----END RSA PRIVATE KEY-----`;
649         formHelper.setValue('ssl', true);
650         formHelper.setValue('ssl_cert', pemCert);
651         fixture.detectChanges();
652         formHelper.expectValid('ssl_cert');
653       });
654     });
655
656     describe('should test service snmp-gateway', () => {
657       beforeEach(() => {
658         formHelper.setValue('service_type', 'snmp-gateway');
659         formHelper.setValue('snmp_destination', '192.168.20.1:8443');
660       });
661
662       it('should test snmp-gateway service with V2c', () => {
663         formHelper.setValue('snmp_version', 'V2c');
664         formHelper.setValue('snmp_community', 'public');
665         component.onSubmit();
666         expect(cephServiceService.create).toHaveBeenCalledWith({
667           service_type: 'snmp-gateway',
668           placement: {},
669           unmanaged: false,
670           snmp_version: 'V2c',
671           snmp_destination: '192.168.20.1:8443',
672           credentials: {
673             snmp_community: 'public'
674           }
675         });
676       });
677
678       it('should test snmp-gateway service with V3', () => {
679         formHelper.setValue('snmp_version', 'V3');
680         formHelper.setValue('engine_id', '800C53F00000');
681         formHelper.setValue('auth_protocol', 'SHA');
682         formHelper.setValue('privacy_protocol', 'DES');
683         formHelper.setValue('snmp_v3_auth_username', 'testuser');
684         formHelper.setValue('snmp_v3_auth_password', 'testpass');
685         formHelper.setValue('snmp_v3_priv_password', 'testencrypt');
686         component.onSubmit();
687         expect(cephServiceService.create).toHaveBeenCalledWith({
688           service_type: 'snmp-gateway',
689           placement: {},
690           unmanaged: false,
691           snmp_version: 'V3',
692           snmp_destination: '192.168.20.1:8443',
693           engine_id: '800C53F00000',
694           auth_protocol: 'SHA',
695           privacy_protocol: 'DES',
696           credentials: {
697             snmp_v3_auth_username: 'testuser',
698             snmp_v3_auth_password: 'testpass',
699             snmp_v3_priv_password: 'testencrypt'
700           }
701         });
702       });
703
704       it('should submit invalid snmp destination', () => {
705         formHelper.setValue('snmp_version', 'V2c');
706         formHelper.setValue('snmp_destination', '192.168.20.1');
707         formHelper.setValue('snmp_community', 'public');
708         formHelper.expectError('snmp_destination', 'snmpDestinationPattern');
709       });
710
711       it('should submit invalid snmp engine id', () => {
712         formHelper.setValue('snmp_version', 'V3');
713         formHelper.setValue('snmp_destination', '192.168.20.1');
714         formHelper.setValue('engine_id', 'AABBCCDDE');
715         formHelper.setValue('auth_protocol', 'SHA');
716         formHelper.setValue('privacy_protocol', 'DES');
717         formHelper.setValue('snmp_v3_auth_username', 'testuser');
718         formHelper.setValue('snmp_v3_auth_password', 'testpass');
719
720         formHelper.expectError('engine_id', 'snmpEngineIdPattern');
721       });
722     });
723
724     describe('should test service oauth2-proxy',()=>{
725       beforeEach(() => {
726         formHelper.setValue('service_type', 'oauth2-proxy');
727         formHelper.setValue('count', '1');
728         formHelper.setValue('provider_display_name', 'My OIDC provider');
729         formHelper.setValue('allowlist_domains', 'test:value');
730       });
731       it('should throw error when there is no client secret', () => {
732         component.onSubmit();
733         formHelper.expectErrorChange('client_secret', '', 'required');
734       });
735       it('should throw error when there is no oidc issuer url', () => {
736         component.onSubmit();
737         formHelper.expectErrorChange('oidc_issuer_url', '', 'required');
738       });   
739     })
740     describe('should test service mgmt-gateway',()=>{
741       beforeEach(() => {
742         formHelper.setValue('service_type', 'mgmt-gateway');
743         formHelper.setValue('port', '443');
744         formHelper.setValue('service_id', 'mgmt-gateway');
745       });
746       it('should submit when service type is mgmt-gateway', () => {
747         component.onSubmit();
748         expect(cephServiceService.create).toHaveBeenCalledWith({
749           enable_auth: null,
750           placement: {},
751           service_type: "mgmt-gateway",
752           ssl_certificate: "",
753           ssl_certificate_key:"",
754           ssl_ciphers: undefined,
755           port: "443",
756           ssl_protocols: [],
757           unmanaged: false
758         });
759       });  
760     })
761     describe('should test service mds', () => {
762       beforeEach(() => {
763         formHelper.setValue('service_type', 'mds');
764         const paginate_obs = new PaginateObservable<any>(of({}));
765         spyOn(cephServiceService, 'list').and.returnValue(paginate_obs);
766       });
767
768       // it('should test mds valid service id', () => {
769       //   formHelper.setValue('service_id', 'svc123');
770       //   formHelper.expectValid('service_id');
771       //   formHelper.setValue('service_id', 'svc_id-1');
772       //   formHelper.expectValid('service_id');
773       // });
774
775       // it('should test mds invalid service id', () => {
776       //   formHelper.setValue('service_id', '123');
777       //   formHelper.expectError('service_id', 'mdsPattern');
778       //   formHelper.setValue('service_id', '123svc');
779       //   formHelper.expectError('service_id', 'mdsPattern');
780       //   formHelper.setValue('service_id', 'svc#1');
781       //   formHelper.expectError('service_id', 'mdsPattern');
782       // });
783     });
784
785     describe('check edit fields', () => {
786       beforeEach(() => {
787         component.editing = true;
788       });
789
790       // it('should check whether edit field is correctly loaded', () => {
791       //   const paginate_obs = new PaginateObservable<any>(of({}));
792       //   const cephServiceSpy = spyOn(cephServiceService, 'list').and.returnValue(paginate_obs);
793       //   component.ngOnInit();
794       //   expect(cephServiceSpy).toBeCalledTimes(2);
795       //   expect(component.action).toBe('Edit');
796       //   const serviceType = fixture.debugElement.query(By.css('#service_type')).nativeElement;
797       //   const serviceId = fixture.debugElement.query(By.css('#service_id')).nativeElement;
798       //   expect(serviceType.disabled).toBeTruthy();
799       //   expect(serviceId.disabled).toBeTruthy();
800       // });
801
802       it('should not edit pools for nvmeof service', () => {
803         component.serviceType = 'nvmeof';
804         formHelper.setValue('service_type', 'nvmeof');
805         component.ngOnInit();
806         fixture.detectChanges();
807         expect(component.serviceForm.get('pool').disabled).toBe(true);
808       });
809
810       it('should not edit groups for nvmeof service', () => {
811         component.serviceType = 'nvmeof';
812         formHelper.setValue('service_type', 'nvmeof');
813         component.ngOnInit();
814         fixture.detectChanges();
815         const groupId = fixture.debugElement.query(By.css('#group')).nativeElement;
816         expect(groupId.disabled).toBeTruthy();
817       });
818
819       it('should update nvmeof service to disable mTLS', () => {
820         spyOn(cephServiceService, 'update').and.stub();
821         component.serviceType = 'nvmeof';
822         formHelper.setValue('service_type', 'nvmeof');
823         formHelper.setValue('pool', 'rbd');
824         formHelper.setValue('group', 'default');
825         // mTLS disabled
826         formHelper.setValue('enable_mtls', false);
827         component.onSubmit();
828         expect(cephServiceService.update).toHaveBeenCalledWith({
829           service_type: 'nvmeof',
830           placement: {},
831           unmanaged: false,
832           pool: 'rbd',
833           group: 'default',
834           enable_auth: false
835         });
836       });
837     });
838     describe("test cases for clearValidations",() => {
839       it('should call clearValidations when snmp_version is v3',()=>{
840         formHelper.setValue('snmp_version', 'V3');
841         formHelper.setValue('privacy_protocol', 'DES');
842         component.clearValidations();
843         fixture.detectChanges();
844         fixture.whenStable().then(() => {
845             expect(component.serviceForm.get('snmp_community').value).to.be(null);
846         })
847       })
848       it('should call clearValidations when snmp_version is v2',()=>{
849         formHelper.setValue('snmp_version', 'V2');
850         formHelper.setValue('privacy_protocol', 'DES');
851         component.clearValidations();
852         fixture.detectChanges();
853         fixture.whenStable().then(() => {
854         expect(form.get('engine_id').value).equals(null);
855         expect(form.get('auth_protocol').value).equals(null);
856         expect(form.get('privacy_protocol').value).equals(null);
857         expect(form.get('snmp_v3_auth_username').value).equals(null);
858         expect(form.get('snmp_v3_auth_password').value).equals(null);
859         })
860       })
861     })
862     
863     // it('should call resolveRoute',()=>{
864     //   spyOn(router , "url");
865     //   Object.defineProperty(router, 'url', {
866     //     get: jasmine.createSpy('url').and.returnValue('services/(modal:create')
867     //   });
868
869     //   // it(`editTemplate() should navigate to template build module with query params`, inject(
870     //     // [Router],
871     //     // (router: Router) => {
872     //       let id = 25;
873     //       spyOn(router, "navigate").and.stub();
874     //       router.navigate(["services/(modal:create)"], {
875     //         queryParams: { templateId: id }
876     //       });
877     //       expect(router.navigate).toHaveBeenCalledWith(["services/(modal:create)"], {
878     //         queryParams: { templateId: id }
879     //       });
880     //     // }
881     //   // ));
882     //   // const req2 = httpTesting.expectOne({
883     //   //   url: 'services/(modal:create)',
884     //   //   method: 'PUT'
885     //   // });
886     //   // expect(req2.request.body).toEqual({
887     //   //   config: {}
888     //   // });
889     //   // req2.flush({});
890     //   // expect(router.url).toBe('/');
891     // })
892     // it('should test closeModal', () => {
893     //   spyOn(component, 'closeModal').and.callThrough();
894     //   component.closeModal();
895     //   expect(component.closeModal).to.tru
896     // });
897   });
898 });