Skip to content
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

Closed
tylersmalley opened this issue Aug 2, 2021 · 5 comments
Closed

Create example using a ES proxy within integration tests #107422

tylersmalley opened this issue Aug 2, 2021 · 5 comments
Labels
Team:Operations Team label for Operations Team

Comments

@tylersmalley
Copy link
Contributor

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.

@tylersmalley tylersmalley added the Team:Operations Team label for Operations Team label Aug 2, 2021
@elasticmachine
Copy link
Contributor

Pinging @elastic/kibana-operations (Team:Operations)

@tylersmalley
Copy link
Contributor Author

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.

@joshdover
Copy link
Contributor

@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();
  });
});

@TinaHeiligers
Copy link
Contributor

@tylersmalley I have a PR in draft to resolve #107246, so we can close this issue.

@tylersmalley
Copy link
Contributor Author

Great, thanks @TinaHeiligers!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Team:Operations Team label for Operations Team
Projects
None yet
Development

No branches or pull requests

4 participants