]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/blob
f240ab7d53f5b66ef9e401007b9ef45a98047f03
[ceph-ci.git] /
1 import { HttpClientTestingModule } from '@angular/common/http/testing';
2 import { ComponentFixture, TestBed } from '@angular/core/testing';
3 import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
4 import { RouterTestingModule } from '@angular/router/testing';
5
6 import { NgbNavModule } from '@ng-bootstrap/ng-bootstrap';
7 import { of } from 'rxjs';
8
9 import { RgwBucketService } from '~/app/shared/api/rgw-bucket.service';
10 import { TableActionsComponent } from '~/app/shared/datatable/table-actions/table-actions.component';
11 import { SharedModule } from '~/app/shared/shared.module';
12 import { configureTestBed, PermissionHelper } from '~/testing/unit-test-helper';
13 import { RgwBucketDetailsComponent } from '../rgw-bucket-details/rgw-bucket-details.component';
14 import { RgwBucketListComponent } from './rgw-bucket-list.component';
15 import { ToastrModule } from 'ngx-toastr';
16
17 describe('RgwBucketListComponent', () => {
18   let component: RgwBucketListComponent;
19   let fixture: ComponentFixture<RgwBucketListComponent>;
20   let rgwBucketService: RgwBucketService;
21   let rgwBucketServiceListSpy: jasmine.Spy;
22
23   configureTestBed({
24     declarations: [RgwBucketListComponent, RgwBucketDetailsComponent],
25     imports: [
26       BrowserAnimationsModule,
27       RouterTestingModule,
28       SharedModule,
29       NgbNavModule,
30       HttpClientTestingModule,
31       ToastrModule.forRoot()
32     ]
33   });
34
35   beforeEach(() => {
36     rgwBucketService = TestBed.inject(RgwBucketService);
37     rgwBucketServiceListSpy = spyOn(rgwBucketService, 'list');
38     rgwBucketServiceListSpy.and.returnValue(of([]));
39     fixture = TestBed.createComponent(RgwBucketListComponent);
40     component = fixture.componentInstance;
41     spyOn(component, 'setTableRefreshTimeout').and.stub();
42     fixture.detectChanges();
43   });
44
45   it('should create', () => {
46     expect(component).toBeTruthy();
47     expect(rgwBucketServiceListSpy).toHaveBeenCalledTimes(1);
48   });
49
50   it('should test all TableActions combinations', () => {
51     const permissionHelper: PermissionHelper = new PermissionHelper(component.permission);
52     const tableActions: TableActionsComponent = permissionHelper.setPermissionsAndGetActions(
53       component.tableActions
54     );
55
56     expect(tableActions).toEqual({
57       'create,update,delete': {
58         actions: ['Create', 'Edit', 'Delete'],
59         primary: {
60           multiple: 'Create',
61           executing: 'Create',
62           single: 'Create',
63           no: 'Create'
64         }
65       },
66       'create,update': {
67         actions: ['Create', 'Edit'],
68         primary: {
69           multiple: 'Create',
70           executing: 'Create',
71           single: 'Create',
72           no: 'Create'
73         }
74       },
75       'create,delete': {
76         actions: ['Create', 'Delete'],
77         primary: {
78           multiple: 'Create',
79           executing: 'Create',
80           single: 'Create',
81           no: 'Create'
82         }
83       },
84       create: {
85         actions: ['Create'],
86         primary: {
87           multiple: 'Create',
88           executing: 'Create',
89           single: 'Create',
90           no: 'Create'
91         }
92       },
93       'update,delete': {
94         actions: ['Edit', 'Delete'],
95         primary: {
96           multiple: '',
97           executing: '',
98           single: '',
99           no: ''
100         }
101       },
102       update: {
103         actions: ['Edit'],
104         primary: {
105           multiple: 'Edit',
106           executing: 'Edit',
107           single: 'Edit',
108           no: 'Edit'
109         }
110       },
111       delete: {
112         actions: ['Delete'],
113         primary: {
114           multiple: 'Delete',
115           executing: 'Delete',
116           single: 'Delete',
117           no: 'Delete'
118         }
119       },
120       'no-permissions': {
121         actions: [],
122         primary: {
123           multiple: '',
124           executing: '',
125           single: '',
126           no: ''
127         }
128       }
129     });
130   });
131
132   it('should test if bucket data is transformed correctly', () => {
133     rgwBucketServiceListSpy.and.returnValue(
134       of([
135         {
136           bucket: 'bucket',
137           owner: 'testid',
138           usage: {
139             'rgw.main': {
140               size_actual: 4,
141               num_objects: 2
142             },
143             'rgw.none': {
144               size_actual: 6,
145               num_objects: 6
146             }
147           },
148           bucket_quota: {
149             max_size: 20,
150             max_objects: 10,
151             enabled: true
152           }
153         }
154       ])
155     );
156     component.getBucketList(null);
157     expect(rgwBucketServiceListSpy).toHaveBeenCalledTimes(2);
158     expect(component.buckets).toEqual([
159       {
160         bucket: 'bucket',
161         owner: 'testid',
162         usage: {
163           'rgw.main': { size_actual: 4, num_objects: 2 },
164           'rgw.none': { size_actual: 6, num_objects: 6 }
165         },
166         bucket_quota: {
167           max_size: 20,
168           max_objects: 10,
169           enabled: true
170         },
171         bucket_size: 4,
172         num_objects: 2,
173         size_usage: 0.2,
174         object_usage: 0.2
175       }
176     ]);
177   });
178
179   it('should usage bars only if quota enabled', () => {
180     rgwBucketServiceListSpy.and.returnValue(
181       of([
182         {
183           bucket: 'bucket',
184           owner: 'testid',
185           bucket_quota: {
186             max_size: 1024,
187             max_objects: 10,
188             enabled: true
189           }
190         }
191       ])
192     );
193     component.getBucketList(null);
194     expect(rgwBucketServiceListSpy).toHaveBeenCalledTimes(2);
195     fixture.detectChanges();
196     const usageBars = fixture.debugElement.nativeElement.querySelectorAll('cd-usage-bar');
197     expect(usageBars.length).toBe(2);
198   });
199
200   it('should not show any usage bars if quota disabled', () => {
201     rgwBucketServiceListSpy.and.returnValue(
202       of([
203         {
204           bucket: 'bucket',
205           owner: 'testid',
206           bucket_quota: {
207             max_size: 1024,
208             max_objects: 10,
209             enabled: false
210           }
211         }
212       ])
213     );
214     component.getBucketList(null);
215     expect(rgwBucketServiceListSpy).toHaveBeenCalledTimes(2);
216     fixture.detectChanges();
217     const usageBars = fixture.debugElement.nativeElement.querySelectorAll('cd-usage-bar');
218     expect(usageBars.length).toBe(0);
219   });
220 });