Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 3 additions & 0 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -1885,6 +1885,9 @@ x-pack/platform/plugins/shared/ml/server/models/data_recognizer/modules/security
/x-pack/solutions/security/test/api_integration/ftr_provider_context.d.ts @elastic/appex-qa
/x-pack/solutions/security/test/api_integration/services/index.ts @elastic/appex-qa
/x-pack/solutions/security/test/alerting_api_integration/ftr_provider_context.d.ts @elastic/appex-qa
/x-pack/test/apm_api_integration @elastic/appex-qa # temporarily due to SKA tests relocation
/x-pack/test/common/utils/observability @elastic/appex-qa # temporarily due to SKA tests relocation
/x-pack/test/common/utils/uptime @elastic/appex-qa # temporarily due to SKA tests relocation

# Core
/src/platform/test/api_integration/fixtures/kbn_archiver/management/saved_objects/relationships.json @elastic/kibana-core @elastic/kibana-data-discovery
Expand Down
2 changes: 1 addition & 1 deletion x-pack/test/accessibility/apps/group1/uptime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import moment from 'moment';
import { makeChecks } from '@kbn/test-suites-xpack-observability/api_integration/apis/uptime/rest/helper/make_checks';
import { makeChecks } from '../../../common/utils/uptime/helper/make_checks';
import { FtrProviderContext } from '../../ftr_provider_context';

const A11Y_TEST_MONITOR_ID = 'a11yTestMonitor';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import expect from '@kbn/expect';
import moment from 'moment';
import { SYNTHETICS_API_URLS } from '@kbn/synthetics-plugin/common/constants';
import { waitForDocumentInIndex } from '@kbn/test-suites-xpack-observability/alerting_api_integration/observability/helpers/alerting_wait_for_helpers';
import { waitForDocumentInIndex } from '../../../../../../common/utils/observability/alerting_wait_for_helpers';
import { RoleCredentials, SupertestWithRoleScopeType } from '../../../../services';
import { DeploymentAgnosticFtrProviderContext } from '../../../../ftr_provider_context';
import {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import expect from '@kbn/expect';
import moment from 'moment';
import { SYNTHETICS_API_URLS } from '@kbn/synthetics-plugin/common/constants';
import type { SyntheticsMonitorStatusRuleParams as StatusRuleParams } from '@kbn/response-ops-rule-params/synthetics_monitor_status';
import { waitForDocumentInIndex } from '@kbn/test-suites-xpack-observability/alerting_api_integration/observability/helpers/alerting_wait_for_helpers';
import { waitForDocumentInIndex } from '../../../../../../common/utils/observability/alerting_wait_for_helpers';
import { RoleCredentials, SupertestWithRoleScopeType } from '../../../../services';
import { DeploymentAgnosticFtrProviderContext } from '../../../../ftr_provider_context';
import {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
* 2.0.
*/

import archivesMetadata from '@kbn/test-suites-xpack-observability/apm_api_integration/common/fixtures/es_archiver/archives_metadata';
import archivesMetadata from '../../../../../../apm_api_integration/common/fixtures/es_archiver/archives_metadata';

export default archivesMetadata;
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { getStaticDataViewId } from '@kbn/apm-data-view';
import {
SupertestReturnType,
ApmApiError,
} from '@kbn/test-suites-xpack-observability/apm_api_integration/common/apm_api_supertest';
} from '../../../../../../apm_api_integration/common/apm_api_supertest';
import { SupertestWithRoleScope } from '../../../../services/role_scoped_supertest';
import type { DeploymentAgnosticFtrProviderContext } from '../../../../ftr_provider_context';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { meanBy, sumBy } from 'lodash';
import { ApmDocumentType } from '@kbn/apm-plugin/common/document_type';
import { RollupInterval } from '@kbn/apm-plugin/common/rollup';
import type { ApmSynthtraceEsClient } from '@kbn/apm-synthtrace';
import { roundNumber } from '@kbn/test-suites-xpack-observability/apm_api_integration/utils';
import { roundNumber } from '../../../../../../apm_api_integration/utils';
import type { DeploymentAgnosticFtrProviderContext } from '../../../../ftr_provider_context';

export default function ApiTest({ getService }: DeploymentAgnosticFtrProviderContext) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
* 2.0.
*/
import expect from '@kbn/expect';
import { ApmApiError } from '@kbn/test-suites-xpack-observability/apm_api_integration/common/apm_api_supertest';
import { expectToReject } from '@kbn/test-suites-xpack-observability/apm_api_integration/common/utils/expect_to_reject';
import { ApmApiError } from '../../../../../../apm_api_integration/common/apm_api_supertest';
import { expectToReject } from '../../../../../../apm_api_integration/common/utils/expect_to_reject';
import type { DeploymentAgnosticFtrProviderContext } from '../../../../ftr_provider_context';
import {
createServiceGroupApi,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import expect from 'expect';
import { serviceMap, timerange, apm, ApmFields } from '@kbn/apm-synthtrace-client';
import { Readable } from 'node:stream';
import { compact } from 'lodash';
import type { SupertestReturnType } from '@kbn/test-suites-xpack-observability/apm_api_integration/common/apm_api_supertest';
import type { SupertestReturnType } from '../../../../../../apm_api_integration/common/apm_api_supertest';
import type { DeploymentAgnosticFtrProviderContext } from '../../../../ftr_provider_context';
import {
extractExitSpansConnections,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import expect from '@kbn/expect';
import archives_metadata from '@kbn/test-suites-xpack-observability/apm_api_integration/common/fixtures/es_archiver/archives_metadata';
import archives_metadata from '../../../../../../apm_api_integration/common/fixtures/es_archiver/archives_metadata';
import type { DeploymentAgnosticFtrProviderContext } from '../../../../ftr_provider_context';
import { ARCHIVER_ROUTES } from '../constants/archiver';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import expect from '@kbn/expect';
import { APIReturnType } from '@kbn/apm-plugin/public/services/rest/create_call_apm_api';
import { ENVIRONMENT_ALL } from '@kbn/apm-plugin/common/environment_filter_values';
import archives_metadata from '@kbn/test-suites-xpack-observability/apm_api_integration/common/fixtures/es_archiver/archives_metadata';
import archives_metadata from '../../../../../../../apm_api_integration/common/fixtures/es_archiver/archives_metadata';
import type { DeploymentAgnosticFtrProviderContext } from '../../../../../ftr_provider_context';
import { ARCHIVER_ROUTES } from '../../constants/archiver';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/
import expect from '@kbn/expect';
import { PrivilegeType, ClusterPrivilegeType } from '@kbn/apm-plugin/common/privilege_type';
import { expectToReject } from '@kbn/test-suites-xpack-observability/apm_api_integration/common/utils/expect_to_reject';
import { expectToReject } from '../../../../../../../apm_api_integration/common/utils/expect_to_reject';
import type { RoleCredentials } from '../../../../../services';
import type { DeploymentAgnosticFtrProviderContext } from '../../../../../ftr_provider_context';
import type { ApmApiError } from '../../../../../services/apm_api';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { APIReturnType } from '@kbn/apm-plugin/public/services/rest/create_call_
import { RollupInterval } from '@kbn/apm-plugin/common/rollup';
import { ApmDocumentType, ApmTransactionDocumentType } from '@kbn/apm-plugin/common/document_type';
import type { ApmSynthtraceEsClient } from '@kbn/apm-synthtrace';
import { roundNumber } from '@kbn/test-suites-xpack-observability/apm_api_integration/utils';
import { roundNumber } from '../../../../../../apm_api_integration/utils';
import type { DeploymentAgnosticFtrProviderContext } from '../../../../ftr_provider_context';

type TransactionsGroupsDetailedStatistics =
Expand Down
131 changes: 131 additions & 0 deletions x-pack/test/apm_api_integration/common/apm_api_supertest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/*
* 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 { format } from 'url';
import supertest from 'supertest';
import request from 'superagent';
import type {
APIReturnType,
APIClientRequestParamsOf,
} from '@kbn/apm-plugin/public/services/rest/create_call_apm_api';
import type { APIEndpoint } from '@kbn/apm-plugin/server';
import type {
APIEndpoint as SourcesAPIEndpoint,
APIReturnType as SourcesAPIReturnType,
APIClientRequestParamsOf as SourcesAPIClientRequestParamsOf,
} from '@kbn/apm-sources-access-plugin/server';
import { formatRequest } from '@kbn/server-route-repository';

type CombinedAPIRequest<TEndpoint extends APIEndpoint | SourcesAPIEndpoint> =
TEndpoint extends APIEndpoint
? APIReturnType<TEndpoint>
: TEndpoint extends SourcesAPIEndpoint
? SourcesAPIReturnType<TEndpoint>
: never;

type CombinedOptions<TEndpoint extends APIEndpoint | SourcesAPIEndpoint> =
TEndpoint extends APIEndpoint
? Options<TEndpoint>
: TEndpoint extends SourcesAPIEndpoint
? SourceOptions<TEndpoint>
: never;

type Options<TEndpoint extends APIEndpoint> = {
type?: 'form-data';
endpoint: TEndpoint;
spaceId?: string;
} & APIClientRequestParamsOf<TEndpoint> & {
params?: { query?: { _inspect?: boolean } };
};

type SourceOptions<TEndpoint extends SourcesAPIEndpoint> = {
type?: 'form-data';
endpoint: TEndpoint;
spaceId?: string;
} & SourcesAPIClientRequestParamsOf<TEndpoint>;

export function createApmApiClient(st: supertest.Agent) {
return async <TEndpoint extends APIEndpoint | SourcesAPIEndpoint>(
options: CombinedOptions<TEndpoint>
): Promise<SupertestReturnType<TEndpoint>> => {
const { endpoint, type } = options;

const params = 'params' in options ? (options.params as Record<string, any>) : {};

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}`);

const headers: Record<string, string> = {
'kbn-xsrf': 'foo',
'x-elastic-internal-origin': 'foo',
};

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 = st[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 st[method](url).send(params.body).set(headers);
} else {
res = await st[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<request.Response, 'body'> & {
body: {
statusCode: number;
error: string;
message: string;
attributes: object;
};
};

export type ApmApiSupertest = ReturnType<typeof createApmApiClient>;

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<TEndpoint extends APIEndpoint | SourcesAPIEndpoint> {
status: number;
body: CombinedAPIRequest<TEndpoint>;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* 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.
*/

export default {
'8.0.0': {
start: '2020-08-26T11:00:43.849Z',
end: '2020-08-26T12:00:43.849Z',
},
'apm_8.0.0': {
start: '2021-08-03T06:50:15.910Z',
end: '2021-08-03T07:20:15.910Z',
},
infra_metrics_and_apm: {
start: '2019-06-29T02:48:39.386555Z',
end: '2022-06-29T06:46:26Z',
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* 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 { Client } from '@elastic/elasticsearch';
import job from '@kbn/ml-plugin/server/models/data_recognizer/modules/apm_transaction/ml/apm_tx_metrics.json';
import datafeed from '@kbn/ml-plugin/server/models/data_recognizer/modules/apm_transaction/ml/datafeed_apm_tx_metrics.json';
import { ToolingLog } from '@kbn/tooling-log';
import { MlApi } from '@kbn/test-suites-xpack-platform/api_integration/services/ml/api';

export async function createAndRunApmMlJobs({
es,
ml,
environments,
logger,
}: {
es: Client;
ml: MlApi;
environments: string[];
logger: ToolingLog;
}) {
// Creating multiple ml jobs in parallel is causing this tests to be flaky
// https://github.com/elastic/elasticsearch/issues/36271
for (const environment of environments) {
await createAndRunApmMlJob({ es, environment, ml, logger });
}
}

async function createAndRunApmMlJob({
es,
ml,
environment,
logger,
}: {
es: Client;
ml: MlApi;
environment: string;
logger: ToolingLog;
}) {
const jobId = `apm-tx-metrics-${environment}`;
await ml.createAndRunAnomalyDetectionLookbackJob(
// @ts-expect-error not entire job config
{
...job,
job_id: jobId,
allow_lazy_open: false,
custom_settings: {
job_tags: {
apm_ml_version: '3',
environment,
},
},
},
{
...datafeed,
indices_options: { allow_no_indices: true },
job_id: jobId,
indices: ['metrics-apm*', 'apm-*'],
datafeed_id: `apm-tx-metrics-${environment}-datafeed`,
query: {
bool: {
filter: [...datafeed.query.bool.filter, { term: { 'service.environment': environment } }],
},
},
}
);

logger.info(`Created ml job ${jobId}`);

await es.cluster.health({ index: '.ml-*', wait_for_status: 'yellow' });

logger.info(`Finished waiting for cluster to be healthy`);
}
17 changes: 17 additions & 0 deletions x-pack/test/apm_api_integration/common/utils/expect_to_reject.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* 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.
*/

export async function expectToReject<T extends Error>(fn: () => Promise<any>): Promise<T> {
let res: any;
try {
res = await fn();
} catch (e) {
return e;
}

throw new Error(`expectToReject resolved: "${JSON.stringify(res)}"`);
}
13 changes: 13 additions & 0 deletions x-pack/test/apm_api_integration/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* 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 { isFiniteNumber } from '@kbn/apm-plugin/common/utils/is_finite_number';
import { Maybe } from '@kbn/apm-plugin/typings/common';

export function roundNumber(num: Maybe<number>) {
return isFiniteNumber(num) ? Number(num.toPrecision(4)) : null;
}
Loading