1 import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
2 import { ComponentFixture, TestBed } from '@angular/core/testing';
3 import { ReactiveFormsModule } from '@angular/forms';
4 import { ActivatedRoute } from '@angular/router';
5 import { RouterTestingModule } from '@angular/router/testing';
7 import { ToastrModule } from 'ngx-toastr';
9 import { ActivatedRouteStub } from '../../../../testing/activated-route-stub';
15 } from '../../../../testing/unit-test-helper';
16 import { CdFormGroup } from '../../../shared/forms/cd-form-group';
17 import { SharedModule } from '../../../shared/shared.module';
18 import { IscsiTargetFormComponent } from './iscsi-target-form.component';
20 describe('IscsiTargetFormComponent', () => {
21 let component: IscsiTargetFormComponent;
22 let fixture: ComponentFixture<IscsiTargetFormComponent>;
23 let httpTesting: HttpTestingController;
24 let activatedRoute: ActivatedRouteStub;
27 config: { minimum_gateways: 2 },
28 disk_default_controls: {
37 target_default_controls: {
42 required_rbd_features: {
46 unsupported_rbd_features: {
50 backstores: ['backstore:1', 'backstore:2'],
51 default_backstore: 'backstore:1',
55 const LIST_TARGET: any[] = [
57 target_iqn: 'iqn.2003-01.com.redhat.iscsi-gw:iscsi-igw',
58 portals: [{ host: 'node1', ip: '192.168.100.201' }],
64 backstore: 'backstore:1',
65 wwn: '64af6678-9694-4367-bacc-f8eb0baa'
70 client_iqn: 'iqn.1994-05.com.redhat:rh7-client',
71 luns: [{ pool: 'rbd', image: 'disk_1', lun: 0 }],
73 user: 'myiscsiusername',
74 password: 'myiscsipassword',
86 { name: 'node1', ip_addresses: ['192.168.100.201', '10.0.2.15'] },
87 { name: 'node2', ip_addresses: ['192.168.100.202'] }
91 ceph_iscsi_config_version: 11
94 const RBD_LIST: any[] = [
95 { status: 0, value: [], pool_name: 'ganesha' },
104 block_name_prefix: 'rbd_data.148162fb31a8',
109 features_name: ['deep-flatten', 'exclusive-lock', 'fast-diff', 'layering', 'object-map'],
110 timestamp: '2019-01-18T10:44:26Z',
112 stripe_unit: 4194304,
124 block_name_prefix: 'rbd_data.14b292cee6cb',
129 features_name: ['deep-flatten', 'exclusive-lock', 'fast-diff', 'layering', 'object-map'],
130 timestamp: '2019-01-18T10:45:56Z',
132 stripe_unit: 4194304,
146 declarations: [IscsiTargetFormComponent],
150 HttpClientTestingModule,
152 ToastrModule.forRoot()
157 provide: ActivatedRoute,
158 useValue: new ActivatedRouteStub({ target_iqn: undefined })
166 fixture = TestBed.createComponent(IscsiTargetFormComponent);
167 component = fixture.componentInstance;
168 httpTesting = TestBed.get(HttpTestingController);
169 activatedRoute = TestBed.get(ActivatedRoute);
170 fixture.detectChanges();
172 httpTesting.expectOne('ui-api/iscsi/settings').flush(SETTINGS);
173 httpTesting.expectOne('ui-api/iscsi/portals').flush(PORTALS);
174 httpTesting.expectOne('ui-api/iscsi/version').flush(VERSION);
175 httpTesting.expectOne('api/block/image').flush(RBD_LIST);
176 httpTesting.expectOne('api/iscsi/target').flush(LIST_TARGET);
177 httpTesting.verify();
180 it('should create', () => {
181 expect(component).toBeTruthy();
184 it('should only show images not used in other targets', () => {
185 expect(component.imagesAll).toEqual([RBD_LIST[1]['value'][1]]);
186 expect(component.imagesSelections).toEqual([
187 { description: '', name: 'rbd/disk_2', selected: false, enabled: true }
191 it('should generate portals selectOptions', () => {
192 expect(component.portalsSelections).toEqual([
193 { description: '', name: 'node1:192.168.100.201', selected: false, enabled: true },
194 { description: '', name: 'node1:10.0.2.15', selected: false, enabled: true },
195 { description: '', name: 'node2:192.168.100.202', selected: false, enabled: true }
199 it('should create the form', () => {
200 expect(component.targetForm.value).toEqual({
213 target_iqn: component.targetForm.value.target_iqn
217 it('should prepare data when selecting an image', () => {
218 expect(component.imagesSettings).toEqual({});
219 component.onImageSelection({ option: { name: 'rbd/disk_2', selected: true } });
220 expect(component.imagesSettings).toEqual({
223 backstore: 'backstore:1',
229 it('should clean data when removing an image', () => {
230 component.onImageSelection({ option: { name: 'rbd/disk_2', selected: true } });
231 component.addGroup();
232 component.groups.controls[0].patchValue({
234 disks: ['rbd/disk_2']
237 expect(component.groups.controls[0].value).toEqual({
238 disks: ['rbd/disk_2'],
243 component.onImageSelection({ option: { name: 'rbd/disk_2', selected: false } });
245 expect(component.groups.controls[0].value).toEqual({ disks: [], group_id: 'foo', members: [] });
246 expect(component.imagesSettings).toEqual({
249 backstore: 'backstore:1',
255 it('should validate authentication', () => {
256 const control = component.targetForm;
257 const formHelper = new FormHelper(control as CdFormGroup);
258 formHelper.expectValid('auth');
259 validateAuth(formHelper);
262 describe('should test initiators', () => {
264 component.onImageSelection({ option: { name: 'rbd/disk_2', selected: true } });
265 component.targetForm.patchValue({ disks: ['rbd/disk_2'], acl_enabled: true });
266 component.addGroup().patchValue({ name: 'group_1' });
267 component.addGroup().patchValue({ name: 'group_2' });
269 component.addInitiator();
270 component.initiators.controls[0].patchValue({
271 client_iqn: 'iqn.initiator'
273 component.updatedInitiatorSelector();
276 it('should prepare data when creating an initiator', () => {
277 expect(component.initiators.controls.length).toBe(1);
278 expect(component.initiators.controls[0].value).toEqual({
279 auth: { mutual_password: '', mutual_user: '', password: '', user: '' },
281 client_iqn: 'iqn.initiator',
284 expect(component.imagesInitiatorSelections).toEqual([
285 [{ description: '', name: 'rbd/disk_2', selected: false, enabled: true }]
287 expect(component.groupMembersSelections).toEqual([
288 [{ description: '', name: 'iqn.initiator', selected: false, enabled: true }],
289 [{ description: '', name: 'iqn.initiator', selected: false, enabled: true }]
293 it('should update data when changing an initiator name', () => {
294 expect(component.groupMembersSelections).toEqual([
295 [{ description: '', name: 'iqn.initiator', selected: false, enabled: true }],
296 [{ description: '', name: 'iqn.initiator', selected: false, enabled: true }]
299 component.initiators.controls[0].patchValue({
300 client_iqn: 'iqn.initiator_new'
302 component.updatedInitiatorSelector();
304 expect(component.groupMembersSelections).toEqual([
305 [{ description: '', name: 'iqn.initiator_new', selected: false, enabled: true }],
306 [{ description: '', name: 'iqn.initiator_new', selected: false, enabled: true }]
310 it('should clean data when removing an initiator', () => {
311 component.groups.controls[0].patchValue({
313 members: ['iqn.initiator']
316 expect(component.groups.controls[0].value).toEqual({
319 members: ['iqn.initiator']
322 component.removeInitiator(0);
324 expect(component.groups.controls[0].value).toEqual({
329 expect(component.groupMembersSelections).toEqual([[], []]);
330 expect(component.imagesInitiatorSelections).toEqual([]);
333 it('should remove images in the initiator when added in a group', () => {
334 component.initiators.controls[0].patchValue({
337 expect(component.initiators.controls[0].value).toEqual({
338 auth: { mutual_password: '', mutual_user: '', password: '', user: '' },
340 client_iqn: 'iqn.initiator',
344 component.addGroup();
345 component.groups.controls[0].patchValue({
347 members: ['iqn.initiator']
349 component.onGroupMemberSelection({
351 name: 'iqn.initiator',
356 expect(component.initiators.controls[0].value).toEqual({
357 auth: { mutual_password: '', mutual_user: '', password: '', user: '' },
359 client_iqn: 'iqn.initiator',
364 it('should disabled the initiator when selected', () => {
365 expect(component.groupMembersSelections).toEqual([
366 [{ description: '', enabled: true, name: 'iqn.initiator', selected: false }],
367 [{ description: '', enabled: true, name: 'iqn.initiator', selected: false }]
370 component.groupMembersSelections[0][0].selected = true;
371 component.onGroupMemberSelection({ option: { name: 'iqn.initiator', selected: true } });
373 expect(component.groupMembersSelections).toEqual([
374 [{ description: '', enabled: false, name: 'iqn.initiator', selected: true }],
375 [{ description: '', enabled: false, name: 'iqn.initiator', selected: false }]
379 it('should validate authentication', () => {
380 const control = component.initiators.controls[0];
381 const formHelper = new FormHelper(control as CdFormGroup);
382 formHelper.expectValid(control);
383 validateAuth(formHelper);
387 describe('should submit request', () => {
389 component.onImageSelection({ option: { name: 'rbd/disk_2', selected: true } });
390 component.targetForm.patchValue({ disks: ['rbd/disk_2'], acl_enabled: true });
391 component.portals.setValue(['node1:192.168.100.201', 'node2:192.168.100.202']);
392 component.addInitiator().patchValue({
393 client_iqn: 'iqn.initiator'
395 component.addGroup().patchValue({
397 members: ['iqn.initiator'],
398 disks: ['rbd/disk_2']
402 it('should call update', () => {
403 activatedRoute.setParams({ target_iqn: 'iqn.iscsi' });
404 component.isEdit = true;
405 component.target_iqn = 'iqn.iscsi';
409 const req = httpTesting.expectOne('api/iscsi/target/iqn.iscsi');
410 expect(req.request.method).toBe('PUT');
411 expect(req.request.body).toEqual({
414 auth: { mutual_password: '', mutual_user: '', password: '', user: '' },
415 client_iqn: 'iqn.initiator',
421 backstore: 'backstore:1',
430 { disks: [{ image: 'disk_2', pool: 'rbd' }], group_id: 'foo', members: ['iqn.initiator'] }
432 new_target_iqn: component.targetForm.value.target_iqn,
434 { host: 'node1', ip: '192.168.100.201' },
435 { host: 'node2', ip: '192.168.100.202' }
438 target_iqn: component.target_iqn,
449 it('should call create', () => {
452 const req = httpTesting.expectOne('api/iscsi/target');
453 expect(req.request.method).toBe('POST');
454 expect(req.request.body).toEqual({
457 auth: { mutual_password: '', mutual_user: '', password: '', user: '' },
458 client_iqn: 'iqn.initiator',
464 backstore: 'backstore:1',
474 disks: [{ image: 'disk_2', pool: 'rbd' }],
476 members: ['iqn.initiator']
480 { host: 'node1', ip: '192.168.100.201' },
481 { host: 'node2', ip: '192.168.100.202' }
484 target_iqn: component.targetForm.value.target_iqn,
495 it('should call create with acl_enabled disabled', () => {
496 component.targetForm.patchValue({ acl_enabled: false });
499 const req = httpTesting.expectOne('api/iscsi/target');
500 expect(req.request.method).toBe('POST');
501 expect(req.request.body).toEqual({
505 backstore: 'backstore:1',
522 { host: 'node1', ip: '192.168.100.201' },
523 { host: 'node2', ip: '192.168.100.202' }
526 target_iqn: component.targetForm.value.target_iqn
531 function validateAuth(formHelper: FormHelper) {
532 IscsiHelper.validateUser(formHelper, 'auth.user');
533 IscsiHelper.validatePassword(formHelper, 'auth.password');
534 IscsiHelper.validateUser(formHelper, 'auth.mutual_user');
535 IscsiHelper.validatePassword(formHelper, 'auth.mutual_password');