Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Reporting] fix unsupported platform crash #120659

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions x-pack/plugins/screenshotting/server/plugin.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* 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 { ScreenshotModePluginSetup } from 'src/plugins/screenshot_mode/server';
import { CoreSetup, CoreStart, PluginInitializerContext } from 'kibana/server';
import { coreMock } from 'src/core/server/mocks';
import { ScreenshottingPlugin } from './plugin';
import { install } from './browsers/install';

let initContext: PluginInitializerContext;
let coreSetup: CoreSetup;
let coreStart: CoreStart;
let setupDeps: {
screenshotMode: ScreenshotModePluginSetup;
};

beforeEach(() => {
const configSchema = {
browser: { chromium: { disableSandbox: false } },
};
initContext = coreMock.createPluginInitializerContext(configSchema);
coreSetup = coreMock.createSetup({});
coreStart = coreMock.createStart();
setupDeps = {
screenshotMode: {} as ScreenshotModePluginSetup,
};
});

test('sets up and starts properly', async () => {
const plugin = new ScreenshottingPlugin(initContext);
const setupContract = plugin.setup(coreSetup, setupDeps);
expect(setupContract).toMatchInlineSnapshot(`Object {}`);

await coreSetup.getStartServices();

const startContract = plugin.start(coreStart);
expect(startContract).toMatchInlineSnapshot(`
Object {
"diagnose": [Function],
"getScreenshots": [Function],
}
`);
});

test('handles setup issues', async () => {
const plugin = new ScreenshottingPlugin(initContext);
(install as jest.Mock).mockRejectedValue(`Unsupported platform!!!`);

const setupContract = plugin.setup(coreSetup, setupDeps);
expect(setupContract).toMatchInlineSnapshot(`Object {}`);

await coreSetup.getStartServices();

const startContract = plugin.start(coreStart);
expect(startContract).toMatchInlineSnapshot(`
Object {
"diagnose": [Function],
"getScreenshots": [Function],
}
`);
});
27 changes: 18 additions & 9 deletions x-pack/plugins/screenshotting/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ export class ScreenshottingPlugin implements Plugin<void, ScreenshottingStart, S
private config: ConfigType;
private logger: Logger;
private screenshotMode!: ScreenshotModePluginSetup;
private browserDriverFactory!: Promise<HeadlessChromiumDriverFactory>;
private browserDriverFactory!: Promise<HeadlessChromiumDriverFactory | undefined>;
private error?: Error;

constructor(context: PluginInitializerContext<ConfigType>) {
this.logger = context.logger.get();
Expand All @@ -65,9 +66,9 @@ export class ScreenshottingPlugin implements Plugin<void, ScreenshottingStart, S

return new HeadlessChromiumDriverFactory(this.screenshotMode, config, logger, binaryPath);
} catch (error) {
this.error = error;
this.logger.error('Error in screenshotting setup, it may not function properly.');

throw error;
this.logger.error(error);
}
})();

Expand All @@ -76,12 +77,20 @@ export class ScreenshottingPlugin implements Plugin<void, ScreenshottingStart, S

start({}: CoreStart): ScreenshottingStart {
return {
diagnose: () =>
from(this.browserDriverFactory).pipe(switchMap((factory) => factory.diagnose())),
getScreenshots: (options) =>
from(this.browserDriverFactory).pipe(
switchMap((factory) => getScreenshots(factory, this.logger.get('screenshot'), options))
),
diagnose: () => {
if (this.error) {
throw this.error;
}
return from(this.browserDriverFactory).pipe(switchMap((factory) => factory!.diagnose()));
},
getScreenshots: (options) => {
if (this.error) {
throw this.error;
}
return from(this.browserDriverFactory).pipe(
switchMap((factory) => getScreenshots(factory!, this.logger.get('screenshot'), options))
);
},
};
}

Expand Down