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,
145 declarations: [IscsiTargetFormComponent],
149 HttpClientTestingModule,
151 ToastrModule.forRoot()
156 provide: ActivatedRoute,
157 useValue: new ActivatedRouteStub({ target_iqn: undefined })
163 fixture = TestBed.createComponent(IscsiTargetFormComponent);
164 component = fixture.componentInstance;
165 httpTesting = TestBed.get(HttpTestingController);
166 activatedRoute = TestBed.get(ActivatedRoute);
167 fixture.detectChanges();
169 httpTesting.expectOne('ui-api/iscsi/settings').flush(SETTINGS);
170 httpTesting.expectOne('ui-api/iscsi/portals').flush(PORTALS);
171 httpTesting.expectOne('ui-api/iscsi/version').flush(VERSION);
172 httpTesting.expectOne('api/block/image').flush(RBD_LIST);
173 httpTesting.expectOne('api/iscsi/target').flush(LIST_TARGET);
174 httpTesting.verify();
177 it('should create', () => {
178 expect(component).toBeTruthy();
181 it('should only show images not used in other targets', () => {
182 expect(component.imagesAll).toEqual([RBD_LIST[1]['value'][1]]);
183 expect(component.imagesSelections).toEqual([
184 { description: '', name: 'rbd/disk_2', selected: false, enabled: true }
188 it('should generate portals selectOptions', () => {
189 expect(component.portalsSelections).toEqual([
190 { description: '', name: 'node1:192.168.100.201', selected: false, enabled: true },
191 { description: '', name: 'node1:10.0.2.15', selected: false, enabled: true },
192 { description: '', name: 'node2:192.168.100.202', selected: false, enabled: true }
196 it('should create the form', () => {
197 expect(component.targetForm.value).toEqual({
210 target_iqn: component.targetForm.value.target_iqn
214 it('should prepare data when selecting an image', () => {
215 expect(component.imagesSettings).toEqual({});
216 component.onImageSelection({ option: { name: 'rbd/disk_2', selected: true } });
217 expect(component.imagesSettings).toEqual({
220 backstore: 'backstore:1',
226 it('should clean data when removing an image', () => {
227 component.onImageSelection({ option: { name: 'rbd/disk_2', selected: true } });
228 component.addGroup();
229 component.groups.controls[0].patchValue({
231 disks: ['rbd/disk_2']
234 expect(component.groups.controls[0].value).toEqual({
235 disks: ['rbd/disk_2'],
240 component.onImageSelection({ option: { name: 'rbd/disk_2', selected: false } });
242 expect(component.groups.controls[0].value).toEqual({ disks: [], group_id: 'foo', members: [] });
243 expect(component.imagesSettings).toEqual({
246 backstore: 'backstore:1',
252 it('should validate authentication', () => {
253 const control = component.targetForm;
254 const formHelper = new FormHelper(control as CdFormGroup);
255 formHelper.expectValid('auth');
256 validateAuth(formHelper);
259 describe('should test initiators', () => {
261 component.onImageSelection({ option: { name: 'rbd/disk_2', selected: true } });
262 component.targetForm.patchValue({ disks: ['rbd/disk_2'], acl_enabled: true });
263 component.addGroup().patchValue({ name: 'group_1' });
264 component.addGroup().patchValue({ name: 'group_2' });
266 component.addInitiator();
267 component.initiators.controls[0].patchValue({
268 client_iqn: 'iqn.initiator'
270 component.updatedInitiatorSelector();
273 it('should prepare data when creating an initiator', () => {
274 expect(component.initiators.controls.length).toBe(1);
275 expect(component.initiators.controls[0].value).toEqual({
276 auth: { mutual_password: '', mutual_user: '', password: '', user: '' },
278 client_iqn: 'iqn.initiator',
281 expect(component.imagesInitiatorSelections).toEqual([
282 [{ description: '', name: 'rbd/disk_2', selected: false, enabled: true }]
284 expect(component.groupMembersSelections).toEqual([
285 [{ description: '', name: 'iqn.initiator', selected: false, enabled: true }],
286 [{ description: '', name: 'iqn.initiator', selected: false, enabled: true }]
290 it('should update data when changing an initiator name', () => {
291 expect(component.groupMembersSelections).toEqual([
292 [{ description: '', name: 'iqn.initiator', selected: false, enabled: true }],
293 [{ description: '', name: 'iqn.initiator', selected: false, enabled: true }]
296 component.initiators.controls[0].patchValue({
297 client_iqn: 'iqn.initiator_new'
299 component.updatedInitiatorSelector();
301 expect(component.groupMembersSelections).toEqual([
302 [{ description: '', name: 'iqn.initiator_new', selected: false, enabled: true }],
303 [{ description: '', name: 'iqn.initiator_new', selected: false, enabled: true }]
307 it('should clean data when removing an initiator', () => {
308 component.groups.controls[0].patchValue({
310 members: ['iqn.initiator']
313 expect(component.groups.controls[0].value).toEqual({
316 members: ['iqn.initiator']
319 component.removeInitiator(0);
321 expect(component.groups.controls[0].value).toEqual({
326 expect(component.groupMembersSelections).toEqual([[], []]);
327 expect(component.imagesInitiatorSelections).toEqual([]);
330 it('should remove images in the initiator when added in a group', () => {
331 component.initiators.controls[0].patchValue({
334 expect(component.initiators.controls[0].value).toEqual({
335 auth: { mutual_password: '', mutual_user: '', password: '', user: '' },
337 client_iqn: 'iqn.initiator',
341 component.addGroup();
342 component.groups.controls[0].patchValue({
344 members: ['iqn.initiator']
346 component.onGroupMemberSelection({
348 name: 'iqn.initiator',
353 expect(component.initiators.controls[0].value).toEqual({
354 auth: { mutual_password: '', mutual_user: '', password: '', user: '' },
356 client_iqn: 'iqn.initiator',
361 it('should disabled the initiator when selected', () => {
362 expect(component.groupMembersSelections).toEqual([
363 [{ description: '', enabled: true, name: 'iqn.initiator', selected: false }],
364 [{ description: '', enabled: true, name: 'iqn.initiator', selected: false }]
367 component.groupMembersSelections[0][0].selected = true;
368 component.onGroupMemberSelection({ option: { name: 'iqn.initiator', selected: true } });
370 expect(component.groupMembersSelections).toEqual([
371 [{ description: '', enabled: false, name: 'iqn.initiator', selected: true }],
372 [{ description: '', enabled: false, name: 'iqn.initiator', selected: false }]
376 it('should validate authentication', () => {
377 const control = component.initiators.controls[0];
378 const formHelper = new FormHelper(control as CdFormGroup);
379 formHelper.expectValid(control);
380 validateAuth(formHelper);
384 describe('should submit request', () => {
386 component.onImageSelection({ option: { name: 'rbd/disk_2', selected: true } });
387 component.targetForm.patchValue({ disks: ['rbd/disk_2'], acl_enabled: true });
388 component.portals.setValue(['node1:192.168.100.201', 'node2:192.168.100.202']);
389 component.addInitiator().patchValue({
390 client_iqn: 'iqn.initiator'
392 component.addGroup().patchValue({
394 members: ['iqn.initiator'],
395 disks: ['rbd/disk_2']
399 it('should call update', () => {
400 activatedRoute.setParams({ target_iqn: 'iqn.iscsi' });
401 component.isEdit = true;
402 component.target_iqn = 'iqn.iscsi';
406 const req = httpTesting.expectOne('api/iscsi/target/iqn.iscsi');
407 expect(req.request.method).toBe('PUT');
408 expect(req.request.body).toEqual({
411 auth: { mutual_password: '', mutual_user: '', password: '', user: '' },
412 client_iqn: 'iqn.initiator',
418 backstore: 'backstore:1',
427 { disks: [{ image: 'disk_2', pool: 'rbd' }], group_id: 'foo', members: ['iqn.initiator'] }
429 new_target_iqn: component.targetForm.value.target_iqn,
431 { host: 'node1', ip: '192.168.100.201' },
432 { host: 'node2', ip: '192.168.100.202' }
435 target_iqn: component.target_iqn,
446 it('should call create', () => {
449 const req = httpTesting.expectOne('api/iscsi/target');
450 expect(req.request.method).toBe('POST');
451 expect(req.request.body).toEqual({
454 auth: { mutual_password: '', mutual_user: '', password: '', user: '' },
455 client_iqn: 'iqn.initiator',
461 backstore: 'backstore:1',
471 disks: [{ image: 'disk_2', pool: 'rbd' }],
473 members: ['iqn.initiator']
477 { host: 'node1', ip: '192.168.100.201' },
478 { host: 'node2', ip: '192.168.100.202' }
481 target_iqn: component.targetForm.value.target_iqn,
492 it('should call create with acl_enabled disabled', () => {
493 component.targetForm.patchValue({ acl_enabled: false });
496 const req = httpTesting.expectOne('api/iscsi/target');
497 expect(req.request.method).toBe('POST');
498 expect(req.request.body).toEqual({
502 backstore: 'backstore:1',
519 { host: 'node1', ip: '192.168.100.201' },
520 { host: 'node2', ip: '192.168.100.202' }
523 target_iqn: component.targetForm.value.target_iqn
528 function validateAuth(formHelper: FormHelper) {
529 IscsiHelper.validateUser(formHelper, 'auth.user');
530 IscsiHelper.validatePassword(formHelper, 'auth.password');
531 IscsiHelper.validateUser(formHelper, 'auth.mutual_user');
532 IscsiHelper.validatePassword(formHelper, 'auth.mutual_password');