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 { ToastModule } from 'ng2-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 supported_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 { status: 0, value: [], pool_name: 'ganesha' },
85 block_name_prefix: 'rbd_data.148162fb31a8',
90 features_name: ['deep-flatten', 'exclusive-lock', 'fast-diff', 'layering', 'object-map'],
91 timestamp: '2019-01-18T10:44:26Z',
105 block_name_prefix: 'rbd_data.14b292cee6cb',
110 features_name: ['deep-flatten', 'exclusive-lock', 'fast-diff', 'layering', 'object-map'],
111 timestamp: '2019-01-18T10:45:56Z',
113 stripe_unit: 4194304,
127 declarations: [IscsiTargetFormComponent],
131 HttpClientTestingModule,
133 ToastModule.forRoot()
138 provide: ActivatedRoute,
139 useValue: new ActivatedRouteStub({ target_iqn: undefined })
147 fixture = TestBed.createComponent(IscsiTargetFormComponent);
148 component = fixture.componentInstance;
149 httpTesting = TestBed.get(HttpTestingController);
150 activatedRoute = TestBed.get(ActivatedRoute);
151 fixture.detectChanges();
153 httpTesting.expectOne('ui-api/iscsi/settings').flush(SETTINGS);
154 httpTesting.expectOne('ui-api/iscsi/portals').flush(PORTALS);
155 httpTesting.expectOne('api/summary').flush({});
156 httpTesting.expectOne('api/block/image').flush(RBD_LIST);
157 httpTesting.expectOne('api/iscsi/target').flush(LIST_TARGET);
158 httpTesting.verify();
161 it('should create', () => {
162 expect(component).toBeTruthy();
165 it('should only show images not used in other targets', () => {
166 expect(component.imagesAll).toEqual([RBD_LIST[1]['value'][1]]);
167 expect(component.imagesSelections).toEqual([
168 { description: '', name: 'rbd/disk_2', selected: false }
172 it('should generate portals selectOptions', () => {
173 expect(component.portalsSelections).toEqual([
174 { description: '', name: 'node1:192.168.100.201', selected: false },
175 { description: '', name: 'node1:10.0.2.15', selected: false },
176 { description: '', name: 'node2:192.168.100.202', selected: false }
180 it('should create the form', () => {
181 expect(component.targetForm.value).toEqual({
188 target_iqn: component.targetForm.value.target_iqn
192 it('should prepare data when selecting an image', () => {
193 expect(component.imagesSettings).toEqual({});
194 component.onImageSelection({ option: { name: 'rbd/disk_2', selected: true } });
195 expect(component.imagesSettings).toEqual({
197 backstore: 'backstore:1',
203 it('should clean data when removing an image', () => {
204 component.onImageSelection({ option: { name: 'rbd/disk_2', selected: true } });
205 component.addGroup();
206 component.groups.controls[0].patchValue({
208 disks: ['rbd/disk_2']
211 expect(component.groups.controls[0].value).toEqual({
212 disks: ['rbd/disk_2'],
217 component.onImageSelection({ option: { name: 'rbd/disk_2', selected: false } });
219 expect(component.groups.controls[0].value).toEqual({ disks: [], group_id: 'foo', members: [] });
220 expect(component.imagesSettings).toEqual({
222 backstore: 'backstore:1',
228 describe('should test initiators', () => {
230 component.targetForm.patchValue({ disks: ['rbd/disk_2'], acl_enabled: true });
231 component.addGroup().patchValue({ name: 'group_1' });
232 component.onImageSelection({ option: { name: 'rbd/disk_2', selected: true } });
234 component.addInitiator();
235 component.initiators.controls[0].patchValue({
236 client_iqn: 'iqn.initiator'
238 component.updatedInitiatorSelector();
241 it('should prepare data when creating an initiator', () => {
242 expect(component.initiators.controls.length).toBe(1);
243 expect(component.initiators.controls[0].value).toEqual({
244 auth: { mutual_password: '', mutual_user: '', password: '', user: '' },
246 client_iqn: 'iqn.initiator',
249 expect(component.imagesInitiatorSelections).toEqual([
250 [{ description: '', name: 'rbd/disk_2', selected: false }]
252 expect(component.groupMembersSelections).toEqual([
253 [{ description: '', name: 'iqn.initiator', selected: false }]
257 it('should update data when changing an initiator name', () => {
258 expect(component.groupMembersSelections).toEqual([
259 [{ description: '', name: 'iqn.initiator', selected: false }]
262 component.initiators.controls[0].patchValue({
263 client_iqn: 'iqn.initiator_new'
265 component.updatedInitiatorSelector();
267 expect(component.groupMembersSelections).toEqual([
268 [{ description: '', name: 'iqn.initiator_new', selected: false }]
272 it('should clean data when removing an initiator', () => {
273 component.groups.controls[0].patchValue({
275 members: ['iqn.initiator']
278 expect(component.groups.controls[0].value).toEqual({
281 members: ['iqn.initiator']
284 component.removeInitiator(0);
286 expect(component.groups.controls[0].value).toEqual({
291 expect(component.groupMembersSelections).toEqual([[]]);
292 expect(component.imagesInitiatorSelections).toEqual([]);
295 it('should remove images in the initiator when added in a group', () => {
296 component.initiators.controls[0].patchValue({
299 expect(component.initiators.controls[0].value).toEqual({
300 auth: { mutual_password: '', mutual_user: '', password: '', user: '' },
302 client_iqn: 'iqn.initiator',
306 component.addGroup();
307 component.groups.controls[0].patchValue({
309 members: ['iqn.initiator']
311 component.onGroupMemberSelection({
313 name: 'iqn.initiator',
318 expect(component.initiators.controls[0].value).toEqual({
319 auth: { mutual_password: '', mutual_user: '', password: '', user: '' },
321 client_iqn: 'iqn.initiator',
327 describe('should submit request', () => {
329 component.targetForm.patchValue({ disks: ['rbd/disk_2'], acl_enabled: true });
330 component.onImageSelection({ option: { name: 'rbd/disk_2', selected: true } });
331 component.portals.setValue(['node1:192.168.100.201', 'node2:192.168.100.202']);
332 component.addInitiator().patchValue({
333 client_iqn: 'iqn.initiator'
335 component.addGroup().patchValue({
337 members: ['iqn.initiator'],
338 disks: ['rbd/disk_2']
342 it('should call update', () => {
343 activatedRoute.setParams({ target_iqn: 'iqn.iscsi' });
344 component.isEdit = true;
345 component.target_iqn = 'iqn.iscsi';
349 const req = httpTesting.expectOne('api/iscsi/target/iqn.iscsi');
350 expect(req.request.method).toBe('PUT');
351 expect(req.request.body).toEqual({
354 auth: { mutual_password: null, mutual_user: null, password: null, user: null },
356 client_iqn: 'iqn.initiator',
360 disks: [{ backstore: 'backstore:1', controls: {}, image: 'disk_2', pool: 'rbd' }],
362 { disks: [{ image: 'disk_2', pool: 'rbd' }], group_id: 'foo', members: ['iqn.initiator'] }
364 new_target_iqn: component.targetForm.value.target_iqn,
366 { host: 'node1', ip: '192.168.100.201' },
367 { host: 'node2', ip: '192.168.100.202' }
370 target_iqn: component.target_iqn,
375 it('should call create', () => {
378 const req = httpTesting.expectOne('api/iscsi/target');
379 expect(req.request.method).toBe('POST');
380 expect(req.request.body).toEqual({
383 auth: { mutual_password: null, mutual_user: null, password: null, user: null },
385 client_iqn: 'iqn.initiator',
389 disks: [{ backstore: 'backstore:1', controls: {}, image: 'disk_2', pool: 'rbd' }],
392 disks: [{ image: 'disk_2', pool: 'rbd' }],
394 members: ['iqn.initiator']
398 { host: 'node1', ip: '192.168.100.201' },
399 { host: 'node2', ip: '192.168.100.202' }
402 target_iqn: component.targetForm.value.target_iqn,
407 it('should call create with acl_enabled disabled', () => {
408 component.targetForm.patchValue({ acl_enabled: false });
411 const req = httpTesting.expectOne('api/iscsi/target');
412 expect(req.request.method).toBe('POST');
413 expect(req.request.body).toEqual({
415 disks: [{ backstore: 'backstore:1', controls: {}, image: 'disk_2', pool: 'rbd' }],
419 { host: 'node1', ip: '192.168.100.201' },
420 { host: 'node2', ip: '192.168.100.202' }
423 target_iqn: component.targetForm.value.target_iqn