]> git.apps.os.sepia.ceph.com Git - ceph.git/blob
5227f28dd03c9a229a6f6673a9be070e8e204233
[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         rbd_image_size: 1073741824
125       });
126     });
127     it('should give error on invalid image size', () => {
128       formHelper.setValue('image_size', -56);
129       component.onSubmit();
130       formHelper.expectError('image_size', 'pattern');
131     });
132     it('should give error on 0 image size', () => {
133       formHelper.setValue('image_size', 0);
134       component.onSubmit();
135       formHelper.expectError('image_size', 'min');
136     });
137   });
138   describe('should test edit form', () => {
139     beforeEach(() => {
140       router = TestBed.inject(Router);
141       nvmeofService = TestBed.inject(NvmeofService);
142       spyOn(nvmeofService, 'getNamespace').and.returnValue(of(MOCK_NS_RESPONSE));
143       spyOn(nvmeofService, 'updateNamespace').and.stub();
144       Object.defineProperty(router, 'url', {
145         get: jasmine.createSpy('url').and.returnValue(MOCK_ROUTER.editUrl)
146       });
147       fixture.detectChanges();
148     });
149     it('should have set edit fields correctly', () => {
150       expect(nvmeofService.getNamespace).toHaveBeenCalledTimes(1);
151       const poolEl = fixture.debugElement.query(By.css('#pool-edit')).nativeElement;
152       expect(poolEl.disabled).toBeTruthy();
153       expect(poolEl.value).toBe(MOCK_NS_RESPONSE['rbd_pool_name']);
154       const sizeEl = fixture.debugElement.query(By.css('#size')).nativeElement;
155       expect(sizeEl.value).toBe('1');
156       const unitEl = fixture.debugElement.query(By.css('#unit')).nativeElement;
157       expect(unitEl.value).toBe('GiB');
158     });
159     it('should not show namesapce count ', () => {
160       const nsCountEl = fixture.debugElement.query(By.css('#namespace-count'));
161       expect(nsCountEl).toBeFalsy();
162     });
163     it('should give error with no change in image size', () => {
164       component.onSubmit();
165       expect(component.invalidSizeError).toBe(true);
166       fixture.detectChanges();
167       const imageSizeInvalidEL = fixture.debugElement.query(By.css('#image-size-invalid'));
168       expect(imageSizeInvalidEL).toBeTruthy();
169     });
170     it('should give error when size less than previous (1 GB) provided', () => {
171       form = component.nsForm;
172       formHelper = new FormHelper(form);
173       formHelper.setValue('unit', 'MiB');
174       component.onSubmit();
175       expect(component.invalidSizeError).toBe(true);
176       fixture.detectChanges();
177       const imageSizeInvalidEL = fixture.debugElement.query(By.css('#image-size-invalid'))
178         .nativeElement;
179       expect(imageSizeInvalidEL).toBeTruthy();
180     });
181     it('should have edited namespace successfully', () => {
182       component.ngOnInit();
183       form = component.nsForm;
184       formHelper = new FormHelper(form);
185       formHelper.setValue('image_size', 2);
186       component.onSubmit();
187       expect(nvmeofService.updateNamespace).toHaveBeenCalledTimes(1);
188       expect(nvmeofService.updateNamespace).toHaveBeenCalledWith(MOCK_SUBSYSTEM, MOCK_NSID, {
189         gw_group: MOCK_GROUP,
190         rbd_image_size: 2147483648
191       });
192     });
193   });
194 });