]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/frontend/src/app/ceph/nfs/nfs-list/nfs-list.component.spec.ts
b3bd32cd59cf246f8410e1a15c74cf7f5e3a4cca
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / src / app / ceph / nfs / nfs-list / nfs-list.component.spec.ts
1 import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
2 import { ComponentFixture, fakeAsync, TestBed, tick } from '@angular/core/testing';
3 import { RouterTestingModule } from '@angular/router/testing';
4
5 import { TabsModule } from 'ngx-bootstrap/tabs';
6 import { ToastrModule } from 'ngx-toastr';
7 import { of } from 'rxjs';
8
9 import {
10 configureTestBed,
11 expectItemTasks,
12 i18nProviders,
13 PermissionHelper
14 } from '../../../../testing/unit-test-helper';
15 import { NfsService } from '../../../shared/api/nfs.service';
16 import { TableActionsComponent } from '../../../shared/datatable/table-actions/table-actions.component';
17 import { ExecutingTask } from '../../../shared/models/executing-task';
18 import { SummaryService } from '../../../shared/services/summary.service';
19 import { TaskListService } from '../../../shared/services/task-list.service';
20 import { SharedModule } from '../../../shared/shared.module';
21 import { NfsDetailsComponent } from '../nfs-details/nfs-details.component';
22 import { NfsListComponent } from './nfs-list.component';
23
24 describe('NfsListComponent', () => {
25 let component: NfsListComponent;
26 let fixture: ComponentFixture<NfsListComponent>;
27 let summaryService: SummaryService;
28 let nfsService: NfsService;
29 let httpTesting: HttpTestingController;
30
31 const refresh = (data: object) => {
32 summaryService['summaryDataSource'].next(data);
33 };
34
35 configureTestBed(
36 {
37 declarations: [NfsListComponent, NfsDetailsComponent],
38 imports: [
39 HttpClientTestingModule,
40 RouterTestingModule,
41 SharedModule,
42 ToastrModule.forRoot(),
43 TabsModule.forRoot()
44 ],
45 providers: [TaskListService, i18nProviders]
46 },
47 true
48 );
49
50 beforeEach(() => {
51 fixture = TestBed.createComponent(NfsListComponent);
52 component = fixture.componentInstance;
53 summaryService = TestBed.get(SummaryService);
54 nfsService = TestBed.get(NfsService);
55 httpTesting = TestBed.get(HttpTestingController);
56 });
57
58 it('should create', () => {
59 expect(component).toBeTruthy();
60 });
61
62 describe('after ngOnInit', () => {
63 beforeEach(() => {
64 fixture.detectChanges();
65 spyOn(nfsService, 'list').and.callThrough();
66 httpTesting.expectOne('api/nfs-ganesha/daemon').flush([]);
67 });
68
69 afterEach(() => {
70 httpTesting.verify();
71 });
72
73 it('should load exports on init', () => {
74 refresh({});
75 httpTesting.expectOne('api/nfs-ganesha/export');
76 expect(nfsService.list).toHaveBeenCalled();
77 });
78
79 it('should not load images on init because no data', () => {
80 refresh(undefined);
81 expect(nfsService.list).not.toHaveBeenCalled();
82 });
83
84 it('should call error function on init when summary service fails', () => {
85 spyOn(component.table, 'reset');
86 summaryService['summaryDataSource'].error(undefined);
87 expect(component.table.reset).toHaveBeenCalled();
88 });
89 });
90
91 describe('handling of executing tasks', () => {
92 let exports: any[];
93
94 const addExport = (export_id: string) => {
95 const model = {
96 export_id: export_id,
97 path: 'path_' + export_id,
98 fsal: 'fsal_' + export_id,
99 cluster_id: 'cluster_' + export_id
100 };
101 exports.push(model);
102 };
103
104 const addTask = (name: string, export_id: string) => {
105 const task = new ExecutingTask();
106 task.name = name;
107 switch (task.name) {
108 case 'nfs/create':
109 task.metadata = {
110 path: 'path_' + export_id,
111 fsal: 'fsal_' + export_id,
112 cluster_id: 'cluster_' + export_id
113 };
114 break;
115 default:
116 task.metadata = {
117 cluster_id: 'cluster_' + export_id,
118 export_id: export_id
119 };
120 break;
121 }
122 summaryService.addRunningTask(task);
123 };
124
125 beforeEach(() => {
126 exports = [];
127 addExport('a');
128 addExport('b');
129 addExport('c');
130 component.exports = exports;
131 refresh({ executing_tasks: [], finished_tasks: [] });
132 spyOn(nfsService, 'list').and.callFake(() => of(exports));
133 fixture.detectChanges();
134
135 const req = httpTesting.expectOne('api/nfs-ganesha/daemon');
136 req.flush([]);
137 });
138
139 it('should gets all exports without tasks', () => {
140 expect(component.exports.length).toBe(3);
141 expect(component.exports.every((expo) => !expo.cdExecuting)).toBeTruthy();
142 });
143
144 it('should add a new export from a task', fakeAsync(() => {
145 addTask('nfs/create', 'd');
146 tick();
147 expect(component.exports.length).toBe(4);
148 expectItemTasks(component.exports[0], undefined);
149 expectItemTasks(component.exports[1], undefined);
150 expectItemTasks(component.exports[2], undefined);
151 expectItemTasks(component.exports[3], 'Creating');
152 }));
153
154 it('should show when an existing export is being modified', () => {
155 addTask('nfs/edit', 'a');
156 addTask('nfs/delete', 'b');
157 expect(component.exports.length).toBe(3);
158 expectItemTasks(component.exports[0], 'Updating');
159 expectItemTasks(component.exports[1], 'Deleting');
160 });
161 });
162
163 it('should test all TableActions combinations', () => {
164 const permissionHelper: PermissionHelper = new PermissionHelper(component.permission);
165 const tableActions: TableActionsComponent = permissionHelper.setPermissionsAndGetActions(
166 component.tableActions
167 );
168
169 expect(tableActions).toEqual({
170 'create,update,delete': {
171 actions: ['Create', 'Edit', 'Delete'],
172 primary: { multiple: 'Create', executing: 'Edit', single: 'Edit', no: 'Create' }
173 },
174 'create,update': {
175 actions: ['Create', 'Edit'],
176 primary: { multiple: 'Create', executing: 'Edit', single: 'Edit', no: 'Create' }
177 },
178 'create,delete': {
179 actions: ['Create', 'Delete'],
180 primary: { multiple: 'Create', executing: 'Delete', single: 'Delete', no: 'Create' }
181 },
182 create: {
183 actions: ['Create'],
184 primary: { multiple: 'Create', executing: 'Create', single: 'Create', no: 'Create' }
185 },
186 'update,delete': {
187 actions: ['Edit', 'Delete'],
188 primary: { multiple: 'Edit', executing: 'Edit', single: 'Edit', no: 'Edit' }
189 },
190 update: {
191 actions: ['Edit'],
192 primary: { multiple: 'Edit', executing: 'Edit', single: 'Edit', no: 'Edit' }
193 },
194 delete: {
195 actions: ['Delete'],
196 primary: { multiple: 'Delete', executing: 'Delete', single: 'Delete', no: 'Delete' }
197 },
198 'no-permissions': {
199 actions: [],
200 primary: { multiple: '', executing: '', single: '', no: '' }
201 }
202 });
203 });
204 });