]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/blob
7ece91e76e59818387fcef07ed93848d5fc2e6eb
[ceph-ci.git] /
1 import { HttpClientTestingModule } from '@angular/common/http/testing';
2 import { ComponentFixture, TestBed } from '@angular/core/testing';
3
4 import _ from 'lodash';
5 import { NgxPipeFunctionModule } from 'ngx-pipe-function';
6 import { ToastrModule } from 'ngx-toastr';
7 import { of } from 'rxjs';
8
9 import { CephModule } from '~/app/ceph/ceph.module';
10 import { CoreModule } from '~/app/core/core.module';
11 import { CephServiceService } from '~/app/shared/api/ceph-service.service';
12 import { HostService } from '~/app/shared/api/host.service';
13 import { PaginateObservable } from '~/app/shared/api/paginate.model';
14 import { CdTableFetchDataContext } from '~/app/shared/models/cd-table-fetch-data-context';
15 import { SharedModule } from '~/app/shared/shared.module';
16 import { configureTestBed } from '~/testing/unit-test-helper';
17 import { ServiceDaemonListComponent } from './service-daemon-list.component';
18
19 describe('ServiceDaemonListComponent', () => {
20   let component: ServiceDaemonListComponent;
21   let fixture: ComponentFixture<ServiceDaemonListComponent>;
22
23   const daemons = [
24     {
25       hostname: 'osd0',
26       container_id: '003c10beafc8c27b635bcdfed1ed832e4c1005be89bb1bb05ad4cc6c2b98e41b',
27       container_image_id: 'e70344c77bcbf3ee389b9bf5128f635cf95f3d59e005c5d8e67fc19bcc74ed23',
28       container_image_name: 'docker.io/ceph/daemon-base:latest-master-devel',
29       daemon_id: '3',
30       daemon_type: 'osd',
31       daemon_name: 'osd.3',
32       version: '15.1.0-1174-g16a11f7',
33       memory_usage: '17.7',
34       cpu_percentage: '3.54%',
35       status: 1,
36       status_desc: 'running',
37       last_refresh: '2020-02-25T04:33:26.465699',
38       events: [
39         { created: '2020-02-24T04:33:26.465699' },
40         { created: '2020-02-25T04:33:26.465699' },
41         { created: '2020-02-26T04:33:26.465699' }
42       ]
43     },
44     {
45       hostname: 'osd0',
46       container_id: 'baeec41a01374b3ed41016d542d19aef4a70d69c27274f271e26381a0cc58e7a',
47       container_image_id: 'e70344c77bcbf3ee389b9bf5128f635cf95f3d59e005c5d8e67fc19bcc74ed23',
48       container_image_name: 'docker.io/ceph/daemon-base:latest-master-devel',
49       daemon_id: '4',
50       daemon_type: 'osd',
51       daemon_name: 'osd.4',
52       version: '15.1.0-1174-g16a11f7',
53       memory_usage: '17.7',
54       cpu_percentage: '3.54%',
55       status: 1,
56       status_desc: 'running',
57       last_refresh: '2020-02-25T04:33:26.465822',
58       events: []
59     },
60     {
61       hostname: 'osd0',
62       container_id: '8483de277e365bea4365cee9e1f26606be85c471e4da5d51f57e4b85a42c616e',
63       container_image_id: 'e70344c77bcbf3ee389b9bf5128f635cf95f3d59e005c5d8e67fc19bcc74ed23',
64       container_image_name: 'docker.io/ceph/daemon-base:latest-master-devel',
65       daemon_id: '5',
66       daemon_type: 'osd',
67       daemon_name: 'osd.5',
68       version: '15.1.0-1174-g16a11f7',
69       memory_usage: '17.7',
70       cpu_percentage: '3.54%',
71       status: 1,
72       status_desc: 'running',
73       last_refresh: '2020-02-25T04:33:26.465886',
74       events: []
75     },
76     {
77       hostname: 'mon0',
78       container_id: '6ca0574f47e300a6979eaf4e7c283a8c4325c2235ae60358482fc4cd58844a21',
79       container_image_id: 'e70344c77bcbf3ee389b9bf5128f635cf95f3d59e005c5d8e67fc19bcc74ed23',
80       container_image_name: 'docker.io/ceph/daemon-base:latest-master-devel',
81       daemon_id: 'a',
82       daemon_name: 'mon.a',
83       daemon_type: 'mon',
84       version: '15.1.0-1174-g16a11f7',
85       memory_usage: '17.7',
86       cpu_percentage: '3.54%',
87       status: 1,
88       status_desc: 'running',
89       last_refresh: '2020-02-25T04:33:26.465886',
90       events: []
91     }
92   ];
93
94   const services = [
95     {
96       service_type: 'osd',
97       service_name: 'osd',
98       status: {
99         container_image_id: 'e70344c77bcbf3ee389b9bf5128f635cf95f3d59e005c5d8e67fc19bcc74ed23',
100         container_image_name: 'docker.io/ceph/daemon-base:latest-master-devel',
101         size: 3,
102         running: 3,
103         last_refresh: '2020-02-25T04:33:26.465699'
104       },
105       events: '2021-03-22T07:34:48.582163Z service:osd [INFO] "service was created"'
106     },
107     {
108       service_type: 'crash',
109       service_name: 'crash',
110       status: {
111         container_image_id: 'e70344c77bcbf3ee389b9bf5128f635cf95f3d59e005c5d8e67fc19bcc74ed23',
112         container_image_name: 'docker.io/ceph/daemon-base:latest-master-devel',
113         size: 1,
114         running: 1,
115         last_refresh: '2020-02-25T04:33:26.465766'
116       },
117       events: '2021-03-22T07:34:48.582163Z service:osd [INFO] "service was created"'
118     }
119   ];
120
121   const context = new CdTableFetchDataContext(() => undefined);
122
123   const getDaemonsByHostname = (hostname?: string) => {
124     return hostname ? _.filter(daemons, { hostname: hostname }) : daemons;
125   };
126
127   const getDaemonsByServiceName = (serviceName?: string) => {
128     return serviceName ? _.filter(daemons, { daemon_type: serviceName }) : daemons;
129   };
130
131   configureTestBed({
132     imports: [
133       HttpClientTestingModule,
134       CephModule,
135       CoreModule,
136       NgxPipeFunctionModule,
137       SharedModule,
138       ToastrModule.forRoot()
139     ]
140   });
141
142   beforeEach(() => {
143     fixture = TestBed.createComponent(ServiceDaemonListComponent);
144     component = fixture.componentInstance;
145     const hostService = TestBed.inject(HostService);
146     const cephServiceService = TestBed.inject(CephServiceService);
147     spyOn(hostService, 'getDaemons').and.callFake(() =>
148       of(getDaemonsByHostname(component.hostname))
149     );
150     spyOn(cephServiceService, 'getDaemons').and.callFake(() =>
151       of(getDaemonsByServiceName(component.serviceName))
152     );
153
154     const paginate_obs = new PaginateObservable<any>(of(services));
155     spyOn(cephServiceService, 'list').and.returnValue(paginate_obs);
156     context.pageInfo.offset = 0;
157     context.pageInfo.limit = -1;
158
159     fixture.detectChanges();
160   });
161
162   it('should create', () => {
163     expect(component).toBeTruthy();
164   });
165
166   it('should list daemons by host', () => {
167     component.hostname = 'mon0';
168     component.getDaemons(context);
169     expect(component.daemons.length).toBe(1);
170   });
171
172   it('should list daemons by service', () => {
173     component.serviceName = 'osd';
174     component.getDaemons(context);
175     expect(component.daemons.length).toBe(3);
176   });
177
178   it('should list services', () => {
179     component.getServices(context);
180     expect(component.services.length).toBe(2);
181   });
182
183   it('should not display doc panel if orchestrator is available', () => {
184     expect(component.showDocPanel).toBeFalsy();
185   });
186
187   it('should call daemon action', () => {
188     const daemon = daemons[0];
189     component.selection.selected = [daemon];
190     component['daemonService'].action = jest.fn(() => of());
191     for (const action of ['start', 'stop', 'restart', 'redeploy']) {
192       component.daemonAction(action);
193       expect(component['daemonService'].action).toHaveBeenCalledWith(daemon.daemon_name, action);
194     }
195   });
196
197   it('should disable daemon actions', () => {
198     const daemon = {
199       daemon_type: 'osd',
200       status_desc: 'running'
201     };
202
203     const states = {
204       start: true,
205       stop: false,
206       restart: false,
207       redeploy: false
208     };
209     const expectBool = (toExpect: boolean, arg: boolean) => {
210       if (toExpect === true) {
211         expect(arg).toBeTruthy();
212       } else {
213         expect(arg).toBeFalsy();
214       }
215     };
216
217     component.selection.selected = [daemon];
218     for (const action of ['start', 'stop', 'restart', 'redeploy']) {
219       expectBool(states[action], component.actionDisabled(action));
220     }
221
222     daemon.status_desc = 'stopped';
223     states.start = false;
224     states.stop = true;
225     component.selection.selected = [daemon];
226     for (const action of ['start', 'stop', 'restart', 'redeploy']) {
227       expectBool(states[action], component.actionDisabled(action));
228     }
229   });
230
231   it('should disable daemon actions in mgr and mon daemon', () => {
232     const daemon = {
233       daemon_type: 'mgr',
234       status_desc: 'running'
235     };
236     for (const action of ['start', 'stop', 'restart', 'redeploy']) {
237       expect(component.actionDisabled(action)).toBeTruthy();
238     }
239     daemon.daemon_type = 'mon';
240     for (const action of ['start', 'stop', 'restart', 'redeploy']) {
241       expect(component.actionDisabled(action)).toBeTruthy();
242     }
243   });
244
245   it('should disable daemon actions if no selection', () => {
246     component.selection.selected = [];
247     for (const action of ['start', 'stop', 'restart', 'redeploy']) {
248       expect(component.actionDisabled(action)).toBeTruthy();
249     }
250   });
251
252   it('should sort daemons events', () => {
253     component.sortDaemonEvents();
254     const daemon = daemons[0];
255     for (let i = 1; i < daemon.events.length; i++) {
256       const t1 = new Date(daemon.events[i - 1].created).getTime();
257       const t2 = new Date(daemon.events[i].created).getTime();
258       expect(t1 >= t2).toBeTruthy();
259     }
260   });
261 });