Skip to content

Commit

Permalink
[Fleet] Make experimental config robust (#168844)
Browse files Browse the repository at this point in the history
  • Loading branch information
nchaulet authored Oct 13, 2023
1 parent 94284a1 commit 046841c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
31 changes: 31 additions & 0 deletions x-pack/plugins/fleet/server/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,18 @@
* 2.0.
*/

import { loggingSystemMock } from '@kbn/core/server/mocks';

import { config } from './config';
import { appContextService } from './services';

jest.mock('./services/app_context');

describe('Config schema', () => {
beforeEach(() => {
const mockedLogger = loggingSystemMock.createLogger();
jest.mocked(appContextService.getLogger).mockReturnValue(mockedLogger);
});
it('should not allow to specify both default output in xpack.fleet.ouputs and xpack.fleet.agents.elasticsearch.hosts ', () => {
expect(() => {
config.schema.validate({
Expand Down Expand Up @@ -70,4 +79,26 @@ describe('Config schema', () => {
});
}).not.toThrow();
});

it('should log a warning when trying to enable a non existing experimental feature', () => {
expect(() => {
config.schema.validate({
enableExperimental: ['notvalid'],
});
}).not.toThrow();

expect(appContextService.getLogger().warn).toBeCalledWith(
'[notvalid] is not a valid fleet experimental feature.'
);
});

it('should not log a warning when enabling an existing experimental feature', () => {
expect(() => {
config.schema.validate({
enableExperimental: ['displayAgentMetrics'],
});
}).not.toThrow();

expect(appContextService.getLogger().warn).not.toBeCalled();
});
});
13 changes: 5 additions & 8 deletions x-pack/plugins/fleet/server/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@ import { schema } from '@kbn/config-schema';
import type { TypeOf } from '@kbn/config-schema';
import type { PluginConfigDescriptor } from '@kbn/core/server';

import {
getExperimentalAllowedValues,
isValidExperimentalValue,
} from '../common/experimental_features';
const allowedExperimentalValues = getExperimentalAllowedValues();
import { isValidExperimentalValue } from '../common/experimental_features';

import {
PreconfiguredPackagesSchema,
Expand All @@ -25,6 +21,7 @@ import {
PreconfiguredFleetProxiesSchema,
} from './types';
import { BULK_CREATE_MAX_ARTIFACTS_BYTES } from './services/artifacts/artifacts';
import { appContextService } from './services';

const DEFAULT_BUNDLED_PACKAGE_LOCATION = path.join(__dirname, '../target/bundled_packages');
const DEFAULT_GPG_KEY_PATH = path.join(__dirname, '../target/keys/GPG-KEY-elasticsearch');
Expand Down Expand Up @@ -162,9 +159,9 @@ export const config: PluginConfigDescriptor = {
validate(list) {
for (const key of list) {
if (!isValidExperimentalValue(key)) {
return `[${key}] is not allowed. Allowed values are: ${allowedExperimentalValues.join(
', '
)}`;
appContextService
.getLogger()
.warn(`[${key}] is not a valid fleet experimental feature.`);
}
}
},
Expand Down

0 comments on commit 046841c

Please sign in to comment.