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