]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/blob
9acfabb0875185de7bffa9c90263265989fd8332
[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 { BootstrapImportModalComponent } from './bootstrap-import-modal.component';
19
20 describe('BootstrapImportModalComponent', () => {
21   let component: BootstrapImportModalComponent;
22   let fixture: ComponentFixture<BootstrapImportModalComponent>;
23   let notificationService: NotificationService;
24   let rbdMirroringService: RbdMirroringService;
25   let formHelper: FormHelper;
26
27   configureTestBed({
28     declarations: [BootstrapImportModalComponent],
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(BootstrapImportModalComponent);
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.importBootstrapForm);
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 import', () => {
66     expect(component).toBeTruthy();
67   });
68
69   describe('import 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, 'importBootstrapToken').and.callFake(() => of({ token: 'token' }));
86
87       component.importBootstrapForm.patchValue({
88         siteName: 'new-site-A',
89         pools: { pool1: true, pool3: true },
90         token: 'e30='
91       });
92       component.import();
93       expect(rbdMirroringService.setSiteName).toHaveBeenCalledWith('new-site-A');
94       expect(rbdMirroringService.updatePool).toHaveBeenCalledWith('pool1', {
95         mirror_mode: 'image'
96       });
97       expect(rbdMirroringService.updatePool).toHaveBeenCalledWith('pool3', {
98         mirror_mode: 'image'
99       });
100       expect(rbdMirroringService.importBootstrapToken).toHaveBeenCalledWith(
101         'pool1',
102         'rx-tx',
103         'e30='
104       );
105       expect(rbdMirroringService.importBootstrapToken).toHaveBeenCalledWith(
106         'pool3',
107         'rx-tx',
108         'e30='
109       );
110     });
111   });
112
113   describe('form validation', () => {
114     beforeEach(() => {
115       fixture.detectChanges();
116     });
117
118     it('should require a site name', () => {
119       formHelper.expectErrorChange('siteName', '', 'required');
120     });
121
122     it('should require at least one pool', () => {
123       formHelper.expectError(component.importBootstrapForm.get('pools'), 'requirePool');
124     });
125
126     it('should require a token', () => {
127       formHelper.expectErrorChange('token', '', 'required');
128     });
129
130     it('should verify token is base64-encoded JSON', () => {
131       formHelper.expectErrorChange('token', 'VEVTVA==', 'invalidToken');
132       formHelper.expectErrorChange('token', 'e2RmYXNqZGZrbH0=', 'invalidToken');
133     });
134   });
135 });