]> git.apps.os.sepia.ceph.com Git - ceph.git/blob
6ed8b414532ab22050f8f93a6a56746732f71672
[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 { RouterTestingModule } from '@angular/router/testing';
5
6 import { NgbTypeaheadModule } from '@ng-bootstrap/ng-bootstrap';
7 import _ from 'lodash';
8 import { ToastrModule } from 'ngx-toastr';
9
10 import { CephServiceService } from '~/app/shared/api/ceph-service.service';
11 import { CdFormGroup } from '~/app/shared/forms/cd-form-group';
12 import { SharedModule } from '~/app/shared/shared.module';
13 import { configureTestBed, FormHelper } from '~/testing/unit-test-helper';
14 import { ServiceFormComponent } from './service-form.component';
15
16 describe('ServiceFormComponent', () => {
17   let component: ServiceFormComponent;
18   let fixture: ComponentFixture<ServiceFormComponent>;
19   let cephServiceService: CephServiceService;
20   let form: CdFormGroup;
21   let formHelper: FormHelper;
22
23   configureTestBed({
24     declarations: [ServiceFormComponent],
25     imports: [
26       HttpClientTestingModule,
27       NgbTypeaheadModule,
28       ReactiveFormsModule,
29       RouterTestingModule,
30       SharedModule,
31       ToastrModule.forRoot()
32     ]
33   });
34
35   beforeEach(() => {
36     fixture = TestBed.createComponent(ServiceFormComponent);
37     component = fixture.componentInstance;
38     form = component.serviceForm;
39     formHelper = new FormHelper(form);
40     fixture.detectChanges();
41   });
42
43   it('should create', () => {
44     expect(component).toBeTruthy();
45   });
46
47   describe('should test form', () => {
48     beforeEach(() => {
49       cephServiceService = TestBed.inject(CephServiceService);
50       spyOn(cephServiceService, 'create').and.stub();
51     });
52
53     it('should test placement (host)', () => {
54       formHelper.setValue('service_type', 'crash');
55       formHelper.setValue('placement', 'hosts');
56       formHelper.setValue('hosts', ['mgr0', 'mon0', 'osd0']);
57       formHelper.setValue('count', 2);
58       component.onSubmit();
59       expect(cephServiceService.create).toHaveBeenCalledWith({
60         service_type: 'crash',
61         placement: {
62           hosts: ['mgr0', 'mon0', 'osd0'],
63           count: 2
64         },
65         unmanaged: false
66       });
67     });
68
69     it('should test placement (label)', () => {
70       formHelper.setValue('service_type', 'mgr');
71       formHelper.setValue('placement', 'label');
72       formHelper.setValue('label', 'foo');
73       component.onSubmit();
74       expect(cephServiceService.create).toHaveBeenCalledWith({
75         service_type: 'mgr',
76         placement: {
77           label: 'foo'
78         },
79         unmanaged: false
80       });
81     });
82
83     it('should submit valid count', () => {
84       formHelper.setValue('count', 1);
85       component.onSubmit();
86       formHelper.expectValid('count');
87     });
88
89     it('should submit invalid count (1)', () => {
90       formHelper.setValue('count', 0);
91       component.onSubmit();
92       formHelper.expectError('count', 'min');
93     });
94
95     it('should submit invalid count (2)', () => {
96       formHelper.setValue('count', 'abc');
97       component.onSubmit();
98       formHelper.expectError('count', 'pattern');
99     });
100
101     it('should test unmanaged', () => {
102       formHelper.setValue('service_type', 'rgw');
103       formHelper.setValue('placement', 'label');
104       formHelper.setValue('label', 'bar');
105       formHelper.setValue('rgw_frontend_port', 4567);
106       formHelper.setValue('unmanaged', true);
107       component.onSubmit();
108       expect(cephServiceService.create).toHaveBeenCalledWith({
109         service_type: 'rgw',
110         placement: {},
111         unmanaged: true
112       });
113     });
114
115     it('should test various services', () => {
116       _.forEach(
117         [
118           'alertmanager',
119           'crash',
120           'grafana',
121           'mds',
122           'mgr',
123           'mon',
124           'node-exporter',
125           'prometheus',
126           'rbd-mirror'
127         ],
128         (serviceType) => {
129           formHelper.setValue('service_type', serviceType);
130           component.onSubmit();
131           expect(cephServiceService.create).toHaveBeenCalledWith({
132             service_type: serviceType,
133             placement: {},
134             unmanaged: false
135           });
136         }
137       );
138     });
139
140     describe('should test service nfs', () => {
141       beforeEach(() => {
142         formHelper.setValue('service_type', 'nfs');
143         formHelper.setValue('pool', 'foo');
144       });
145
146       it('should submit nfs with namespace', () => {
147         formHelper.setValue('namespace', 'bar');
148         component.onSubmit();
149         expect(cephServiceService.create).toHaveBeenCalledWith({
150           service_type: 'nfs',
151           placement: {},
152           unmanaged: false,
153           pool: 'foo',
154           namespace: 'bar'
155         });
156       });
157
158       it('should submit nfs w/o namespace', () => {
159         component.onSubmit();
160         expect(cephServiceService.create).toHaveBeenCalledWith({
161           service_type: 'nfs',
162           placement: {},
163           unmanaged: false,
164           pool: 'foo'
165         });
166       });
167     });
168
169     describe('should test service rgw', () => {
170       beforeEach(() => {
171         formHelper.setValue('service_type', 'rgw');
172       });
173
174       it('should test rgw valid service id', () => {
175         formHelper.setValue('service_id', 'foo.bar');
176         formHelper.expectValid('service_id');
177         formHelper.setValue('service_id', 'foo.bar.bas');
178         formHelper.expectValid('service_id');
179       });
180
181       it('should test rgw invalid service id', () => {
182         formHelper.setValue('service_id', 'foo');
183         formHelper.expectError('service_id', 'rgwPattern');
184         formHelper.setValue('service_id', 'foo.');
185         formHelper.expectError('service_id', 'rgwPattern');
186         formHelper.setValue('service_id', 'foo.bar.');
187         formHelper.expectError('service_id', 'rgwPattern');
188         formHelper.setValue('service_id', 'foo.bar.bas.');
189         formHelper.expectError('service_id', 'rgwPattern');
190       });
191
192       it('should submit rgw with port', () => {
193         formHelper.setValue('rgw_frontend_port', 1234);
194         formHelper.setValue('ssl', true);
195         component.onSubmit();
196         expect(cephServiceService.create).toHaveBeenCalledWith({
197           service_type: 'rgw',
198           placement: {},
199           unmanaged: false,
200           rgw_frontend_port: 1234,
201           rgw_frontend_ssl_certificate: '',
202           rgw_frontend_ssl_key: '',
203           ssl: true
204         });
205       });
206
207       it('should submit valid rgw port (1)', () => {
208         formHelper.setValue('rgw_frontend_port', 1);
209         component.onSubmit();
210         formHelper.expectValid('rgw_frontend_port');
211       });
212
213       it('should submit valid rgw port (2)', () => {
214         formHelper.setValue('rgw_frontend_port', 65535);
215         component.onSubmit();
216         formHelper.expectValid('rgw_frontend_port');
217       });
218
219       it('should submit invalid rgw port (1)', () => {
220         formHelper.setValue('rgw_frontend_port', 0);
221         component.onSubmit();
222         formHelper.expectError('rgw_frontend_port', 'min');
223       });
224
225       it('should submit invalid rgw port (2)', () => {
226         formHelper.setValue('rgw_frontend_port', 65536);
227         component.onSubmit();
228         formHelper.expectError('rgw_frontend_port', 'max');
229       });
230
231       it('should submit invalid rgw port (3)', () => {
232         formHelper.setValue('rgw_frontend_port', 'abc');
233         component.onSubmit();
234         formHelper.expectError('rgw_frontend_port', 'pattern');
235       });
236
237       it('should submit rgw w/o port', () => {
238         formHelper.setValue('ssl', false);
239         component.onSubmit();
240         expect(cephServiceService.create).toHaveBeenCalledWith({
241           service_type: 'rgw',
242           placement: {},
243           unmanaged: false,
244           ssl: false
245         });
246       });
247     });
248
249     describe('should test service iscsi', () => {
250       beforeEach(() => {
251         formHelper.setValue('service_type', 'iscsi');
252         formHelper.setValue('pool', 'xyz');
253         formHelper.setValue('api_user', 'user');
254         formHelper.setValue('api_password', 'password');
255         formHelper.setValue('ssl', false);
256       });
257
258       it('should submit iscsi', () => {
259         component.onSubmit();
260         expect(cephServiceService.create).toHaveBeenCalledWith({
261           service_type: 'iscsi',
262           placement: {},
263           unmanaged: false,
264           pool: 'xyz',
265           api_user: 'user',
266           api_password: 'password',
267           api_secure: false
268         });
269       });
270
271       it('should submit iscsi with trusted ips', () => {
272         formHelper.setValue('ssl', true);
273         formHelper.setValue('trusted_ip_list', ' 172.16.0.5, 192.1.1.10  ');
274         component.onSubmit();
275         expect(cephServiceService.create).toHaveBeenCalledWith({
276           service_type: 'iscsi',
277           placement: {},
278           unmanaged: false,
279           pool: 'xyz',
280           api_user: 'user',
281           api_password: 'password',
282           api_secure: true,
283           ssl_cert: '',
284           ssl_key: '',
285           trusted_ip_list: '172.16.0.5, 192.1.1.10'
286         });
287       });
288
289       it('should submit iscsi with port', () => {
290         formHelper.setValue('api_port', 456);
291         component.onSubmit();
292         expect(cephServiceService.create).toHaveBeenCalledWith({
293           service_type: 'iscsi',
294           placement: {},
295           unmanaged: false,
296           pool: 'xyz',
297           api_user: 'user',
298           api_password: 'password',
299           api_secure: false,
300           api_port: 456
301         });
302       });
303
304       it('should submit valid iscsi port (1)', () => {
305         formHelper.setValue('api_port', 1);
306         component.onSubmit();
307         formHelper.expectValid('api_port');
308       });
309
310       it('should submit valid iscsi port (2)', () => {
311         formHelper.setValue('api_port', 65535);
312         component.onSubmit();
313         formHelper.expectValid('api_port');
314       });
315
316       it('should submit invalid iscsi port (1)', () => {
317         formHelper.setValue('api_port', 0);
318         component.onSubmit();
319         formHelper.expectError('api_port', 'min');
320       });
321
322       it('should submit invalid iscsi port (2)', () => {
323         formHelper.setValue('api_port', 65536);
324         component.onSubmit();
325         formHelper.expectError('api_port', 'max');
326       });
327
328       it('should submit invalid iscsi port (3)', () => {
329         formHelper.setValue('api_port', 'abc');
330         component.onSubmit();
331         formHelper.expectError('api_port', 'pattern');
332       });
333     });
334   });
335 });