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 { LoadingPanelComponent } from '~/app/shared/components/loading-panel/loading-panel.component';
10 import { SelectOption } from '~/app/shared/components/select/select-option.model';
11 import { CdFormGroup } from '~/app/shared/forms/cd-form-group';
12 import { SharedModule } from '~/app/shared/shared.module';
13 import { ActivatedRouteStub } from '~/testing/activated-route-stub';
14 import { configureTestBed, FormHelper, IscsiHelper } from '~/testing/unit-test-helper';
15 import { IscsiTargetFormComponent } from './iscsi-target-form.component';
17 describe('IscsiTargetFormComponent', () => {
18 let component: IscsiTargetFormComponent;
19 let fixture: ComponentFixture<IscsiTargetFormComponent>;
20 let httpTesting: HttpTestingController;
21 let activatedRoute: ActivatedRouteStub;
24 config: { minimum_gateways: 2 },
25 disk_default_controls: {
34 target_default_controls: {
39 required_rbd_features: {
43 unsupported_rbd_features: {
47 backstores: ['backstore:1', 'backstore:2'],
48 default_backstore: 'backstore:1',
52 const LIST_TARGET: any[] = [
54 target_iqn: 'iqn.2003-01.com.redhat.iscsi-gw:iscsi-igw',
55 portals: [{ host: 'node1', ip: '192.168.100.201' }],
61 backstore: 'backstore:1',
62 wwn: '64af6678-9694-4367-bacc-f8eb0baa'
67 client_iqn: 'iqn.1994-05.com.redhat:rh7-client',
68 luns: [{ pool: 'rbd', image: 'disk_1', lun: 0 }],
70 user: 'myiscsiusername',
71 password: 'myiscsipassword',
83 { name: 'node1', ip_addresses: ['192.168.100.201', '10.0.2.15'] },
84 { name: 'node2', ip_addresses: ['192.168.100.202'] }
88 ceph_iscsi_config_version: 11
91 const RBD_LIST: any[] = [
92 { status: 0, value: [], pool_name: 'ganesha' },
101 block_name_prefix: 'rbd_data.148162fb31a8',
106 features_name: ['deep-flatten', 'exclusive-lock', 'fast-diff', 'layering', 'object-map'],
107 timestamp: '2019-01-18T10:44:26Z',
109 stripe_unit: 4194304,
121 block_name_prefix: 'rbd_data.14b292cee6cb',
126 features_name: ['deep-flatten', 'exclusive-lock', 'fast-diff', 'layering', 'object-map'],
127 timestamp: '2019-01-18T10:45:56Z',
129 stripe_unit: 4194304,
143 declarations: [IscsiTargetFormComponent],
147 HttpClientTestingModule,
149 ToastrModule.forRoot()
153 provide: ActivatedRoute,
154 useValue: new ActivatedRouteStub({ target_iqn: undefined })
158 [LoadingPanelComponent]
162 fixture = TestBed.createComponent(IscsiTargetFormComponent);
163 component = fixture.componentInstance;
164 httpTesting = TestBed.inject(HttpTestingController);
165 activatedRoute = <ActivatedRouteStub>TestBed.inject(ActivatedRoute);
166 fixture.detectChanges();
168 httpTesting.expectOne('ui-api/iscsi/settings').flush(SETTINGS);
169 httpTesting.expectOne('ui-api/iscsi/portals').flush(PORTALS);
170 httpTesting.expectOne('ui-api/iscsi/version').flush(VERSION);
171 httpTesting.expectOne('api/block/image').flush(RBD_LIST);
172 httpTesting.expectOne('api/iscsi/target').flush(LIST_TARGET);
173 httpTesting.verify();
176 it('should create', () => {
177 expect(component).toBeTruthy();
180 it('should only show images not used in other targets', () => {
181 expect(component.imagesAll).toEqual([RBD_LIST[1]['value'][1]]);
182 expect(component.imagesSelections).toEqual([
183 { description: '', name: 'rbd/disk_2', selected: false, enabled: true }
187 it('should generate portals selectOptions', () => {
188 expect(component.portalsSelections).toEqual([
189 { description: '', name: 'node1:192.168.100.201', selected: false, enabled: true },
190 { description: '', name: 'node1:10.0.2.15', selected: false, enabled: true },
191 { description: '', name: 'node2:192.168.100.202', selected: false, enabled: true }
195 it('should create the form', () => {
196 expect(component.targetForm.value).toEqual({
209 target_iqn: component.targetForm.value.target_iqn
213 it('should prepare data when selecting an image', () => {
214 expect(component.imagesSettings).toEqual({});
215 component.onImageSelection({ option: { name: 'rbd/disk_2', selected: true } });
216 expect(component.imagesSettings).toEqual({
219 backstore: 'backstore:1',
225 it('should clean data when removing an image', () => {
226 component.onImageSelection({ option: { name: 'rbd/disk_2', selected: true } });
227 component.addGroup();
228 component.groups.controls[0].patchValue({
230 disks: ['rbd/disk_2']
233 expect(component.groups.controls[0].value).toEqual({
234 disks: ['rbd/disk_2'],
239 component.onImageSelection({ option: { name: 'rbd/disk_2', selected: false } });
241 expect(component.groups.controls[0].value).toEqual({ disks: [], group_id: 'foo', members: [] });
242 expect(component.imagesSettings).toEqual({
245 backstore: 'backstore:1',
251 it('should validate authentication', () => {
252 const control = component.targetForm;
253 const formHelper = new FormHelper(control as CdFormGroup);
254 formHelper.expectValid('auth');
255 validateAuth(formHelper);
258 describe('should test initiators', () => {
260 component.onImageSelection({ option: { name: 'rbd/disk_2', selected: true } });
261 component.targetForm.patchValue({ disks: ['rbd/disk_2'], acl_enabled: true });
262 component.addGroup().patchValue({ name: 'group_1' });
263 component.addGroup().patchValue({ name: 'group_2' });
265 component.addInitiator();
266 component.initiators.controls[0].patchValue({
267 client_iqn: 'iqn.initiator'
269 component.updatedInitiatorSelector();
272 it('should prepare data when creating an initiator', () => {
273 expect(component.initiators.controls.length).toBe(1);
274 expect(component.initiators.controls[0].value).toEqual({
275 auth: { mutual_password: '', mutual_user: '', password: '', user: '' },
277 client_iqn: 'iqn.initiator',
280 expect(component.imagesInitiatorSelections).toEqual([
281 [{ description: '', name: 'rbd/disk_2', selected: false, enabled: true }]
283 expect(component.groupMembersSelections).toEqual([
284 [{ description: '', name: 'iqn.initiator', selected: false, enabled: true }],
285 [{ description: '', name: 'iqn.initiator', selected: false, enabled: true }]
289 it('should update data when changing an initiator name', () => {
290 expect(component.groupMembersSelections).toEqual([
291 [{ description: '', name: 'iqn.initiator', selected: false, enabled: true }],
292 [{ description: '', name: 'iqn.initiator', selected: false, enabled: true }]
295 component.initiators.controls[0].patchValue({
296 client_iqn: 'iqn.initiator_new'
298 component.updatedInitiatorSelector();
300 expect(component.groupMembersSelections).toEqual([
301 [{ description: '', name: 'iqn.initiator_new', selected: false, enabled: true }],
302 [{ description: '', name: 'iqn.initiator_new', selected: false, enabled: true }]
306 it('should clean data when removing an initiator', () => {
307 component.groups.controls[0].patchValue({
309 members: ['iqn.initiator']
312 expect(component.groups.controls[0].value).toEqual({
315 members: ['iqn.initiator']
318 component.removeInitiator(0);
320 expect(component.groups.controls[0].value).toEqual({
325 expect(component.groupMembersSelections).toEqual([[], []]);
326 expect(component.imagesInitiatorSelections).toEqual([]);
329 it('should remove images in the initiator when added in a group', () => {
330 component.initiators.controls[0].patchValue({
333 component.imagesInitiatorSelections[0] = [
341 expect(component.initiators.controls[0].value).toEqual({
342 auth: { mutual_password: '', mutual_user: '', password: '', user: '' },
344 client_iqn: 'iqn.initiator',
348 component.groups.controls[0].patchValue({
350 members: ['iqn.initiator']
352 component.onGroupMemberSelection(
355 name: 'iqn.initiator',
362 expect(component.initiators.controls[0].value).toEqual({
363 auth: { mutual_password: '', mutual_user: '', password: '', user: '' },
365 client_iqn: 'iqn.initiator',
368 expect(component.imagesInitiatorSelections[0]).toEqual([
378 it('should disabled the initiator when selected', () => {
379 expect(component.groupMembersSelections).toEqual([
380 [{ description: '', enabled: true, name: 'iqn.initiator', selected: false }],
381 [{ description: '', enabled: true, name: 'iqn.initiator', selected: false }]
384 component.groupMembersSelections[0][0].selected = true;
385 component.onGroupMemberSelection({ option: { name: 'iqn.initiator', selected: true } }, 0);
387 expect(component.groupMembersSelections).toEqual([
388 [{ description: '', enabled: false, name: 'iqn.initiator', selected: true }],
389 [{ description: '', enabled: false, name: 'iqn.initiator', selected: false }]
393 describe('should remove from group', () => {
395 component.onGroupMemberSelection(
396 { option: new SelectOption(true, 'iqn.initiator', '') },
399 component.groupDiskSelections[0][0].selected = true;
400 component.groups.controls[0].patchValue({
401 disks: ['rbd/disk_2'],
402 members: ['iqn.initiator']
405 expect(component.initiators.value[0].luns).toEqual([]);
406 expect(component.imagesInitiatorSelections[0]).toEqual([
407 { description: '', enabled: true, name: 'rbd/disk_2', selected: false }
409 expect(component.initiators.value[0].cdIsInGroup).toBe(true);
412 it('should update initiator images when deselecting', () => {
413 component.onGroupMemberSelection(
414 { option: new SelectOption(false, 'iqn.initiator', '') },
418 expect(component.initiators.value[0].luns).toEqual(['rbd/disk_2']);
419 expect(component.imagesInitiatorSelections[0]).toEqual([
420 { description: '', enabled: true, name: 'rbd/disk_2', selected: true }
422 expect(component.initiators.value[0].cdIsInGroup).toBe(false);
425 it('should update initiator when removing', () => {
426 component.removeGroupInitiator(component.groups.controls[0] as CdFormGroup, 0, 0);
428 expect(component.initiators.value[0].luns).toEqual(['rbd/disk_2']);
429 expect(component.imagesInitiatorSelections[0]).toEqual([
430 { description: '', enabled: true, name: 'rbd/disk_2', selected: true }
432 expect(component.initiators.value[0].cdIsInGroup).toBe(false);
436 it('should validate authentication', () => {
437 const control = component.initiators.controls[0];
438 const formHelper = new FormHelper(control as CdFormGroup);
439 formHelper.expectValid(control);
440 validateAuth(formHelper);
444 describe('should submit request', () => {
446 component.onImageSelection({ option: { name: 'rbd/disk_2', selected: true } });
447 component.targetForm.patchValue({ disks: ['rbd/disk_2'], acl_enabled: true });
448 component.portals.setValue(['node1:192.168.100.201', 'node2:192.168.100.202']);
449 component.addInitiator().patchValue({
450 client_iqn: 'iqn.initiator'
452 component.addGroup().patchValue({
454 members: ['iqn.initiator'],
455 disks: ['rbd/disk_2']
459 it('should call update', () => {
460 activatedRoute.setParams({ target_iqn: 'iqn.iscsi' });
461 component.isEdit = true;
462 component.target_iqn = 'iqn.iscsi';
466 const req = httpTesting.expectOne('api/iscsi/target/iqn.iscsi');
467 expect(req.request.method).toBe('PUT');
468 expect(req.request.body).toEqual({
471 auth: { mutual_password: '', mutual_user: '', password: '', user: '' },
472 client_iqn: 'iqn.initiator',
478 backstore: 'backstore:1',
487 { disks: [{ image: 'disk_2', pool: 'rbd' }], group_id: 'foo', members: ['iqn.initiator'] }
489 new_target_iqn: component.targetForm.value.target_iqn,
491 { host: 'node1', ip: '192.168.100.201' },
492 { host: 'node2', ip: '192.168.100.202' }
495 target_iqn: component.target_iqn,
506 it('should call create', () => {
509 const req = httpTesting.expectOne('api/iscsi/target');
510 expect(req.request.method).toBe('POST');
511 expect(req.request.body).toEqual({
514 auth: { mutual_password: '', mutual_user: '', password: '', user: '' },
515 client_iqn: 'iqn.initiator',
521 backstore: 'backstore:1',
531 disks: [{ image: 'disk_2', pool: 'rbd' }],
533 members: ['iqn.initiator']
537 { host: 'node1', ip: '192.168.100.201' },
538 { host: 'node2', ip: '192.168.100.202' }
541 target_iqn: component.targetForm.value.target_iqn,
552 it('should call create with acl_enabled disabled', () => {
553 component.targetForm.patchValue({ acl_enabled: false });
556 const req = httpTesting.expectOne('api/iscsi/target');
557 expect(req.request.method).toBe('POST');
558 expect(req.request.body).toEqual({
562 backstore: 'backstore:1',
579 { host: 'node1', ip: '192.168.100.201' },
580 { host: 'node2', ip: '192.168.100.202' }
583 target_iqn: component.targetForm.value.target_iqn
588 function validateAuth(formHelper: FormHelper) {
589 IscsiHelper.validateUser(formHelper, 'auth.user');
590 IscsiHelper.validatePassword(formHelper, 'auth.password');
591 IscsiHelper.validateUser(formHelper, 'auth.mutual_user');
592 IscsiHelper.validatePassword(formHelper, 'auth.mutual_password');