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

[SavedObjects] Create serverless utils for jest integration tests #164157

Merged
merged 25 commits into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,15 @@ export {
createRoot,
createRootWithCorePlugins,
createTestServers,
createTestServerlessInstances,
request,
} from './src';

export type { HttpMethod, TestElasticsearchUtils, TestKibanaUtils, TestUtils } from './src';
export type {
HttpMethod,
TestElasticsearchUtils,
TestKibanaUtils,
TestUtils,
TestServerlessESUtils,
TestServerlessKibanaUtils,
} from './src';
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* 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 { defaultsDeep } from 'lodash';
import { Cluster } from '@kbn/es';
import Path from 'path';
import { REPO_ROOT } from '@kbn/repo-info';
import { ToolingLog } from '@kbn/tooling-log';
import execa from 'execa';
import { CliArgs } from '@kbn/config';
import { createRoot, type TestElasticsearchUtils, type TestKibanaUtils } from './create_root';

export type TestServerlessESUtils = Pick<TestElasticsearchUtils, 'stop' | 'es'>;
export type TestServerlessKibanaUtils = TestKibanaUtils;
export interface TestServerlessUtils {
startES: () => Promise<TestServerlessESUtils>;
startKibana: (abortSignal?: AbortSignal) => Promise<TestServerlessKibanaUtils>;
}

/**
* See docs in {@link TestUtils}. This function provides the same utilities but
* configured for serverless.
*
* @note requires a Docker installation to be running
*/
export function createTestServerlessInstances({
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seemed easiest to just create a separate function rather than adding a flag to the existing createTestServers logic.

However, we mirror the existing API as closely as possible so that these helpers can be easily substituted.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why didn't you name it createTestServerlessServers :trollface:

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you said the quiet part out loud 🤫

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but in all seriousness: if you have a better idea for a name I'll gladly accept 😄

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Na, naming is hard, it's fine. We can rename later if we really want so.

adjustTimeout,
}: {
adjustTimeout: (timeout: number) => void;
}): TestServerlessUtils {
const esUtils = createServerlessES();
const kbUtils = createServerlessKibana();
adjustTimeout?.(120_000);
return {
startES: async () => {
const { stop } = await esUtils.start();
return {
es: esUtils.es,
stop,
};
},
startKibana: async (abortSignal) => {
abortSignal?.addEventListener('abort', async () => await kbUtils.shutdown());
await kbUtils.preboot();
const coreSetup = await kbUtils.setup();
const coreStart = await kbUtils.start();
return {
root: kbUtils,
coreSetup,
coreStart,
stop: kbUtils.shutdown.bind(kbUtils),
};
},
};
}

function createServerlessES() {
const log = new ToolingLog({
level: 'info',
writeTo: process.stdout,
});
const es = new Cluster({ log });
return {
es,
start: async () => {
await es.runServerless({
basePath: Path.join(REPO_ROOT, '.es/es_test_serverless'),
});
return {
stop: async () => {
// hack to stop the ES cluster
await execa('docker', ['container', 'stop', 'es01', 'es02', 'es03']);
},
};
},
};
}

const defaults = {
server: {
restrictInternalApis: true,
versioned: {
versionResolution: 'newest',
strictClientVersionCheck: false,
},
},
migrations: {
algorithm: 'zdt',
},
elasticsearch: {
serviceAccountToken: 'BEEF',
},
};
function createServerlessKibana(settings = {}, cliArgs: Partial<CliArgs> = {}) {
return createRoot(defaultsDeep(settings, defaults), { ...cliArgs, serverless: true });
}
Comment on lines +98 to +100
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is serverless: true here sufficient to load the serverless config file(s)?

Copy link
Contributor Author

@jloleysens jloleysens Aug 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, great point, this still needs to be figured out a bit.

I added some basic config that we'll need in serverless in the meantime:

{
  server: {
    restrictInternalApis: true,
    versioned: {
      versionResolution: 'newest',
      strictClientVersionCheck: false,
    },
  },
  migrations: {
    algorithm: 'zdt',
  },
  elasticsearch: {
    serviceAccountToken: 'BEEF',
  },
}

Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,12 @@ export {
request,
} from './create_root';

export { createTestServerlessInstances } from './create_serverless_root';

export type {
TestServerlessUtils,
TestServerlessESUtils,
TestServerlessKibanaUtils,
} from './create_serverless_root';

export type { HttpMethod, TestElasticsearchUtils, TestKibanaUtils, TestUtils } from './create_root';
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"@kbn/core-root-server-internal",
"@kbn/repo-info",
"@kbn/repo-packages",
"@kbn/es",
],
"exclude": [
"target/**/*",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* 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.
*/

module.exports = {
// TODO replace the line below with
// preset: '@kbn/test/jest_integration_node
// to do so, we must fix all integration tests first
// see https://github.com/elastic/kibana/pull/130255/
preset: '@kbn/test/jest_integration',
rootDir: '../../../../../../..',
roots: ['<rootDir>/src/core/server/integration_tests/saved_objects/serverless/migrations'],
// must override to match all test given there is no `integration_tests` subfolder
testMatch: ['**/*.test.{js,mjs,ts,tsx}'],
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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 {
request,
TestServerlessESUtils,
TestServerlessKibanaUtils,
createTestServerlessInstances,
} from '@kbn/core-test-helpers-kbn-server';

/**
* Until we merge https://github.com/elastic/kibana/pull/162673 this test should remain skipped.
*/
describe.skip('smoke', () => {
Copy link
Contributor Author

@jloleysens jloleysens Aug 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to get this working on CI, but it is currently timing out. It seems as though the instance might be too small to start 3 ES containers. We should also wait for QA to figure this out for FTRs.

let serverlessES: TestServerlessESUtils;
let serverlessKibana: TestServerlessKibanaUtils;
let root: TestServerlessKibanaUtils['root'];
beforeEach(async () => {
const { startES, startKibana } = createTestServerlessInstances({
adjustTimeout: jest.setTimeout,
});
serverlessES = await startES();
serverlessKibana = await startKibana();
root = serverlessKibana.root;
});
afterEach(async () => {
await serverlessES?.stop();
await serverlessKibana?.stop();
});
test('it can start Kibana and ES serverless', async () => {
const { body } = await request.get(root, '/api/status').expect(200);
expect(body).toMatchObject({ status: { overall: { level: 'available' } } });
});
});