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, i18nProviders } from '../../../../testing/unit-test-helper';
11 import { SharedModule } from '../../../shared/shared.module';
12 import { IscsiTargetFormComponent } from './iscsi-target-form.component';
14 describe('IscsiTargetFormComponent', () => {
15 let component: IscsiTargetFormComponent;
16 let fixture: ComponentFixture<IscsiTargetFormComponent>;
17 let httpTesting: HttpTestingController;
18 let activatedRoute: ActivatedRouteStub;
21 config: { minimum_gateways: 2 },
22 disk_default_controls: {
31 target_default_controls: {
36 required_rbd_features: {
40 unsupported_rbd_features: {
44 backstores: ['backstore:1', 'backstore:2'],
45 default_backstore: 'backstore:1',
49 const LIST_TARGET: any[] = [
51 target_iqn: 'iqn.2003-01.com.redhat.iscsi-gw:iscsi-igw',
52 portals: [{ host: 'node1', ip: '192.168.100.201' }],
58 backstore: 'backstore:1',
59 wwn: '64af6678-9694-4367-bacc-f8eb0baa'
64 client_iqn: 'iqn.1994-05.com.redhat:rh7-client',
65 luns: [{ pool: 'rbd', image: 'disk_1', lun: 0 }],
67 user: 'myiscsiusername',
68 password: 'myiscsipassword',
80 { name: 'node1', ip_addresses: ['192.168.100.201', '10.0.2.15'] },
81 { name: 'node2', ip_addresses: ['192.168.100.202'] }
85 ceph_iscsi_config_version: 11
88 const RBD_LIST: any[] = [
89 { status: 0, value: [], pool_name: 'ganesha' },
98 block_name_prefix: 'rbd_data.148162fb31a8',
103 features_name: ['deep-flatten', 'exclusive-lock', 'fast-diff', 'layering', 'object-map'],
104 timestamp: '2019-01-18T10:44:26Z',
106 stripe_unit: 4194304,
118 block_name_prefix: 'rbd_data.14b292cee6cb',
123 features_name: ['deep-flatten', 'exclusive-lock', 'fast-diff', 'layering', 'object-map'],
124 timestamp: '2019-01-18T10:45:56Z',
126 stripe_unit: 4194304,
140 declarations: [IscsiTargetFormComponent],
144 HttpClientTestingModule,
146 ToastrModule.forRoot()
151 provide: ActivatedRoute,
152 useValue: new ActivatedRouteStub({ target_iqn: undefined })
160 fixture = TestBed.createComponent(IscsiTargetFormComponent);
161 component = fixture.componentInstance;
162 httpTesting = TestBed.get(HttpTestingController);
163 activatedRoute = TestBed.get(ActivatedRoute);
164 fixture.detectChanges();
166 httpTesting.expectOne('ui-api/iscsi/settings').flush(SETTINGS);
167 httpTesting.expectOne('ui-api/iscsi/portals').flush(PORTALS);
168 httpTesting.expectOne('ui-api/iscsi/version').flush(VERSION);
169 httpTesting.expectOne('api/summary').flush({});
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 describe('should test initiators', () => {
252 component.onImageSelection({ option: { name: 'rbd/disk_2', selected: true } });
253 component.targetForm.patchValue({ disks: ['rbd/disk_2'], acl_enabled: true });
254 component.addGroup().patchValue({ name: 'group_1' });
255 component.addGroup().patchValue({ name: 'group_2' });
257 component.addInitiator();
258 component.initiators.controls[0].patchValue({
259 client_iqn: 'iqn.initiator'
261 component.updatedInitiatorSelector();
264 it('should prepare data when creating an initiator', () => {
265 expect(component.initiators.controls.length).toBe(1);
266 expect(component.initiators.controls[0].value).toEqual({
267 auth: { mutual_password: '', mutual_user: '', password: '', user: '' },
269 client_iqn: 'iqn.initiator',
272 expect(component.imagesInitiatorSelections).toEqual([
273 [{ description: '', name: 'rbd/disk_2', selected: false, enabled: true }]
275 expect(component.groupMembersSelections).toEqual([
276 [{ description: '', name: 'iqn.initiator', selected: false, enabled: true }],
277 [{ description: '', name: 'iqn.initiator', selected: false, enabled: true }]
281 it('should update data when changing an initiator name', () => {
282 expect(component.groupMembersSelections).toEqual([
283 [{ description: '', name: 'iqn.initiator', selected: false, enabled: true }],
284 [{ description: '', name: 'iqn.initiator', selected: false, enabled: true }]
287 component.initiators.controls[0].patchValue({
288 client_iqn: 'iqn.initiator_new'
290 component.updatedInitiatorSelector();
292 expect(component.groupMembersSelections).toEqual([
293 [{ description: '', name: 'iqn.initiator_new', selected: false, enabled: true }],
294 [{ description: '', name: 'iqn.initiator_new', selected: false, enabled: true }]
298 it('should clean data when removing an initiator', () => {
299 component.groups.controls[0].patchValue({
301 members: ['iqn.initiator']
304 expect(component.groups.controls[0].value).toEqual({
307 members: ['iqn.initiator']
310 component.removeInitiator(0);
312 expect(component.groups.controls[0].value).toEqual({
317 expect(component.groupMembersSelections).toEqual([[], []]);
318 expect(component.imagesInitiatorSelections).toEqual([]);
321 it('should remove images in the initiator when added in a group', () => {
322 component.initiators.controls[0].patchValue({
325 expect(component.initiators.controls[0].value).toEqual({
326 auth: { mutual_password: '', mutual_user: '', password: '', user: '' },
328 client_iqn: 'iqn.initiator',
332 component.addGroup();
333 component.groups.controls[0].patchValue({
335 members: ['iqn.initiator']
337 component.onGroupMemberSelection({
339 name: 'iqn.initiator',
344 expect(component.initiators.controls[0].value).toEqual({
345 auth: { mutual_password: '', mutual_user: '', password: '', user: '' },
347 client_iqn: 'iqn.initiator',
352 it('should disabled the initiator when selected', () => {
353 expect(component.groupMembersSelections).toEqual([
354 [{ description: '', enabled: true, name: 'iqn.initiator', selected: false }],
355 [{ description: '', enabled: true, name: 'iqn.initiator', selected: false }]
358 component.groupMembersSelections[0][0].selected = true;
359 component.onGroupMemberSelection({ option: { name: 'iqn.initiator', selected: true } });
361 expect(component.groupMembersSelections).toEqual([
362 [{ description: '', enabled: false, name: 'iqn.initiator', selected: true }],
363 [{ description: '', enabled: false, name: 'iqn.initiator', selected: false }]
368 describe('should submit request', () => {
370 component.onImageSelection({ option: { name: 'rbd/disk_2', selected: true } });
371 component.targetForm.patchValue({ disks: ['rbd/disk_2'], acl_enabled: true });
372 component.portals.setValue(['node1:192.168.100.201', 'node2:192.168.100.202']);
373 component.addInitiator().patchValue({
374 client_iqn: 'iqn.initiator'
376 component.addGroup().patchValue({
378 members: ['iqn.initiator'],
379 disks: ['rbd/disk_2']
383 it('should call update', () => {
384 activatedRoute.setParams({ target_iqn: 'iqn.iscsi' });
385 component.isEdit = true;
386 component.target_iqn = 'iqn.iscsi';
390 const req = httpTesting.expectOne('api/iscsi/target/iqn.iscsi');
391 expect(req.request.method).toBe('PUT');
392 expect(req.request.body).toEqual({
395 auth: { mutual_password: '', mutual_user: '', password: '', user: '' },
396 client_iqn: 'iqn.initiator',
402 backstore: 'backstore:1',
411 { disks: [{ image: 'disk_2', pool: 'rbd' }], group_id: 'foo', members: ['iqn.initiator'] }
413 new_target_iqn: component.targetForm.value.target_iqn,
415 { host: 'node1', ip: '192.168.100.201' },
416 { host: 'node2', ip: '192.168.100.202' }
419 target_iqn: component.target_iqn,
430 it('should call create', () => {
433 const req = httpTesting.expectOne('api/iscsi/target');
434 expect(req.request.method).toBe('POST');
435 expect(req.request.body).toEqual({
438 auth: { mutual_password: '', mutual_user: '', password: '', user: '' },
439 client_iqn: 'iqn.initiator',
445 backstore: 'backstore:1',
455 disks: [{ image: 'disk_2', pool: 'rbd' }],
457 members: ['iqn.initiator']
461 { host: 'node1', ip: '192.168.100.201' },
462 { host: 'node2', ip: '192.168.100.202' }
465 target_iqn: component.targetForm.value.target_iqn,
476 it('should call create with acl_enabled disabled', () => {
477 component.targetForm.patchValue({ acl_enabled: false });
480 const req = httpTesting.expectOne('api/iscsi/target');
481 expect(req.request.method).toBe('POST');
482 expect(req.request.body).toEqual({
486 backstore: 'backstore:1',
503 { host: 'node1', ip: '192.168.100.201' },
504 { host: 'node2', ip: '192.168.100.202' }
507 target_iqn: component.targetForm.value.target_iqn