]> git.apps.os.sepia.ceph.com Git - ceph.git/blob
96efaa53963bfc3cd5895d19fad84b507c2ca7c7
[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 { PoolEditPeerModalComponent } from './pool-edit-peer-modal.component';
15 import { PoolEditPeerResponseModel } from './pool-edit-peer-response.model';
16
17 describe('PoolEditPeerModalComponent', () => {
18   let component: PoolEditPeerModalComponent;
19   let fixture: ComponentFixture<PoolEditPeerModalComponent>;
20   let notificationService: NotificationService;
21   let rbdMirroringService: RbdMirroringService;
22   let formHelper: FormHelper;
23
24   configureTestBed({
25     declarations: [PoolEditPeerModalComponent],
26     imports: [
27       HttpClientTestingModule,
28       ReactiveFormsModule,
29       RouterTestingModule,
30       SharedModule,
31       ToastrModule.forRoot()
32     ],
33     providers: [NgbActiveModal]
34   });
35
36   beforeEach(() => {
37     fixture = TestBed.createComponent(PoolEditPeerModalComponent);
38     component = fixture.componentInstance;
39     component.mode = 'add';
40     component.poolName = 'somePool';
41
42     notificationService = TestBed.inject(NotificationService);
43     spyOn(notificationService, 'show').and.stub();
44
45     rbdMirroringService = TestBed.inject(RbdMirroringService);
46
47     formHelper = new FormHelper(component.editPeerForm);
48   });
49
50   it('should create', () => {
51     expect(component).toBeTruthy();
52   });
53
54   describe('add pool peer', () => {
55     beforeEach(() => {
56       component.mode = 'add';
57       component.peerUUID = undefined;
58       spyOn(rbdMirroringService, 'refresh').and.stub();
59       spyOn(component.activeModal, 'close').and.callThrough();
60       fixture.detectChanges();
61     });
62
63     afterEach(() => {
64       expect(rbdMirroringService.refresh).toHaveBeenCalledTimes(1);
65       expect(component.activeModal.close).toHaveBeenCalledTimes(1);
66     });
67
68     it('should call addPeer', () => {
69       spyOn(rbdMirroringService, 'addPeer').and.callFake(() => of(''));
70
71       component.editPeerForm.patchValue({
72         clusterName: 'cluster',
73         clientID: 'id',
74         monAddr: 'mon_host',
75         key: 'dGVzdA=='
76       });
77
78       component.update();
79       expect(rbdMirroringService.addPeer).toHaveBeenCalledWith('somePool', {
80         cluster_name: 'cluster',
81         client_id: 'id',
82         mon_host: 'mon_host',
83         key: 'dGVzdA=='
84       });
85     });
86   });
87
88   describe('edit pool peer', () => {
89     beforeEach(() => {
90       component.mode = 'edit';
91       component.peerUUID = 'somePeer';
92
93       const response = new PoolEditPeerResponseModel();
94       response.uuid = 'somePeer';
95       response.cluster_name = 'cluster';
96       response.client_id = 'id';
97       response.mon_host = '1.2.3.4:1234';
98       response.key = 'dGVzdA==';
99
100       spyOn(rbdMirroringService, 'getPeer').and.callFake(() => of(response));
101       spyOn(rbdMirroringService, 'refresh').and.stub();
102       spyOn(component.activeModal, 'close').and.callThrough();
103       fixture.detectChanges();
104     });
105
106     afterEach(() => {
107       expect(rbdMirroringService.getPeer).toHaveBeenCalledWith('somePool', 'somePeer');
108       expect(rbdMirroringService.refresh).toHaveBeenCalledTimes(1);
109       expect(component.activeModal.close).toHaveBeenCalledTimes(1);
110     });
111
112     it('should call updatePeer', () => {
113       spyOn(rbdMirroringService, 'updatePeer').and.callFake(() => of(''));
114
115       component.update();
116       expect(rbdMirroringService.updatePeer).toHaveBeenCalledWith('somePool', 'somePeer', {
117         cluster_name: 'cluster',
118         client_id: 'id',
119         mon_host: '1.2.3.4:1234',
120         key: 'dGVzdA=='
121       });
122     });
123   });
124
125   describe('form validation', () => {
126     beforeEach(() => {
127       fixture.detectChanges();
128     });
129
130     it('should validate cluster name', () => {
131       formHelper.expectErrorChange('clusterName', '', 'required');
132       formHelper.expectErrorChange('clusterName', ' ', 'invalidClusterName');
133     });
134
135     it('should validate client ID', () => {
136       formHelper.expectErrorChange('clientID', '', 'required');
137       formHelper.expectErrorChange('clientID', 'client.id', 'invalidClientID');
138     });
139
140     it('should validate monitor address', () => {
141       formHelper.expectErrorChange('monAddr', '@', 'invalidMonAddr');
142     });
143
144     it('should validate key', () => {
145       formHelper.expectErrorChange('key', '(', 'invalidKey');
146     });
147   });
148 });