-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create example using a ES proxy within integration tests #107422
Comments
Pinging @elastic/kibana-operations (Team:Operations) |
This issue is to track to see if we can perform this work to help the Core team out, but it's worth noting that this is pretty outside our responsibilities. Anyone with Jest knowledge should be able to configure this behavior. cc @TinaHeiligers since I don't want us to block your work. |
@TinaHeiligers I believe we should be able to do something like this, let me know if you'd like to talk through it at all: /*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import Hapi from '@hapi/hapi';
import h2o2 from '@hapi/h2o2';
import { URL } from 'url';
import * as kbnTestServer from '../../../../test_helpers/kbn_server';
describe('404s from proxies', () => {
it('returns unavailable errors', async () => {
// Create an ES instance
const { startES } = kbnTestServer.createTestServers({
adjustTimeout: jest.setTimeout,
});
const { hosts, stop: stopES } = await startES();
const esUrl = new URL(hosts[0]);
// For the proxy, use a port number that is 100 higher than the one that the actual ES instance is using
const proxyPort = parseInt(esUrl.port, 10) + 100;
// Set this variable when you want the proxy to return this instead of proxying to ES
let customResponse: undefined | { body: any; statusCode: number };
// Setup custom hapi server with h2o2 plugin for proxying
const server = Hapi.server({
port: proxyPort,
});
await server.register(h2o2);
server.route({
method: '*',
path: '/*',
handler: (req, h) => {
if (customResponse) {
return h.response(customResponse.body).code(customResponse.statusCode);
}
return h.proxy({ host: esUrl.host, port: esUrl.port, protocol: 'http' });
},
});
await server.start();
// Setup kibana configured to use proxy as ES backend
const root = kbnTestServer.createRoot({
elasticsearch: { hosts: [`http://${esUrl.host}:${proxyPort}`] },
});
await root.preboot();
await root.setup();
const { savedObjects } = await root.start();
// Get a saved object repository
const repository = savedObjects.createInternalRepository();
// set a custom response
customResponse = { body: { reason: 'proxy response!' }, statusCode: 404 };
// ... do something with repository
const response = await repository.get(...);
expect(response).toEqual(...);
// unset custom response (should fallback to ES now again)
customResponse = undefined;
await root.shutdown();
await server.stop();
await stopES();
});
}); |
@tylersmalley I have a PR in draft to resolve #107246, so we can close this issue. |
Great, thanks @TinaHeiligers! |
Blocks #107246
The Core team would like to add integration tests that proxy Elasticsearch requests in order to modify the responses and validate the behavior.
They are asking that we provide an example of how to accomplish this using Jest.
The text was updated successfully, but these errors were encountered: