/
cluster.test.js
428 lines (356 loc) 路 13.4 KB
/
cluster.test.js
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/*
* 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.
*/
const { ToolingLog, OPENSEARCH_P12_PATH, OPENSEARCH_P12_PASSWORD } = require('@osd/dev-utils');
const execa = require('execa');
const { Cluster } = require('../cluster');
const { installSource, installSnapshot, installArchive } = require('../install');
const { extractConfigFiles } = require('../utils/extract_config_files');
jest.mock('../install', () => ({
installSource: jest.fn(),
installSnapshot: jest.fn(),
installArchive: jest.fn(),
}));
jest.mock('execa', () => jest.fn());
jest.mock('../utils/extract_config_files', () => ({
extractConfigFiles: jest.fn(),
}));
const log = new ToolingLog();
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
async function ensureNoResolve(promise) {
await Promise.race([
sleep(100),
promise.then(() => {
throw new Error('promise was not supposed to resolve');
}),
]);
}
async function ensureResolve(promise) {
return await Promise.race([
promise,
sleep(100).then(() => {
throw new Error('promise was supposed to resolve with installSource() resolution');
}),
]);
}
function mockOpenSearchBin({ exitCode, start }) {
execa.mockImplementationOnce((cmd, args, options) =>
jest.requireActual('execa')(
process.execPath,
[
require.resolve('./__fixtures__/opensearch_bin.js'),
JSON.stringify({
exitCode,
start,
ssl: args.includes('xpack.security.http.ssl.enabled=true'),
}),
],
options
)
);
}
beforeEach(() => {
jest.resetAllMocks();
extractConfigFiles.mockImplementation((config) => config);
});
describe('#installSource()', () => {
it('awaits installSource() promise and returns { installPath }', async () => {
let resolveInstallSource;
installSource.mockImplementationOnce(
() =>
new Promise((resolve) => {
resolveInstallSource = () => {
resolve({ installPath: 'foo' });
};
})
);
const cluster = new Cluster({ log });
const promise = cluster.installSource();
await ensureNoResolve(promise);
resolveInstallSource();
await expect(ensureResolve(promise)).resolves.toEqual({
installPath: 'foo',
});
});
it('passes through all options+log to installSource()', async () => {
installSource.mockResolvedValue({});
const cluster = new Cluster({ log });
await cluster.installSource({ foo: 'bar' });
expect(installSource).toHaveBeenCalledTimes(1);
expect(installSource).toHaveBeenCalledWith({
log,
foo: 'bar',
});
});
it('rejects if installSource() rejects', async () => {
installSource.mockRejectedValue(new Error('foo'));
const cluster = new Cluster({ log });
await expect(cluster.installSource()).rejects.toThrowError('foo');
});
});
describe('#installSnapshot()', () => {
it('awaits installSnapshot() promise and returns { installPath }', async () => {
let resolveInstallSnapshot;
installSnapshot.mockImplementationOnce(
() =>
new Promise((resolve) => {
resolveInstallSnapshot = () => {
resolve({ installPath: 'foo' });
};
})
);
const cluster = new Cluster({ log });
const promise = cluster.installSnapshot();
await ensureNoResolve(promise);
resolveInstallSnapshot();
await expect(ensureResolve(promise)).resolves.toEqual({
installPath: 'foo',
});
});
it('passes through all options+log to installSnapshot()', async () => {
installSnapshot.mockResolvedValue({});
const cluster = new Cluster({ log });
await cluster.installSnapshot({ foo: 'bar' });
expect(installSnapshot).toHaveBeenCalledTimes(1);
expect(installSnapshot).toHaveBeenCalledWith({
log,
foo: 'bar',
});
});
it('rejects if installSnapshot() rejects', async () => {
installSnapshot.mockRejectedValue(new Error('foo'));
const cluster = new Cluster({ log });
await expect(cluster.installSnapshot()).rejects.toThrowError('foo');
});
});
describe('#installArchive(path)', () => {
it('awaits installArchive() promise and returns { installPath }', async () => {
let resolveInstallArchive;
installArchive.mockImplementationOnce(
() =>
new Promise((resolve) => {
resolveInstallArchive = () => {
resolve({ installPath: 'foo' });
};
})
);
const cluster = new Cluster({ log });
const promise = cluster.installArchive();
await ensureNoResolve(promise);
resolveInstallArchive();
await expect(ensureResolve(promise)).resolves.toEqual({
installPath: 'foo',
});
});
it('passes through path and all options+log to installArchive()', async () => {
installArchive.mockResolvedValue({});
const cluster = new Cluster({ log });
await cluster.installArchive('path', { foo: 'bar' });
expect(installArchive).toHaveBeenCalledTimes(1);
expect(installArchive).toHaveBeenCalledWith('path', {
log,
foo: 'bar',
});
});
it('rejects if installArchive() rejects', async () => {
installArchive.mockRejectedValue(new Error('foo'));
const cluster = new Cluster({ log });
await expect(cluster.installArchive()).rejects.toThrowError('foo');
});
});
describe('#start(installPath)', () => {
it('rejects when bin/OpenSearch exists with 0 before starting', async () => {
mockOpenSearchBin({ exitCode: 0, start: false });
await expect(new Cluster({ log }).start()).rejects.toThrowError(
'OpenSearch exited without starting'
);
});
it('rejects when bin/opensearch exists with 143 before starting', async () => {
mockOpenSearchBin({ exitCode: 143, start: false });
await expect(new Cluster({ log }).start()).rejects.toThrowError(
'OpenSearch exited without starting'
);
});
it('rejects when bin/opensearch exists with 130 before starting', async () => {
mockOpenSearchBin({ exitCode: 130, start: false });
await expect(new Cluster({ log }).start()).rejects.toThrowError(
'OpenSearch exited without starting'
);
});
it('rejects when bin/opensearch exists with 1 before starting', async () => {
mockOpenSearchBin({ exitCode: 1, start: false });
await expect(new Cluster({ log }).start()).rejects.toThrowError(
'OpenSearch exited with code 1'
);
});
it('resolves when bin/opensearch logs "started"', async () => {
mockOpenSearchBin({ start: true });
await new Cluster({ log }).start();
});
it('rejects if #start() was called previously', async () => {
mockOpenSearchBin({ start: true });
const cluster = new Cluster({ log });
await cluster.start();
await expect(cluster.start()).rejects.toThrowError('OpenSearch has already been started');
});
it('rejects if #run() was called previously', async () => {
mockOpenSearchBin({ start: true });
const cluster = new Cluster({ log });
await cluster.run();
await expect(cluster.start()).rejects.toThrowError('OpenSearch has already been started');
});
// TODO: [RENAMEME] REPLACE PKCS12 FILES, CERTS, AND KEYS. Temporarily removed until we regenerate them ourselves.
it.skip('sets up SSL when enabled', async () => {
mockOpenSearchBin({ start: true, ssl: true });
const cluster = new Cluster({ log, ssl: true });
await cluster.start();
const config = extractConfigFiles.mock.calls[0][0];
expect(config).toContain('xpack.security.http.ssl.enabled=true');
expect(config).toContain(`xpack.security.http.ssl.keystore.path=${OPENSEARCH_P12_PATH}`);
expect(config).toContain(`xpack.security.http.ssl.keystore.type=PKCS12`);
expect(config).toContain(
`xpack.security.http.ssl.keystore.password=${OPENSEARCH_P12_PASSWORD}`
);
});
it(`doesn't setup SSL when disabled`, async () => {
mockOpenSearchBin({ start: true });
extractConfigFiles.mockReturnValueOnce([]);
const cluster = new Cluster({ log, ssl: false });
await cluster.start();
const config = extractConfigFiles.mock.calls[0][0];
expect(config).toHaveLength(0);
});
});
describe('#installOpenSearchPlugins()', () => {
it('install array of plugins on cluster snapshot', async () => {
const cluster = new Cluster({ log });
await cluster.installOpenSearchPlugins('foo', ['foo1', 'foo2']);
expect(execa).toHaveBeenCalledTimes(2);
expect(execa).toHaveBeenCalledWith('./bin/opensearch-plugin', ['install', '--batch', 'foo1'], {
cwd: 'foo',
});
expect(execa).toHaveBeenCalledWith('./bin/opensearch-plugin', ['install', '--batch', 'foo2'], {
cwd: 'foo',
});
});
it('installs single plugin on cluster snapshot', async () => {
const cluster = new Cluster({ log });
await cluster.installOpenSearchPlugins('foo', 'foo1');
expect(execa).toHaveBeenCalledTimes(1);
expect(execa).toHaveBeenCalledWith('./bin/opensearch-plugin', ['install', '--batch', 'foo1'], {
cwd: 'foo',
});
});
it('do not execute plugin installation script when no plugins in the param list', async () => {
const cluster = new Cluster({ log });
await cluster.installOpenSearchPlugins('foo');
expect(execa).toHaveBeenCalledTimes(0);
});
});
describe('#run()', () => {
it('resolves when bin/opensearch exists with 0', async () => {
mockOpenSearchBin({ exitCode: 0 });
await new Cluster({ log }).run();
});
it('resolves when bin/opensearch exists with 143', async () => {
mockOpenSearchBin({ exitCode: 143 });
await new Cluster({ log }).run();
});
it('resolves when bin/opensearch exists with 130', async () => {
mockOpenSearchBin({ exitCode: 130 });
await new Cluster({ log }).run();
});
it('rejects when bin/opensearch exists with 1', async () => {
mockOpenSearchBin({ exitCode: 1 });
await expect(new Cluster({ log }).run()).rejects.toThrowError('OpenSearch exited with code 1');
});
it('rejects if #start() was called previously', async () => {
mockOpenSearchBin({ exitCode: 0, start: true });
const cluster = new Cluster({ log });
await cluster.start();
await expect(cluster.run()).rejects.toThrowError('OpenSearch has already been started');
});
it('rejects if #run() was called previously', async () => {
mockOpenSearchBin({ exitCode: 0 });
const cluster = new Cluster({ log });
await cluster.run();
await expect(cluster.run()).rejects.toThrowError('OpenSearch has already been started');
});
it('sets up SSL when enabled', async () => {
mockOpenSearchBin({ start: true, ssl: true });
const cluster = new Cluster({ log, ssl: true });
await cluster.run();
const config = extractConfigFiles.mock.calls[0][0];
expect(config).toContain('xpack.security.http.ssl.enabled=true');
expect(config).toContain(`xpack.security.http.ssl.keystore.path=${OPENSEARCH_P12_PATH}`);
expect(config).toContain(`xpack.security.http.ssl.keystore.type=PKCS12`);
expect(config).toContain(
`xpack.security.http.ssl.keystore.password=${OPENSEARCH_P12_PASSWORD}`
);
});
it(`doesn't setup SSL when disabled`, async () => {
mockOpenSearchBin({ start: true });
extractConfigFiles.mockReturnValueOnce([]);
const cluster = new Cluster({ log, ssl: false });
await cluster.run();
const config = extractConfigFiles.mock.calls[0][0];
expect(config).toHaveLength(0);
});
});
describe('#stop()', () => {
it('rejects if #run() or #start() was not called', async () => {
const cluster = new Cluster({ log });
await expect(cluster.stop()).rejects.toThrowError('OpenSearch has not been started');
});
it('resolves when OpenSearch exits with 0', async () => {
mockOpenSearchBin({ exitCode: 0, start: true });
const cluster = new Cluster({ log });
await cluster.start();
await cluster.stop();
});
it('resolves when OpenSearch exits with 143', async () => {
mockOpenSearchBin({ exitCode: 143, start: true });
const cluster = new Cluster({ log });
await cluster.start();
await cluster.stop();
});
it('resolves when OpenSearch exits with 130', async () => {
mockOpenSearchBin({ exitCode: 130, start: true });
const cluster = new Cluster({ log });
await cluster.start();
await cluster.stop();
});
it('rejects when OpenSearch exits with 1', async () => {
mockOpenSearchBin({ exitCode: 1, start: true });
const cluster = new Cluster({ log });
await expect(cluster.run()).rejects.toThrowError('OpenSearch exited with code 1');
await expect(cluster.stop()).rejects.toThrowError('OpenSearch exited with code 1');
});
});