From e62dc02b3929abff3b05d973131bd495c342fd04 Mon Sep 17 00:00:00 2001 From: Tim Sullivan Date: Tue, 7 Dec 2021 15:23:47 -0700 Subject: [PATCH] [Reporting] fix unsupported platform crash (#120659) * [Reporting] fix unsupported platform crash * revert test code * improve types and add test * Apply suggestions from code review Co-authored-by: Michael Dokolin * updates per feedback - remove the error class member * add stack trace of error * revert change to guard a type Co-authored-by: Michael Dokolin --- .../screenshotting/server/plugin.test.ts | 76 +++++++++++++++++++ .../plugins/screenshotting/server/plugin.ts | 25 +++--- 2 files changed, 88 insertions(+), 13 deletions(-) create mode 100644 x-pack/plugins/screenshotting/server/plugin.test.ts diff --git a/x-pack/plugins/screenshotting/server/plugin.test.ts b/x-pack/plugins/screenshotting/server/plugin.test.ts new file mode 100644 index 0000000000000..22cd2c2f75ac5 --- /dev/null +++ b/x-pack/plugins/screenshotting/server/plugin.test.ts @@ -0,0 +1,76 @@ +/* + * 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; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +jest.mock('./browsers/install'); + +import type { CoreSetup, CoreStart, PluginInitializerContext } from 'kibana/server'; +import { coreMock } from 'src/core/server/mocks'; +import type { ScreenshotModePluginSetup } from 'src/plugins/screenshot_mode/server'; +import { install } from './browsers/install'; +import { ScreenshottingPlugin } from './plugin'; + +describe('ScreenshottingPlugin', () => { + let initContext: PluginInitializerContext; + let coreSetup: CoreSetup; + let coreStart: CoreStart; + let setupDeps: Parameters[1]; + let plugin: ScreenshottingPlugin; + + beforeEach(() => { + const configSchema = { + browser: { chromium: { disableSandbox: false } }, + }; + initContext = coreMock.createPluginInitializerContext(configSchema); + coreSetup = coreMock.createSetup({}); + coreStart = coreMock.createStart(); + setupDeps = { + screenshotMode: {} as ScreenshotModePluginSetup, + }; + plugin = new ScreenshottingPlugin(initContext); + }); + + describe('setup', () => { + test('returns a setup contract', async () => { + const setupContract = plugin.setup(coreSetup, setupDeps); + expect(setupContract).toEqual({}); + }); + + test('handles setup issues', async () => { + (install as jest.Mock).mockRejectedValue(`Unsupported platform!!!`); + + const setupContract = plugin.setup(coreSetup, setupDeps); + expect(setupContract).toEqual({}); + + await coreSetup.getStartServices(); + + const startContract = plugin.start(coreStart); + expect(startContract).toEqual( + expect.objectContaining({ + diagnose: expect.any(Function), + getScreenshots: expect.any(Function), + }) + ); + }); + }); + + describe('start', () => { + beforeEach(async () => { + plugin.setup(coreSetup, setupDeps); + await coreSetup.getStartServices(); + }); + + test('returns a start contract', async () => { + const startContract = plugin.start(coreStart); + expect(startContract).toEqual( + expect.objectContaining({ + diagnose: expect.any(Function), + getScreenshots: expect.any(Function), + }) + ); + }); + }); +}); diff --git a/x-pack/plugins/screenshotting/server/plugin.ts b/x-pack/plugins/screenshotting/server/plugin.ts index 53f855e1f544d..a301dd6764367 100755 --- a/x-pack/plugins/screenshotting/server/plugin.ts +++ b/x-pack/plugins/screenshotting/server/plugin.ts @@ -55,22 +55,21 @@ export class ScreenshottingPlugin implements Plugin { - try { - const paths = new ChromiumArchivePaths(); - const logger = this.logger.get('chromium'); - const [config, binaryPath] = await Promise.all([ - createConfig(this.logger, this.config), - install(paths, logger), - ]); + const paths = new ChromiumArchivePaths(); + const logger = this.logger.get('chromium'); + const [config, binaryPath] = await Promise.all([ + createConfig(this.logger, this.config), + install(paths, logger), + ]); - return new HeadlessChromiumDriverFactory(this.screenshotMode, config, logger, binaryPath); - } catch (error) { - this.logger.error('Error in screenshotting setup, it may not function properly.'); - - throw error; - } + return new HeadlessChromiumDriverFactory(this.screenshotMode, config, logger, binaryPath); })(); + this.browserDriverFactory.catch((error) => { + this.logger.error('Error in screenshotting setup, it may not function properly.'); + this.logger.error(error); + }); + return {}; }