]> git.apps.os.sepia.ceph.com Git - ceph.git/blob
856fc19389df3f65d0c1bebd471800923f69b7a5
[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
20 const mockPools = [
21   Mocks.getPool('pool-1', 1, ['cephfs']),
22   Mocks.getPool('rbd', 2),
23   Mocks.getPool('pool-2', 3)
24 ];
25 class MockPoolService {
26   getList() {
27     return of(mockPools);
28   }
29 }
30
31 describe('NvmeofNamespacesFormComponent', () => {
32   let component: NvmeofNamespacesFormComponent;
33   let fixture: ComponentFixture<NvmeofNamespacesFormComponent>;
34   let nvmeofService: NvmeofService;
35   let form: CdFormGroup;
36   let formHelper: FormHelper;
37   const mockRandomString = 1720693470789;
38   const mockSubsystemNQN = 'nqn.2021-11.com.example:subsystem';
39   const mockGWgroup = 'default';
40
41   beforeEach(async () => {
42     await TestBed.configureTestingModule({
43       declarations: [NvmeofNamespacesFormComponent],
44       providers: [NgbActiveModal, { provide: PoolService, useClass: MockPoolService }],
45       imports: [
46         HttpClientTestingModule,
47         NgbTypeaheadModule,
48         ReactiveFormsModule,
49         RouterTestingModule,
50         SharedModule,
51         NumberModule,
52         ToastrModule.forRoot()
53       ]
54     }).compileComponents();
55     fixture = TestBed.createComponent(NvmeofNamespacesFormComponent);
56     component = fixture.componentInstance;
57     component.ngOnInit();
58     form = component.nsForm;
59     formHelper = new FormHelper(form);
60     fixture.detectChanges();
61   });
62
63   it('should create', () => {
64     expect(component).toBeTruthy();
65   });
66
67   describe('should test form', () => {
68     beforeEach(() => {
69       component.subsystemNQN = mockSubsystemNQN;
70       component.group = mockGWgroup;
71       nvmeofService = TestBed.inject(NvmeofService);
72       spyOn(nvmeofService, 'createNamespace').and.stub();
73       spyOn(component, 'randomString').and.returnValue(mockRandomString);
74     });
75     it('should create 5 namespaces correctly', () => {
76       component.onSubmit();
77       expect(nvmeofService.createNamespace).toHaveBeenCalledTimes(5);
78       expect(nvmeofService.createNamespace).toHaveBeenCalledWith(mockSubsystemNQN, {
79         gw_group: mockGWgroup,
80         rbd_image_name: `nvme_rbd_default_${mockRandomString}`,
81         rbd_pool: 'rbd',
82         rbd_image_size: 1073741824
83       });
84     });
85     it('should give error on invalid image size', () => {
86       formHelper.setValue('image_size', -56);
87       component.onSubmit();
88       formHelper.expectError('image_size', 'pattern');
89     });
90     it('should give error on 0 image size', () => {
91       formHelper.setValue('image_size', 0);
92       component.onSubmit();
93       formHelper.expectError('image_size', 'min');
94     });
95   });
96 });