migrate warning mixin to core (#94273)

This commit is contained in:
Pierre Gayvallet 2021-03-10 19:59:44 +01:00 committed by GitHub
parent 672bd95a48
commit 7d70ad776a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 44 additions and 98 deletions

View file

@ -62,18 +62,24 @@ describe('UuidService', () => {
let logger: ReturnType<typeof loggingSystemMock.create>;
let configService: ReturnType<typeof configServiceMock.create>;
let coreContext: CoreContext;
let service: EnvironmentService;
beforeEach(() => {
jest.clearAllMocks();
beforeEach(async () => {
logger = loggingSystemMock.create();
configService = getConfigService();
coreContext = mockCoreContext.create({ logger, configService });
service = new EnvironmentService(coreContext);
});
afterEach(() => {
jest.clearAllMocks();
});
describe('#setup()', () => {
it('calls resolveInstanceUuid with correct parameters', async () => {
const service = new EnvironmentService(coreContext);
await service.setup();
expect(resolveInstanceUuid).toHaveBeenCalledTimes(1);
expect(resolveInstanceUuid).toHaveBeenCalledWith({
pathConfig,
@ -83,8 +89,8 @@ describe('UuidService', () => {
});
it('calls createDataFolder with correct parameters', async () => {
const service = new EnvironmentService(coreContext);
await service.setup();
expect(createDataFolder).toHaveBeenCalledTimes(1);
expect(createDataFolder).toHaveBeenCalledWith({
pathConfig,
@ -93,8 +99,8 @@ describe('UuidService', () => {
});
it('calls writePidFile with correct parameters', async () => {
const service = new EnvironmentService(coreContext);
await service.setup();
expect(writePidFile).toHaveBeenCalledTimes(1);
expect(writePidFile).toHaveBeenCalledWith({
pidConfig,
@ -103,9 +109,31 @@ describe('UuidService', () => {
});
it('returns the uuid resolved from resolveInstanceUuid', async () => {
const service = new EnvironmentService(coreContext);
const setup = await service.setup();
expect(setup.instanceUuid).toEqual('SOME_UUID');
});
describe('process warnings', () => {
it('logs warnings coming from the process', async () => {
await service.setup();
const warning = new Error('something went wrong');
process.emit('warning', warning);
expect(logger.get('process').warn).toHaveBeenCalledTimes(1);
expect(logger.get('process').warn).toHaveBeenCalledWith(warning);
});
it('does not log deprecation warnings', async () => {
await service.setup();
const warning = new Error('something went wrong');
warning.name = 'DeprecationWarning';
process.emit('warning', warning);
expect(logger.get('process').warn).not.toHaveBeenCalled();
});
});
});
});

View file

@ -30,11 +30,13 @@ export interface InternalEnvironmentServiceSetup {
/** @internal */
export class EnvironmentService {
private readonly log: Logger;
private readonly processLogger: Logger;
private readonly configService: IConfigService;
private uuid: string = '';
constructor(core: CoreContext) {
this.log = core.logger.get('environment');
this.processLogger = core.logger.get('process');
this.configService = core.configService;
}
@ -50,6 +52,14 @@ export class EnvironmentService {
this.log.warn(`Detected an unhandled Promise rejection.\n${reason}`);
});
process.on('warning', (warning) => {
// deprecation warnings do no reflect a current problem for the user and should be filtered out.
if (warning.name === 'DeprecationWarning') {
return;
}
this.processLogger.warn(warning);
});
await createDataFolder({ pathConfig, logger: this.log });
await writePidFile({ pidConfig, logger: this.log });

View file

@ -1,13 +0,0 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
export default function (kbnServer, server) {
server.decorate('server', 'config', function () {
return kbnServer.config;
});
}

View file

@ -1,54 +0,0 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import completeMixin from './complete';
import sinon from 'sinon';
describe('server/config completeMixin()', function () {
const sandbox = sinon.createSandbox();
afterEach(() => sandbox.restore());
const setup = (options = {}) => {
const { settings = {}, configValues = {}, disabledPluginSpecs = [], plugins = [] } = options;
const server = {
decorate: sinon.stub(),
};
const config = {
get: sinon.stub().returns(configValues),
};
const kbnServer = {
newPlatform: {},
settings,
server,
config,
disabledPluginSpecs,
plugins,
};
const callCompleteMixin = () => completeMixin(kbnServer, server, config);
return { config, callCompleteMixin, server };
};
describe('server decoration', () => {
it('adds a config() function to the server', async () => {
const { config, callCompleteMixin, server } = setup({
settings: {},
configValues: {},
});
await callCompleteMixin();
sinon.assert.calledOnce(server.decorate);
sinon.assert.calledWith(server.decorate, 'server', 'config', sinon.match.func);
expect(server.decorate.firstCall.args[2]()).toBe(config);
});
});
});

View file

@ -15,8 +15,6 @@ import { Config } from './config';
import httpMixin from './http';
import { coreMixin } from './core';
import { loggingMixin } from './logging';
import warningsMixin from './warnings';
import configCompleteMixin from './config/complete';
import { optimizeMixin } from '../../optimize';
/**
@ -66,10 +64,6 @@ export default class KbnServer {
coreMixin,
loggingMixin,
warningsMixin,
// tell the config we are done loading plugins
configCompleteMixin,
// setup routes that serve the @kbn/optimizer output
optimizeMixin

View file

@ -1,19 +0,0 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
export default function (kbnServer, server) {
process.on('warning', (warning) => {
// deprecation warnings do no reflect a current problem for
// the user and therefor should be filtered out.
if (warning.name === 'DeprecationWarning') {
return;
}
server.log(['warning', 'process'], warning);
});
}