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