From eee7cd31c2268b3aa56fb93d8e5ac171a338f87f Mon Sep 17 00:00:00 2001 From: Carlos Crespo Date: Mon, 4 Nov 2024 09:42:11 +0100 Subject: [PATCH 01/11] Add foundation to run apm tests Migrate agent_explorer test Update latest_agent_versions test --- .../agent_explorer/agent_explorer.spec.ts | 21 ++- .../apis/observability/apm/index.ts | 55 ++++++++ .../configs/serverless/oblt.index.ts | 1 + .../serverless/oblt.serverless.config.ts | 1 + .../configs/stateful/oblt.index.ts | 1 + .../configs/stateful/oblt.stateful.config.ts | 1 + .../default_configs/serverless.config.base.ts | 2 + .../default_configs/stateful.config.base.ts | 2 + .../deployment_agnostic/services/apm_api.ts | 121 ++++++++++++++++++ .../deployment_agnostic/services/index.ts | 6 + .../deployment_agnostic/services/registry.ts | 97 ++++++++++++++ .../services/synthtrace.ts | 46 +++++++ 12 files changed, 347 insertions(+), 7 deletions(-) rename x-pack/test/{apm_api_integration/tests => api_integration/deployment_agnostic/apis/observability/apm}/agent_explorer/agent_explorer.spec.ts (90%) create mode 100644 x-pack/test/api_integration/deployment_agnostic/apis/observability/apm/index.ts create mode 100644 x-pack/test/api_integration/deployment_agnostic/services/apm_api.ts create mode 100644 x-pack/test/api_integration/deployment_agnostic/services/registry.ts create mode 100644 x-pack/test/api_integration/deployment_agnostic/services/synthtrace.ts diff --git a/x-pack/test/apm_api_integration/tests/agent_explorer/agent_explorer.spec.ts b/x-pack/test/api_integration/deployment_agnostic/apis/observability/apm/agent_explorer/agent_explorer.spec.ts similarity index 90% rename from x-pack/test/apm_api_integration/tests/agent_explorer/agent_explorer.spec.ts rename to x-pack/test/api_integration/deployment_agnostic/apis/observability/apm/agent_explorer/agent_explorer.spec.ts index 95e71167aaab..5ea2697eb2eb 100644 --- a/x-pack/test/apm_api_integration/tests/agent_explorer/agent_explorer.spec.ts +++ b/x-pack/test/api_integration/deployment_agnostic/apis/observability/apm/agent_explorer/agent_explorer.spec.ts @@ -8,13 +8,14 @@ import expect from '@kbn/expect'; import { apm, timerange } from '@kbn/apm-synthtrace-client'; import { APIClientRequestParamsOf } from '@kbn/apm-plugin/public/services/rest/create_call_apm_api'; import { RecursivePartial } from '@kbn/apm-plugin/typings/common'; +import type { ApmSynthtraceEsClient } from '@kbn/apm-synthtrace'; import { keyBy } from 'lodash'; -import { FtrProviderContext } from '../../common/ftr_provider_context'; +import type { DeploymentAgnosticFtrProviderContext } from '../../../../ftr_provider_context'; -export default function ApiTest({ getService }: FtrProviderContext) { +export default function ApiTest({ getService }: DeploymentAgnosticFtrProviderContext) { + const apiApi = getService('apmApi'); const registry = getService('registry'); - const apmApiClient = getService('apmApiClient'); - const apmSynthtraceEsClient = getService('apmSynthtraceEsClient'); + const synthtrace = getService('synthtrace'); const start = new Date('2021-01-01T00:00:00.000Z').getTime(); const end = new Date('2021-01-01T00:15:00.000Z').getTime() - 1; @@ -27,7 +28,8 @@ export default function ApiTest({ getService }: FtrProviderContext) { APIClientRequestParamsOf<'GET /internal/apm/get_agents_per_service'>['params'] > ) { - return await apmApiClient.readUser({ + const client = await apiApi.createApmApiClient(); + return await client.readUser({ endpoint: 'GET /internal/apm/get_agents_per_service', params: { query: { @@ -42,7 +44,7 @@ export default function ApiTest({ getService }: FtrProviderContext) { }); } - registry.when('Agent explorer when data is not loaded', { config: 'basic', archives: [] }, () => { + registry.when('Agent explorer when data is not loaded', () => { it('handles empty state', async () => { const { status, body } = await callApi(); @@ -51,9 +53,14 @@ export default function ApiTest({ getService }: FtrProviderContext) { }); }); - registry.when('Agent explorer', { config: 'basic', archives: [] }, () => { + registry.when('Agent explorer', () => { describe('when data is loaded', () => { + let apmSynthtraceEsClient: ApmSynthtraceEsClient; + before(async () => { + const version = (await synthtrace.apmSynthtraceKibanaClient.installApmPackage()).version; + apmSynthtraceEsClient = await synthtrace.createApmSynthtraceEsClient(version); + const serviceOtelJava = apm .service({ name: otelJavaServiceName, diff --git a/x-pack/test/api_integration/deployment_agnostic/apis/observability/apm/index.ts b/x-pack/test/api_integration/deployment_agnostic/apis/observability/apm/index.ts new file mode 100644 index 000000000000..9758e0c50a2d --- /dev/null +++ b/x-pack/test/api_integration/deployment_agnostic/apis/observability/apm/index.ts @@ -0,0 +1,55 @@ +/* + * 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; you may not use this file except in compliance with the Elastic License + * 2.0. + */ +import globby from 'globby'; +import path from 'path'; +import type { DeploymentAgnosticFtrProviderContext } from '../../../ftr_provider_context'; + +const cwd = path.join(__dirname); +const envGrepFiles = process.env.APM_TEST_GREP_FILES as string; + +function getGlobPattern() { + try { + const envGrepFilesParsed = JSON.parse(envGrepFiles as string) as string[]; + return envGrepFilesParsed.map((pattern) => { + return pattern.includes('spec') ? `**/${pattern}**` : `**/${pattern}**.spec.ts`; + }); + } catch (e) { + // ignore + } + return '**/*.spec.ts'; +} + +export default function apmApiIntegrationTests({ + getService, + loadTestFile, +}: DeploymentAgnosticFtrProviderContext) { + // DO NOT SKIP + // Skipping here will skip the entire apm api test suite + // Instead skip (flaky) tests individually + // Failing: See https://github.com/elastic/kibana/issues/176948 + describe('APM API tests', function () { + const registry = getService('registry'); + const filePattern = getGlobPattern(); + const tests = globby.sync(filePattern, { cwd }); + + if (envGrepFiles) { + // eslint-disable-next-line no-console + console.log( + `\nCommand "--grep-files=${filePattern}" matched ${tests.length} file(s):\n${tests + .map((name) => ` - ${name}`) + .join('\n')}\n` + ); + } + + tests.forEach((testName) => { + describe(testName, () => { + loadTestFile(require.resolve(`./${testName}`)); + registry.run(); + }); + }); + }); +} diff --git a/x-pack/test/api_integration/deployment_agnostic/configs/serverless/oblt.index.ts b/x-pack/test/api_integration/deployment_agnostic/configs/serverless/oblt.index.ts index d9ee2daa42aa..70bf555062c7 100644 --- a/x-pack/test/api_integration/deployment_agnostic/configs/serverless/oblt.index.ts +++ b/x-pack/test/api_integration/deployment_agnostic/configs/serverless/oblt.index.ts @@ -19,5 +19,6 @@ export default function ({ loadTestFile }: DeploymentAgnosticFtrProviderContext) loadTestFile(require.resolve('../../apis/painless_lab')); loadTestFile(require.resolve('../../apis/saved_objects_management')); loadTestFile(require.resolve('../../apis/observability/slo')); + loadTestFile(require.resolve('../../apis/observability/apm')); }); } diff --git a/x-pack/test/api_integration/deployment_agnostic/configs/serverless/oblt.serverless.config.ts b/x-pack/test/api_integration/deployment_agnostic/configs/serverless/oblt.serverless.config.ts index 245663416243..e1864f0444ab 100644 --- a/x-pack/test/api_integration/deployment_agnostic/configs/serverless/oblt.serverless.config.ts +++ b/x-pack/test/api_integration/deployment_agnostic/configs/serverless/oblt.serverless.config.ts @@ -10,6 +10,7 @@ import { createServerlessTestConfig } from '../../default_configs/serverless.con export default createServerlessTestConfig({ serverlessProject: 'oblt', testFiles: [require.resolve('./oblt.index.ts')], + servicesRequiredForTestAnalysis: ['registry'], junit: { reportName: 'Serverless Observability - Deployment-agnostic API Integration Tests', }, diff --git a/x-pack/test/api_integration/deployment_agnostic/configs/stateful/oblt.index.ts b/x-pack/test/api_integration/deployment_agnostic/configs/stateful/oblt.index.ts index a467264698e5..5f38067fa3f1 100644 --- a/x-pack/test/api_integration/deployment_agnostic/configs/stateful/oblt.index.ts +++ b/x-pack/test/api_integration/deployment_agnostic/configs/stateful/oblt.index.ts @@ -13,5 +13,6 @@ export default function ({ loadTestFile }: DeploymentAgnosticFtrProviderContext) loadTestFile(require.resolve('../../apis/observability/alerting')); loadTestFile(require.resolve('../../apis/observability/dataset_quality')); loadTestFile(require.resolve('../../apis/observability/slo')); + loadTestFile(require.resolve('../../apis/observability/apm')); }); } diff --git a/x-pack/test/api_integration/deployment_agnostic/configs/stateful/oblt.stateful.config.ts b/x-pack/test/api_integration/deployment_agnostic/configs/stateful/oblt.stateful.config.ts index 7b3cf3a7f181..2c2e33a126b5 100644 --- a/x-pack/test/api_integration/deployment_agnostic/configs/stateful/oblt.stateful.config.ts +++ b/x-pack/test/api_integration/deployment_agnostic/configs/stateful/oblt.stateful.config.ts @@ -9,6 +9,7 @@ import { createStatefulTestConfig } from '../../default_configs/stateful.config. export default createStatefulTestConfig({ testFiles: [require.resolve('./oblt.index.ts')], + servicesRequiredForTestAnalysis: ['registry'], junit: { reportName: 'Stateful Observability - Deployment-agnostic API Integration Tests', }, diff --git a/x-pack/test/api_integration/deployment_agnostic/default_configs/serverless.config.base.ts b/x-pack/test/api_integration/deployment_agnostic/default_configs/serverless.config.base.ts index e7df37f5aa31..353021aa343a 100644 --- a/x-pack/test/api_integration/deployment_agnostic/default_configs/serverless.config.base.ts +++ b/x-pack/test/api_integration/deployment_agnostic/default_configs/serverless.config.base.ts @@ -16,6 +16,7 @@ interface CreateTestConfigOptions { esServerArgs?: string[]; kbnServerArgs?: string[]; services?: T; + servicesRequiredForTestAnalysis?: string[]; testFiles: string[]; junit: { reportName: string }; suiteTags?: { include?: string[]; exclude?: string[] }; @@ -85,6 +86,7 @@ export function createServerlessTestConfig { kbnServerArgs?: string[]; services?: T; testFiles: string[]; + servicesRequiredForTestAnalysis?: string[]; junit: { reportName: string }; suiteTags?: { include?: string[]; exclude?: string[] }; } @@ -100,6 +101,7 @@ export function createStatefulTestConfig( + options: { + type?: 'form-data'; + endpoint: TEndpoint; + spaceId?: string; + } & APIClientRequestParamsOf & { + params?: { query?: { _inspect?: boolean } }; + } + ): Promise> => { + const { endpoint, type } = options; + + const params = 'params' in options ? (options.params as Record) : {}; + + const roleAuthc = await samlAuth.createM2mApiKeyWithRoleScope(role); + + const headers: Record = { + ...samlAuth.getInternalRequestHeader(), + ...roleAuthc.apiKeyHeader, + }; + + const { method, pathname, version } = formatRequest(endpoint, params.path); + const pathnameWithSpaceId = options.spaceId ? `/s/${options.spaceId}${pathname}` : pathname; + const url = format({ pathname: pathnameWithSpaceId, query: params?.query }); + + // eslint-disable-next-line no-console + console.debug(`Calling APM API: ${method.toUpperCase()} ${url}`); + + if (version) { + headers['Elastic-Api-Version'] = version; + } + + let res: request.Response; + if (type === 'form-data') { + const fields: Array<[string, any]> = Object.entries(params.body); + const formDataRequest = supertestWithoutAuth[method](url) + .set(headers) + .set('Content-type', 'multipart/form-data'); + + for (const field of fields) { + void formDataRequest.field(field[0], field[1]); + } + + res = await formDataRequest; + } else if (params.body) { + res = await supertestWithoutAuth[method](url).send(params.body).set(headers); + } else { + res = await supertestWithoutAuth[method](url).set(headers); + } + + // supertest doesn't throw on http errors + if (res?.status !== 200) { + throw new ApmApiError(res, endpoint); + } + + return res; + }; +} + +type ApiErrorResponse = Omit & { + body: { + statusCode: number; + error: string; + message: string; + attributes: object; + }; +}; + +export type ApmApiSupertest = ReturnType; + +export class ApmApiError extends Error { + res: ApiErrorResponse; + + constructor(res: request.Response, endpoint: string) { + super( + `Unhandled ApmApiError. +Status: "${res.status}" +Endpoint: "${endpoint}" +Body: ${JSON.stringify(res.body)}` + ); + + this.res = res; + } +} + +export interface SupertestReturnType { + status: number; + body: APIReturnType; +} + +export function ApmApiProvider(context: DeploymentAgnosticFtrProviderContext) { + return { + async createApmApiClient() { + return { + readUser: createApmApiClient(context, 'viewer'), + adminUser: createApmApiClient(context, 'admin'), + writeUser: createApmApiClient(context, 'editor'), + }; + }, + }; +} diff --git a/x-pack/test/api_integration/deployment_agnostic/services/index.ts b/x-pack/test/api_integration/deployment_agnostic/services/index.ts index bea63ea216c9..a37469988b7d 100644 --- a/x-pack/test/api_integration/deployment_agnostic/services/index.ts +++ b/x-pack/test/api_integration/deployment_agnostic/services/index.ts @@ -13,6 +13,9 @@ import { PackageApiProvider } from './package_api'; import { RoleScopedSupertestProvider, SupertestWithRoleScope } from './role_scoped_supertest'; import { SloApiProvider } from './slo_api'; import { LogsSynthtraceEsClientProvider } from './logs_synthtrace_es_client'; +import { SynthtraceProvider } from './synthtrace'; +import { RegistryProvider } from './registry'; +import { ApmApiProvider } from './apm_api'; export type { InternalRequestHeader, @@ -31,6 +34,9 @@ export const services = { roleScopedSupertest: RoleScopedSupertestProvider, logsSynthtraceEsClient: LogsSynthtraceEsClientProvider, // create a new deployment-agnostic service and load here + synthtrace: SynthtraceProvider, + apmApi: ApmApiProvider, + registry: RegistryProvider, }; export type SupertestWithRoleScopeType = SupertestWithRoleScope; diff --git a/x-pack/test/api_integration/deployment_agnostic/services/registry.ts b/x-pack/test/api_integration/deployment_agnostic/services/registry.ts new file mode 100644 index 000000000000..b1e03a92094e --- /dev/null +++ b/x-pack/test/api_integration/deployment_agnostic/services/registry.ts @@ -0,0 +1,97 @@ +/* + * 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; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import { groupBy } from 'lodash'; +import callsites from 'callsites'; +import { joinByKey } from '@kbn/observability-utils/array/join_by_key'; +import { maybe } from '@kbn/apm-plugin/common/utils/maybe'; + +export function RegistryProvider() { + const callbacks: Array<{ + runs: Array<{ + cb: () => void; + }>; + }> = []; + + let running: boolean = false; + + function when(title: string, callback: () => void, skip?: boolean) { + if (running) { + throw new Error("Can't add tests when running"); + } + + const frame = maybe(callsites()[1]); + + const file = frame?.getFileName(); + + if (!file) { + throw new Error('Could not infer file for suite'); + } + + callbacks.push({ + runs: [ + { + cb: () => { + const suite: ReturnType = (skip ? describe.skip : describe)( + title, + () => { + callback(); + } + ) as any; + + suite.file = file; + suite.eachTest((test) => { + test.file = file; + }); + }, + }, + ], + }); + } + + when.skip = (title: string, callback: () => void) => { + when(title, callback, true); + }; + + const registry = { + when, + run: () => { + running = true; + + const groups = joinByKey(callbacks, [], (a, b) => ({ + ...a, + ...b, + runs: a.runs.concat(b.runs), + })); + + callbacks.length = 0; + + const byConfig = groupBy(groups, 'config'); + + Object.keys(byConfig).forEach((config) => { + const groupsForConfig = byConfig[config]; + // register suites for other configs, but skip them so tests are marked as such + // and their snapshots are not marked as obsolete + describe(config, () => { + groupsForConfig.forEach((group) => { + const { runs } = group; + + describe(config, () => { + runs.forEach((run) => { + run.cb(); + }); + }); + }); + }); + }); + + running = false; + }, + }; + + return registry; +} diff --git a/x-pack/test/api_integration/deployment_agnostic/services/synthtrace.ts b/x-pack/test/api_integration/deployment_agnostic/services/synthtrace.ts new file mode 100644 index 000000000000..1ab2692095ca --- /dev/null +++ b/x-pack/test/api_integration/deployment_agnostic/services/synthtrace.ts @@ -0,0 +1,46 @@ +/* + * 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; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import { ApmSynthtraceKibanaClient, createLogger, LogLevel } from '@kbn/apm-synthtrace'; +import url, { format, UrlObject } from 'url'; + +import { getLogsSynthtraceEsClient } from '../../../common/utils/synthtrace/logs_es_client'; +import { getApmSynthtraceEsClient } from '../../../common/utils/synthtrace/apm_es_client'; +import type { DeploymentAgnosticFtrProviderContext } from '../ftr_provider_context'; + +function getSynthtraceKibanaClient(kibanaServerUrl: string) { + const kibanaServerUrlWithAuth = url + .format({ + ...url.parse(kibanaServerUrl), + }) + .slice(0, -1); + + const kibanaClient = new ApmSynthtraceKibanaClient({ + target: kibanaServerUrlWithAuth, + logger: createLogger(LogLevel.debug), + }); + + return kibanaClient; +} + +export function SynthtraceProvider({ getService }: DeploymentAgnosticFtrProviderContext) { + const client = getService('es'); + const config = getService('config'); + + const servers = config.get('servers'); + const kibanaServer = servers.kibana as UrlObject; + const kibanaServerUrl = format(kibanaServer); + const apmSynthtraceKibanaClient = getSynthtraceKibanaClient(kibanaServerUrl); + + return { + apmSynthtraceKibanaClient, + createLogsSynthtraceEsClient: () => getLogsSynthtraceEsClient(client), + async createApmSynthtraceEsClient(packageVersion: string) { + return getApmSynthtraceEsClient({ client, packageVersion }); + }, + }; +} From 5857da270aff222977cbe0b51d3630fc6f63aa1b Mon Sep 17 00:00:00 2001 From: Carlos Crespo Date: Mon, 4 Nov 2024 14:27:21 +0100 Subject: [PATCH 02/11] Force Git to recognize latest_agent_versions.spec.ts as moved --- .../apm}/agent_explorer/latest_agent_versions.spec.ts | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename x-pack/test/{apm_api_integration/tests => api_integration/deployment_agnostic/apis/observability/apm}/agent_explorer/latest_agent_versions.spec.ts (100%) diff --git a/x-pack/test/apm_api_integration/tests/agent_explorer/latest_agent_versions.spec.ts b/x-pack/test/api_integration/deployment_agnostic/apis/observability/apm/agent_explorer/latest_agent_versions.spec.ts similarity index 100% rename from x-pack/test/apm_api_integration/tests/agent_explorer/latest_agent_versions.spec.ts rename to x-pack/test/api_integration/deployment_agnostic/apis/observability/apm/agent_explorer/latest_agent_versions.spec.ts From c45c66213a683d27e22691d1bf268c4bd77f0e6a Mon Sep 17 00:00:00 2001 From: Carlos Crespo Date: Mon, 4 Nov 2024 14:29:37 +0100 Subject: [PATCH 03/11] Updated latest_agent_versions.spec.ts after moving --- .../latest_agent_versions.spec.ts | 45 +++++++++---------- 1 file changed, 21 insertions(+), 24 deletions(-) diff --git a/x-pack/test/api_integration/deployment_agnostic/apis/observability/apm/agent_explorer/latest_agent_versions.spec.ts b/x-pack/test/api_integration/deployment_agnostic/apis/observability/apm/agent_explorer/latest_agent_versions.spec.ts index 00e3fedf4620..94c4ba08008f 100644 --- a/x-pack/test/api_integration/deployment_agnostic/apis/observability/apm/agent_explorer/latest_agent_versions.spec.ts +++ b/x-pack/test/api_integration/deployment_agnostic/apis/observability/apm/agent_explorer/latest_agent_versions.spec.ts @@ -6,45 +6,42 @@ */ import { ElasticApmAgentLatestVersion } from '@kbn/apm-plugin/common/agent_explorer'; import expect from '@kbn/expect'; -import { FtrProviderContext } from '../../common/ftr_provider_context'; +import type { DeploymentAgnosticFtrProviderContext } from '../../../../ftr_provider_context'; -export default function ApiTest({ getService }: FtrProviderContext) { +export default function ApiTest({ getService }: DeploymentAgnosticFtrProviderContext) { const registry = getService('registry'); - const apmApiClient = getService('apmApiClient'); + const apiApi = getService('apmApi'); const nodeAgentName = 'nodejs'; const unlistedAgentName = 'unlistedAgent'; async function callApi() { + const apmApiClient = await apiApi.createApmApiClient(); return await apmApiClient.readUser({ endpoint: 'GET /internal/apm/get_latest_agent_versions', }); } - registry.when( - 'Agent latest versions when configuration is defined', - { config: 'basic', archives: [] }, - () => { - it('returns a version when agent is listed in the file', async () => { - const { status, body } = await callApi(); - expect(status).to.be(200); + registry.when('Agent latest versions when configuration is defined', () => { + it('returns a version when agent is listed in the file', async () => { + const { status, body } = await callApi(); + expect(status).to.be(200); - const agents = body.data; + const agents = body.data; - const nodeAgent = agents[nodeAgentName] as ElasticApmAgentLatestVersion; - expect(nodeAgent?.latest_version).not.to.be(undefined); - }); + const nodeAgent = agents[nodeAgentName] as ElasticApmAgentLatestVersion; + expect(nodeAgent?.latest_version).not.to.be(undefined); + }); - it('returns undefined when agent is not listed in the file', async () => { - const { status, body } = await callApi(); - expect(status).to.be(200); + it('returns undefined when agent is not listed in the file', async () => { + const { status, body } = await callApi(); + expect(status).to.be(200); - const agents = body.data; + const agents = body.data; - // @ts-ignore - const unlistedAgent = agents[unlistedAgentName] as ElasticApmAgentLatestVersion; - expect(unlistedAgent?.latest_version).to.be(undefined); - }); - } - ); + // @ts-ignore + const unlistedAgent = agents[unlistedAgentName] as ElasticApmAgentLatestVersion; + expect(unlistedAgent?.latest_version).to.be(undefined); + }); + }); } From 66f5e4cf5be0a8d02359d42b6b442229ea857bad Mon Sep 17 00:00:00 2001 From: kibanamachine <42973632+kibanamachine@users.noreply.github.com> Date: Mon, 4 Nov 2024 13:47:20 +0000 Subject: [PATCH 04/11] [CI] Auto-commit changed files from 'node scripts/lint_ts_projects --fix' --- x-pack/test/tsconfig.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/x-pack/test/tsconfig.json b/x-pack/test/tsconfig.json index 2ba14ceb1218..8fa19baa5f21 100644 --- a/x-pack/test/tsconfig.json +++ b/x-pack/test/tsconfig.json @@ -187,6 +187,7 @@ "@kbn/alerting-types", "@kbn/ai-assistant-common", "@kbn/core-deprecations-common", - "@kbn/usage-collection-plugin" + "@kbn/usage-collection-plugin", + "@kbn/observability-utils" ] } From 17fb5e21f914dc0e4a0a383d8c4b57ebb81ca441 Mon Sep 17 00:00:00 2001 From: Carlos Crespo Date: Mon, 4 Nov 2024 15:18:30 +0100 Subject: [PATCH 05/11] Simplify apmApi --- .../apm/agent_explorer/agent_explorer.spec.ts | 5 ++--- .../apm/agent_explorer/latest_agent_versions.spec.ts | 3 +-- .../deployment_agnostic/services/apm_api.ts | 10 +++------- 3 files changed, 6 insertions(+), 12 deletions(-) diff --git a/x-pack/test/api_integration/deployment_agnostic/apis/observability/apm/agent_explorer/agent_explorer.spec.ts b/x-pack/test/api_integration/deployment_agnostic/apis/observability/apm/agent_explorer/agent_explorer.spec.ts index 5ea2697eb2eb..3549e9e62237 100644 --- a/x-pack/test/api_integration/deployment_agnostic/apis/observability/apm/agent_explorer/agent_explorer.spec.ts +++ b/x-pack/test/api_integration/deployment_agnostic/apis/observability/apm/agent_explorer/agent_explorer.spec.ts @@ -13,7 +13,7 @@ import { keyBy } from 'lodash'; import type { DeploymentAgnosticFtrProviderContext } from '../../../../ftr_provider_context'; export default function ApiTest({ getService }: DeploymentAgnosticFtrProviderContext) { - const apiApi = getService('apmApi'); + const apmApiClient = getService('apmApi'); const registry = getService('registry'); const synthtrace = getService('synthtrace'); @@ -28,8 +28,7 @@ export default function ApiTest({ getService }: DeploymentAgnosticFtrProviderCon APIClientRequestParamsOf<'GET /internal/apm/get_agents_per_service'>['params'] > ) { - const client = await apiApi.createApmApiClient(); - return await client.readUser({ + return await apmApiClient.readUser({ endpoint: 'GET /internal/apm/get_agents_per_service', params: { query: { diff --git a/x-pack/test/api_integration/deployment_agnostic/apis/observability/apm/agent_explorer/latest_agent_versions.spec.ts b/x-pack/test/api_integration/deployment_agnostic/apis/observability/apm/agent_explorer/latest_agent_versions.spec.ts index 94c4ba08008f..c939e8911fc1 100644 --- a/x-pack/test/api_integration/deployment_agnostic/apis/observability/apm/agent_explorer/latest_agent_versions.spec.ts +++ b/x-pack/test/api_integration/deployment_agnostic/apis/observability/apm/agent_explorer/latest_agent_versions.spec.ts @@ -10,13 +10,12 @@ import type { DeploymentAgnosticFtrProviderContext } from '../../../../ftr_provi export default function ApiTest({ getService }: DeploymentAgnosticFtrProviderContext) { const registry = getService('registry'); - const apiApi = getService('apmApi'); + const apmApiClient = getService('apmApi'); const nodeAgentName = 'nodejs'; const unlistedAgentName = 'unlistedAgent'; async function callApi() { - const apmApiClient = await apiApi.createApmApiClient(); return await apmApiClient.readUser({ endpoint: 'GET /internal/apm/get_latest_agent_versions', }); diff --git a/x-pack/test/api_integration/deployment_agnostic/services/apm_api.ts b/x-pack/test/api_integration/deployment_agnostic/services/apm_api.ts index 0de6427000c5..2cfbbc526459 100644 --- a/x-pack/test/api_integration/deployment_agnostic/services/apm_api.ts +++ b/x-pack/test/api_integration/deployment_agnostic/services/apm_api.ts @@ -110,12 +110,8 @@ export interface SupertestReturnType { export function ApmApiProvider(context: DeploymentAgnosticFtrProviderContext) { return { - async createApmApiClient() { - return { - readUser: createApmApiClient(context, 'viewer'), - adminUser: createApmApiClient(context, 'admin'), - writeUser: createApmApiClient(context, 'editor'), - }; - }, + readUser: createApmApiClient(context, 'viewer'), + adminUser: createApmApiClient(context, 'admin'), + writeUser: createApmApiClient(context, 'editor'), }; } From 4d684c60a0ba0bc32bf1b6a81eea3a80756936b4 Mon Sep 17 00:00:00 2001 From: Carlos Crespo Date: Mon, 4 Nov 2024 15:24:43 +0100 Subject: [PATCH 06/11] Symplify createApmSynthtraceEsClient --- .../observability/apm/agent_explorer/agent_explorer.spec.ts | 3 +-- .../api_integration/deployment_agnostic/services/synthtrace.ts | 3 ++- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/x-pack/test/api_integration/deployment_agnostic/apis/observability/apm/agent_explorer/agent_explorer.spec.ts b/x-pack/test/api_integration/deployment_agnostic/apis/observability/apm/agent_explorer/agent_explorer.spec.ts index 3549e9e62237..073f3c65634e 100644 --- a/x-pack/test/api_integration/deployment_agnostic/apis/observability/apm/agent_explorer/agent_explorer.spec.ts +++ b/x-pack/test/api_integration/deployment_agnostic/apis/observability/apm/agent_explorer/agent_explorer.spec.ts @@ -57,8 +57,7 @@ export default function ApiTest({ getService }: DeploymentAgnosticFtrProviderCon let apmSynthtraceEsClient: ApmSynthtraceEsClient; before(async () => { - const version = (await synthtrace.apmSynthtraceKibanaClient.installApmPackage()).version; - apmSynthtraceEsClient = await synthtrace.createApmSynthtraceEsClient(version); + apmSynthtraceEsClient = await synthtrace.createApmSynthtraceEsClient(); const serviceOtelJava = apm .service({ diff --git a/x-pack/test/api_integration/deployment_agnostic/services/synthtrace.ts b/x-pack/test/api_integration/deployment_agnostic/services/synthtrace.ts index 1ab2692095ca..eda492ff37a4 100644 --- a/x-pack/test/api_integration/deployment_agnostic/services/synthtrace.ts +++ b/x-pack/test/api_integration/deployment_agnostic/services/synthtrace.ts @@ -39,7 +39,8 @@ export function SynthtraceProvider({ getService }: DeploymentAgnosticFtrProvider return { apmSynthtraceKibanaClient, createLogsSynthtraceEsClient: () => getLogsSynthtraceEsClient(client), - async createApmSynthtraceEsClient(packageVersion: string) { + async createApmSynthtraceEsClient() { + const packageVersion = (await apmSynthtraceKibanaClient.installApmPackage()).version; return getApmSynthtraceEsClient({ client, packageVersion }); }, }; From c2b1f1f03e40c570affaf97b666d8a0459df33fa Mon Sep 17 00:00:00 2001 From: Carlos Crespo Date: Tue, 5 Nov 2024 19:36:35 +0100 Subject: [PATCH 07/11] Remove registry service --- .../apm/agent_explorer/agent_explorer.spec.ts | 15 ++- .../observability/apm/agent_explorer/index.ts | 15 +++ .../latest_agent_versions.spec.ts | 4 +- .../apis/observability/apm/index.ts | 43 +------- .../serverless/oblt.serverless.config.ts | 1 - .../configs/stateful/oblt.stateful.config.ts | 1 - .../default_configs/serverless.config.base.ts | 2 - .../default_configs/stateful.config.base.ts | 2 - .../deployment_agnostic/services/index.ts | 2 - .../deployment_agnostic/services/registry.ts | 97 ------------------- 10 files changed, 25 insertions(+), 157 deletions(-) create mode 100644 x-pack/test/api_integration/deployment_agnostic/apis/observability/apm/agent_explorer/index.ts delete mode 100644 x-pack/test/api_integration/deployment_agnostic/services/registry.ts diff --git a/x-pack/test/api_integration/deployment_agnostic/apis/observability/apm/agent_explorer/agent_explorer.spec.ts b/x-pack/test/api_integration/deployment_agnostic/apis/observability/apm/agent_explorer/agent_explorer.spec.ts index 073f3c65634e..95d438ba87ca 100644 --- a/x-pack/test/api_integration/deployment_agnostic/apis/observability/apm/agent_explorer/agent_explorer.spec.ts +++ b/x-pack/test/api_integration/deployment_agnostic/apis/observability/apm/agent_explorer/agent_explorer.spec.ts @@ -14,7 +14,6 @@ import type { DeploymentAgnosticFtrProviderContext } from '../../../../ftr_provi export default function ApiTest({ getService }: DeploymentAgnosticFtrProviderContext) { const apmApiClient = getService('apmApi'); - const registry = getService('registry'); const synthtrace = getService('synthtrace'); const start = new Date('2021-01-01T00:00:00.000Z').getTime(); @@ -43,16 +42,16 @@ export default function ApiTest({ getService }: DeploymentAgnosticFtrProviderCon }); } - registry.when('Agent explorer when data is not loaded', () => { - it('handles empty state', async () => { - const { status, body } = await callApi(); + describe('Agent explorer', () => { + describe('when data is not loaded', () => { + it('handles empty state', async () => { + const { status, body } = await callApi(); - expect(status).to.be(200); - expect(body.items).to.be.empty(); + expect(status).to.be(200); + expect(body.items).to.be.empty(); + }); }); - }); - registry.when('Agent explorer', () => { describe('when data is loaded', () => { let apmSynthtraceEsClient: ApmSynthtraceEsClient; diff --git a/x-pack/test/api_integration/deployment_agnostic/apis/observability/apm/agent_explorer/index.ts b/x-pack/test/api_integration/deployment_agnostic/apis/observability/apm/agent_explorer/index.ts new file mode 100644 index 000000000000..f77b13923930 --- /dev/null +++ b/x-pack/test/api_integration/deployment_agnostic/apis/observability/apm/agent_explorer/index.ts @@ -0,0 +1,15 @@ +/* + * 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; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import { DeploymentAgnosticFtrProviderContext } from '../../../../ftr_provider_context'; + +export default function ({ loadTestFile }: DeploymentAgnosticFtrProviderContext) { + describe('agent_explorer', () => { + loadTestFile(require.resolve('./agent_explorer.spec.ts')); + loadTestFile(require.resolve('./latest_agent_versions.spec.ts')); + }); +} diff --git a/x-pack/test/api_integration/deployment_agnostic/apis/observability/apm/agent_explorer/latest_agent_versions.spec.ts b/x-pack/test/api_integration/deployment_agnostic/apis/observability/apm/agent_explorer/latest_agent_versions.spec.ts index c939e8911fc1..7d4099388525 100644 --- a/x-pack/test/api_integration/deployment_agnostic/apis/observability/apm/agent_explorer/latest_agent_versions.spec.ts +++ b/x-pack/test/api_integration/deployment_agnostic/apis/observability/apm/agent_explorer/latest_agent_versions.spec.ts @@ -9,9 +9,7 @@ import expect from '@kbn/expect'; import type { DeploymentAgnosticFtrProviderContext } from '../../../../ftr_provider_context'; export default function ApiTest({ getService }: DeploymentAgnosticFtrProviderContext) { - const registry = getService('registry'); const apmApiClient = getService('apmApi'); - const nodeAgentName = 'nodejs'; const unlistedAgentName = 'unlistedAgent'; @@ -21,7 +19,7 @@ export default function ApiTest({ getService }: DeploymentAgnosticFtrProviderCon }); } - registry.when('Agent latest versions when configuration is defined', () => { + describe('Agent latest versions when configuration is defined', () => { it('returns a version when agent is listed in the file', async () => { const { status, body } = await callApi(); expect(status).to.be(200); diff --git a/x-pack/test/api_integration/deployment_agnostic/apis/observability/apm/index.ts b/x-pack/test/api_integration/deployment_agnostic/apis/observability/apm/index.ts index 9758e0c50a2d..c21a307111d3 100644 --- a/x-pack/test/api_integration/deployment_agnostic/apis/observability/apm/index.ts +++ b/x-pack/test/api_integration/deployment_agnostic/apis/observability/apm/index.ts @@ -4,52 +4,13 @@ * 2.0; you may not use this file except in compliance with the Elastic License * 2.0. */ -import globby from 'globby'; -import path from 'path'; -import type { DeploymentAgnosticFtrProviderContext } from '../../../ftr_provider_context'; -const cwd = path.join(__dirname); -const envGrepFiles = process.env.APM_TEST_GREP_FILES as string; - -function getGlobPattern() { - try { - const envGrepFilesParsed = JSON.parse(envGrepFiles as string) as string[]; - return envGrepFilesParsed.map((pattern) => { - return pattern.includes('spec') ? `**/${pattern}**` : `**/${pattern}**.spec.ts`; - }); - } catch (e) { - // ignore - } - return '**/*.spec.ts'; -} +import { DeploymentAgnosticFtrProviderContext } from '../../../ftr_provider_context'; export default function apmApiIntegrationTests({ - getService, loadTestFile, }: DeploymentAgnosticFtrProviderContext) { - // DO NOT SKIP - // Skipping here will skip the entire apm api test suite - // Instead skip (flaky) tests individually - // Failing: See https://github.com/elastic/kibana/issues/176948 describe('APM API tests', function () { - const registry = getService('registry'); - const filePattern = getGlobPattern(); - const tests = globby.sync(filePattern, { cwd }); - - if (envGrepFiles) { - // eslint-disable-next-line no-console - console.log( - `\nCommand "--grep-files=${filePattern}" matched ${tests.length} file(s):\n${tests - .map((name) => ` - ${name}`) - .join('\n')}\n` - ); - } - - tests.forEach((testName) => { - describe(testName, () => { - loadTestFile(require.resolve(`./${testName}`)); - registry.run(); - }); - }); + loadTestFile(require.resolve('./agent_explorer')); }); } diff --git a/x-pack/test/api_integration/deployment_agnostic/configs/serverless/oblt.serverless.config.ts b/x-pack/test/api_integration/deployment_agnostic/configs/serverless/oblt.serverless.config.ts index e1864f0444ab..245663416243 100644 --- a/x-pack/test/api_integration/deployment_agnostic/configs/serverless/oblt.serverless.config.ts +++ b/x-pack/test/api_integration/deployment_agnostic/configs/serverless/oblt.serverless.config.ts @@ -10,7 +10,6 @@ import { createServerlessTestConfig } from '../../default_configs/serverless.con export default createServerlessTestConfig({ serverlessProject: 'oblt', testFiles: [require.resolve('./oblt.index.ts')], - servicesRequiredForTestAnalysis: ['registry'], junit: { reportName: 'Serverless Observability - Deployment-agnostic API Integration Tests', }, diff --git a/x-pack/test/api_integration/deployment_agnostic/configs/stateful/oblt.stateful.config.ts b/x-pack/test/api_integration/deployment_agnostic/configs/stateful/oblt.stateful.config.ts index 2c2e33a126b5..7b3cf3a7f181 100644 --- a/x-pack/test/api_integration/deployment_agnostic/configs/stateful/oblt.stateful.config.ts +++ b/x-pack/test/api_integration/deployment_agnostic/configs/stateful/oblt.stateful.config.ts @@ -9,7 +9,6 @@ import { createStatefulTestConfig } from '../../default_configs/stateful.config. export default createStatefulTestConfig({ testFiles: [require.resolve('./oblt.index.ts')], - servicesRequiredForTestAnalysis: ['registry'], junit: { reportName: 'Stateful Observability - Deployment-agnostic API Integration Tests', }, diff --git a/x-pack/test/api_integration/deployment_agnostic/default_configs/serverless.config.base.ts b/x-pack/test/api_integration/deployment_agnostic/default_configs/serverless.config.base.ts index 353021aa343a..e7df37f5aa31 100644 --- a/x-pack/test/api_integration/deployment_agnostic/default_configs/serverless.config.base.ts +++ b/x-pack/test/api_integration/deployment_agnostic/default_configs/serverless.config.base.ts @@ -16,7 +16,6 @@ interface CreateTestConfigOptions { esServerArgs?: string[]; kbnServerArgs?: string[]; services?: T; - servicesRequiredForTestAnalysis?: string[]; testFiles: string[]; junit: { reportName: string }; suiteTags?: { include?: string[]; exclude?: string[] }; @@ -86,7 +85,6 @@ export function createServerlessTestConfig { kbnServerArgs?: string[]; services?: T; testFiles: string[]; - servicesRequiredForTestAnalysis?: string[]; junit: { reportName: string }; suiteTags?: { include?: string[]; exclude?: string[] }; } @@ -101,7 +100,6 @@ export function createStatefulTestConfig void; - }>; - }> = []; - - let running: boolean = false; - - function when(title: string, callback: () => void, skip?: boolean) { - if (running) { - throw new Error("Can't add tests when running"); - } - - const frame = maybe(callsites()[1]); - - const file = frame?.getFileName(); - - if (!file) { - throw new Error('Could not infer file for suite'); - } - - callbacks.push({ - runs: [ - { - cb: () => { - const suite: ReturnType = (skip ? describe.skip : describe)( - title, - () => { - callback(); - } - ) as any; - - suite.file = file; - suite.eachTest((test) => { - test.file = file; - }); - }, - }, - ], - }); - } - - when.skip = (title: string, callback: () => void) => { - when(title, callback, true); - }; - - const registry = { - when, - run: () => { - running = true; - - const groups = joinByKey(callbacks, [], (a, b) => ({ - ...a, - ...b, - runs: a.runs.concat(b.runs), - })); - - callbacks.length = 0; - - const byConfig = groupBy(groups, 'config'); - - Object.keys(byConfig).forEach((config) => { - const groupsForConfig = byConfig[config]; - // register suites for other configs, but skip them so tests are marked as such - // and their snapshots are not marked as obsolete - describe(config, () => { - groupsForConfig.forEach((group) => { - const { runs } = group; - - describe(config, () => { - runs.forEach((run) => { - run.cb(); - }); - }); - }); - }); - }); - - running = false; - }, - }; - - return registry; -} From 980adb6f656bc85106c02b05ab96b22e70dd003c Mon Sep 17 00:00:00 2001 From: kibanamachine <42973632+kibanamachine@users.noreply.github.com> Date: Tue, 5 Nov 2024 18:48:31 +0000 Subject: [PATCH 08/11] [CI] Auto-commit changed files from 'node scripts/lint_ts_projects --fix' --- x-pack/test/tsconfig.json | 1 - 1 file changed, 1 deletion(-) diff --git a/x-pack/test/tsconfig.json b/x-pack/test/tsconfig.json index 8fa19baa5f21..9db41aecbb61 100644 --- a/x-pack/test/tsconfig.json +++ b/x-pack/test/tsconfig.json @@ -188,6 +188,5 @@ "@kbn/ai-assistant-common", "@kbn/core-deprecations-common", "@kbn/usage-collection-plugin", - "@kbn/observability-utils" ] } From 76262fa66056cd77d038295f2bb32c008cacfd0c Mon Sep 17 00:00:00 2001 From: Carlos Crespo Date: Wed, 6 Nov 2024 10:03:49 +0100 Subject: [PATCH 09/11] Fix after merge --- .../deployment_agnostic/apis/observability/infra/services.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/x-pack/test/api_integration/deployment_agnostic/apis/observability/infra/services.ts b/x-pack/test/api_integration/deployment_agnostic/apis/observability/infra/services.ts index 54a74ff72c26..deb5dba8d29c 100644 --- a/x-pack/test/api_integration/deployment_agnostic/apis/observability/infra/services.ts +++ b/x-pack/test/api_integration/deployment_agnostic/apis/observability/infra/services.ts @@ -108,8 +108,7 @@ export default function ({ getService }: DeploymentAgnosticFtrProviderContext) { withInternalHeaders: true, }); - const version = (await synthtrace.apmSynthtraceKibanaClient.installApmPackage()).version; - synthtraceApmClient = await synthtrace.createApmSynthtraceEsClient(version); + synthtraceApmClient = await synthtrace.createApmSynthtraceEsClient(); }); after(async () => { await synthtrace.apmSynthtraceKibanaClient.uninstallApmPackage(); From 8f40435c926a7d65676d2d8f0716d097da7c7714 Mon Sep 17 00:00:00 2001 From: Carlos Crespo Date: Wed, 6 Nov 2024 13:52:35 +0100 Subject: [PATCH 10/11] Fix Infra and APM api calls --- .../apis/observability/apm/index.ts | 2 +- .../observability/infra/infra_asset_count.ts | 1 + .../infra/infra_custom_dashboards.ts | 1 + .../observability/infra/ip_to_hostname.ts | 1 + .../infra/metrics_overview_top.ts | 1 + .../infra/metrics_process_list.ts | 1 + .../infra/metrics_process_list_chart.ts | 1 + .../apis/observability/infra/node_details.ts | 1 + .../apis/observability/infra/services.ts | 1 + .../apis/observability/infra/snapshot.ts | 1 + .../apis/observability/infra/sources.ts | 1 + .../configs/serverless/oblt.index.ts | 18 ++++---- .../deployment_agnostic/services/apm_api.ts | 42 ++++++++++++++----- 13 files changed, 51 insertions(+), 21 deletions(-) diff --git a/x-pack/test/api_integration/deployment_agnostic/apis/observability/apm/index.ts b/x-pack/test/api_integration/deployment_agnostic/apis/observability/apm/index.ts index c21a307111d3..a62c11d40b1a 100644 --- a/x-pack/test/api_integration/deployment_agnostic/apis/observability/apm/index.ts +++ b/x-pack/test/api_integration/deployment_agnostic/apis/observability/apm/index.ts @@ -10,7 +10,7 @@ import { DeploymentAgnosticFtrProviderContext } from '../../../ftr_provider_cont export default function apmApiIntegrationTests({ loadTestFile, }: DeploymentAgnosticFtrProviderContext) { - describe('APM API tests', function () { + describe('APM', function () { loadTestFile(require.resolve('./agent_explorer')); }); } diff --git a/x-pack/test/api_integration/deployment_agnostic/apis/observability/infra/infra_asset_count.ts b/x-pack/test/api_integration/deployment_agnostic/apis/observability/infra/infra_asset_count.ts index 0c139ccb2023..c76e7dbff888 100644 --- a/x-pack/test/api_integration/deployment_agnostic/apis/observability/infra/infra_asset_count.ts +++ b/x-pack/test/api_integration/deployment_agnostic/apis/observability/infra/infra_asset_count.ts @@ -48,6 +48,7 @@ export default function ({ getService }: DeploymentAgnosticFtrProviderContext) { before(async () => { supertestWithAdminScope = await roleScopedSupertest.getSupertestWithRoleScope('admin', { withInternalHeaders: true, + useCookieHeader: true, }); await esArchiver.load('x-pack/test/functional/es_archives/infra/8.0.0/logs_and_metrics'); }); diff --git a/x-pack/test/api_integration/deployment_agnostic/apis/observability/infra/infra_custom_dashboards.ts b/x-pack/test/api_integration/deployment_agnostic/apis/observability/infra/infra_custom_dashboards.ts index 140b0540c7d7..0357d7b1fff7 100644 --- a/x-pack/test/api_integration/deployment_agnostic/apis/observability/infra/infra_custom_dashboards.ts +++ b/x-pack/test/api_integration/deployment_agnostic/apis/observability/infra/infra_custom_dashboards.ts @@ -28,6 +28,7 @@ export default function ({ getService }: DeploymentAgnosticFtrProviderContext) { before(async () => { supertestWithAdminScope = await roleScopedSupertest.getSupertestWithRoleScope('admin', { withInternalHeaders: true, + useCookieHeader: true, }); }); diff --git a/x-pack/test/api_integration/deployment_agnostic/apis/observability/infra/ip_to_hostname.ts b/x-pack/test/api_integration/deployment_agnostic/apis/observability/infra/ip_to_hostname.ts index d75c16aacd7c..084a7ea9ac27 100644 --- a/x-pack/test/api_integration/deployment_agnostic/apis/observability/infra/ip_to_hostname.ts +++ b/x-pack/test/api_integration/deployment_agnostic/apis/observability/infra/ip_to_hostname.ts @@ -19,6 +19,7 @@ export default function ipToHostNameTest({ getService }: DeploymentAgnosticFtrPr before(async () => { supertestWithAdminScope = await roleScopedSupertest.getSupertestWithRoleScope('admin', { withInternalHeaders: true, + useCookieHeader: true, }); await esArchiver.load('x-pack/test/functional/es_archives/infra/metrics_and_logs'); }); diff --git a/x-pack/test/api_integration/deployment_agnostic/apis/observability/infra/metrics_overview_top.ts b/x-pack/test/api_integration/deployment_agnostic/apis/observability/infra/metrics_overview_top.ts index 8411d0e9d664..2a1ee8d3e682 100644 --- a/x-pack/test/api_integration/deployment_agnostic/apis/observability/infra/metrics_overview_top.ts +++ b/x-pack/test/api_integration/deployment_agnostic/apis/observability/infra/metrics_overview_top.ts @@ -25,6 +25,7 @@ export default function ({ getService }: DeploymentAgnosticFtrProviderContext) { before(async () => { supertestWithAdminScope = await roleScopedSupertest.getSupertestWithRoleScope('admin', { withInternalHeaders: true, + useCookieHeader: true, }); await esArchiver.load('x-pack/test/functional/es_archives/infra/7.0.0/hosts'); }); diff --git a/x-pack/test/api_integration/deployment_agnostic/apis/observability/infra/metrics_process_list.ts b/x-pack/test/api_integration/deployment_agnostic/apis/observability/infra/metrics_process_list.ts index 7b49950ef996..c10d5181372b 100644 --- a/x-pack/test/api_integration/deployment_agnostic/apis/observability/infra/metrics_process_list.ts +++ b/x-pack/test/api_integration/deployment_agnostic/apis/observability/infra/metrics_process_list.ts @@ -24,6 +24,7 @@ export default function ({ getService }: DeploymentAgnosticFtrProviderContext) { before(async () => { supertestWithAdminScope = await roleScopedSupertest.getSupertestWithRoleScope('admin', { withInternalHeaders: true, + useCookieHeader: true, }); await esArchiver.load('x-pack/test/functional/es_archives/infra/8.0.0/metrics_and_apm'); }); diff --git a/x-pack/test/api_integration/deployment_agnostic/apis/observability/infra/metrics_process_list_chart.ts b/x-pack/test/api_integration/deployment_agnostic/apis/observability/infra/metrics_process_list_chart.ts index a1b453d5b074..10f214ea642f 100644 --- a/x-pack/test/api_integration/deployment_agnostic/apis/observability/infra/metrics_process_list_chart.ts +++ b/x-pack/test/api_integration/deployment_agnostic/apis/observability/infra/metrics_process_list_chart.ts @@ -25,6 +25,7 @@ export default function ({ getService }: DeploymentAgnosticFtrProviderContext) { before(async () => { supertestWithAdminScope = await roleScopedSupertest.getSupertestWithRoleScope('admin', { withInternalHeaders: true, + useCookieHeader: true, }); await esArchiver.load('x-pack/test/functional/es_archives/infra/8.0.0/metrics_and_apm'); }); diff --git a/x-pack/test/api_integration/deployment_agnostic/apis/observability/infra/node_details.ts b/x-pack/test/api_integration/deployment_agnostic/apis/observability/infra/node_details.ts index e492e0dc9723..7452c319be91 100644 --- a/x-pack/test/api_integration/deployment_agnostic/apis/observability/infra/node_details.ts +++ b/x-pack/test/api_integration/deployment_agnostic/apis/observability/infra/node_details.ts @@ -36,6 +36,7 @@ export default function ({ getService }: DeploymentAgnosticFtrProviderContext) { before(async () => { supertestWithAdminScope = await roleScopedSupertest.getSupertestWithRoleScope('admin', { withInternalHeaders: true, + useCookieHeader: true, }); await esArchiver.load('x-pack/test/functional/es_archives/infra/8.0.0/pods_only'); }); diff --git a/x-pack/test/api_integration/deployment_agnostic/apis/observability/infra/services.ts b/x-pack/test/api_integration/deployment_agnostic/apis/observability/infra/services.ts index deb5dba8d29c..b8e47a40a912 100644 --- a/x-pack/test/api_integration/deployment_agnostic/apis/observability/infra/services.ts +++ b/x-pack/test/api_integration/deployment_agnostic/apis/observability/infra/services.ts @@ -106,6 +106,7 @@ export default function ({ getService }: DeploymentAgnosticFtrProviderContext) { before(async () => { supertestWithAdminScope = await roleScopedSupertest.getSupertestWithRoleScope('admin', { withInternalHeaders: true, + useCookieHeader: true, }); synthtraceApmClient = await synthtrace.createApmSynthtraceEsClient(); diff --git a/x-pack/test/api_integration/deployment_agnostic/apis/observability/infra/snapshot.ts b/x-pack/test/api_integration/deployment_agnostic/apis/observability/infra/snapshot.ts index 0b32cf5b8ce8..f3636662f2b5 100644 --- a/x-pack/test/api_integration/deployment_agnostic/apis/observability/infra/snapshot.ts +++ b/x-pack/test/api_integration/deployment_agnostic/apis/observability/infra/snapshot.ts @@ -37,6 +37,7 @@ export default function ({ getService }: DeploymentAgnosticFtrProviderContext) { before(async () => { supertestWithAdminScope = await roleScopedSupertest.getSupertestWithRoleScope('admin', { withInternalHeaders: true, + useCookieHeader: true, }); }); after(async () => { diff --git a/x-pack/test/api_integration/deployment_agnostic/apis/observability/infra/sources.ts b/x-pack/test/api_integration/deployment_agnostic/apis/observability/infra/sources.ts index a2f7d7ca591a..0710c5112b0f 100644 --- a/x-pack/test/api_integration/deployment_agnostic/apis/observability/infra/sources.ts +++ b/x-pack/test/api_integration/deployment_agnostic/apis/observability/infra/sources.ts @@ -39,6 +39,7 @@ export default function ({ getService }: DeploymentAgnosticFtrProviderContext) { before(async () => { supertestWithAdminScope = await roleScopedSupertest.getSupertestWithRoleScope('admin', { withInternalHeaders: true, + useCookieHeader: true, }); await esArchiver.load('x-pack/test/functional/es_archives/infra/metrics_and_logs'); await kibanaServer.savedObjects.cleanStandardList(); diff --git a/x-pack/test/api_integration/deployment_agnostic/configs/serverless/oblt.index.ts b/x-pack/test/api_integration/deployment_agnostic/configs/serverless/oblt.index.ts index 6353f871a807..a857509d9a76 100644 --- a/x-pack/test/api_integration/deployment_agnostic/configs/serverless/oblt.index.ts +++ b/x-pack/test/api_integration/deployment_agnostic/configs/serverless/oblt.index.ts @@ -11,15 +11,15 @@ export default function ({ loadTestFile }: DeploymentAgnosticFtrProviderContext) this.tags(['esGate']); // load new oblt and platform deployment-agnostic test here - loadTestFile(require.resolve('../../apis/console')); - loadTestFile(require.resolve('../../apis/core')); - loadTestFile(require.resolve('../../apis/management')); - loadTestFile(require.resolve('../../apis/observability/infra')); - loadTestFile(require.resolve('../../apis/observability/alerting')); - loadTestFile(require.resolve('../../apis/observability/dataset_quality')); - loadTestFile(require.resolve('../../apis/painless_lab')); - loadTestFile(require.resolve('../../apis/saved_objects_management')); - loadTestFile(require.resolve('../../apis/observability/slo')); + // loadTestFile(require.resolve('../../apis/console')); + // loadTestFile(require.resolve('../../apis/core')); + // loadTestFile(require.resolve('../../apis/management')); + // loadTestFile(require.resolve('../../apis/observability/infra')); + // loadTestFile(require.resolve('../../apis/observability/alerting')); + // loadTestFile(require.resolve('../../apis/observability/dataset_quality')); + // loadTestFile(require.resolve('../../apis/painless_lab')); + // loadTestFile(require.resolve('../../apis/saved_objects_management')); + // loadTestFile(require.resolve('../../apis/observability/slo')); loadTestFile(require.resolve('../../apis/observability/apm')); }); } diff --git a/x-pack/test/api_integration/deployment_agnostic/services/apm_api.ts b/x-pack/test/api_integration/deployment_agnostic/services/apm_api.ts index 2cfbbc526459..26d92997a602 100644 --- a/x-pack/test/api_integration/deployment_agnostic/services/apm_api.ts +++ b/x-pack/test/api_integration/deployment_agnostic/services/apm_api.ts @@ -13,38 +13,58 @@ import type { } from '@kbn/apm-plugin/public/services/rest/create_call_apm_api'; import { APIEndpoint } from '@kbn/apm-plugin/server'; import { formatRequest } from '@kbn/server-route-repository'; +import { RoleCredentials } from '@kbn/ftr-common-functional-services'; import type { DeploymentAgnosticFtrProviderContext } from '../ftr_provider_context'; +const INTERNAL_API_REGEX = /^\S+\s(\/)?internal\/[^\s]*$/; + +type InternalApi = `${string} /internal/${string}`; +interface ExternalEndpointParams { + roleAuthc: RoleCredentials; +} + +type Options = (TEndpoint extends InternalApi + ? {} + : ExternalEndpointParams) & { + type?: 'form-data'; + endpoint: TEndpoint; + spaceId?: string; +} & APIClientRequestParamsOf & { + params?: { query?: { _inspect?: boolean } }; + }; + +function isPublicApi( + options: Options +): options is Options & ExternalEndpointParams { + return !INTERNAL_API_REGEX.test(options.endpoint); +} + function createApmApiClient({ getService }: DeploymentAgnosticFtrProviderContext, role: string) { const supertestWithoutAuth = getService('supertestWithoutAuth'); const samlAuth = getService('samlAuth'); + const logger = getService('log'); return async ( - options: { - type?: 'form-data'; - endpoint: TEndpoint; - spaceId?: string; - } & APIClientRequestParamsOf & { - params?: { query?: { _inspect?: boolean } }; - } + options: Options ): Promise> => { const { endpoint, type } = options; const params = 'params' in options ? (options.params as Record) : {}; - const roleAuthc = await samlAuth.createM2mApiKeyWithRoleScope(role); + const credentials = isPublicApi(options) + ? options.roleAuthc.apiKeyHeader + : await samlAuth.getM2MApiCookieCredentialsWithRoleScope(role); const headers: Record = { ...samlAuth.getInternalRequestHeader(), - ...roleAuthc.apiKeyHeader, + ...credentials, }; const { method, pathname, version } = formatRequest(endpoint, params.path); const pathnameWithSpaceId = options.spaceId ? `/s/${options.spaceId}${pathname}` : pathname; const url = format({ pathname: pathnameWithSpaceId, query: params?.query }); - // eslint-disable-next-line no-console - console.debug(`Calling APM API: ${method.toUpperCase()} ${url}`); + logger.debug(`Calling APM API: ${method.toUpperCase()} ${url}`); if (version) { headers['Elastic-Api-Version'] = version; From 9f531aa4b5dfb0979374dabf25e168038a514945 Mon Sep 17 00:00:00 2001 From: Carlos Crespo Date: Wed, 6 Nov 2024 13:59:48 +0100 Subject: [PATCH 11/11] Revert change --- .../configs/serverless/oblt.index.ts | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/x-pack/test/api_integration/deployment_agnostic/configs/serverless/oblt.index.ts b/x-pack/test/api_integration/deployment_agnostic/configs/serverless/oblt.index.ts index a857509d9a76..6353f871a807 100644 --- a/x-pack/test/api_integration/deployment_agnostic/configs/serverless/oblt.index.ts +++ b/x-pack/test/api_integration/deployment_agnostic/configs/serverless/oblt.index.ts @@ -11,15 +11,15 @@ export default function ({ loadTestFile }: DeploymentAgnosticFtrProviderContext) this.tags(['esGate']); // load new oblt and platform deployment-agnostic test here - // loadTestFile(require.resolve('../../apis/console')); - // loadTestFile(require.resolve('../../apis/core')); - // loadTestFile(require.resolve('../../apis/management')); - // loadTestFile(require.resolve('../../apis/observability/infra')); - // loadTestFile(require.resolve('../../apis/observability/alerting')); - // loadTestFile(require.resolve('../../apis/observability/dataset_quality')); - // loadTestFile(require.resolve('../../apis/painless_lab')); - // loadTestFile(require.resolve('../../apis/saved_objects_management')); - // loadTestFile(require.resolve('../../apis/observability/slo')); + loadTestFile(require.resolve('../../apis/console')); + loadTestFile(require.resolve('../../apis/core')); + loadTestFile(require.resolve('../../apis/management')); + loadTestFile(require.resolve('../../apis/observability/infra')); + loadTestFile(require.resolve('../../apis/observability/alerting')); + loadTestFile(require.resolve('../../apis/observability/dataset_quality')); + loadTestFile(require.resolve('../../apis/painless_lab')); + loadTestFile(require.resolve('../../apis/saved_objects_management')); + loadTestFile(require.resolve('../../apis/observability/slo')); loadTestFile(require.resolve('../../apis/observability/apm')); }); }