]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/frontend/src/app/ceph/nfs/nfs-form/nfs-form.component.spec.ts
import 15.2.9
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / src / app / ceph / nfs / nfs-form / nfs-form.component.spec.ts
1 import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
2 import { ComponentFixture, TestBed } from '@angular/core/testing';
3 import { ReactiveFormsModule } from '@angular/forms';
4 import { ActivatedRoute } from '@angular/router';
5 import { RouterTestingModule } from '@angular/router/testing';
6
7 import { TypeaheadModule } from 'ngx-bootstrap/typeahead';
8 import { ToastrModule } from 'ngx-toastr';
9
10 import { ActivatedRouteStub } from '../../../../testing/activated-route-stub';
11 import { configureTestBed, i18nProviders } from '../../../../testing/unit-test-helper';
12 import { SharedModule } from '../../../shared/shared.module';
13 import { NFSClusterType } from '../nfs-cluster-type.enum';
14 import { NfsFormClientComponent } from '../nfs-form-client/nfs-form-client.component';
15 import { NfsFormComponent } from './nfs-form.component';
16
17 describe('NfsFormComponent', () => {
18 let component: NfsFormComponent;
19 let fixture: ComponentFixture<NfsFormComponent>;
20 let httpTesting: HttpTestingController;
21 let activatedRoute: ActivatedRouteStub;
22
23 configureTestBed({
24 declarations: [NfsFormComponent, NfsFormClientComponent],
25 imports: [
26 HttpClientTestingModule,
27 ReactiveFormsModule,
28 RouterTestingModule,
29 SharedModule,
30 ToastrModule.forRoot(),
31 TypeaheadModule.forRoot()
32 ],
33 providers: [
34 {
35 provide: ActivatedRoute,
36 useValue: new ActivatedRouteStub({ cluster_id: undefined, export_id: undefined })
37 },
38 i18nProviders
39 ]
40 });
41
42 beforeEach(() => {
43 fixture = TestBed.createComponent(NfsFormComponent);
44 component = fixture.componentInstance;
45 httpTesting = TestBed.get(HttpTestingController);
46 activatedRoute = TestBed.get(ActivatedRoute);
47 fixture.detectChanges();
48
49 httpTesting.expectOne('api/nfs-ganesha/daemon').flush([
50 { daemon_id: 'node1', cluster_id: 'cluster1', cluster_type: NFSClusterType.user },
51 { daemon_id: 'node2', cluster_id: 'cluster1', cluster_type: NFSClusterType.user },
52 { daemon_id: 'node5', cluster_id: 'cluster2', cluster_type: NFSClusterType.orchestrator }
53 ]);
54 httpTesting.expectOne('ui-api/nfs-ganesha/fsals').flush(['CEPH', 'RGW']);
55 httpTesting.expectOne('ui-api/nfs-ganesha/cephx/clients').flush(['admin', 'fs', 'rgw']);
56 httpTesting.expectOne('ui-api/nfs-ganesha/cephfs/filesystems').flush([{ id: 1, name: 'a' }]);
57 httpTesting.expectOne('api/rgw/user').flush(['test', 'dev']);
58 const user_dev = {
59 suspended: 0,
60 user_id: 'dev',
61 keys: ['a']
62 };
63 httpTesting.expectOne('api/rgw/user/dev').flush(user_dev);
64 const user_test = {
65 suspended: 1,
66 user_id: 'test',
67 keys: ['a']
68 };
69 httpTesting.expectOne('api/rgw/user/test').flush(user_test);
70 httpTesting.verify();
71 });
72
73 it('should create', () => {
74 expect(component).toBeTruthy();
75 });
76
77 it('should process all data', () => {
78 expect(component.allDaemons).toEqual({ cluster1: ['node1', 'node2'], cluster2: ['node5'] });
79 expect(component.isDefaultCluster).toEqual(false);
80 expect(component.allFsals).toEqual([
81 { descr: 'CephFS', value: 'CEPH' },
82 { descr: 'Object Gateway', value: 'RGW' }
83 ]);
84 expect(component.allCephxClients).toEqual(['admin', 'fs', 'rgw']);
85 expect(component.allFsNames).toEqual([{ id: 1, name: 'a' }]);
86 expect(component.allRgwUsers).toEqual(['dev']);
87 });
88
89 it('should create the form', () => {
90 expect(component.nfsForm.value).toEqual({
91 access_type: 'RW',
92 clients: [],
93 cluster_id: '',
94 daemons: [],
95 fsal: { fs_name: 'a', name: '', rgw_user_id: '', user_id: '' },
96 path: '',
97 protocolNfsv3: true,
98 protocolNfsv4: true,
99 pseudo: '',
100 sec_label_xattr: 'security.selinux',
101 security_label: false,
102 squash: '',
103 tag: '',
104 transportTCP: true,
105 transportUDP: true
106 });
107 expect(component.nfsForm.get('cluster_id').disabled).toBeFalsy();
108 });
109
110 it('should prepare data when selecting an cluster', () => {
111 expect(component.allDaemons).toEqual({ cluster1: ['node1', 'node2'], cluster2: ['node5'] });
112 expect(component.daemonsSelections).toEqual([]);
113 expect(component.clusterType).toBeNull();
114
115 component.nfsForm.patchValue({ cluster_id: 'cluster1' });
116 component.onClusterChange();
117
118 expect(component.daemonsSelections).toEqual([
119 { description: '', name: 'node1', selected: false, enabled: true },
120 { description: '', name: 'node2', selected: false, enabled: true }
121 ]);
122 expect(component.clusterType).toBe(NFSClusterType.user);
123
124 component.nfsForm.patchValue({ cluster_id: 'cluster2' });
125 component.onClusterChange();
126 expect(component.clusterType).toBe(NFSClusterType.orchestrator);
127 expect(component.daemonsSelections).toEqual([]);
128 });
129
130 it('should clean data when changing cluster', () => {
131 component.nfsForm.patchValue({ cluster_id: 'cluster1', daemons: ['node1'] });
132 component.nfsForm.patchValue({ cluster_id: 'node2' });
133 component.onClusterChange();
134
135 expect(component.nfsForm.getValue('daemons')).toEqual([]);
136 });
137
138 it('should not allow changing cluster in edit mode', () => {
139 component.isEdit = true;
140 component.ngOnInit();
141 expect(component.nfsForm.get('cluster_id').disabled).toBeTruthy();
142 });
143
144 describe('should submit request', () => {
145 beforeEach(() => {
146 component.nfsForm.patchValue({
147 access_type: 'RW',
148 clients: [],
149 cluster_id: 'cluster1',
150 daemons: ['node2'],
151 fsal: { name: 'CEPH', user_id: 'fs', fs_name: 1, rgw_user_id: '' },
152 path: '/foo',
153 protocolNfsv3: true,
154 protocolNfsv4: true,
155 pseudo: '/baz',
156 squash: 'no_root_squash',
157 tag: 'bar',
158 transportTCP: true,
159 transportUDP: true
160 });
161 });
162
163 it('should remove "pseudo" requirement when NFS v4 disabled', () => {
164 component.nfsForm.patchValue({
165 protocolNfsv4: false,
166 pseudo: ''
167 });
168
169 component.nfsForm.updateValueAndValidity({ emitEvent: false });
170 expect(component.nfsForm.valid).toBeTruthy();
171 });
172
173 it('should call update', () => {
174 activatedRoute.setParams({ cluster_id: 'cluster1', export_id: '1' });
175 component.isEdit = true;
176 component.cluster_id = 'cluster1';
177 component.export_id = '1';
178 component.nfsForm.patchValue({ export_id: 1 });
179 component.submitAction();
180
181 const req = httpTesting.expectOne('api/nfs-ganesha/export/cluster1/1');
182 expect(req.request.method).toBe('PUT');
183 expect(req.request.body).toEqual({
184 access_type: 'RW',
185 clients: [],
186 cluster_id: 'cluster1',
187 daemons: ['node2'],
188 export_id: '1',
189 fsal: { fs_name: 1, name: 'CEPH', sec_label_xattr: null, user_id: 'fs' },
190 path: '/foo',
191 protocols: [3, 4],
192 pseudo: '/baz',
193 security_label: false,
194 squash: 'no_root_squash',
195 tag: 'bar',
196 transports: ['TCP', 'UDP']
197 });
198 });
199
200 it('should call create', () => {
201 activatedRoute.setParams({ cluster_id: undefined, export_id: undefined });
202 component.submitAction();
203
204 const req = httpTesting.expectOne('api/nfs-ganesha/export');
205 expect(req.request.method).toBe('POST');
206 expect(req.request.body).toEqual({
207 access_type: 'RW',
208 clients: [],
209 cluster_id: 'cluster1',
210 daemons: ['node2'],
211 fsal: {
212 fs_name: 1,
213 name: 'CEPH',
214 sec_label_xattr: null,
215 user_id: 'fs'
216 },
217 path: '/foo',
218 protocols: [3, 4],
219 pseudo: '/baz',
220 security_label: false,
221 squash: 'no_root_squash',
222 tag: 'bar',
223 transports: ['TCP', 'UDP']
224 });
225 });
226 });
227 });