]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/blob
68dc6303f46befa8208ba8250fb7558d7e571918
[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 { TabsModule } from 'ngx-bootstrap/tabs';
7 import { ToastrModule } from 'ngx-toastr';
8 import { of } from 'rxjs';
9
10 import { By } from '@angular/platform-browser';
11 import {
12   configureTestBed,
13   expectItemTasks,
14   i18nProviders
15 } from '../../../../testing/unit-test-helper';
16 import { RbdService } from '../../../shared/api/rbd.service';
17 import { CdTableSelection } from '../../../shared/models/cd-table-selection';
18 import { ExecutingTask } from '../../../shared/models/executing-task';
19 import { SummaryService } from '../../../shared/services/summary.service';
20 import { TaskListService } from '../../../shared/services/task-list.service';
21 import { SharedModule } from '../../../shared/shared.module';
22 import { RbdTabsComponent } from '../rbd-tabs/rbd-tabs.component';
23 import { RbdTrashListComponent } from './rbd-trash-list.component';
24
25 describe('RbdTrashListComponent', () => {
26   let component: RbdTrashListComponent;
27   let fixture: ComponentFixture<RbdTrashListComponent>;
28   let summaryService: SummaryService;
29   let rbdService: RbdService;
30
31   configureTestBed({
32     declarations: [RbdTrashListComponent, RbdTabsComponent],
33     imports: [
34       BrowserAnimationsModule,
35       HttpClientTestingModule,
36       RouterTestingModule,
37       SharedModule,
38       TabsModule.forRoot(),
39       ToastrModule.forRoot()
40     ],
41     providers: [TaskListService, i18nProviders]
42   });
43
44   beforeEach(() => {
45     fixture = TestBed.createComponent(RbdTrashListComponent);
46     component = fixture.componentInstance;
47     summaryService = TestBed.get(SummaryService);
48     rbdService = TestBed.get(RbdService);
49     fixture.detectChanges();
50   });
51
52   it('should create', () => {
53     expect(component).toBeTruthy();
54   });
55
56   it('should load trash images when summary is trigged', () => {
57     spyOn(rbdService, 'listTrash').and.callThrough();
58
59     summaryService['summaryDataSource'].next({ executingTasks: null });
60     expect(rbdService.listTrash).toHaveBeenCalled();
61   });
62
63   it('should call updateSelection', () => {
64     expect(component.selection.hasSelection).toBeFalsy();
65     component.updateSelection(new CdTableSelection(['foo']));
66     expect(component.selection.hasSelection).toBeTruthy();
67   });
68
69   describe('handling of executing tasks', () => {
70     let images: any[];
71
72     const addImage = (id: string) => {
73       images.push({
74         id: id,
75         pool_name: 'pl'
76       });
77     };
78
79     const addTask = (name: string, image_id: string) => {
80       const task = new ExecutingTask();
81       task.name = name;
82       task.metadata = {
83         image_id_spec: `pl/${image_id}`
84       };
85       summaryService.addRunningTask(task);
86     };
87
88     beforeEach(() => {
89       images = [];
90       addImage('1');
91       addImage('2');
92       component.images = images;
93       summaryService['summaryDataSource'].next({ executingTasks: [] });
94       spyOn(rbdService, 'listTrash').and.callFake(() =>
95         of([{ pool_name: 'rbd', status: 1, value: images }])
96       );
97       fixture.detectChanges();
98     });
99
100     it('should gets all images without tasks', () => {
101       expect(component.images.length).toBe(2);
102       expect(
103         component.images.every((image: Record<string, any>) => !image.cdExecuting)
104       ).toBeTruthy();
105     });
106
107     it('should show when an existing image is being modified', () => {
108       addTask('rbd/trash/remove', '1');
109       addTask('rbd/trash/restore', '2');
110       expect(component.images.length).toBe(2);
111       expectItemTasks(component.images[0], 'Deleting');
112       expectItemTasks(component.images[1], 'Restoring');
113     });
114   });
115
116   describe('display purge button', () => {
117     let images: any[];
118     const addImage = (id: string) => {
119       images.push({
120         id: id,
121         pool_name: 'pl',
122         deferment_end_time: 'abc'
123       });
124     };
125
126     beforeEach(() => {
127       summaryService['summaryDataSource'].next({ executingTasks: [] });
128       spyOn(rbdService, 'listTrash').and.callFake(() => {
129         of([{ pool_name: 'rbd', status: 1, value: images }]);
130       });
131       fixture.detectChanges();
132     });
133
134     it('should show button disabled when no image is in trash', () => {
135       expect(component.disablePurgeBtn).toBeTruthy();
136     });
137
138     it('should show button enabled when an existing image is in trash', () => {
139       images = [];
140       addImage('1');
141       const payload = [{ pool_name: 'rbd', status: 1, value: images }];
142       component.prepareResponse(payload);
143       expect(component.disablePurgeBtn).toBeFalsy();
144     });
145
146     it('should show button with delete permission', () => {
147       component.permission = {
148         read: true,
149         create: true,
150         delete: true,
151         update: true
152       };
153       fixture.detectChanges();
154
155       const purge = fixture.debugElement.query(By.css('.table-actions button .fa-times'));
156       expect(purge).not.toBeNull();
157     });
158
159     it('should remove button without delete permission', () => {
160       component.permission = {
161         read: true,
162         create: true,
163         delete: false,
164         update: true
165       };
166       fixture.detectChanges();
167
168       const purge = fixture.debugElement.query(By.css('.table-actions button .fa-times'));
169       expect(purge).toBeNull();
170     });
171   });
172 });