]> git.apps.os.sepia.ceph.com Git - ceph.git/blob
f8f63447644586b7a0d88f6611afbfdcce2c5994
[ceph.git] /
1 import { HttpClientTestingModule } from '@angular/common/http/testing';
2 import { ComponentFixture, TestBed } from '@angular/core/testing';
3 import { ReactiveFormsModule } from '@angular/forms';
4 import { RouterTestingModule } from '@angular/router/testing';
5
6 import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
7 import { ToastrModule } from 'ngx-toastr';
8 import { of } from 'rxjs';
9
10 import { RbdMirroringService } from '~/app/shared/api/rbd-mirroring.service';
11 import { NotificationService } from '~/app/shared/services/notification.service';
12 import { SharedModule } from '~/app/shared/shared.module';
13 import { configureTestBed, FormHelper } from '~/testing/unit-test-helper';
14 import { BootstrapCreateModalComponent } from './bootstrap-create-modal.component';
15
16 describe('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     ],
32     providers: [NgbActiveModal]
33   });
34
35   beforeEach(() => {
36     fixture = TestBed.createComponent(BootstrapCreateModalComponent);
37     component = fixture.componentInstance;
38     component.siteName = 'site-A';
39
40     notificationService = TestBed.inject(NotificationService);
41     spyOn(notificationService, 'show').and.stub();
42
43     rbdMirroringService = TestBed.inject(RbdMirroringService);
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();
68       spyOn(component.activeModal, 'close').and.callThrough();
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 });