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'
50 target_iqn: 'iqn.2003-01.com.redhat.iscsi-gw:iscsi-igw',
51 portals: [{ host: 'node1', ip: '192.168.100.201' }],
52 disks: [{ pool: 'rbd', image: 'disk_1', controls: {}, backstore: 'backstore:1' }],
55 client_iqn: 'iqn.1994-05.com.redhat:rh7-client',
56 luns: [{ pool: 'rbd', image: 'disk_1' }],
58 user: 'myiscsiusername',
59 password: 'myiscsipassword',
71 { name: 'node1', ip_addresses: ['192.168.100.201', '10.0.2.15'] },
72 { name: 'node2', ip_addresses: ['192.168.100.202'] }
76 ceph_iscsi_config_version: 11
80 { status: 0, value: [], pool_name: 'ganesha' },
89 block_name_prefix: 'rbd_data.148162fb31a8',
94 features_name: ['deep-flatten', 'exclusive-lock', 'fast-diff', 'layering', 'object-map'],
95 timestamp: '2019-01-18T10:44:26Z',
109 block_name_prefix: 'rbd_data.14b292cee6cb',
114 features_name: ['deep-flatten', 'exclusive-lock', 'fast-diff', 'layering', 'object-map'],
115 timestamp: '2019-01-18T10:45:56Z',
117 stripe_unit: 4194304,
131 declarations: [IscsiTargetFormComponent],
135 HttpClientTestingModule,
137 ToastrModule.forRoot()
142 provide: ActivatedRoute,
143 useValue: new ActivatedRouteStub({ target_iqn: undefined })
151 fixture = TestBed.createComponent(IscsiTargetFormComponent);
152 component = fixture.componentInstance;
153 httpTesting = TestBed.get(HttpTestingController);
154 activatedRoute = TestBed.get(ActivatedRoute);
155 fixture.detectChanges();
157 httpTesting.expectOne('ui-api/iscsi/settings').flush(SETTINGS);
158 httpTesting.expectOne('ui-api/iscsi/portals').flush(PORTALS);
159 httpTesting.expectOne('ui-api/iscsi/version').flush(VERSION);
160 httpTesting.expectOne('api/summary').flush({});
161 httpTesting.expectOne('api/block/image').flush(RBD_LIST);
162 httpTesting.expectOne('api/iscsi/target').flush(LIST_TARGET);
163 httpTesting.verify();
166 it('should create', () => {
167 expect(component).toBeTruthy();
170 it('should only show images not used in other targets', () => {
171 expect(component.imagesAll).toEqual([RBD_LIST[1]['value'][1]]);
172 expect(component.imagesSelections).toEqual([
173 { description: '', name: 'rbd/disk_2', selected: false, enabled: true }
177 it('should generate portals selectOptions', () => {
178 expect(component.portalsSelections).toEqual([
179 { description: '', name: 'node1:192.168.100.201', selected: false, enabled: true },
180 { description: '', name: 'node1:10.0.2.15', selected: false, enabled: true },
181 { description: '', name: 'node2:192.168.100.202', selected: false, enabled: true }
185 it('should create the form', () => {
186 expect(component.targetForm.value).toEqual({
199 target_iqn: component.targetForm.value.target_iqn
203 it('should prepare data when selecting an image', () => {
204 expect(component.imagesSettings).toEqual({});
205 component.onImageSelection({ option: { name: 'rbd/disk_2', selected: true } });
206 expect(component.imagesSettings).toEqual({
208 backstore: 'backstore:1',
214 it('should clean data when removing an image', () => {
215 component.onImageSelection({ option: { name: 'rbd/disk_2', selected: true } });
216 component.addGroup();
217 component.groups.controls[0].patchValue({
219 disks: ['rbd/disk_2']
222 expect(component.groups.controls[0].value).toEqual({
223 disks: ['rbd/disk_2'],
228 component.onImageSelection({ option: { name: 'rbd/disk_2', selected: false } });
230 expect(component.groups.controls[0].value).toEqual({ disks: [], group_id: 'foo', members: [] });
231 expect(component.imagesSettings).toEqual({
233 backstore: 'backstore:1',
239 describe('should test initiators', () => {
241 component.targetForm.patchValue({ disks: ['rbd/disk_2'], acl_enabled: true });
242 component.addGroup().patchValue({ name: 'group_1' });
243 component.addGroup().patchValue({ name: 'group_2' });
244 component.onImageSelection({ option: { name: 'rbd/disk_2', selected: true } });
246 component.addInitiator();
247 component.initiators.controls[0].patchValue({
248 client_iqn: 'iqn.initiator'
250 component.updatedInitiatorSelector();
253 it('should prepare data when creating an initiator', () => {
254 expect(component.initiators.controls.length).toBe(1);
255 expect(component.initiators.controls[0].value).toEqual({
256 auth: { mutual_password: '', mutual_user: '', password: '', user: '' },
258 client_iqn: 'iqn.initiator',
261 expect(component.imagesInitiatorSelections).toEqual([
262 [{ description: '', name: 'rbd/disk_2', selected: false, enabled: true }]
264 expect(component.groupMembersSelections).toEqual([
265 [{ description: '', name: 'iqn.initiator', selected: false, enabled: true }],
266 [{ description: '', name: 'iqn.initiator', selected: false, enabled: true }]
270 it('should update data when changing an initiator name', () => {
271 expect(component.groupMembersSelections).toEqual([
272 [{ description: '', name: 'iqn.initiator', selected: false, enabled: true }],
273 [{ description: '', name: 'iqn.initiator', selected: false, enabled: true }]
276 component.initiators.controls[0].patchValue({
277 client_iqn: 'iqn.initiator_new'
279 component.updatedInitiatorSelector();
281 expect(component.groupMembersSelections).toEqual([
282 [{ description: '', name: 'iqn.initiator_new', selected: false, enabled: true }],
283 [{ description: '', name: 'iqn.initiator_new', selected: false, enabled: true }]
287 it('should clean data when removing an initiator', () => {
288 component.groups.controls[0].patchValue({
290 members: ['iqn.initiator']
293 expect(component.groups.controls[0].value).toEqual({
296 members: ['iqn.initiator']
299 component.removeInitiator(0);
301 expect(component.groups.controls[0].value).toEqual({
306 expect(component.groupMembersSelections).toEqual([[], []]);
307 expect(component.imagesInitiatorSelections).toEqual([]);
310 it('should remove images in the initiator when added in a group', () => {
311 component.initiators.controls[0].patchValue({
314 expect(component.initiators.controls[0].value).toEqual({
315 auth: { mutual_password: '', mutual_user: '', password: '', user: '' },
317 client_iqn: 'iqn.initiator',
321 component.addGroup();
322 component.groups.controls[0].patchValue({
324 members: ['iqn.initiator']
326 component.onGroupMemberSelection({
328 name: 'iqn.initiator',
333 expect(component.initiators.controls[0].value).toEqual({
334 auth: { mutual_password: '', mutual_user: '', password: '', user: '' },
336 client_iqn: 'iqn.initiator',
341 it('should disabled the initiator when selected', () => {
342 expect(component.groupMembersSelections).toEqual([
343 [{ description: '', enabled: true, name: 'iqn.initiator', selected: false }],
344 [{ description: '', enabled: true, name: 'iqn.initiator', selected: false }]
347 component.groupMembersSelections[0][0].selected = true;
348 component.onGroupMemberSelection({ option: { name: 'iqn.initiator', selected: true } });
350 expect(component.groupMembersSelections).toEqual([
351 [{ description: '', enabled: false, name: 'iqn.initiator', selected: true }],
352 [{ description: '', enabled: false, name: 'iqn.initiator', selected: false }]
357 describe('should submit request', () => {
359 component.targetForm.patchValue({ disks: ['rbd/disk_2'], acl_enabled: true });
360 component.onImageSelection({ option: { name: 'rbd/disk_2', selected: true } });
361 component.portals.setValue(['node1:192.168.100.201', 'node2:192.168.100.202']);
362 component.addInitiator().patchValue({
363 client_iqn: 'iqn.initiator'
365 component.addGroup().patchValue({
367 members: ['iqn.initiator'],
368 disks: ['rbd/disk_2']
372 it('should call update', () => {
373 activatedRoute.setParams({ target_iqn: 'iqn.iscsi' });
374 component.isEdit = true;
375 component.target_iqn = 'iqn.iscsi';
379 const req = httpTesting.expectOne('api/iscsi/target/iqn.iscsi');
380 expect(req.request.method).toBe('PUT');
381 expect(req.request.body).toEqual({
384 auth: { mutual_password: '', mutual_user: '', password: '', user: '' },
385 client_iqn: 'iqn.initiator',
389 disks: [{ backstore: 'backstore:1', controls: {}, image: 'disk_2', pool: 'rbd' }],
391 { disks: [{ image: 'disk_2', pool: 'rbd' }], group_id: 'foo', members: ['iqn.initiator'] }
393 new_target_iqn: component.targetForm.value.target_iqn,
395 { host: 'node1', ip: '192.168.100.201' },
396 { host: 'node2', ip: '192.168.100.202' }
399 target_iqn: component.target_iqn,
410 it('should call create', () => {
413 const req = httpTesting.expectOne('api/iscsi/target');
414 expect(req.request.method).toBe('POST');
415 expect(req.request.body).toEqual({
418 auth: { mutual_password: '', mutual_user: '', password: '', user: '' },
419 client_iqn: 'iqn.initiator',
423 disks: [{ backstore: 'backstore:1', controls: {}, image: 'disk_2', pool: 'rbd' }],
426 disks: [{ image: 'disk_2', pool: 'rbd' }],
428 members: ['iqn.initiator']
432 { host: 'node1', ip: '192.168.100.201' },
433 { host: 'node2', ip: '192.168.100.202' }
436 target_iqn: component.targetForm.value.target_iqn,
447 it('should call create with acl_enabled disabled', () => {
448 component.targetForm.patchValue({ acl_enabled: false });
451 const req = httpTesting.expectOne('api/iscsi/target');
452 expect(req.request.method).toBe('POST');
453 expect(req.request.body).toEqual({
455 disks: [{ backstore: 'backstore:1', controls: {}, image: 'disk_2', pool: 'rbd' }],
465 { host: 'node1', ip: '192.168.100.201' },
466 { host: 'node2', ip: '192.168.100.202' }
469 target_iqn: component.targetForm.value.target_iqn