Skip to content

Commit

Permalink
[Reporting] fix unsupported platform crash (elastic#120659)
Browse files Browse the repository at this point in the history
* [Reporting] fix unsupported platform crash

* revert test code

* improve types and add test

* Apply suggestions from code review

Co-authored-by: Michael Dokolin <[email protected]>

* updates per feedback - remove the error class member

* add stack trace of error

* revert change to guard a type

Co-authored-by: Michael Dokolin <[email protected]>
  • Loading branch information
2 people authored and TinLe committed Dec 22, 2021
1 parent 697a86f commit cfb36f8
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 13 deletions.
76 changes: 76 additions & 0 deletions x-pack/plugins/screenshotting/server/plugin.test.ts
Original file line number Diff line number Diff line change
@@ -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<ScreenshottingPlugin['setup']>[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),
})
);
});
});
});
25 changes: 12 additions & 13 deletions x-pack/plugins/screenshotting/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,21 @@ export class ScreenshottingPlugin implements Plugin<void, ScreenshottingStart, S
setup({}: CoreSetup, { screenshotMode }: SetupDeps) {
this.screenshotMode = screenshotMode;
this.browserDriverFactory = (async () => {
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 {};
}

Expand Down

0 comments on commit cfb36f8

Please sign in to comment.