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';
10 import { configureTestBed, FormHelper, IscsiHelper } from '~/testing/unit-test-helper';
11 import { LoadingPanelComponent } from '~/app/shared/components/loading-panel/loading-panel.component';
12 import { CdFormGroup } from '~/app/shared/forms/cd-form-group';
13 import { SharedModule } from '~/app/shared/shared.module';
14 import { IscsiTargetFormComponent } from './iscsi-target-form.component';
16 describe('IscsiTargetFormComponent', () => {
17 let component: IscsiTargetFormComponent;
18 let fixture: ComponentFixture<IscsiTargetFormComponent>;
19 let httpTesting: HttpTestingController;
20 let activatedRoute: ActivatedRouteStub;
23 config: { minimum_gateways: 2 },
24 disk_default_controls: {
33 target_default_controls: {
38 required_rbd_features: {
42 unsupported_rbd_features: {
46 backstores: ['backstore:1', 'backstore:2'],
47 default_backstore: 'backstore:1',
51 const LIST_TARGET: any[] = [
53 target_iqn: 'iqn.2003-01.com.redhat.iscsi-gw:iscsi-igw',
54 portals: [{ host: 'node1', ip: '192.168.100.201' }],
60 backstore: 'backstore:1',
61 wwn: '64af6678-9694-4367-bacc-f8eb0baa'
66 client_iqn: 'iqn.1994-05.com.redhat:rh7-client',
67 luns: [{ pool: 'rbd', image: 'disk_1', lun: 0 }],
69 user: 'myiscsiusername',
70 password: 'myiscsipassword',
82 { name: 'node1', ip_addresses: ['192.168.100.201', '10.0.2.15'] },
83 { name: 'node2', ip_addresses: ['192.168.100.202'] }
87 ceph_iscsi_config_version: 11
90 const RBD_LIST: any[] = [
91 { status: 0, value: [], pool_name: 'ganesha' },
100 block_name_prefix: 'rbd_data.148162fb31a8',
105 features_name: ['deep-flatten', 'exclusive-lock', 'fast-diff', 'layering', 'object-map'],
106 timestamp: '2019-01-18T10:44:26Z',
108 stripe_unit: 4194304,
120 block_name_prefix: 'rbd_data.14b292cee6cb',
125 features_name: ['deep-flatten', 'exclusive-lock', 'fast-diff', 'layering', 'object-map'],
126 timestamp: '2019-01-18T10:45:56Z',
128 stripe_unit: 4194304,
142 declarations: [IscsiTargetFormComponent],
146 HttpClientTestingModule,
148 ToastrModule.forRoot()
152 provide: ActivatedRoute,
153 useValue: new ActivatedRouteStub({ target_iqn: undefined })
157 [LoadingPanelComponent]
161 fixture = TestBed.createComponent(IscsiTargetFormComponent);
162 component = fixture.componentInstance;
163 httpTesting = TestBed.inject(HttpTestingController);
164 activatedRoute = <ActivatedRouteStub>TestBed.inject(ActivatedRoute);
165 fixture.detectChanges();
167 httpTesting.expectOne('ui-api/iscsi/settings').flush(SETTINGS);
168 httpTesting.expectOne('ui-api/iscsi/portals').flush(PORTALS);
169 httpTesting.expectOne('ui-api/iscsi/version').flush(VERSION);
170 httpTesting.expectOne('api/block/image').flush(RBD_LIST);
171 httpTesting.expectOne('api/iscsi/target').flush(LIST_TARGET);
172 httpTesting.verify();
175 it('should create', () => {
176 expect(component).toBeTruthy();
179 it('should only show images not used in other targets', () => {
180 expect(component.imagesAll).toEqual([RBD_LIST[1]['value'][1]]);
181 expect(component.imagesSelections).toEqual([
182 { description: '', name: 'rbd/disk_2', selected: false, enabled: true }
186 it('should generate portals selectOptions', () => {
187 expect(component.portalsSelections).toEqual([
188 { description: '', name: 'node1:192.168.100.201', selected: false, enabled: true },
189 { description: '', name: 'node1:10.0.2.15', selected: false, enabled: true },
190 { description: '', name: 'node2:192.168.100.202', selected: false, enabled: true }
194 it('should create the form', () => {
195 expect(component.targetForm.value).toEqual({
208 target_iqn: component.targetForm.value.target_iqn
212 it('should prepare data when selecting an image', () => {
213 expect(component.imagesSettings).toEqual({});
214 component.onImageSelection({ option: { name: 'rbd/disk_2', selected: true } });
215 expect(component.imagesSettings).toEqual({
218 backstore: 'backstore:1',
224 it('should clean data when removing an image', () => {
225 component.onImageSelection({ option: { name: 'rbd/disk_2', selected: true } });
226 component.addGroup();
227 component.groups.controls[0].patchValue({
229 disks: ['rbd/disk_2']
232 expect(component.groups.controls[0].value).toEqual({
233 disks: ['rbd/disk_2'],
238 component.onImageSelection({ option: { name: 'rbd/disk_2', selected: false } });
240 expect(component.groups.controls[0].value).toEqual({ disks: [], group_id: 'foo', members: [] });
241 expect(component.imagesSettings).toEqual({
244 backstore: 'backstore:1',
250 it('should validate authentication', () => {
251 const control = component.targetForm;
252 const formHelper = new FormHelper(control as CdFormGroup);
253 formHelper.expectValid('auth');
254 validateAuth(formHelper);
257 describe('should test initiators', () => {
259 component.onImageSelection({ option: { name: 'rbd/disk_2', selected: true } });
260 component.targetForm.patchValue({ disks: ['rbd/disk_2'], acl_enabled: true });
261 component.addGroup().patchValue({ name: 'group_1' });
262 component.addGroup().patchValue({ name: 'group_2' });
264 component.addInitiator();
265 component.initiators.controls[0].patchValue({
266 client_iqn: 'iqn.initiator'
268 component.updatedInitiatorSelector();
271 it('should prepare data when creating an initiator', () => {
272 expect(component.initiators.controls.length).toBe(1);
273 expect(component.initiators.controls[0].value).toEqual({
274 auth: { mutual_password: '', mutual_user: '', password: '', user: '' },
276 client_iqn: 'iqn.initiator',
279 expect(component.imagesInitiatorSelections).toEqual([
280 [{ description: '', name: 'rbd/disk_2', selected: false, enabled: true }]
282 expect(component.groupMembersSelections).toEqual([
283 [{ description: '', name: 'iqn.initiator', selected: false, enabled: true }],
284 [{ description: '', name: 'iqn.initiator', selected: false, enabled: true }]
288 it('should update data when changing an initiator name', () => {
289 expect(component.groupMembersSelections).toEqual([
290 [{ description: '', name: 'iqn.initiator', selected: false, enabled: true }],
291 [{ description: '', name: 'iqn.initiator', selected: false, enabled: true }]
294 component.initiators.controls[0].patchValue({
295 client_iqn: 'iqn.initiator_new'
297 component.updatedInitiatorSelector();
299 expect(component.groupMembersSelections).toEqual([
300 [{ description: '', name: 'iqn.initiator_new', selected: false, enabled: true }],
301 [{ description: '', name: 'iqn.initiator_new', selected: false, enabled: true }]
305 it('should clean data when removing an initiator', () => {
306 component.groups.controls[0].patchValue({
308 members: ['iqn.initiator']
311 expect(component.groups.controls[0].value).toEqual({
314 members: ['iqn.initiator']
317 component.removeInitiator(0);
319 expect(component.groups.controls[0].value).toEqual({
324 expect(component.groupMembersSelections).toEqual([[], []]);
325 expect(component.imagesInitiatorSelections).toEqual([]);
328 it('should remove images in the initiator when added in a group', () => {
329 component.initiators.controls[0].patchValue({
332 expect(component.initiators.controls[0].value).toEqual({
333 auth: { mutual_password: '', mutual_user: '', password: '', user: '' },
335 client_iqn: 'iqn.initiator',
339 component.addGroup();
340 component.groups.controls[0].patchValue({
342 members: ['iqn.initiator']
344 component.onGroupMemberSelection({
346 name: 'iqn.initiator',
351 expect(component.initiators.controls[0].value).toEqual({
352 auth: { mutual_password: '', mutual_user: '', password: '', user: '' },
354 client_iqn: 'iqn.initiator',
359 it('should disabled the initiator when selected', () => {
360 expect(component.groupMembersSelections).toEqual([
361 [{ description: '', enabled: true, name: 'iqn.initiator', selected: false }],
362 [{ description: '', enabled: true, name: 'iqn.initiator', selected: false }]
365 component.groupMembersSelections[0][0].selected = true;
366 component.onGroupMemberSelection({ option: { name: 'iqn.initiator', selected: true } });
368 expect(component.groupMembersSelections).toEqual([
369 [{ description: '', enabled: false, name: 'iqn.initiator', selected: true }],
370 [{ description: '', enabled: false, name: 'iqn.initiator', selected: false }]
374 it('should validate authentication', () => {
375 const control = component.initiators.controls[0];
376 const formHelper = new FormHelper(control as CdFormGroup);
377 formHelper.expectValid(control);
378 validateAuth(formHelper);
382 describe('should submit request', () => {
384 component.onImageSelection({ option: { name: 'rbd/disk_2', selected: true } });
385 component.targetForm.patchValue({ disks: ['rbd/disk_2'], acl_enabled: true });
386 component.portals.setValue(['node1:192.168.100.201', 'node2:192.168.100.202']);
387 component.addInitiator().patchValue({
388 client_iqn: 'iqn.initiator'
390 component.addGroup().patchValue({
392 members: ['iqn.initiator'],
393 disks: ['rbd/disk_2']
397 it('should call update', () => {
398 activatedRoute.setParams({ target_iqn: 'iqn.iscsi' });
399 component.isEdit = true;
400 component.target_iqn = 'iqn.iscsi';
404 const req = httpTesting.expectOne('api/iscsi/target/iqn.iscsi');
405 expect(req.request.method).toBe('PUT');
406 expect(req.request.body).toEqual({
409 auth: { mutual_password: '', mutual_user: '', password: '', user: '' },
410 client_iqn: 'iqn.initiator',
416 backstore: 'backstore:1',
425 { disks: [{ image: 'disk_2', pool: 'rbd' }], group_id: 'foo', members: ['iqn.initiator'] }
427 new_target_iqn: component.targetForm.value.target_iqn,
429 { host: 'node1', ip: '192.168.100.201' },
430 { host: 'node2', ip: '192.168.100.202' }
433 target_iqn: component.target_iqn,
444 it('should call create', () => {
447 const req = httpTesting.expectOne('api/iscsi/target');
448 expect(req.request.method).toBe('POST');
449 expect(req.request.body).toEqual({
452 auth: { mutual_password: '', mutual_user: '', password: '', user: '' },
453 client_iqn: 'iqn.initiator',
459 backstore: 'backstore:1',
469 disks: [{ image: 'disk_2', pool: 'rbd' }],
471 members: ['iqn.initiator']
475 { host: 'node1', ip: '192.168.100.201' },
476 { host: 'node2', ip: '192.168.100.202' }
479 target_iqn: component.targetForm.value.target_iqn,
490 it('should call create with acl_enabled disabled', () => {
491 component.targetForm.patchValue({ acl_enabled: false });
494 const req = httpTesting.expectOne('api/iscsi/target');
495 expect(req.request.method).toBe('POST');
496 expect(req.request.body).toEqual({
500 backstore: 'backstore:1',
517 { host: 'node1', ip: '192.168.100.201' },
518 { host: 'node2', ip: '192.168.100.202' }
521 target_iqn: component.targetForm.value.target_iqn
526 function validateAuth(formHelper: FormHelper) {
527 IscsiHelper.validateUser(formHelper, 'auth.user');
528 IscsiHelper.validatePassword(formHelper, 'auth.password');
529 IscsiHelper.validateUser(formHelper, 'auth.mutual_user');
530 IscsiHelper.validatePassword(formHelper, 'auth.mutual_password');