Skip to content

Commit

Permalink
Revamp core environment class to support upcoming core<-->legacy boot…
Browse files Browse the repository at this point in the history
…strap inversion. (#21885)
  • Loading branch information
azasypkin authored Aug 23, 2018
1 parent 35abc06 commit 60ae2f5
Show file tree
Hide file tree
Showing 16 changed files with 479 additions and 473 deletions.
33 changes: 7 additions & 26 deletions src/core/server/config/__tests__/__mocks__/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,14 @@

import { EnvOptions } from '../../env';

interface MockEnvOptions {
config?: string;
kbnServer?: any;
mode?: EnvOptions['mode']['name'];
packageInfo?: Partial<EnvOptions['packageInfo']>;
}

export function getEnvOptions({
config,
kbnServer,
mode = 'development',
packageInfo = {},
}: MockEnvOptions = {}): EnvOptions {
export function getEnvOptions(options: Partial<EnvOptions> = {}): EnvOptions {
return {
config,
kbnServer,
mode: {
dev: mode === 'development',
name: mode,
prod: mode === 'production',
},
packageInfo: {
branch: 'some-branch',
buildNum: 1,
buildSha: 'some-sha-256',
version: 'some-version',
...packageInfo,
configs: options.configs || [],
cliArgs: {
dev: true,
...(options.cliArgs || {}),
},
isDevClusterMaster:
options.isDevClusterMaster !== undefined ? options.isDevClusterMaster : false,
};
}
149 changes: 149 additions & 0 deletions src/core/server/config/__tests__/__snapshots__/env.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`correctly creates default environment in dev mode.: env properties 1`] = `
Env {
"binDir": "/test/cwd/bin",
"cliArgs": Object {
"dev": true,
"someArg": 1,
"someOtherArg": "2",
},
"configDir": "/test/cwd/config",
"configs": Array [
"/test/cwd/config/kibana.yml",
],
"corePluginsDir": "/test/cwd/core_plugins",
"homeDir": "/test/cwd",
"isDevClusterMaster": true,
"legacy": EventEmitter {
"_events": Object {},
"_eventsCount": 0,
"_maxListeners": undefined,
"domain": null,
},
"logDir": "/test/cwd/log",
"mode": Object {
"dev": true,
"name": "development",
"prod": false,
},
"packageInfo": Object {
"branch": "some-branch",
"buildNum": 9007199254740991,
"buildSha": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
"version": "some-version",
},
"staticFilesDir": "/test/cwd/ui",
}
`;

exports[`correctly creates default environment in prod distributable mode.: env properties 1`] = `
Env {
"binDir": "/test/cwd/bin",
"cliArgs": Object {
"dev": false,
"someArg": 1,
"someOtherArg": "2",
},
"configDir": "/test/cwd/config",
"configs": Array [
"/some/other/path/some-kibana.yml",
],
"corePluginsDir": "/test/cwd/core_plugins",
"homeDir": "/test/cwd",
"isDevClusterMaster": false,
"legacy": EventEmitter {
"_events": Object {},
"_eventsCount": 0,
"_maxListeners": undefined,
"domain": null,
},
"logDir": "/test/cwd/log",
"mode": Object {
"dev": false,
"name": "production",
"prod": true,
},
"packageInfo": Object {
"branch": "feature-v1",
"buildNum": 100,
"buildSha": "feature-v1-build-sha",
"version": "v1",
},
"staticFilesDir": "/test/cwd/ui",
}
`;

exports[`correctly creates default environment in prod non-distributable mode.: env properties 1`] = `
Env {
"binDir": "/test/cwd/bin",
"cliArgs": Object {
"dev": false,
"someArg": 1,
"someOtherArg": "2",
},
"configDir": "/test/cwd/config",
"configs": Array [
"/some/other/path/some-kibana.yml",
],
"corePluginsDir": "/test/cwd/core_plugins",
"homeDir": "/test/cwd",
"isDevClusterMaster": false,
"legacy": EventEmitter {
"_events": Object {},
"_eventsCount": 0,
"_maxListeners": undefined,
"domain": null,
},
"logDir": "/test/cwd/log",
"mode": Object {
"dev": false,
"name": "production",
"prod": true,
},
"packageInfo": Object {
"branch": "feature-v1",
"buildNum": 9007199254740991,
"buildSha": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
"version": "v1",
},
"staticFilesDir": "/test/cwd/ui",
}
`;

exports[`correctly creates environment with constructor.: env properties 1`] = `
Env {
"binDir": "/some/home/dir/bin",
"cliArgs": Object {
"dev": false,
"someArg": 1,
"someOtherArg": "2",
},
"configDir": "/some/home/dir/config",
"configs": Array [
"/some/other/path/some-kibana.yml",
],
"corePluginsDir": "/some/home/dir/core_plugins",
"homeDir": "/some/home/dir",
"isDevClusterMaster": false,
"legacy": EventEmitter {
"_events": Object {},
"_eventsCount": 0,
"_maxListeners": undefined,
"domain": null,
},
"logDir": "/some/home/dir/log",
"mode": Object {
"dev": false,
"name": "production",
"prod": true,
},
"packageInfo": Object {
"branch": "feature-v1",
"buildNum": 100,
"buildSha": "feature-v1-build-sha",
"version": "v1",
},
"staticFilesDir": "/some/home/dir/ui",
}
`;
29 changes: 16 additions & 13 deletions src/core/server/config/__tests__/config_service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,13 @@
*/

/* tslint:disable max-classes-per-file */

import { BehaviorSubject } from 'rxjs';
import { first } from 'rxjs/operators';

const mockPackage = new Proxy({ raw: {} as any }, { get: (obj, prop) => obj.raw[prop] });
jest.mock('../../../../utils/package_json', () => ({ pkg: mockPackage }));

import { schema, Type, TypeOf } from '../schema';

import { ConfigService, ObjectToRawConfigAdapter } from '..';
Expand Down Expand Up @@ -161,21 +166,19 @@ test('tracks unhandled paths', async () => {
});

test('correctly passes context', async () => {
const config$ = new BehaviorSubject(new ObjectToRawConfigAdapter({ foo: {} }));
mockPackage.raw = {
branch: 'feature-v1',
version: 'v1',
build: {
distributable: true,
number: 100,
sha: 'feature-v1-build-sha',
},
};

const env = new Env(
'/kibana',
getEnvOptions({
mode: 'development',
packageInfo: {
branch: 'feature-v1',
buildNum: 100,
buildSha: 'feature-v1-build-sha',
version: 'v1',
},
})
);
const env = new Env('/kibana', getEnvOptions());

const config$ = new BehaviorSubject(new ObjectToRawConfigAdapter({ foo: {} }));
const configService = new ConfigService(config$, env, logger);
const configs = configService.atPath(
'foo',
Expand Down
126 changes: 66 additions & 60 deletions src/core/server/config/__tests__/env.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,76 +29,82 @@ jest.mock('path', () => ({
},
}));

const mockPackage = new Proxy({ raw: {} as any }, { get: (obj, prop) => obj.raw[prop] });
jest.mock('../../../../utils/package_json', () => ({ pkg: mockPackage }));

import { Env } from '../env';
import { getEnvOptions } from './__mocks__/env';

test('correctly creates default environment with empty options.', () => {
const envOptions = getEnvOptions();
const defaultEnv = Env.createDefault(envOptions);

expect(defaultEnv.homeDir).toEqual('/test/cwd');
expect(defaultEnv.configDir).toEqual('/test/cwd/config');
expect(defaultEnv.corePluginsDir).toEqual('/test/cwd/core_plugins');
expect(defaultEnv.binDir).toEqual('/test/cwd/bin');
expect(defaultEnv.logDir).toEqual('/test/cwd/log');
expect(defaultEnv.staticFilesDir).toEqual('/test/cwd/ui');

expect(defaultEnv.getConfigFile()).toEqual('/test/cwd/config/kibana.yml');
expect(defaultEnv.getLegacyKbnServer()).toBeUndefined();
expect(defaultEnv.getMode()).toEqual(envOptions.mode);
expect(defaultEnv.getPackageInfo()).toEqual(envOptions.packageInfo);

test('correctly creates default environment in dev mode.', () => {
mockPackage.raw = {
branch: 'some-branch',
version: 'some-version',
};

const defaultEnv = Env.createDefault({
cliArgs: { dev: true, someArg: 1, someOtherArg: '2' },
configs: ['/test/cwd/config/kibana.yml'],
isDevClusterMaster: true,
});

expect(defaultEnv).toMatchSnapshot('env properties');
});

test('correctly creates default environment with options overrides.', () => {
const mockEnvOptions = getEnvOptions({
config: '/some/other/path/some-kibana.yml',
kbnServer: {},
mode: 'production',
packageInfo: {
branch: 'feature-v1',
buildNum: 100,
buildSha: 'feature-v1-build-sha',
version: 'v1',
test('correctly creates default environment in prod distributable mode.', () => {
mockPackage.raw = {
branch: 'feature-v1',
version: 'v1',
build: {
distributable: true,
number: 100,
sha: 'feature-v1-build-sha',
},
};

const defaultEnv = Env.createDefault({
cliArgs: { dev: false, someArg: 1, someOtherArg: '2' },
configs: ['/some/other/path/some-kibana.yml'],
isDevClusterMaster: false,
});
const defaultEnv = Env.createDefault(mockEnvOptions);

expect(defaultEnv.homeDir).toEqual('/test/cwd');
expect(defaultEnv.configDir).toEqual('/test/cwd/config');
expect(defaultEnv.corePluginsDir).toEqual('/test/cwd/core_plugins');
expect(defaultEnv.binDir).toEqual('/test/cwd/bin');
expect(defaultEnv.logDir).toEqual('/test/cwd/log');
expect(defaultEnv.staticFilesDir).toEqual('/test/cwd/ui');

expect(defaultEnv.getConfigFile()).toEqual(mockEnvOptions.config);
expect(defaultEnv.getLegacyKbnServer()).toBe(mockEnvOptions.kbnServer);
expect(defaultEnv.getMode()).toEqual(mockEnvOptions.mode);
expect(defaultEnv.getPackageInfo()).toEqual(mockEnvOptions.packageInfo);

expect(defaultEnv).toMatchSnapshot('env properties');
});

test('correctly creates environment with constructor.', () => {
const mockEnvOptions = getEnvOptions({
config: '/some/other/path/some-kibana.yml',
mode: 'production',
packageInfo: {
branch: 'feature-v1',
buildNum: 100,
buildSha: 'feature-v1-build-sha',
version: 'v1',
test('correctly creates default environment in prod non-distributable mode.', () => {
mockPackage.raw = {
branch: 'feature-v1',
version: 'v1',
build: {
distributable: false,
number: 100,
sha: 'feature-v1-build-sha',
},
};

const defaultEnv = Env.createDefault({
cliArgs: { dev: false, someArg: 1, someOtherArg: '2' },
configs: ['/some/other/path/some-kibana.yml'],
isDevClusterMaster: false,
});

const defaultEnv = new Env('/some/home/dir', mockEnvOptions);
expect(defaultEnv).toMatchSnapshot('env properties');
});

expect(defaultEnv.homeDir).toEqual('/some/home/dir');
expect(defaultEnv.configDir).toEqual('/some/home/dir/config');
expect(defaultEnv.corePluginsDir).toEqual('/some/home/dir/core_plugins');
expect(defaultEnv.binDir).toEqual('/some/home/dir/bin');
expect(defaultEnv.logDir).toEqual('/some/home/dir/log');
expect(defaultEnv.staticFilesDir).toEqual('/some/home/dir/ui');
test('correctly creates environment with constructor.', () => {
mockPackage.raw = {
branch: 'feature-v1',
version: 'v1',
build: {
distributable: true,
number: 100,
sha: 'feature-v1-build-sha',
},
};

const env = new Env('/some/home/dir', {
cliArgs: { dev: false, someArg: 1, someOtherArg: '2' },
configs: ['/some/other/path/some-kibana.yml'],
isDevClusterMaster: false,
});

expect(defaultEnv.getConfigFile()).toEqual(mockEnvOptions.config);
expect(defaultEnv.getLegacyKbnServer()).toBeUndefined();
expect(defaultEnv.getMode()).toEqual(mockEnvOptions.mode);
expect(defaultEnv.getPackageInfo()).toEqual(mockEnvOptions.packageInfo);
expect(env).toMatchSnapshot('env properties');
});
7 changes: 3 additions & 4 deletions src/core/server/config/config_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,12 @@ export class ConfigService {
);
}

const environmentMode = this.env.getMode();
const config = ConfigClass.schema.validate(
rawConfig,
{
dev: environmentMode.dev,
prod: environmentMode.prod,
...this.env.getPackageInfo(),
dev: this.env.mode.dev,
prod: this.env.mode.prod,
...this.env.packageInfo,
},
namespace
);
Expand Down
Loading

0 comments on commit 60ae2f5

Please sign in to comment.