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 { RouterTestingModule } from '@angular/router/testing';
6 import { ToastModule } from 'ng2-toastr';
8 import { configureTestBed, i18nProviders } from '../../../../testing/unit-test-helper';
9 import { SharedModule } from '../../../shared/shared.module';
10 import { IscsiTargetFormComponent } from './iscsi-target-form.component';
12 describe('IscsiTargetFormComponent', () => {
13 let component: IscsiTargetFormComponent;
14 let fixture: ComponentFixture<IscsiTargetFormComponent>;
15 let httpTesting: HttpTestingController;
18 config: { minimum_gateways: 2 },
19 disk_default_controls: {
24 target_default_controls: {
33 target_iqn: 'iqn.2003-01.com.redhat.iscsi-gw:iscsi-igw',
34 portals: [{ host: 'node1', ip: '192.168.100.201' }],
35 disks: [{ pool: 'rbd', image: 'disk_1', controls: {} }],
38 client_iqn: 'iqn.1994-05.com.redhat:rh7-client',
39 luns: [{ pool: 'rbd', image: 'disk_1' }],
41 user: 'myiscsiusername',
42 password: 'myiscsipassword',
54 { name: 'node1', ip_addresses: ['192.168.100.201', '10.0.2.15'] },
55 { name: 'node2', ip_addresses: ['192.168.100.202'] }
59 { status: 0, value: [], pool_name: 'ganesha' },
68 block_name_prefix: 'rbd_data.148162fb31a8',
73 features_name: ['deep-flatten', 'exclusive-lock', 'fast-diff', 'layering', 'object-map'],
74 timestamp: '2019-01-18T10:44:26Z',
88 block_name_prefix: 'rbd_data.14b292cee6cb',
93 features_name: ['deep-flatten', 'exclusive-lock', 'fast-diff', 'layering', 'object-map'],
94 timestamp: '2019-01-18T10:45:56Z',
110 declarations: [IscsiTargetFormComponent],
114 HttpClientTestingModule,
116 ToastModule.forRoot()
118 providers: [i18nProviders]
124 fixture = TestBed.createComponent(IscsiTargetFormComponent);
125 component = fixture.componentInstance;
126 httpTesting = TestBed.get(HttpTestingController);
127 fixture.detectChanges();
129 httpTesting.expectOne('ui-api/iscsi/settings').flush(SETTINGS);
130 httpTesting.expectOne('ui-api/iscsi/portals').flush(PORTALS);
131 httpTesting.expectOne('api/summary').flush({});
132 httpTesting.expectOne('api/block/image').flush(RBD_LIST);
133 httpTesting.expectOne('api/iscsi/target').flush(LIST_TARGET);
134 httpTesting.verify();
137 it('should create', () => {
138 expect(component).toBeTruthy();
141 it('should only show images not used in other targets', () => {
142 expect(component.imagesAll).toEqual(['rbd/disk_2']);
143 expect(component.imagesSelections).toEqual([
144 { description: '', name: 'rbd/disk_2', selected: false }
148 it('should generate portals selectOptions', () => {
149 expect(component.portalsSelections).toEqual([
150 { description: '', name: 'node1:192.168.100.201', selected: false },
151 { description: '', name: 'node1:10.0.2.15', selected: false },
152 { description: '', name: 'node2:192.168.100.202', selected: false }
156 it('should create the form', () => {
157 expect(component.targetForm.value).toEqual({
163 target_iqn: component.targetForm.value.target_iqn
167 it('should prepare data when selecting an image', () => {
168 expect(component.imagesInitiatorSelections).toEqual([]);
169 expect(component.groupDiskSelections).toEqual([]);
170 expect(component.imagesSettings).toEqual({});
172 component.onImageSelection({ option: { name: 'rbd/disk_1', selected: true } });
174 expect(component.imagesInitiatorSelections).toEqual([
175 { description: '', name: 'rbd/disk_1', selected: false }
177 expect(component.groupDiskSelections).toEqual([
178 { description: '', name: 'rbd/disk_1', selected: false }
180 expect(component.imagesSettings).toEqual({ 'rbd/disk_1': {} });
183 it('should clean data when removing an image', () => {
184 component.onImageSelection({ option: { name: 'rbd/disk_1', selected: true } });
185 component.addGroup();
186 component.groups.controls[0].patchValue({
188 disks: ['rbd/disk_1']
191 expect(component.groups.controls[0].value).toEqual({
192 disks: ['rbd/disk_1'],
197 component.onImageSelection({ option: { name: 'rbd/disk_1', selected: false } });
199 expect(component.groups.controls[0].value).toEqual({ disks: [], group_id: 'foo', members: [] });
200 expect(component.imagesInitiatorSelections).toEqual([]);
201 expect(component.groupDiskSelections).toEqual([]);
202 expect(component.imagesSettings).toEqual({ 'rbd/disk_1': {} });
205 describe('should test initiators', () => {
207 component.addInitiator();
208 component.initiators.controls[0].patchValue({
209 client_iqn: 'iqn.initiator'
211 component.updatedInitiatorSelector();
214 it('should prepare data when creating an initiator', () => {
215 expect(component.initiators.controls.length).toBe(1);
216 expect(component.initiators.controls[0].value).toEqual({
217 auth: { mutual_password: '', mutual_user: '', password: '', user: '' },
219 client_iqn: 'iqn.initiator',
222 expect(component.groupMembersSelections).toEqual([
223 { description: '', name: 'iqn.initiator', selected: false }
227 it('should update data when changing an initiator name', () => {
228 expect(component.groupMembersSelections).toEqual([
229 { description: '', name: 'iqn.initiator', selected: false }
232 component.initiators.controls[0].patchValue({
233 client_iqn: 'iqn.initiator_new'
235 component.updatedInitiatorSelector();
237 expect(component.groupMembersSelections).toEqual([
238 { description: '', name: 'iqn.initiator_new', selected: false }
242 it('should clean data when removing an initiator', () => {
243 component.addGroup();
244 component.groups.controls[0].patchValue({
246 members: ['iqn.initiator']
249 expect(component.groups.controls[0].value).toEqual({
252 members: ['iqn.initiator']
255 component.removeInitiator(0);
257 expect(component.groups.controls[0].value).toEqual({
262 expect(component.groupMembersSelections).toEqual([]);
265 it('should remove images in the initiator when added in a group', () => {
266 component.initiators.controls[0].patchValue({
269 expect(component.initiators.controls[0].value).toEqual({
270 auth: { mutual_password: '', mutual_user: '', password: '', user: '' },
272 client_iqn: 'iqn.initiator',
276 component.addGroup();
277 component.groups.controls[0].patchValue({
279 members: ['iqn.initiator']
281 component.onGroupMemberSelection({
283 name: 'iqn.initiator',
288 expect(component.initiators.controls[0].value).toEqual({
289 auth: { mutual_password: '', mutual_user: '', password: '', user: '' },
291 client_iqn: 'iqn.initiator',
297 it('should generate the request data', () => {
298 component.onImageSelection({ option: { name: 'rbd/disk_1', selected: true } });
299 component.portals.setValue(['node1:192.168.100.201', 'node2:192.168.100.202']);
300 component.addInitiator();
301 component.initiators.controls[0].patchValue({
302 client_iqn: 'iqn.initiator'
304 component.addGroup();
305 component.groups.controls[0].patchValue({
307 members: ['iqn.initiator'],
308 disks: ['rbd/disk_1']
313 const req = httpTesting.expectOne('api/iscsi/target');
314 expect(req.request.method).toBe('POST');
315 expect(req.request.body).toEqual({
318 auth: { mutual_password: null, mutual_user: null, password: null, user: null },
320 client_iqn: 'iqn.initiator',
326 { disks: [{ image: 'disk_1', pool: 'rbd' }], group_id: 'foo', members: ['iqn.initiator'] }
328 portals: [{ host: 'node1', ip: '192.168.100.201' }, { host: 'node2', ip: '192.168.100.202' }],
330 target_iqn: component.targetForm.value.target_iqn