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 '@circlon/angular-tree-component';
7 import { NgbNavModule } from '@ng-bootstrap/ng-bootstrap';
8 import { ToastrModule } from 'ngx-toastr';
9 import { BehaviorSubject, of } from 'rxjs';
11 import { IscsiService } from '~/app/shared/api/iscsi.service';
12 import { TableActionsComponent } from '~/app/shared/datatable/table-actions/table-actions.component';
13 import { CdTableAction } from '~/app/shared/models/cd-table-action';
14 import { ExecutingTask } from '~/app/shared/models/executing-task';
15 import { SummaryService } from '~/app/shared/services/summary.service';
16 import { TaskListService } from '~/app/shared/services/task-list.service';
17 import { SharedModule } from '~/app/shared/shared.module';
18 import { configureTestBed, expectItemTasks, PermissionHelper } from '~/testing/unit-test-helper';
19 import { IscsiTabsComponent } from '../iscsi-tabs/iscsi-tabs.component';
20 import { IscsiTargetDetailsComponent } from '../iscsi-target-details/iscsi-target-details.component';
21 import { IscsiTargetListComponent } from './iscsi-target-list.component';
23 describe('IscsiTargetListComponent', () => {
24 let component: IscsiTargetListComponent;
25 let fixture: ComponentFixture<IscsiTargetListComponent>;
26 let summaryService: SummaryService;
27 let iscsiService: IscsiService;
29 const refresh = (data: any) => {
30 summaryService['summaryDataSource'].next(data);
35 BrowserAnimationsModule,
36 HttpClientTestingModule,
40 ToastrModule.forRoot(),
43 declarations: [IscsiTargetListComponent, IscsiTabsComponent, IscsiTargetDetailsComponent],
44 providers: [TaskListService]
48 fixture = TestBed.createComponent(IscsiTargetListComponent);
49 component = fixture.componentInstance;
50 summaryService = TestBed.inject(SummaryService);
51 iscsiService = TestBed.inject(IscsiService);
53 // this is needed because summaryService isn't being reset after each test.
54 summaryService['summaryDataSource'] = new BehaviorSubject(null);
55 summaryService['summaryData$'] = summaryService['summaryDataSource'].asObservable();
57 spyOn(iscsiService, 'status').and.returnValue(of({ available: true }));
58 spyOn(iscsiService, 'version').and.returnValue(of({ ceph_iscsi_config_version: 11 }));
59 spyOn(component, 'setTableRefreshTimeout').and.stub();
62 it('should create', () => {
63 expect(component).toBeTruthy();
66 describe('after ngOnInit', () => {
68 spyOn(iscsiService, 'listTargets').and.callThrough();
69 fixture.detectChanges();
72 it('should load targets on init', () => {
74 expect(iscsiService.status).toHaveBeenCalled();
75 expect(iscsiService.listTargets).toHaveBeenCalled();
78 it('should not load targets on init because no data', () => {
80 expect(iscsiService.listTargets).not.toHaveBeenCalled();
83 it('should call error function on init when summary service fails', () => {
84 spyOn(component.table, 'reset');
85 summaryService['summaryDataSource'].error(undefined);
86 expect(component.table.reset).toHaveBeenCalled();
89 it('should call settings on the getTargets methods', () => {
90 spyOn(iscsiService, 'settings').and.callThrough();
91 component.getTargets();
92 expect(iscsiService.settings).toHaveBeenCalled();
96 describe('handling of executing tasks', () => {
99 const addTarget = (name: string) => {
102 portals: [{ host: 'node1', ip: '192.168.100.201' }],
103 disks: [{ pool: 'rbd', image: 'disk_1', controls: {} }],
106 client_iqn: 'iqn.1994-05.com.redhat:rh7-client',
107 luns: [{ pool: 'rbd', image: 'disk_1' }],
109 user: 'myiscsiusername',
110 password: 'myiscsipassword',
112 mutual_password: null
122 const addTask = (name: string, target_iqn: string) => {
123 const task = new ExecutingTask();
126 case 'iscsi/target/create':
128 target_iqn: target_iqn
131 case 'iscsi/target/delete':
133 target_iqn: target_iqn
138 target_iqn: target_iqn
142 summaryService.addRunningTask(task);
151 component.targets = targets;
152 refresh({ executing_tasks: [], finished_tasks: [] });
153 spyOn(iscsiService, 'listTargets').and.callFake(() => of(targets));
154 fixture.detectChanges();
157 it('should gets all targets without tasks', () => {
158 expect(component.targets.length).toBe(3);
159 expect(component.targets.every((target) => !target.cdExecuting)).toBeTruthy();
162 it('should add a new target from a task', () => {
163 addTask('iscsi/target/create', 'iqn.d');
164 expect(component.targets.length).toBe(4);
165 expectItemTasks(component.targets[0], undefined);
166 expectItemTasks(component.targets[1], undefined);
167 expectItemTasks(component.targets[2], undefined);
168 expectItemTasks(component.targets[3], 'Creating');
171 it('should show when an existing target is being modified', () => {
172 addTask('iscsi/target/delete', 'iqn.b');
173 expect(component.targets.length).toBe(3);
174 expectItemTasks(component.targets[1], 'Deleting');
178 describe('handling of actions', () => {
180 fixture.detectChanges();
183 let action: CdTableAction;
185 const getAction = (name: string): CdTableAction => {
186 return component.tableActions.find((tableAction) => tableAction.name === name);
189 describe('edit', () => {
191 action = getAction('Edit');
194 it('should be disabled if no gateways', () => {
195 component.selection.selected = [
200 expect(action.disable(undefined)).toBe('Unavailable gateway(s)');
203 it('should be enabled if active sessions', () => {
204 component.selection.selected = [
212 expect(action.disable(undefined)).toBeFalsy();
215 it('should be enabled if no active sessions', () => {
216 component.selection.selected = [
224 expect(action.disable(undefined)).toBeFalsy();
228 describe('delete', () => {
230 action = getAction('Delete');
233 it('should be disabled if no gateways', () => {
234 component.selection.selected = [
239 expect(action.disable(undefined)).toBe('Unavailable gateway(s)');
242 it('should be disabled if active sessions', () => {
243 component.selection.selected = [
251 expect(action.disable(undefined)).toBe('Target has active sessions');
254 it('should be enabled if no active sessions', () => {
255 component.selection.selected = [
263 expect(action.disable(undefined)).toBeFalsy();
268 it('should test all TableActions combinations', () => {
269 const permissionHelper: PermissionHelper = new PermissionHelper(component.permission);
270 const tableActions: TableActionsComponent = permissionHelper.setPermissionsAndGetActions(
271 component.tableActions
274 expect(tableActions).toEqual({
275 'create,update,delete': {
276 actions: ['Create', 'Edit', 'Delete'],
277 primary: { multiple: 'Create', executing: 'Edit', single: 'Edit', no: 'Create' }
280 actions: ['Create', 'Edit'],
281 primary: { multiple: 'Create', executing: 'Edit', single: 'Edit', no: 'Create' }
284 actions: ['Create', 'Delete'],
285 primary: { multiple: 'Create', executing: 'Delete', single: 'Delete', no: 'Create' }
289 primary: { multiple: 'Create', executing: 'Create', single: 'Create', no: 'Create' }
292 actions: ['Edit', 'Delete'],
293 primary: { multiple: 'Edit', executing: 'Edit', single: 'Edit', no: 'Edit' }
297 primary: { multiple: 'Edit', executing: 'Edit', single: 'Edit', no: 'Edit' }
301 primary: { multiple: 'Delete', executing: 'Delete', single: 'Delete', no: 'Delete' }
305 primary: { multiple: '', executing: '', single: '', no: '' }