Skip to content

Commit

Permalink
move plugin schema registration in plugins plugins service
Browse files Browse the repository at this point in the history
plugins system is not setup when kibana is run in optimizer mode,
so config keys aren't marked as applied.
  • Loading branch information
mshustov committed May 9, 2019
1 parent 12d9947 commit 2a01ed7
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 22 deletions.
47 changes: 46 additions & 1 deletion src/core/server/plugins/plugins_service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@

import { mockDiscover, mockPackage } from './plugins_service.test.mocks';

import { resolve } from 'path';
import { resolve, join } from 'path';
import { BehaviorSubject, from } from 'rxjs';
import { schema } from '@kbn/config-schema';

import { Config, ConfigService, Env, ObjectToConfigAdapter } from '../config';
import { getEnvOptions } from '../config/__mocks__/env';
Expand All @@ -44,6 +45,13 @@ const setupDeps = {
http: httpServiceMock.createSetupContract(),
};
const logger = loggingServiceMock.create();

['path-1', 'path-2', 'path-3', 'path-4', 'path-5'].forEach(path => {
jest.doMock(join(path, 'server'), () => ({}), {
virtual: true,
});
});

beforeEach(() => {
mockPackage.raw = {
branch: 'feature-v1',
Expand Down Expand Up @@ -328,3 +336,40 @@ test('`stop` stops plugins system', async () => {
await pluginsService.stop();
expect(mockPluginSystem.stopPlugins).toHaveBeenCalledTimes(1);
});

test('`setup` registers plugin config schema in config service', async () => {
const configSchema = schema.string();
jest.spyOn(configService, 'setSchema').mockImplementation(() => Promise.resolve());
jest.doMock(
join('path-with-schema', 'server'),
() => ({
config: {
schema: configSchema,
},
}),
{
virtual: true,
}
);
mockDiscover.mockReturnValue({
error$: from([]),
plugin$: from([
new PluginWrapper(
'path-with-schema',
{
id: 'some-id',
version: 'some-version',
configPath: 'path',
kibanaVersion: '7.0.0',
requiredPlugins: [],
optionalPlugins: [],
server: true,
ui: true,
},
{ logger } as any
),
]),
});
await pluginsService.setup(setupDeps);
expect(configService.setSchema).toBeCalledWith('path', configSchema);
});
4 changes: 4 additions & 0 deletions src/core/server/plugins/plugins_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ export class PluginsService implements CoreService<PluginsServiceSetup, PluginsS
await plugin$
.pipe(
mergeMap(async plugin => {
const schema = plugin.getConfigSchema();
if (schema) {
await this.coreContext.configService.setSchema(plugin.configPath, schema);
}
const isEnabled = await this.coreContext.configService.isEnabledAtPath(plugin.configPath);

if (pluginEnableStatuses.has(plugin.name)) {
Expand Down
15 changes: 0 additions & 15 deletions src/core/server/plugins/plugins_system.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import {
} from './plugins_system.test.mocks';

import { BehaviorSubject } from 'rxjs';
import { schema } from '@kbn/config-schema';

import { Env } from '../config';
import { getEnvOptions } from '../config/__mocks__/env';
Expand Down Expand Up @@ -119,7 +118,6 @@ test('`setupPlugins` throws if plugins have circular optional dependency', async
test('`setupPlugins` ignores missing optional dependency', async () => {
const plugin = createPlugin('some-id', { optional: ['missing-dep'] });
jest.spyOn(plugin, 'setup').mockResolvedValue('test');
jest.spyOn(plugin, 'getConfigSchema').mockReturnValue(null);

pluginsSystem.addPlugin(plugin);

Expand Down Expand Up @@ -182,7 +180,6 @@ test('correctly orders plugins and returns exposed values for "setup" and "start
[...plugins.keys()].forEach((plugin, index) => {
jest.spyOn(plugin, 'setup').mockResolvedValue(`added-as-${index}`);
jest.spyOn(plugin, 'start').mockResolvedValue(`started-as-${index}`);
jest.spyOn(plugin, 'getConfigSchema').mockReturnValue(null);

setupContextMap.set(plugin.name, `setup-for-${plugin.name}`);
startContextMap.set(plugin.name, `start-for-${plugin.name}`);
Expand Down Expand Up @@ -269,7 +266,6 @@ test('`setupPlugins` only setups plugins that have server side', async () => {

[firstPluginToRun, secondPluginNotToRun, thirdPluginToRun].forEach((plugin, index) => {
jest.spyOn(plugin, 'setup').mockResolvedValue(`added-as-${index}`);
jest.spyOn(plugin, 'getConfigSchema').mockReturnValue(null);

pluginsSystem.addPlugin(plugin);
});
Expand Down Expand Up @@ -359,7 +355,6 @@ test('`startPlugins` only starts plugins that were setup', async () => {
[firstPluginToRun, secondPluginNotToRun, thirdPluginToRun].forEach((plugin, index) => {
jest.spyOn(plugin, 'setup').mockResolvedValue(`setup-as-${index}`);
jest.spyOn(plugin, 'start').mockResolvedValue(`started-as-${index}`);
jest.spyOn(plugin, 'getConfigSchema').mockReturnValue(null);

pluginsSystem.addPlugin(plugin);
});
Expand All @@ -378,13 +373,3 @@ Array [
]
`);
});

test('`setup` calls configService.setSchema if plugin specifies config schema', async () => {
const pluginSchema = schema.string();
const plugin = createPlugin('plugin-1');
jest.spyOn(plugin, 'setup').mockResolvedValue('test');
jest.spyOn(plugin, 'getConfigSchema').mockReturnValue(pluginSchema);
pluginsSystem.addPlugin(plugin);
await pluginsSystem.setupPlugins(setupDeps);
await expect(configService.setSchema).toBeCalledWith('path', pluginSchema);
});
6 changes: 0 additions & 6 deletions src/core/server/plugins/plugins_system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,6 @@ export class PluginsSystem {
{} as Record<PluginName, unknown>
);

const schema = plugin.getConfigSchema();

if (schema) {
await this.coreContext.configService.setSchema(plugin.configPath, schema);
}

contracts.set(
pluginName,
await plugin.setup(
Expand Down

0 comments on commit 2a01ed7

Please sign in to comment.