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 { TreeModule } from 'angular-tree-component';
7 import { TabsModule } from 'ngx-bootstrap/tabs';
8 import { ToastrModule } from 'ngx-toastr';
9 import { BehaviorSubject, of } from 'rxjs';
16 } from '../../../../testing/unit-test-helper';
17 import { IscsiService } from '../../../shared/api/iscsi.service';
18 import { TableActionsComponent } from '../../../shared/datatable/table-actions/table-actions.component';
19 import { CdTableAction } from '../../../shared/models/cd-table-action';
20 import { ExecutingTask } from '../../../shared/models/executing-task';
21 import { SummaryService } from '../../../shared/services/summary.service';
22 import { TaskListService } from '../../../shared/services/task-list.service';
23 import { SharedModule } from '../../../shared/shared.module';
24 import { IscsiTabsComponent } from '../iscsi-tabs/iscsi-tabs.component';
25 import { IscsiTargetDetailsComponent } from '../iscsi-target-details/iscsi-target-details.component';
26 import { IscsiTargetListComponent } from './iscsi-target-list.component';
28 describe('IscsiTargetListComponent', () => {
29 let component: IscsiTargetListComponent;
30 let fixture: ComponentFixture<IscsiTargetListComponent>;
31 let summaryService: SummaryService;
32 let iscsiService: IscsiService;
34 const refresh = (data: any) => {
35 summaryService['summaryDataSource'].next(data);
40 BrowserAnimationsModule,
41 HttpClientTestingModule,
46 ToastrModule.forRoot()
48 declarations: [IscsiTargetListComponent, IscsiTabsComponent, IscsiTargetDetailsComponent],
49 providers: [TaskListService, i18nProviders]
53 fixture = TestBed.createComponent(IscsiTargetListComponent);
54 component = fixture.componentInstance;
55 summaryService = TestBed.get(SummaryService);
56 iscsiService = TestBed.get(IscsiService);
58 // this is needed because summaryService isn't being reset after each test.
59 summaryService['summaryDataSource'] = new BehaviorSubject(null);
60 summaryService['summaryData$'] = summaryService['summaryDataSource'].asObservable();
62 spyOn(iscsiService, 'status').and.returnValue(of({ available: true }));
63 spyOn(iscsiService, 'version').and.returnValue(of({ ceph_iscsi_config_version: 11 }));
66 it('should create', () => {
67 expect(component).toBeTruthy();
70 describe('after ngOnInit', () => {
72 spyOn(iscsiService, 'listTargets').and.callThrough();
73 fixture.detectChanges();
76 it('should load targets on init', () => {
78 expect(iscsiService.status).toHaveBeenCalled();
79 expect(iscsiService.listTargets).toHaveBeenCalled();
82 it('should not load targets on init because no data', () => {
84 expect(iscsiService.listTargets).not.toHaveBeenCalled();
87 it('should call error function on init when summary service fails', () => {
88 spyOn(component.table, 'reset');
89 summaryService['summaryDataSource'].error(undefined);
90 expect(component.table.reset).toHaveBeenCalled();
94 describe('handling of executing tasks', () => {
97 const addTarget = (name: string) => {
100 portals: [{ host: 'node1', ip: '192.168.100.201' }],
101 disks: [{ pool: 'rbd', image: 'disk_1', controls: {} }],
104 client_iqn: 'iqn.1994-05.com.redhat:rh7-client',
105 luns: [{ pool: 'rbd', image: 'disk_1' }],
107 user: 'myiscsiusername',
108 password: 'myiscsipassword',
110 mutual_password: null
120 const addTask = (name: string, target_iqn: string) => {
121 const task = new ExecutingTask();
124 case 'iscsi/target/create':
126 target_iqn: target_iqn
129 case 'iscsi/target/delete':
131 target_iqn: target_iqn
136 target_iqn: target_iqn
140 summaryService.addRunningTask(task);
149 component.targets = targets;
150 refresh({ executing_tasks: [], finished_tasks: [] });
151 spyOn(iscsiService, 'listTargets').and.callFake(() => of(targets));
152 fixture.detectChanges();
155 it('should gets all targets without tasks', () => {
156 expect(component.targets.length).toBe(3);
157 expect(component.targets.every((target) => !target.cdExecuting)).toBeTruthy();
160 it('should add a new target from a task', () => {
161 addTask('iscsi/target/create', 'iqn.d');
162 expect(component.targets.length).toBe(4);
163 expectItemTasks(component.targets[0], undefined);
164 expectItemTasks(component.targets[1], undefined);
165 expectItemTasks(component.targets[2], undefined);
166 expectItemTasks(component.targets[3], 'Creating');
169 it('should show when an existing target is being modified', () => {
170 addTask('iscsi/target/delete', 'iqn.b');
171 expect(component.targets.length).toBe(3);
172 expectItemTasks(component.targets[1], 'Deleting');
176 describe('handling of actions', () => {
178 fixture.detectChanges();
181 let action: CdTableAction;
183 const getAction = (name: string): CdTableAction => {
184 return component.tableActions.find((tableAction) => tableAction.name === name);
187 describe('edit', () => {
189 action = getAction('Edit');
192 it('should be disabled if no gateways', () => {
193 component.selection.selected = [
198 expect(action.disable(undefined)).toBeTruthy();
199 expect(action.disableDesc(undefined)).toBe('Unavailable gateway(s)');
202 it('should be enabled if active sessions', () => {
203 component.selection.selected = [
211 expect(action.disable(undefined)).toBeFalsy();
212 expect(action.disableDesc(undefined)).toBeUndefined();
215 it('should be enabled if no active sessions', () => {
216 component.selection.selected = [
224 expect(action.disable(undefined)).toBeFalsy();
225 expect(action.disableDesc(undefined)).toBeUndefined();
229 describe('delete', () => {
231 action = getAction('Delete');
234 it('should be disabled if no gateways', () => {
235 component.selection.selected = [
240 expect(action.disable(undefined)).toBeTruthy();
241 expect(action.disableDesc(undefined)).toBe('Unavailable gateway(s)');
244 it('should be disabled if active sessions', () => {
245 component.selection.selected = [
253 expect(action.disable(undefined)).toBeTruthy();
254 expect(action.disableDesc(undefined)).toBe('Target has active sessions');
257 it('should be enabled if no active sessions', () => {
258 component.selection.selected = [
266 expect(action.disable(undefined)).toBeFalsy();
267 expect(action.disableDesc(undefined)).toBeUndefined();
272 it('should test all TableActions combinations', () => {
273 const permissionHelper: PermissionHelper = new PermissionHelper(component.permission);
274 const tableActions: TableActionsComponent = permissionHelper.setPermissionsAndGetActions(
275 component.tableActions
278 expect(tableActions).toEqual({
279 'create,update,delete': {
280 actions: ['Create', 'Edit', 'Delete'],
281 primary: { multiple: 'Create', executing: 'Edit', single: 'Edit', no: 'Create' }
284 actions: ['Create', 'Edit'],
285 primary: { multiple: 'Create', executing: 'Edit', single: 'Edit', no: 'Create' }
288 actions: ['Create', 'Delete'],
289 primary: { multiple: 'Create', executing: 'Delete', single: 'Delete', no: 'Create' }
293 primary: { multiple: 'Create', executing: 'Create', single: 'Create', no: 'Create' }
296 actions: ['Edit', 'Delete'],
297 primary: { multiple: 'Edit', executing: 'Edit', single: 'Edit', no: 'Edit' }
301 primary: { multiple: 'Edit', executing: 'Edit', single: 'Edit', no: 'Edit' }
305 primary: { multiple: 'Delete', executing: 'Delete', single: 'Delete', no: 'Delete' }
309 primary: { multiple: '', executing: '', single: '', no: '' }