]> git.apps.os.sepia.ceph.com Git - ceph.git/blob
17d8eed0fb68616faca6eca23c43fd7ce2bc7613
[ceph.git] /
1 import { HttpClientTestingModule } from '@angular/common/http/testing';
2 import { ComponentFixture, TestBed } from '@angular/core/testing';
3 import { By } from '@angular/platform-browser';
4 import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
5 import { RouterTestingModule } from '@angular/router/testing';
6
7 import { NgbNavModule } from '@ng-bootstrap/ng-bootstrap';
8 import moment from 'moment';
9 import { NgxPipeFunctionModule } from 'ngx-pipe-function';
10 import { ToastrModule } from 'ngx-toastr';
11 import { of } from 'rxjs';
12
13 import { RbdService } from '~/app/shared/api/rbd.service';
14 import { CdTableSelection } from '~/app/shared/models/cd-table-selection';
15 import { ExecutingTask } from '~/app/shared/models/executing-task';
16 import { Summary } from '~/app/shared/models/summary.model';
17 import { SummaryService } from '~/app/shared/services/summary.service';
18 import { TaskListService } from '~/app/shared/services/task-list.service';
19 import { SharedModule } from '~/app/shared/shared.module';
20 import { configureTestBed, expectItemTasks } from '~/testing/unit-test-helper';
21 import { RbdTabsComponent } from '../rbd-tabs/rbd-tabs.component';
22 import { RbdTrashListComponent } from './rbd-trash-list.component';
23
24 describe('RbdTrashListComponent', () => {
25   let component: RbdTrashListComponent;
26   let fixture: ComponentFixture<RbdTrashListComponent>;
27   let summaryService: SummaryService;
28   let rbdService: RbdService;
29
30   configureTestBed({
31     declarations: [RbdTrashListComponent, RbdTabsComponent],
32     imports: [
33       BrowserAnimationsModule,
34       HttpClientTestingModule,
35       RouterTestingModule,
36       SharedModule,
37       NgbNavModule,
38       NgxPipeFunctionModule,
39       ToastrModule.forRoot()
40     ],
41     providers: [TaskListService]
42   });
43
44   beforeEach(() => {
45     fixture = TestBed.createComponent(RbdTrashListComponent);
46     component = fixture.componentInstance;
47     summaryService = TestBed.inject(SummaryService);
48     rbdService = TestBed.inject(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(new Summary());
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(new Summary());
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: moment()
123       });
124     };
125
126     beforeEach(() => {
127       summaryService['summaryDataSource'].next(new Summary());
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 });