]> git.apps.os.sepia.ceph.com Git - ceph.git/blob
c7aaddcd08fdb87eaeb592b06be3531c08b87c2e
[ceph.git] /
1 import { ComponentFixture, TestBed, fakeAsync, tick } from '@angular/core/testing';
2 import { of, BehaviorSubject, combineLatest } from 'rxjs';
3 import { RgwOverviewDashboardComponent } from './rgw-overview-dashboard.component';
4 import { HttpClientTestingModule } from '@angular/common/http/testing';
5 import { RgwBucketService } from '~/app/shared/api/rgw-bucket.service';
6 import { RgwDaemonService } from '~/app/shared/api/rgw-daemon.service';
7 import { RgwDaemon } from '../models/rgw-daemon';
8 import { CardComponent } from '~/app/shared/components/card/card.component';
9 import { CardRowComponent } from '~/app/shared/components/card-row/card-row.component';
10 import { DimlessBinaryPipe } from '~/app/shared/pipes/dimless-binary.pipe';
11 import { NO_ERRORS_SCHEMA } from '@angular/core';
12 import { RgwRealmService } from '~/app/shared/api/rgw-realm.service';
13 import { RgwZoneService } from '~/app/shared/api/rgw-zone.service';
14 import { RgwZonegroupService } from '~/app/shared/api/rgw-zonegroup.service';
15
16 describe('RgwOverviewDashboardComponent', () => {
17   let component: RgwOverviewDashboardComponent;
18   let fixture: ComponentFixture<RgwOverviewDashboardComponent>;
19   let listDaemonsSpy: jest.SpyInstance;
20   let listRealmsSpy: jest.SpyInstance;
21   let listZonegroupsSpy: jest.SpyInstance;
22   let listZonesSpy: jest.SpyInstance;
23   let fetchAndTransformBucketsSpy: jest.SpyInstance;
24   let totalBucketsAndUsersSpy: jest.SpyInstance;
25
26   const totalNumObjectsSubject = new BehaviorSubject<number>(290);
27   const totalUsedCapacitySubject = new BehaviorSubject<number>(9338880);
28   const averageObjectSizeSubject = new BehaviorSubject<number>(1280);
29   const bucketsCount = 2;
30   const usersCount = 5;
31   const daemon: RgwDaemon = {
32     id: '8000',
33     service_map_id: '4803',
34     version: 'ceph version',
35     server_hostname: 'ceph',
36     realm_name: 'realm1',
37     zonegroup_name: 'zg1-realm1',
38     zonegroup_id: 'zg1-id',
39     zone_name: 'zone1-zg1-realm1',
40     default: true,
41     port: 80
42   };
43
44   const realmList = {
45     default_info: '20f61d29-7e45-4418-8e19-b7e962e4860b',
46     realms: ['realm2', 'realm1']
47   };
48
49   const zonegroupList = {
50     default_info: '20f61d29-7e45-4418-8e19-b7e962e4860b',
51     zonegroups: ['zg-1', 'zg-2', 'zg-3']
52   };
53
54   const zoneList = {
55     default_info: '20f61d29-7e45-4418-8e19-b7e962e4860b',
56     zones: ['zone4', 'zone5', 'zone6', 'zone7']
57   };
58
59   beforeEach(() => {
60     TestBed.configureTestingModule({
61       declarations: [
62         RgwOverviewDashboardComponent,
63         CardComponent,
64         CardRowComponent,
65         DimlessBinaryPipe
66       ],
67       schemas: [NO_ERRORS_SCHEMA],
68       providers: [
69         { provide: RgwDaemonService, useValue: { list: jest.fn() } },
70         { provide: RgwRealmService, useValue: { list: jest.fn() } },
71         { provide: RgwZonegroupService, useValue: { list: jest.fn() } },
72         { provide: RgwZoneService, useValue: { list: jest.fn() } },
73         {
74           provide: RgwBucketService,
75           useValue: {
76             fetchAndTransformBuckets: jest.fn(),
77             totalNumObjects$: totalNumObjectsSubject.asObservable(),
78             totalUsedCapacity$: totalUsedCapacitySubject.asObservable(),
79             averageObjectSize$: averageObjectSizeSubject.asObservable(),
80             getTotalBucketsAndUsersLength: jest.fn()
81           }
82         }
83       ],
84       imports: [HttpClientTestingModule]
85     }).compileComponents();
86     fixture = TestBed.createComponent(RgwOverviewDashboardComponent);
87     component = fixture.componentInstance;
88     listDaemonsSpy = jest
89       .spyOn(TestBed.inject(RgwDaemonService), 'list')
90       .mockReturnValue(of([daemon]));
91     fetchAndTransformBucketsSpy = jest
92       .spyOn(TestBed.inject(RgwBucketService), 'fetchAndTransformBuckets')
93       .mockReturnValue(of(null));
94     totalBucketsAndUsersSpy = jest
95       .spyOn(TestBed.inject(RgwBucketService), 'getTotalBucketsAndUsersLength')
96       .mockReturnValue(of({ buckets_count: bucketsCount, users_count: usersCount }));
97     listRealmsSpy = jest
98       .spyOn(TestBed.inject(RgwRealmService), 'list')
99       .mockReturnValue(of(realmList));
100     listZonegroupsSpy = jest
101       .spyOn(TestBed.inject(RgwZonegroupService), 'list')
102       .mockReturnValue(of(zonegroupList));
103     listZonesSpy = jest.spyOn(TestBed.inject(RgwZoneService), 'list').mockReturnValue(of(zoneList));
104     fixture.detectChanges();
105   });
106
107   it('should create the component', () => {
108     expect(component).toBeTruthy();
109   });
110
111   it('should render all cards', () => {
112     const dashboardCards = fixture.debugElement.nativeElement.querySelectorAll('cd-card');
113     expect(dashboardCards.length).toBe(5);
114   });
115
116   it('should get data for Realms', () => {
117     expect(listRealmsSpy).toHaveBeenCalled();
118     expect(component.rgwRealmCount).toEqual(2);
119   });
120
121   it('should get data for Zonegroups', () => {
122     expect(listZonegroupsSpy).toHaveBeenCalled();
123     expect(component.rgwZonegroupCount).toEqual(3);
124   });
125
126   it('should get data for Zones', () => {
127     expect(listZonesSpy).toHaveBeenCalled();
128     expect(component.rgwZoneCount).toEqual(4);
129   });
130
131   it('should set component properties from services using combineLatest', fakeAsync(() => {
132     component.interval = of(null).subscribe(() => {
133       component.fetchDataSub = combineLatest([
134         TestBed.inject(RgwDaemonService).list(),
135         TestBed.inject(RgwBucketService).fetchAndTransformBuckets(),
136         totalNumObjectsSubject.asObservable(),
137         totalUsedCapacitySubject.asObservable(),
138         averageObjectSizeSubject.asObservable(),
139         TestBed.inject(RgwBucketService).getTotalBucketsAndUsersLength()
140       ]).subscribe(([daemonData, _, objectCount, usedCapacity, averageSize, bucketData]) => {
141         component.rgwDaemonCount = daemonData.length;
142         component.objectCount = objectCount;
143         component.totalPoolUsedBytes = usedCapacity;
144         component.averageObjectSize = averageSize;
145         component.rgwBucketCount = bucketData.buckets_count;
146         component.UserCount = bucketData.users_count;
147       });
148     });
149     tick();
150     expect(listDaemonsSpy).toHaveBeenCalled();
151     expect(fetchAndTransformBucketsSpy).toHaveBeenCalled();
152     expect(totalBucketsAndUsersSpy).toHaveBeenCalled();
153     expect(component.rgwDaemonCount).toEqual(1);
154     expect(component.objectCount).toEqual(290);
155     expect(component.totalPoolUsedBytes).toEqual(9338880);
156     expect(component.averageObjectSize).toEqual(1280);
157     expect(component.rgwBucketCount).toEqual(bucketsCount);
158     expect(component.UserCount).toEqual(usersCount);
159   }));
160 });