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 { CdTableFetchDataContext } from '~/app/shared/models/cd-table-fetch-data-context';
14 import { SharedModule } from '~/app/shared/shared.module';
15 import { configureTestBed } from '~/testing/unit-test-helper';
16 import { ServiceDaemonListComponent } from './service-daemon-list.component';
18 describe('ServiceDaemonListComponent', () => {
19 let component: ServiceDaemonListComponent;
20 let fixture: ComponentFixture<ServiceDaemonListComponent>;
25 container_id: '003c10beafc8c27b635bcdfed1ed832e4c1005be89bb1bb05ad4cc6c2b98e41b',
26 container_image_id: 'e70344c77bcbf3ee389b9bf5128f635cf95f3d59e005c5d8e67fc19bcc74ed23',
27 container_image_name: 'docker.io/ceph/daemon-base:latest-master-devel',
31 version: '15.1.0-1174-g16a11f7',
33 status_desc: 'running',
34 last_refresh: '2020-02-25T04:33:26.465699',
36 { created: '2020-02-24T04:33:26.465699' },
37 { created: '2020-02-25T04:33:26.465699' },
38 { created: '2020-02-26T04:33:26.465699' }
43 container_id: 'baeec41a01374b3ed41016d542d19aef4a70d69c27274f271e26381a0cc58e7a',
44 container_image_id: 'e70344c77bcbf3ee389b9bf5128f635cf95f3d59e005c5d8e67fc19bcc74ed23',
45 container_image_name: 'docker.io/ceph/daemon-base:latest-master-devel',
49 version: '15.1.0-1174-g16a11f7',
51 status_desc: 'running',
52 last_refresh: '2020-02-25T04:33:26.465822',
57 container_id: '8483de277e365bea4365cee9e1f26606be85c471e4da5d51f57e4b85a42c616e',
58 container_image_id: 'e70344c77bcbf3ee389b9bf5128f635cf95f3d59e005c5d8e67fc19bcc74ed23',
59 container_image_name: 'docker.io/ceph/daemon-base:latest-master-devel',
63 version: '15.1.0-1174-g16a11f7',
65 status_desc: 'running',
66 last_refresh: '2020-02-25T04:33:26.465886',
71 container_id: '6ca0574f47e300a6979eaf4e7c283a8c4325c2235ae60358482fc4cd58844a21',
72 container_image_id: 'e70344c77bcbf3ee389b9bf5128f635cf95f3d59e005c5d8e67fc19bcc74ed23',
73 container_image_name: 'docker.io/ceph/daemon-base:latest-master-devel',
77 version: '15.1.0-1174-g16a11f7',
79 status_desc: 'running',
80 last_refresh: '2020-02-25T04:33:26.465886',
90 container_image_id: 'e70344c77bcbf3ee389b9bf5128f635cf95f3d59e005c5d8e67fc19bcc74ed23',
91 container_image_name: 'docker.io/ceph/daemon-base:latest-master-devel',
94 last_refresh: '2020-02-25T04:33:26.465699'
96 events: '2021-03-22T07:34:48.582163Z service:osd [INFO] "service was created"'
99 service_type: 'crash',
100 service_name: 'crash',
102 container_image_id: 'e70344c77bcbf3ee389b9bf5128f635cf95f3d59e005c5d8e67fc19bcc74ed23',
103 container_image_name: 'docker.io/ceph/daemon-base:latest-master-devel',
106 last_refresh: '2020-02-25T04:33:26.465766'
108 events: '2021-03-22T07:34:48.582163Z service:osd [INFO] "service was created"'
112 const getDaemonsByHostname = (hostname?: string) => {
113 return hostname ? _.filter(daemons, { hostname: hostname }) : daemons;
116 const getDaemonsByServiceName = (serviceName?: string) => {
117 return serviceName ? _.filter(daemons, { daemon_type: serviceName }) : daemons;
122 HttpClientTestingModule,
125 NgxPipeFunctionModule,
127 ToastrModule.forRoot()
132 fixture = TestBed.createComponent(ServiceDaemonListComponent);
133 component = fixture.componentInstance;
134 const hostService = TestBed.inject(HostService);
135 const cephServiceService = TestBed.inject(CephServiceService);
136 spyOn(hostService, 'getDaemons').and.callFake(() =>
137 of(getDaemonsByHostname(component.hostname))
139 spyOn(cephServiceService, 'getDaemons').and.callFake(() =>
140 of(getDaemonsByServiceName(component.serviceName))
142 spyOn(cephServiceService, 'list').and.returnValue(of(services));
143 fixture.detectChanges();
146 it('should create', () => {
147 expect(component).toBeTruthy();
150 it('should list daemons by host', () => {
151 component.hostname = 'mon0';
152 component.getDaemons(new CdTableFetchDataContext(() => undefined));
153 expect(component.daemons.length).toBe(1);
156 it('should list daemons by service', () => {
157 component.serviceName = 'osd';
158 component.getDaemons(new CdTableFetchDataContext(() => undefined));
159 expect(component.daemons.length).toBe(3);
162 it('should list services', () => {
163 component.getServices(new CdTableFetchDataContext(() => undefined));
164 expect(component.services.length).toBe(2);
167 it('should not display doc panel if orchestrator is available', () => {
168 expect(component.showDocPanel).toBeFalsy();
171 it('should call daemon action', () => {
172 const daemon = daemons[0];
173 component.selection.selected = [daemon];
174 component['daemonService'].action = jest.fn(() => of());
175 for (const action of ['start', 'stop', 'restart', 'redeploy']) {
176 component.daemonAction(action);
177 expect(component['daemonService'].action).toHaveBeenCalledWith(daemon.daemon_name, action);
181 it('should disable daemon actions', () => {
184 status_desc: 'running'
193 const expectBool = (toExpect: boolean, arg: boolean) => {
194 if (toExpect === true) {
195 expect(arg).toBeTruthy();
197 expect(arg).toBeFalsy();
201 component.selection.selected = [daemon];
202 for (const action of ['start', 'stop', 'restart', 'redeploy']) {
203 expectBool(states[action], component.actionDisabled(action));
206 daemon.status_desc = 'stopped';
207 states.start = false;
209 component.selection.selected = [daemon];
210 for (const action of ['start', 'stop', 'restart', 'redeploy']) {
211 expectBool(states[action], component.actionDisabled(action));
215 it('should disable daemon actions in mgr and mon daemon', () => {
218 status_desc: 'running'
220 for (const action of ['start', 'stop', 'restart', 'redeploy']) {
221 expect(component.actionDisabled(action)).toBeTruthy();
223 daemon.daemon_type = 'mon';
224 for (const action of ['start', 'stop', 'restart', 'redeploy']) {
225 expect(component.actionDisabled(action)).toBeTruthy();
229 it('should disable daemon actions if no selection', () => {
230 component.selection.selected = [];
231 for (const action of ['start', 'stop', 'restart', 'redeploy']) {
232 expect(component.actionDisabled(action)).toBeTruthy();
236 it('should sort daemons events', () => {
237 component.sortDaemonEvents();
238 const daemon = daemons[0];
239 for (let i = 1; i < daemon.events.length; i++) {
240 const t1 = new Date(daemon.events[i - 1].created).getTime();
241 const t2 = new Date(daemon.events[i].created).getTime();
242 expect(t1 >= t2).toBeTruthy();