]> git.proxmox.com Git - ceph.git/blame - ceph/src/pybind/mgr/dashboard/frontend/src/app/ceph/block/mirroring/bootstrap-create-modal/bootstrap-create-modal.component.spec.ts
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / src / app / ceph / block / mirroring / bootstrap-create-modal / bootstrap-create-modal.component.spec.ts
CommitLineData
9f95a23c
TL
1import { HttpClientTestingModule } from '@angular/common/http/testing';
2import { ComponentFixture, TestBed } from '@angular/core/testing';
3import { ReactiveFormsModule } from '@angular/forms';
4import { RouterTestingModule } from '@angular/router/testing';
5
f67539c2 6import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
9f95a23c
TL
7import { ToastrModule } from 'ngx-toastr';
8import { of } from 'rxjs';
9
f67539c2
TL
10import { RbdMirroringService } from '~/app/shared/api/rbd-mirroring.service';
11import { NotificationService } from '~/app/shared/services/notification.service';
12import { SharedModule } from '~/app/shared/shared.module';
13import { configureTestBed, FormHelper } from '~/testing/unit-test-helper';
9f95a23c
TL
14import { BootstrapCreateModalComponent } from './bootstrap-create-modal.component';
15
16describe('BootstrapCreateModalComponent', () => {
17 let component: BootstrapCreateModalComponent;
18 let fixture: ComponentFixture<BootstrapCreateModalComponent>;
19 let notificationService: NotificationService;
20 let rbdMirroringService: RbdMirroringService;
21 let formHelper: FormHelper;
22
23 configureTestBed({
24 declarations: [BootstrapCreateModalComponent],
25 imports: [
26 HttpClientTestingModule,
27 ReactiveFormsModule,
28 RouterTestingModule,
29 SharedModule,
30 ToastrModule.forRoot()
31 ],
f67539c2 32 providers: [NgbActiveModal]
9f95a23c
TL
33 });
34
35 beforeEach(() => {
36 fixture = TestBed.createComponent(BootstrapCreateModalComponent);
37 component = fixture.componentInstance;
38 component.siteName = 'site-A';
39
f67539c2 40 notificationService = TestBed.inject(NotificationService);
9f95a23c
TL
41 spyOn(notificationService, 'show').and.stub();
42
f67539c2 43 rbdMirroringService = TestBed.inject(RbdMirroringService);
9f95a23c
TL
44
45 formHelper = new FormHelper(component.createBootstrapForm);
46
47 spyOn(rbdMirroringService, 'getSiteName').and.callFake(() => of({ site_name: 'site-A' }));
48 spyOn(rbdMirroringService, 'subscribeSummary').and.callFake((call) =>
49 of({
50 content_data: {
51 pools: [
52 { name: 'pool1', mirror_mode: 'disabled' },
53 { name: 'pool2', mirror_mode: 'disabled' },
54 { name: 'pool3', mirror_mode: 'disabled' }
55 ]
56 }
57 }).subscribe(call)
58 );
59 });
60
61 it('should create', () => {
62 expect(component).toBeTruthy();
63 });
64
65 describe('generate token', () => {
66 beforeEach(() => {
67 spyOn(rbdMirroringService, 'refresh').and.stub();
f67539c2 68 spyOn(component.activeModal, 'close').and.callThrough();
9f95a23c
TL
69 fixture.detectChanges();
70 });
71
72 afterEach(() => {
73 expect(rbdMirroringService.getSiteName).toHaveBeenCalledTimes(1);
74 expect(rbdMirroringService.subscribeSummary).toHaveBeenCalledTimes(1);
75 expect(rbdMirroringService.refresh).toHaveBeenCalledTimes(1);
76 });
77
78 it('should generate a bootstrap token', () => {
79 spyOn(rbdMirroringService, 'setSiteName').and.callFake(() => of({ site_name: 'new-site-A' }));
80 spyOn(rbdMirroringService, 'updatePool').and.callFake(() => of({}));
81 spyOn(rbdMirroringService, 'createBootstrapToken').and.callFake(() => of({ token: 'token' }));
82
83 component.createBootstrapForm.patchValue({
84 siteName: 'new-site-A',
85 pools: { pool1: true, pool3: true }
86 });
87 component.generate();
88 expect(rbdMirroringService.setSiteName).toHaveBeenCalledWith('new-site-A');
89 expect(rbdMirroringService.updatePool).toHaveBeenCalledWith('pool1', {
90 mirror_mode: 'image'
91 });
92 expect(rbdMirroringService.updatePool).toHaveBeenCalledWith('pool3', {
93 mirror_mode: 'image'
94 });
95 expect(rbdMirroringService.createBootstrapToken).toHaveBeenCalledWith('pool3');
96 expect(component.createBootstrapForm.getValue('token')).toBe('token');
97 });
98 });
99
100 describe('form validation', () => {
101 beforeEach(() => {
102 fixture.detectChanges();
103 });
104
105 it('should require a site name', () => {
106 formHelper.expectErrorChange('siteName', '', 'required');
107 });
108
109 it('should require at least one pool', () => {
110 formHelper.expectError(component.createBootstrapForm.get('pools'), 'requirePool');
111 });
112 });
113});