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