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';
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 { 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, i18nProviders]
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 it('should test all TableActions combinations', () => {
176 const permissionHelper: PermissionHelper = new PermissionHelper(component.permission);
177 const tableActions: TableActionsComponent = permissionHelper.setPermissionsAndGetActions(
178 component.tableActions
181 expect(tableActions).toEqual({
182 'create,update,delete': {
183 actions: ['Create', 'Edit', 'Delete'],
184 primary: { multiple: 'Create', executing: 'Edit', single: 'Edit', no: 'Create' }
187 actions: ['Create', 'Edit'],
188 primary: { multiple: 'Create', executing: 'Edit', single: 'Edit', no: 'Create' }
191 actions: ['Create', 'Delete'],
192 primary: { multiple: 'Create', executing: 'Delete', single: 'Delete', no: 'Create' }
196 primary: { multiple: 'Create', executing: 'Create', single: 'Create', no: 'Create' }
199 actions: ['Edit', 'Delete'],
200 primary: { multiple: 'Edit', executing: 'Edit', single: 'Edit', no: 'Edit' }
204 primary: { multiple: 'Edit', executing: 'Edit', single: 'Edit', no: 'Edit' }
208 primary: { multiple: 'Delete', executing: 'Delete', single: 'Delete', no: 'Delete' }
212 primary: { multiple: '', executing: '', single: '', no: '' }