-
Notifications
You must be signed in to change notification settings - Fork 891
/
osd_server.ts
299 lines (269 loc) · 8.69 KB
/
osd_server.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
298
299
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*
* Any modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/
/*
* 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.
*/
import { Client } from 'elasticsearch';
import { ToolingLog, REPO_ROOT } from '@osd/dev-utils';
import {
createLegacyOpenSearchTestCluster,
opensearchTestConfig,
opensearchDashboardsServerTestUser,
opensearchDashboardsTestUser,
} from '@osd/test';
import { defaultsDeep, get } from 'lodash';
import { resolve } from 'path';
import { BehaviorSubject } from 'rxjs';
import supertest from 'supertest';
import { CoreStart } from 'src/core/server';
import { LegacyAPICaller } from '../server/opensearch';
import { CliArgs, Env } from '../server/config';
import { Root } from '../server/root';
import OsdServer from '../../legacy/server/osd_server';
export type HttpMethod = 'delete' | 'get' | 'head' | 'post' | 'put';
const DEFAULTS_SETTINGS = {
server: {
autoListen: true,
// Use the ephemeral port to make sure that tests use the first available
// port and aren't affected by the timing issues in test environment.
port: 0,
xsrf: { disableProtection: true },
},
logging: { silent: true },
plugins: {},
migrations: { skip: true },
};
const DEFAULT_SETTINGS_WITH_CORE_PLUGINS = {
plugins: { scanDirs: [resolve(__dirname, '../../legacy/core_plugins')] },
opensearch: {
hosts: [opensearchTestConfig.getUrl()],
username: opensearchDashboardsServerTestUser.username,
password: opensearchDashboardsServerTestUser.password,
},
};
export function createRootWithSettings(
settings: Record<string, any>,
cliArgs: Partial<CliArgs> = {}
) {
const env = Env.createDefault(REPO_ROOT, {
configs: [],
cliArgs: {
dev: false,
quiet: false,
silent: false,
watch: false,
repl: false,
basePath: false,
runExamples: false,
disableOptimizer: true,
cache: true,
dist: false,
...cliArgs,
},
isDevClusterMaster: false,
});
return new Root(
{
getConfig$: () => new BehaviorSubject(defaultsDeep({}, settings, DEFAULTS_SETTINGS)),
},
env
);
}
/**
* Returns supertest request attached to the core's internal native Node server.
* @param root
* @param method
* @param path
*/
export function getSupertest(root: Root, method: HttpMethod, path: string) {
const testUserCredentials = Buffer.from(
`${opensearchDashboardsTestUser.username}:${opensearchDashboardsTestUser.password}`
);
return supertest((root as any).server.http.httpServer.server.listener)
[method](path)
.set('Authorization', `Basic ${testUserCredentials.toString('base64')}`);
}
/**
* Creates an instance of Root with default configuration
* tailored for unit tests.
*
* @param {Object} [settings={}] Any config overrides for this instance.
* @returns {Root}
*/
export function createRoot(settings = {}, cliArgs: Partial<CliArgs> = {}) {
return createRootWithSettings(settings, cliArgs);
}
/**
* Creates an instance of Root, including all of the core plugins,
* with default configuration tailored for unit tests.
*
* @param {Object} [settings={}] Any config overrides for this instance.
* @returns {Root}
*/
export function createRootWithCorePlugins(settings = {}) {
return createRootWithSettings(defaultsDeep({}, settings, DEFAULT_SETTINGS_WITH_CORE_PLUGINS));
}
/**
* Returns `osdServer` instance used in the "legacy" OpenSearch Dashboards.
* @param root
*/
export function getOsdServer(root: Root): OsdServer {
return (root as any).server.legacy.osdServer;
}
export const request: Record<
HttpMethod,
(root: Root, path: string) => ReturnType<typeof getSupertest>
> = {
delete: (root, path) => getSupertest(root, 'delete', path),
get: (root, path) => getSupertest(root, 'get', path),
head: (root, path) => getSupertest(root, 'head', path),
post: (root, path) => getSupertest(root, 'post', path),
put: (root, path) => getSupertest(root, 'put', path),
};
export interface TestOpenSearchServer {
getStartTimeout: () => number;
start: (opensearchArgs: string[], opensearchEnvVars: Record<string, string>) => Promise<void>;
stop: () => Promise<void>;
cleanup: () => Promise<void>;
getClient: () => Client;
getCallCluster: () => LegacyAPICaller;
getUrl: () => string;
}
export interface TestOpenSearchUtils {
stop: () => Promise<void>;
opensearch: TestOpenSearchServer;
hosts: string[];
username: string;
password: string;
}
export interface TestOpenSearchDashboardsUtils {
root: Root;
coreStart: CoreStart;
osdServer: OsdServer;
stop: () => Promise<void>;
}
export interface TestUtils {
startOpenSearch: () => Promise<TestOpenSearchUtils>;
startOpenSearchDashboards: () => Promise<TestOpenSearchDashboardsUtils>;
}
/**
* Creates an instance of the Root, including all of the core "legacy" plugins,
* with default configuration tailored for unit tests, and starts opensearch.
*
* @param options
* @prop settings Any config overrides for this instance.
* @prop adjustTimeout A function(t) => this.timeout(t) that adjust the timeout of a
* test, ensuring the test properly waits for the server to boot without timing out.
*/
export function createTestServers({
adjustTimeout,
settings = {},
}: {
adjustTimeout: (timeout: number) => void;
settings?: {
opensearch?: {
license: 'oss' | 'basic' | 'gold' | 'trial';
[key: string]: any;
};
osd?: {
/**
* An array of directories paths, passed in via absolute path strings
*/
plugins?: {
paths: string[];
[key: string]: any;
};
[key: string]: any;
};
/**
* Users passed in via this prop are created in OpenSearch in adition to the standard opensearch and opensearchDashboards users.
* Note, this prop is ignored when using an oss, or basic license
*/
users?: Array<{ username: string; password: string; roles: string[] }>;
};
}): TestUtils {
if (!adjustTimeout) {
throw new Error('adjustTimeout is required in order to avoid flaky tests');
}
const license = get(settings, 'opensearch.license', 'oss');
const usersToBeAdded = get(settings, 'users', []);
if (usersToBeAdded.length > 0) {
if (license !== 'trial') {
throw new Error(
'Adding users is only supported by createTestServers when using a trial license'
);
}
}
const log = new ToolingLog({
level: 'debug',
writeTo: process.stdout,
});
log.indent(6);
log.info('starting opensearch');
log.indent(4);
const opensearch = createLegacyOpenSearchTestCluster(
defaultsDeep({}, get(settings, 'opensearch', {}), {
log,
license,
})
);
log.indent(-4);
// Add time for OSD and adding users
adjustTimeout(opensearch.getStartTimeout() + 100000);
const osdSettings: any = get(settings, 'osd', {});
return {
startOpenSearch: async () => {
await opensearch.start(get(settings, 'opensearch.opensearchArgs', []));
if (['gold', 'trial'].includes(license)) {
// Override provided configs
osdSettings.opensearch = {
hosts: [opensearchTestConfig.getUrl()],
username: opensearchDashboardsServerTestUser.username,
password: opensearchDashboardsServerTestUser.password,
};
}
return {
stop: async () => await opensearch.cleanup(),
opensearch,
hosts: [opensearchTestConfig.getUrl()],
username: opensearchDashboardsServerTestUser.username,
password: opensearchDashboardsServerTestUser.password,
};
},
startOpenSearchDashboards: async () => {
const root = createRootWithCorePlugins(osdSettings);
await root.setup();
const coreStart = await root.start();
const osdServer = getOsdServer(root);
return {
root,
osdServer,
coreStart,
stop: async () => await root.shutdown(),
};
},
};
}