forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_service.test.ts
297 lines (235 loc) · 9.67 KB
/
config_service.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
/* eslint-disable max-classes-per-file */
import { BehaviorSubject } from 'rxjs';
import { first } from 'rxjs/operators';
import { mockPackage } from './config_service.test.mocks';
import { schema, Type, TypeOf } from '@kbn/config-schema';
import { ConfigService, Env, ObjectToConfigAdapter } from '.';
import { loggingServiceMock } from '../logging/logging_service.mock';
import { getEnvOptions } from './__mocks__/env';
const emptyArgv = getEnvOptions();
const defaultEnv = new Env('/kibana', emptyArgv);
const logger = loggingServiceMock.create();
class ExampleClassWithStringSchema {
public static schema = schema.string();
constructor(readonly value: string) {}
}
test('returns config at path as observable', async () => {
const config$ = new BehaviorSubject(new ObjectToConfigAdapter({ key: 'foo' }));
const configService = new ConfigService(config$, defaultEnv, logger);
configService.setSchema('key', schema.string());
const configs = configService.atPath('key', ExampleClassWithStringSchema);
const exampleConfig = await configs.pipe(first()).toPromise();
expect(exampleConfig.value).toBe('foo');
});
test('throws if config at path does not match schema', async () => {
expect.assertions(1);
const config$ = new BehaviorSubject(new ObjectToConfigAdapter({ key: 123 }));
const configService = new ConfigService(config$, defaultEnv, logger);
configService.setSchema('key', schema.string());
const configs = configService.atPath('key', ExampleClassWithStringSchema);
try {
await configs.pipe(first()).toPromise();
} catch (e) {
expect(e.message).toMatchSnapshot();
}
});
test("returns undefined if fetching optional config at a path that doesn't exist", async () => {
const config$ = new BehaviorSubject(new ObjectToConfigAdapter({ foo: 'bar' }));
const configService = new ConfigService(config$, defaultEnv, logger);
configService.setSchema('unique-name', schema.string());
const configs = configService.optionalAtPath('unique-name', ExampleClassWithStringSchema);
const exampleConfig = await configs.pipe(first()).toPromise();
expect(exampleConfig).toBeUndefined();
});
test('returns observable config at optional path if it exists', async () => {
const config$ = new BehaviorSubject(new ObjectToConfigAdapter({ value: 'bar' }));
const configService = new ConfigService(config$, defaultEnv, logger);
configService.setSchema('value', schema.string());
const configs = configService.optionalAtPath('value', ExampleClassWithStringSchema);
const exampleConfig: any = await configs.pipe(first()).toPromise();
expect(exampleConfig).toBeDefined();
expect(exampleConfig.value).toBe('bar');
});
test("does not push new configs when reloading if config at path hasn't changed", async () => {
const config$ = new BehaviorSubject(new ObjectToConfigAdapter({ key: 'value' }));
const configService = new ConfigService(config$, defaultEnv, logger);
configService.setSchema('key', schema.string());
const valuesReceived: any[] = [];
configService.atPath('key', ExampleClassWithStringSchema).subscribe(config => {
valuesReceived.push(config.value);
});
config$.next(new ObjectToConfigAdapter({ key: 'value' }));
expect(valuesReceived).toEqual(['value']);
});
test('pushes new config when reloading and config at path has changed', async () => {
const config$ = new BehaviorSubject(new ObjectToConfigAdapter({ key: 'value' }));
const configService = new ConfigService(config$, defaultEnv, logger);
configService.setSchema('key', schema.string());
const valuesReceived: any[] = [];
configService.atPath('key', ExampleClassWithStringSchema).subscribe(config => {
valuesReceived.push(config.value);
});
config$.next(new ObjectToConfigAdapter({ key: 'new value' }));
expect(valuesReceived).toEqual(['value', 'new value']);
});
test("throws error if 'schema' is not defined for a key", async () => {
class ExampleClass {}
const config$ = new BehaviorSubject(new ObjectToConfigAdapter({ key: 'value' }));
const configService = new ConfigService(config$, defaultEnv, logger);
configService.setSchema('no-key', schema.string());
const configs = configService.atPath('key', ExampleClass as any);
expect(configs.pipe(first()).toPromise()).rejects.toMatchInlineSnapshot(
`[Error: No validation schema has been defined for key]`
);
});
test("throws error if 'setSchema' called several times for the same key", async () => {
const config$ = new BehaviorSubject(new ObjectToConfigAdapter({ key: 'value' }));
const configService = new ConfigService(config$, defaultEnv, logger);
const addSchema = async () => await configService.setSchema('key', schema.string());
await addSchema();
expect(addSchema()).rejects.toMatchInlineSnapshot(
`[Error: Validation schema for key was already registered.]`
);
});
test('tracks unhandled paths', async () => {
const initialConfig = {
bar: {
deep1: {
key: '123',
},
deep2: {
key: '321',
},
},
foo: 'value',
quux: {
deep1: {
key: 'hello',
},
deep2: {
key: 'world',
},
},
};
const config$ = new BehaviorSubject(new ObjectToConfigAdapter(initialConfig));
const configService = new ConfigService(config$, defaultEnv, logger);
configService.atPath('foo', createClassWithSchema(schema.string()));
configService.atPath(
['bar', 'deep2'],
createClassWithSchema(
schema.object({
key: schema.string(),
})
)
);
const unused = await configService.getUnusedPaths();
expect(unused).toEqual(['bar.deep1.key', 'quux.deep1.key', 'quux.deep2.key']);
});
test('correctly passes context', async () => {
mockPackage.raw = {
branch: 'feature-v1',
version: 'v1',
build: {
distributable: true,
number: 100,
sha: 'feature-v1-build-sha',
},
};
const env = new Env('/kibana', getEnvOptions());
const config$ = new BehaviorSubject(new ObjectToConfigAdapter({ foo: {} }));
const schemaDefinition = schema.object({
branchRef: schema.string({
defaultValue: schema.contextRef('branch'),
}),
buildNumRef: schema.number({
defaultValue: schema.contextRef('buildNum'),
}),
buildShaRef: schema.string({
defaultValue: schema.contextRef('buildSha'),
}),
devRef: schema.boolean({ defaultValue: schema.contextRef('dev') }),
prodRef: schema.boolean({ defaultValue: schema.contextRef('prod') }),
versionRef: schema.string({
defaultValue: schema.contextRef('version'),
}),
});
const configService = new ConfigService(config$, env, logger);
configService.setSchema('foo', schemaDefinition);
const configs = configService.atPath('foo', createClassWithSchema(schemaDefinition));
expect(await configs.pipe(first()).toPromise()).toMatchSnapshot();
});
test('handles enabled path, but only marks the enabled path as used', async () => {
const initialConfig = {
pid: {
enabled: true,
file: '/some/file.pid',
},
};
const config$ = new BehaviorSubject(new ObjectToConfigAdapter(initialConfig));
const configService = new ConfigService(config$, defaultEnv, logger);
const isEnabled = await configService.isEnabledAtPath('pid');
expect(isEnabled).toBe(true);
const unusedPaths = await configService.getUnusedPaths();
expect(unusedPaths).toEqual(['pid.file']);
});
test('handles enabled path when path is array', async () => {
const initialConfig = {
pid: {
enabled: true,
file: '/some/file.pid',
},
};
const config$ = new BehaviorSubject(new ObjectToConfigAdapter(initialConfig));
const configService = new ConfigService(config$, defaultEnv, logger);
const isEnabled = await configService.isEnabledAtPath(['pid']);
expect(isEnabled).toBe(true);
const unusedPaths = await configService.getUnusedPaths();
expect(unusedPaths).toEqual(['pid.file']);
});
test('handles disabled path and marks config as used', async () => {
const initialConfig = {
pid: {
enabled: false,
file: '/some/file.pid',
},
};
const config$ = new BehaviorSubject(new ObjectToConfigAdapter(initialConfig));
const configService = new ConfigService(config$, defaultEnv, logger);
const isEnabled = await configService.isEnabledAtPath('pid');
expect(isEnabled).toBe(false);
const unusedPaths = await configService.getUnusedPaths();
expect(unusedPaths).toEqual([]);
});
test('treats config as enabled if config path is not present in config', async () => {
const initialConfig = {};
const config$ = new BehaviorSubject(new ObjectToConfigAdapter(initialConfig));
const configService = new ConfigService(config$, defaultEnv, logger);
const isEnabled = await configService.isEnabledAtPath('pid');
expect(isEnabled).toBe(true);
const unusedPaths = await configService.getUnusedPaths();
expect(unusedPaths).toEqual([]);
});
function createClassWithSchema(s: Type<any>) {
return class ExampleClassWithSchema {
public static schema = s;
constructor(readonly value: TypeOf<typeof s>) {}
};
}