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

Add plugin test for sync/error cases #7

Merged
merged 2 commits into from
Jun 9, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
103 changes: 103 additions & 0 deletions x-pack/plugins/reporting/server/plugin.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
jest.mock('./browsers/install', () => ({
installBrowser: jest.fn().mockImplementation(() => ({
binaryPath$: {
pipe: jest.fn().mockImplementation(() => ({
toPromise: () => Promise.resolve(),
})),
},
})),
}));

import { coreMock } from 'src/core/server/mocks';
import { ReportingPlugin } from './plugin';
import { createMockConfig } from './test_helpers';

const sleep = (time: number) => new Promise((r) => setTimeout(r, time));

describe('Reporting Plugin', () => {
let config: any;
let initContext: any;
let coreSetup: any;
let coreStart: any;
let pluginSetup: any;
let pluginStart: any;

beforeEach(async () => {
config = createMockConfig();
initContext = coreMock.createPluginInitializerContext(config);
coreSetup = await coreMock.createSetup(config);
coreStart = await coreMock.createStart();
pluginSetup = ({
licensing: {},
usageCollection: {
makeUsageCollector: jest.fn(),
registerCollector: jest.fn(),
},
security: {
authc: {
getCurrentUser: () => ({
id: '123',
roles: ['superuser'],
username: 'Tom Riddle',
}),
},
},
} as unknown) as any;
pluginStart = ({
data: {
fieldFormats: {},
},
} as unknown) as any;
});

it('has a sync setup process', () => {
const plugin = new ReportingPlugin(initContext);

expect(plugin.setup(coreSetup, pluginSetup)).not.toHaveProperty('then');
});

it('logs setup issues', async () => {
const plugin = new ReportingPlugin(initContext);
// @ts-ignore overloading error logger
plugin.logger.error = jest.fn();
coreSetup.elasticsearch = null;
plugin.setup(coreSetup, pluginSetup);

await sleep(5);

// @ts-ignore overloading error logger
expect(plugin.logger.error.mock.calls[0][0]).toMatch(
/Error in Reporting setup, reporting may not function properly/
);
// @ts-ignore overloading error logger
expect(plugin.logger.error).toHaveBeenCalledTimes(2);
});

it('has a sync startup process', async () => {
const plugin = new ReportingPlugin(initContext);
plugin.setup(coreSetup, pluginSetup);
await sleep(5);
expect(plugin.start(coreStart, pluginStart)).not.toHaveProperty('then');
});

it('logs start issues', async () => {
const plugin = new ReportingPlugin(initContext);
// @ts-ignore overloading error logger
plugin.logger.error = jest.fn();
plugin.setup(coreSetup, pluginSetup);
await sleep(5);
plugin.start(null as any, pluginStart);
await sleep(10);
// @ts-ignore overloading error logger
expect(plugin.logger.error.mock.calls[0][0]).toMatch(
/Error in Reporting start, reporting may not function properly/
);
// @ts-ignore overloading error logger
expect(plugin.logger.error).toHaveBeenCalledTimes(2);
});
});
12 changes: 10 additions & 2 deletions x-pack/plugins/reporting/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ export class ReportingPlugin
const basePath = http.basePath.get;

// async background setup
buildConfig(initContext, core, this.logger).then((config) => {
(async () => {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This felt better/easier to write more async setup tasks with in the future since we have our own .catch in case issues should arise

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like it!

const config = await buildConfig(initContext, core, this.logger);
const reportingCore = new ReportingCore(config);

reportingCore.pluginSetup({
Expand All @@ -55,6 +56,9 @@ export class ReportingPlugin

this.logger.debug('Setup complete');
this.setup$.next(true);
})().catch((e) => {
this.logger.error(`Error in Reporting setup, reporting may not function properly`);
this.logger.error(e);
});

return {};
Expand All @@ -70,7 +74,8 @@ export class ReportingPlugin
const { elasticsearch } = reportingCore.getPluginSetupDeps();

// async background start
initializeBrowserDriverFactory(config, logger).then(async (browserDriverFactory) => {
(async () => {
const browserDriverFactory = await initializeBrowserDriverFactory(config, logger);
reportingCore.setBrowserDriverFactory(browserDriverFactory);

const esqueue = await createQueueFactory(reportingCore, logger);
Expand All @@ -88,6 +93,9 @@ export class ReportingPlugin

this.logger.debug('Start complete');
this.start$.next(true);
})().catch((e) => {
this.logger.error(`Error in Reporting start, reporting may not function properly`);
this.logger.error(e);
});

return {};
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/reporting/server/routes/jobs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ interface ListQuery {
}
const MAIN_ENTRY = `${API_BASE_URL}/jobs`;

export async function registerJobInfoRoutes(reporting: ReportingCore) {
export function registerJobInfoRoutes(reporting: ReportingCore) {
const config = reporting.getConfig();
const setupDeps = reporting.getPluginSetupDeps();
const userHandler = authorizedUserPreRoutingFactory(reporting);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,30 +36,35 @@ const createMockSetupDeps = (setupMock?: any): ReportingSetupDeps => {
licensing: {
license$: of({ isAvailable: true, isActive: true, type: 'basic' }),
} as any,
usageCollection: {} as any,
usageCollection: {
makeUsageCollector: jest.fn(),
registerCollector: jest.fn(),
} as any,
};
};

export const createMockConfig = (overrides?: any) => ({
index: '.reporting',
kibanaServer: {
hostname: 'localhost',
port: '80',
},
capture: {
browser: {
chromium: {
disableSandbox: true,
},
},
},
...overrides,
});
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: since this doesn't return a ReportingConfig object, it may be better to have this be called createMockConfigSchema


export const createMockStartDeps = (startMock?: any): ReportingStartDeps => ({
data: startMock.data,
});

const createMockReportingPlugin = async (config: ReportingConfig): Promise<ReportingPlugin> => {
const mockConfig = {
index: '.reporting',
kibanaServer: {
hostname: 'localhost',
port: '80',
},
capture: {
browser: {
chromium: {
disableSandbox: true,
},
},
},
...config,
};
const mockConfig = createMockConfig(config);
const plugin = new ReportingPlugin(coreMock.createPluginInitializerContext(mockConfig));
const setupMock = coreMock.createSetup();
const coreStartMock = coreMock.createStart();
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/reporting/server/test_helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
*/

export { createMockServer } from './create_mock_server';
export { createMockReportingCore } from './create_mock_reportingplugin';
export { createMockReportingCore, createMockConfig } from './create_mock_reportingplugin';
export { createMockBrowserDriverFactory } from './create_mock_browserdriverfactory';
export { createMockLayoutInstance } from './create_mock_layoutinstance';