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