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