]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/blob
28612801c55e2b5d040bf5158fb4d1315226fe3b
[ceph.git] /
1 import { HttpClientTestingModule } from '@angular/common/http/testing';
2 import { ReactiveFormsModule } from '@angular/forms';
3 import { RouterTestingModule } from '@angular/router/testing';
4 import { ComponentFixture, TestBed } from '@angular/core/testing';
5
6 import { ToastrModule } from 'ngx-toastr';
7
8 import { NgbActiveModal, NgbTypeaheadModule } from '@ng-bootstrap/ng-bootstrap';
9
10 import { CdFormGroup } from '~/app/shared/forms/cd-form-group';
11 import { SharedModule } from '~/app/shared/shared.module';
12
13 import { NvmeofNamespacesFormComponent } from './nvmeof-namespaces-form.component';
14 import { FormHelper, Mocks } from '~/testing/unit-test-helper';
15 import { NvmeofService } from '~/app/shared/api/nvmeof.service';
16 import { of } from 'rxjs';
17 import { PoolService } from '~/app/shared/api/pool.service';
18 import { NumberModule } from 'carbon-components-angular';
19 import { ActivatedRoute, Router } from '@angular/router';
20 import { By } from '@angular/platform-browser';
21 import { ActivatedRouteStub } from '~/testing/activated-route-stub';
22
23 const MOCK_POOLS = [
24   Mocks.getPool('pool-1', 1, ['cephfs']),
25   Mocks.getPool('rbd', 2),
26   Mocks.getPool('pool-2', 3)
27 ];
28 class MockPoolsService {
29   getList() {
30     return of(MOCK_POOLS);
31   }
32 }
33
34 const MOCK_NS_RESPONSE = {
35   nsid: 1,
36   uuid: '185d541f-76bf-45b5-b445-f71829346c38',
37   bdev_name: 'bdev_185d541f-76bf-45b5-b445-f71829346c38',
38   rbd_image_name: 'nvme_rbd_default_sscfagwuvvr',
39   rbd_pool_name: 'rbd',
40   load_balancing_group: 1,
41   rbd_image_size: '1073741824',
42   block_size: 512,
43   rw_ios_per_second: '0',
44   rw_mbytes_per_second: '0',
45   r_mbytes_per_second: '0',
46   w_mbytes_per_second: '0',
47   trash_image: false
48 };
49
50 const MOCK_ROUTER = {
51   editUrl:
52     'https://192.168.100.100:8443/#/block/nvmeof/subsystems/(modal:edit/nqn.2001-07.com.ceph:1744881547418.default/namespace/1)?group=default',
53   createUrl: 'https://192.168.100.100:8443/#/block/nvmeof/subsystems/(modal:create)?group=default'
54 };
55
56 describe('NvmeofNamespacesFormComponent', () => {
57   let component: NvmeofNamespacesFormComponent;
58   let fixture: ComponentFixture<NvmeofNamespacesFormComponent>;
59   let nvmeofService: NvmeofService;
60   let router: Router;
61   let form: CdFormGroup;
62   let formHelper: FormHelper;
63   let activatedRouteStub: ActivatedRouteStub;
64   const MOCK_RANDOM_STRING = 1720693470789;
65   const MOCK_SUBSYSTEM = 'nqn.2021-11.com.example:subsystem';
66   const MOCK_GROUP = 'default';
67   const MOCK_NSID = String(MOCK_NS_RESPONSE['nsid']);
68
69   beforeEach(async () => {
70     activatedRouteStub = new ActivatedRouteStub(
71       { subsystem_nqn: MOCK_SUBSYSTEM, nsid: MOCK_NSID },
72       { group: MOCK_GROUP }
73     );
74     await TestBed.configureTestingModule({
75       declarations: [NvmeofNamespacesFormComponent],
76       providers: [
77         NgbActiveModal,
78         { provide: PoolService, useClass: MockPoolsService },
79         { provide: ActivatedRoute, useValue: activatedRouteStub }
80       ],
81       imports: [
82         HttpClientTestingModule,
83         NgbTypeaheadModule,
84         ReactiveFormsModule,
85         RouterTestingModule,
86         SharedModule,
87         NumberModule,
88         ToastrModule.forRoot()
89       ]
90     }).compileComponents();
91     fixture = TestBed.createComponent(NvmeofNamespacesFormComponent);
92     component = fixture.componentInstance;
93   });
94
95   it('should create component', () => {
96     expect(component).toBeTruthy();
97   });
98   describe('should test create form', () => {
99     beforeEach(() => {
100       router = TestBed.inject(Router);
101       nvmeofService = TestBed.inject(NvmeofService);
102       spyOn(nvmeofService, 'createNamespace').and.stub();
103       spyOn(component, 'randomString').and.returnValue(MOCK_RANDOM_STRING);
104       Object.defineProperty(router, 'url', {
105         get: jasmine.createSpy('url').and.returnValue(MOCK_ROUTER.createUrl)
106       });
107       component.ngOnInit();
108       form = component.nsForm;
109       formHelper = new FormHelper(form);
110     });
111     it('should have set create fields correctly', () => {
112       expect(component.rbdPools.length).toBe(2);
113       fixture.detectChanges();
114       const poolEl = fixture.debugElement.query(By.css('#pool-create')).nativeElement;
115       expect(poolEl.value).toBe('rbd');
116     });
117     it('should create 5 namespaces correctly', () => {
118       component.onSubmit();
119       expect(nvmeofService.createNamespace).toHaveBeenCalledTimes(5);
120       expect(nvmeofService.createNamespace).toHaveBeenCalledWith(MOCK_SUBSYSTEM, {
121         gw_group: MOCK_GROUP,
122         rbd_image_name: `nvme_rbd_default_${MOCK_RANDOM_STRING}`,
123         rbd_pool: 'rbd',
124         create_image: true,
125         rbd_image_size: 1073741824
126       });
127     });
128     it('should give error on invalid image size', () => {
129       formHelper.setValue('image_size', -56);
130       component.onSubmit();
131       formHelper.expectError('image_size', 'pattern');
132     });
133     it('should give error on 0 image size', () => {
134       formHelper.setValue('image_size', 0);
135       component.onSubmit();
136       formHelper.expectError('image_size', 'min');
137     });
138   });
139   describe('should test edit form', () => {
140     beforeEach(() => {
141       router = TestBed.inject(Router);
142       nvmeofService = TestBed.inject(NvmeofService);
143       spyOn(nvmeofService, 'getNamespace').and.returnValue(of(MOCK_NS_RESPONSE));
144       spyOn(nvmeofService, 'updateNamespace').and.stub();
145       Object.defineProperty(router, 'url', {
146         get: jasmine.createSpy('url').and.returnValue(MOCK_ROUTER.editUrl)
147       });
148       fixture.detectChanges();
149     });
150     it('should have set edit fields correctly', () => {
151       expect(nvmeofService.getNamespace).toHaveBeenCalledTimes(1);
152       const poolEl = fixture.debugElement.query(By.css('#pool-edit')).nativeElement;
153       expect(poolEl.disabled).toBeTruthy();
154       expect(poolEl.value).toBe(MOCK_NS_RESPONSE['rbd_pool_name']);
155       const sizeEl = fixture.debugElement.query(By.css('#size')).nativeElement;
156       expect(sizeEl.value).toBe('1');
157       const unitEl = fixture.debugElement.query(By.css('#unit')).nativeElement;
158       expect(unitEl.value).toBe('GiB');
159     });
160     it('should not show namesapce count ', () => {
161       const nsCountEl = fixture.debugElement.query(By.css('#namespace-count'));
162       expect(nsCountEl).toBeFalsy();
163     });
164     it('should give error with no change in image size', () => {
165       component.onSubmit();
166       expect(component.invalidSizeError).toBe(true);
167       fixture.detectChanges();
168       const imageSizeInvalidEL = fixture.debugElement.query(By.css('#image-size-invalid'));
169       expect(imageSizeInvalidEL).toBeTruthy();
170     });
171     it('should give error when size less than previous (1 GB) provided', () => {
172       form = component.nsForm;
173       formHelper = new FormHelper(form);
174       formHelper.setValue('unit', 'MiB');
175       component.onSubmit();
176       expect(component.invalidSizeError).toBe(true);
177       fixture.detectChanges();
178       const imageSizeInvalidEL = fixture.debugElement.query(By.css('#image-size-invalid'))
179         .nativeElement;
180       expect(imageSizeInvalidEL).toBeTruthy();
181     });
182     it('should have edited namespace successfully', () => {
183       component.ngOnInit();
184       form = component.nsForm;
185       formHelper = new FormHelper(form);
186       formHelper.setValue('image_size', 2);
187       component.onSubmit();
188       expect(nvmeofService.updateNamespace).toHaveBeenCalledTimes(1);
189       expect(nvmeofService.updateNamespace).toHaveBeenCalledWith(MOCK_SUBSYSTEM, MOCK_NSID, {
190         gw_group: MOCK_GROUP,
191         rbd_image_size: 2147483648
192       });
193     });
194   });
195 });