1 import { HttpClientTestingModule } from '@angular/common/http/testing';
2 import { ComponentFixture, TestBed } from '@angular/core/testing';
4 import _ from 'lodash';
5 import { NgxPipeFunctionModule } from 'ngx-pipe-function';
6 import { ToastrModule } from 'ngx-toastr';
7 import { of } from 'rxjs';
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';
19 describe('ServiceDaemonListComponent', () => {
20 let component: ServiceDaemonListComponent;
21 let fixture: ComponentFixture<ServiceDaemonListComponent>;
26 container_id: '003c10beafc8c27b635bcdfed1ed832e4c1005be89bb1bb05ad4cc6c2b98e41b',
27 container_image_id: 'e70344c77bcbf3ee389b9bf5128f635cf95f3d59e005c5d8e67fc19bcc74ed23',
28 container_image_name: 'docker.io/ceph/daemon-base:latest-master-devel',
32 version: '15.1.0-1174-g16a11f7',
34 cpu_percentage: '3.54%',
36 status_desc: 'running',
37 last_refresh: '2020-02-25T04:33:26.465699',
39 { created: '2020-02-24T04:33:26.465699' },
40 { created: '2020-02-25T04:33:26.465699' },
41 { created: '2020-02-26T04:33:26.465699' }
46 container_id: 'baeec41a01374b3ed41016d542d19aef4a70d69c27274f271e26381a0cc58e7a',
47 container_image_id: 'e70344c77bcbf3ee389b9bf5128f635cf95f3d59e005c5d8e67fc19bcc74ed23',
48 container_image_name: 'docker.io/ceph/daemon-base:latest-master-devel',
52 version: '15.1.0-1174-g16a11f7',
54 cpu_percentage: '3.54%',
56 status_desc: 'running',
57 last_refresh: '2020-02-25T04:33:26.465822',
62 container_id: '8483de277e365bea4365cee9e1f26606be85c471e4da5d51f57e4b85a42c616e',
63 container_image_id: 'e70344c77bcbf3ee389b9bf5128f635cf95f3d59e005c5d8e67fc19bcc74ed23',
64 container_image_name: 'docker.io/ceph/daemon-base:latest-master-devel',
68 version: '15.1.0-1174-g16a11f7',
70 cpu_percentage: '3.54%',
72 status_desc: 'running',
73 last_refresh: '2020-02-25T04:33:26.465886',
78 container_id: '6ca0574f47e300a6979eaf4e7c283a8c4325c2235ae60358482fc4cd58844a21',
79 container_image_id: 'e70344c77bcbf3ee389b9bf5128f635cf95f3d59e005c5d8e67fc19bcc74ed23',
80 container_image_name: 'docker.io/ceph/daemon-base:latest-master-devel',
84 version: '15.1.0-1174-g16a11f7',
86 cpu_percentage: '3.54%',
88 status_desc: 'running',
89 last_refresh: '2020-02-25T04:33:26.465886',
99 container_image_id: 'e70344c77bcbf3ee389b9bf5128f635cf95f3d59e005c5d8e67fc19bcc74ed23',
100 container_image_name: 'docker.io/ceph/daemon-base:latest-master-devel',
103 last_refresh: '2020-02-25T04:33:26.465699'
105 events: '2021-03-22T07:34:48.582163Z service:osd [INFO] "service was created"'
108 service_type: 'crash',
109 service_name: 'crash',
111 container_image_id: 'e70344c77bcbf3ee389b9bf5128f635cf95f3d59e005c5d8e67fc19bcc74ed23',
112 container_image_name: 'docker.io/ceph/daemon-base:latest-master-devel',
115 last_refresh: '2020-02-25T04:33:26.465766'
117 events: '2021-03-22T07:34:48.582163Z service:osd [INFO] "service was created"'
121 const context = new CdTableFetchDataContext(() => undefined);
123 const getDaemonsByHostname = (hostname?: string) => {
124 return hostname ? _.filter(daemons, { hostname: hostname }) : daemons;
127 const getDaemonsByServiceName = (serviceName?: string) => {
128 return serviceName ? _.filter(daemons, { daemon_type: serviceName }) : daemons;
133 HttpClientTestingModule,
136 NgxPipeFunctionModule,
138 ToastrModule.forRoot()
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))
150 spyOn(cephServiceService, 'getDaemons').and.callFake(() =>
151 of(getDaemonsByServiceName(component.serviceName))
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;
159 fixture.detectChanges();
162 it('should create', () => {
163 expect(component).toBeTruthy();
166 it('should list daemons by host', () => {
167 component.hostname = 'mon0';
168 component.getDaemons(context);
169 expect(component.daemons.length).toBe(1);
172 it('should list daemons by service', () => {
173 component.serviceName = 'osd';
174 component.getDaemons(context);
175 expect(component.daemons.length).toBe(3);
178 it('should list services', () => {
179 component.getServices(context);
180 expect(component.services.length).toBe(2);
183 it('should not display doc panel if orchestrator is available', () => {
184 expect(component.showDocPanel).toBeFalsy();
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);
197 it('should disable daemon actions', () => {
200 status_desc: 'running'
209 const expectBool = (toExpect: boolean, arg: boolean) => {
210 if (toExpect === true) {
211 expect(arg).toBeTruthy();
213 expect(arg).toBeFalsy();
217 component.selection.selected = [daemon];
218 for (const action of ['start', 'stop', 'restart', 'redeploy']) {
219 expectBool(states[action], component.actionDisabled(action));
222 daemon.status_desc = 'stopped';
223 states.start = false;
225 component.selection.selected = [daemon];
226 for (const action of ['start', 'stop', 'restart', 'redeploy']) {
227 expectBool(states[action], component.actionDisabled(action));
231 it('should disable daemon actions in mgr and mon daemon', () => {
234 status_desc: 'running'
236 for (const action of ['start', 'stop', 'restart', 'redeploy']) {
237 expect(component.actionDisabled(action)).toBeTruthy();
239 daemon.daemon_type = 'mon';
240 for (const action of ['start', 'stop', 'restart', 'redeploy']) {
241 expect(component.actionDisabled(action)).toBeTruthy();
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();
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();