1 import { HttpClientTestingModule } from '@angular/common/http/testing';
2 import { ComponentFixture, TestBed } from '@angular/core/testing';
3 import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
4 import { RouterTestingModule } from '@angular/router/testing';
6 import { NgbNavModule } from '@ng-bootstrap/ng-bootstrap';
7 import { TreeModule } from 'angular-tree-component';
8 import { ToastrModule } from 'ngx-toastr';
9 import { BehaviorSubject, of } from 'rxjs';
15 } from '../../../../testing/unit-test-helper';
16 import { IscsiService } from '../../../shared/api/iscsi.service';
17 import { TableActionsComponent } from '../../../shared/datatable/table-actions/table-actions.component';
18 import { CdTableAction } from '../../../shared/models/cd-table-action';
19 import { ExecutingTask } from '../../../shared/models/executing-task';
20 import { SummaryService } from '../../../shared/services/summary.service';
21 import { TaskListService } from '../../../shared/services/task-list.service';
22 import { SharedModule } from '../../../shared/shared.module';
23 import { IscsiTabsComponent } from '../iscsi-tabs/iscsi-tabs.component';
24 import { IscsiTargetDetailsComponent } from '../iscsi-target-details/iscsi-target-details.component';
25 import { IscsiTargetListComponent } from './iscsi-target-list.component';
27 describe('IscsiTargetListComponent', () => {
28 let component: IscsiTargetListComponent;
29 let fixture: ComponentFixture<IscsiTargetListComponent>;
30 let summaryService: SummaryService;
31 let iscsiService: IscsiService;
33 const refresh = (data: any) => {
34 summaryService['summaryDataSource'].next(data);
39 BrowserAnimationsModule,
40 HttpClientTestingModule,
44 ToastrModule.forRoot(),
47 declarations: [IscsiTargetListComponent, IscsiTabsComponent, IscsiTargetDetailsComponent],
48 providers: [TaskListService]
52 fixture = TestBed.createComponent(IscsiTargetListComponent);
53 component = fixture.componentInstance;
54 summaryService = TestBed.inject(SummaryService);
55 iscsiService = TestBed.inject(IscsiService);
57 // this is needed because summaryService isn't being reset after each test.
58 summaryService['summaryDataSource'] = new BehaviorSubject(null);
59 summaryService['summaryData$'] = summaryService['summaryDataSource'].asObservable();
61 spyOn(iscsiService, 'status').and.returnValue(of({ available: true }));
62 spyOn(iscsiService, 'version').and.returnValue(of({ ceph_iscsi_config_version: 11 }));
65 it('should create', () => {
66 expect(component).toBeTruthy();
69 describe('after ngOnInit', () => {
71 spyOn(iscsiService, 'listTargets').and.callThrough();
72 fixture.detectChanges();
75 it('should load targets on init', () => {
77 expect(iscsiService.status).toHaveBeenCalled();
78 expect(iscsiService.listTargets).toHaveBeenCalled();
81 it('should not load targets on init because no data', () => {
83 expect(iscsiService.listTargets).not.toHaveBeenCalled();
86 it('should call error function on init when summary service fails', () => {
87 spyOn(component.table, 'reset');
88 summaryService['summaryDataSource'].error(undefined);
89 expect(component.table.reset).toHaveBeenCalled();
93 describe('handling of executing tasks', () => {
96 const addTarget = (name: string) => {
99 portals: [{ host: 'node1', ip: '192.168.100.201' }],
100 disks: [{ pool: 'rbd', image: 'disk_1', controls: {} }],
103 client_iqn: 'iqn.1994-05.com.redhat:rh7-client',
104 luns: [{ pool: 'rbd', image: 'disk_1' }],
106 user: 'myiscsiusername',
107 password: 'myiscsipassword',
109 mutual_password: null
119 const addTask = (name: string, target_iqn: string) => {
120 const task = new ExecutingTask();
123 case 'iscsi/target/create':
125 target_iqn: target_iqn
128 case 'iscsi/target/delete':
130 target_iqn: target_iqn
135 target_iqn: target_iqn
139 summaryService.addRunningTask(task);
148 component.targets = targets;
149 refresh({ executing_tasks: [], finished_tasks: [] });
150 spyOn(iscsiService, 'listTargets').and.callFake(() => of(targets));
151 fixture.detectChanges();
154 it('should gets all targets without tasks', () => {
155 expect(component.targets.length).toBe(3);
156 expect(component.targets.every((target) => !target.cdExecuting)).toBeTruthy();
159 it('should add a new target from a task', () => {
160 addTask('iscsi/target/create', 'iqn.d');
161 expect(component.targets.length).toBe(4);
162 expectItemTasks(component.targets[0], undefined);
163 expectItemTasks(component.targets[1], undefined);
164 expectItemTasks(component.targets[2], undefined);
165 expectItemTasks(component.targets[3], 'Creating');
168 it('should show when an existing target is being modified', () => {
169 addTask('iscsi/target/delete', 'iqn.b');
170 expect(component.targets.length).toBe(3);
171 expectItemTasks(component.targets[1], 'Deleting');
175 describe('handling of actions', () => {
177 fixture.detectChanges();
180 let action: CdTableAction;
182 const getAction = (name: string): CdTableAction => {
183 return component.tableActions.find((tableAction) => tableAction.name === name);
186 describe('edit', () => {
188 action = getAction('Edit');
191 it('should be disabled if no gateways', () => {
192 component.selection.selected = [
197 expect(action.disable(undefined)).toBeTruthy();
198 expect(action.disableDesc(undefined)).toBe('Unavailable gateway(s)');
201 it('should be enabled if active sessions', () => {
202 component.selection.selected = [
210 expect(action.disable(undefined)).toBeFalsy();
211 expect(action.disableDesc(undefined)).toBeUndefined();
214 it('should be enabled if no active sessions', () => {
215 component.selection.selected = [
223 expect(action.disable(undefined)).toBeFalsy();
224 expect(action.disableDesc(undefined)).toBeUndefined();
228 describe('delete', () => {
230 action = getAction('Delete');
233 it('should be disabled if no gateways', () => {
234 component.selection.selected = [
239 expect(action.disable(undefined)).toBeTruthy();
240 expect(action.disableDesc(undefined)).toBe('Unavailable gateway(s)');
243 it('should be disabled if active sessions', () => {
244 component.selection.selected = [
252 expect(action.disable(undefined)).toBeTruthy();
253 expect(action.disableDesc(undefined)).toBe('Target has active sessions');
256 it('should be enabled if no active sessions', () => {
257 component.selection.selected = [
265 expect(action.disable(undefined)).toBeFalsy();
266 expect(action.disableDesc(undefined)).toBeUndefined();
271 it('should test all TableActions combinations', () => {
272 const permissionHelper: PermissionHelper = new PermissionHelper(component.permission);
273 const tableActions: TableActionsComponent = permissionHelper.setPermissionsAndGetActions(
274 component.tableActions
277 expect(tableActions).toEqual({
278 'create,update,delete': {
279 actions: ['Create', 'Edit', 'Delete'],
280 primary: { multiple: 'Create', executing: 'Edit', single: 'Edit', no: 'Create' }
283 actions: ['Create', 'Edit'],
284 primary: { multiple: 'Create', executing: 'Edit', single: 'Edit', no: 'Create' }
287 actions: ['Create', 'Delete'],
288 primary: { multiple: 'Create', executing: 'Delete', single: 'Delete', no: 'Create' }
292 primary: { multiple: 'Create', executing: 'Create', single: 'Create', no: 'Create' }
295 actions: ['Edit', 'Delete'],
296 primary: { multiple: 'Edit', executing: 'Edit', single: 'Edit', no: 'Edit' }
300 primary: { multiple: 'Edit', executing: 'Edit', single: 'Edit', no: 'Edit' }
304 primary: { multiple: 'Delete', executing: 'Delete', single: 'Delete', no: 'Delete' }
308 primary: { multiple: '', executing: '', single: '', no: '' }