From 74c50af341662f17301f3ce9affabfc7c3a5d5fb Mon Sep 17 00:00:00 2001 From: Pierre Gayvallet Date: Wed, 10 Mar 2021 19:59:44 +0100 Subject: [PATCH] migrate warning mixin to core (#94273) --- .../environment/environment_service.test.ts | 40 +++++++++++--- .../server/environment/environment_service.ts | 10 ++++ src/legacy/server/config/complete.js | 13 ----- src/legacy/server/config/complete.test.js | 54 ------------------- src/legacy/server/kbn_server.js | 6 --- src/legacy/server/warnings/index.js | 19 ------- 6 files changed, 44 insertions(+), 98 deletions(-) delete mode 100644 src/legacy/server/config/complete.js delete mode 100644 src/legacy/server/config/complete.test.js delete mode 100644 src/legacy/server/warnings/index.js diff --git a/src/core/server/environment/environment_service.test.ts b/src/core/server/environment/environment_service.test.ts index efcf349075940..fb3ddaa77b416 100644 --- a/src/core/server/environment/environment_service.test.ts +++ b/src/core/server/environment/environment_service.test.ts @@ -62,18 +62,24 @@ describe('UuidService', () => { let logger: ReturnType; let configService: ReturnType; 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(); + }); + }); }); }); diff --git a/src/core/server/environment/environment_service.ts b/src/core/server/environment/environment_service.ts index a6bcdf4c35661..e652622049cfa 100644 --- a/src/core/server/environment/environment_service.ts +++ b/src/core/server/environment/environment_service.ts @@ -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 }); diff --git a/src/legacy/server/config/complete.js b/src/legacy/server/config/complete.js deleted file mode 100644 index 5d3b2e55288bb..0000000000000 --- a/src/legacy/server/config/complete.js +++ /dev/null @@ -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; - }); -} diff --git a/src/legacy/server/config/complete.test.js b/src/legacy/server/config/complete.test.js deleted file mode 100644 index be12414d77e7b..0000000000000 --- a/src/legacy/server/config/complete.test.js +++ /dev/null @@ -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); - }); - }); -}); diff --git a/src/legacy/server/kbn_server.js b/src/legacy/server/kbn_server.js index 34dd700aef414..55593d13d4687 100644 --- a/src/legacy/server/kbn_server.js +++ b/src/legacy/server/kbn_server.js @@ -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 diff --git a/src/legacy/server/warnings/index.js b/src/legacy/server/warnings/index.js deleted file mode 100644 index d08b9c4219744..0000000000000 --- a/src/legacy/server/warnings/index.js +++ /dev/null @@ -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); - }); -}