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';
7 import { TypeaheadModule } from 'ngx-bootstrap/typeahead';
8 import { ToastrModule } from 'ngx-toastr';
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';
17 describe('NfsFormComponent', () => {
18 let component: NfsFormComponent;
19 let fixture: ComponentFixture<NfsFormComponent>;
20 let httpTesting: HttpTestingController;
21 let activatedRoute: ActivatedRouteStub;
24 declarations: [NfsFormComponent, NfsFormClientComponent],
26 HttpClientTestingModule,
30 ToastrModule.forRoot(),
31 TypeaheadModule.forRoot()
35 provide: ActivatedRoute,
36 useValue: new ActivatedRouteStub({ cluster_id: undefined, export_id: undefined })
43 fixture = TestBed.createComponent(NfsFormComponent);
44 component = fixture.componentInstance;
45 httpTesting = TestBed.get(HttpTestingController);
46 activatedRoute = TestBed.get(ActivatedRoute);
47 fixture.detectChanges();
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 }
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']);
63 httpTesting.expectOne('api/rgw/user/dev').flush(user_dev);
69 httpTesting.expectOne('api/rgw/user/test').flush(user_test);
73 it('should create', () => {
74 expect(component).toBeTruthy();
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' }
84 expect(component.allCephxClients).toEqual(['admin', 'fs', 'rgw']);
85 expect(component.allFsNames).toEqual([{ id: 1, name: 'a' }]);
86 expect(component.allRgwUsers).toEqual(['dev']);
89 it('should create the form', () => {
90 expect(component.nfsForm.value).toEqual({
95 fsal: { fs_name: 'a', name: '', rgw_user_id: '', user_id: '' },
100 sec_label_xattr: 'security.selinux',
101 security_label: false,
107 expect(component.nfsForm.get('cluster_id').disabled).toBeFalsy();
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();
115 component.nfsForm.patchValue({ cluster_id: 'cluster1' });
116 component.onClusterChange();
118 expect(component.daemonsSelections).toEqual([
119 { description: '', name: 'node1', selected: false, enabled: true },
120 { description: '', name: 'node2', selected: false, enabled: true }
122 expect(component.clusterType).toBe(NFSClusterType.user);
124 component.nfsForm.patchValue({ cluster_id: 'cluster2' });
125 component.onClusterChange();
126 expect(component.clusterType).toBe(NFSClusterType.orchestrator);
127 expect(component.daemonsSelections).toEqual([]);
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();
135 expect(component.nfsForm.getValue('daemons')).toEqual([]);
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();
144 describe('should submit request', () => {
146 component.nfsForm.patchValue({
149 cluster_id: 'cluster1',
151 fsal: { name: 'CEPH', user_id: 'fs', fs_name: 1, rgw_user_id: '' },
156 squash: 'no_root_squash',
163 it('should remove "pseudo" requirement when NFS v4 disabled', () => {
164 component.nfsForm.patchValue({
165 protocolNfsv4: false,
169 component.nfsForm.updateValueAndValidity({ emitEvent: false });
170 expect(component.nfsForm.valid).toBeTruthy();
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();
181 const req = httpTesting.expectOne('api/nfs-ganesha/export/cluster1/1');
182 expect(req.request.method).toBe('PUT');
183 expect(req.request.body).toEqual({
186 cluster_id: 'cluster1',
189 fsal: { fs_name: 1, name: 'CEPH', sec_label_xattr: null, user_id: 'fs' },
193 security_label: false,
194 squash: 'no_root_squash',
196 transports: ['TCP', 'UDP']
200 it('should call create', () => {
201 activatedRoute.setParams({ cluster_id: undefined, export_id: undefined });
202 component.submitAction();
204 const req = httpTesting.expectOne('api/nfs-ganesha/export');
205 expect(req.request.method).toBe('POST');
206 expect(req.request.body).toEqual({
209 cluster_id: 'cluster1',
214 sec_label_xattr: null,
220 security_label: false,
221 squash: 'no_root_squash',
223 transports: ['TCP', 'UDP']