From b8237fce987c8366c9b326a08de2a43867f640c7 Mon Sep 17 00:00:00 2001 From: Panagiota Mitsopoulou Date: Tue, 14 May 2024 11:21:36 +0200 Subject: [PATCH] refactor reading services from test_serverless --- .../config/ess/config.base.ts | 15 +- .../config/serverless/config.base.ts | 1 - .../alerting/burn_rate/configs/ess.config.ts | 6 - .../burn_rate/configs/serverless.config.ts | 6 - .../api_integration/services/alerting_api.ts | 162 --------------- .../api_integration/services/index.ts | 5 - .../api_integration/services/slo_api.ts | 185 ------------------ .../shared}/services/alerting_api.ts | 2 +- .../test_serverless/shared/services/index.ts | 5 +- .../shared}/services/slo_api.ts | 0 10 files changed, 18 insertions(+), 369 deletions(-) delete mode 100644 x-pack/test_serverless/api_integration/services/alerting_api.ts delete mode 100644 x-pack/test_serverless/api_integration/services/slo_api.ts rename x-pack/{test/observability_solution_api_integration => test_serverless/shared}/services/alerting_api.ts (98%) rename x-pack/{test/observability_solution_api_integration => test_serverless/shared}/services/slo_api.ts (100%) diff --git a/x-pack/test/observability_solution_api_integration/config/ess/config.base.ts b/x-pack/test/observability_solution_api_integration/config/ess/config.base.ts index e19b33241ea32..13a6b28c271c8 100644 --- a/x-pack/test/observability_solution_api_integration/config/ess/config.base.ts +++ b/x-pack/test/observability_solution_api_integration/config/ess/config.base.ts @@ -7,18 +7,29 @@ import { FtrConfigProviderContext } from '@kbn/test'; import { services } from '../../../api_integration/services'; -export function createTestConfig(options) { +export interface CreateTestConfigOptions { + testFiles: string[]; + junit: { reportName: string }; +} + +export function createTestConfig(options: CreateTestConfigOptions) { return async ({ readConfigFile }: FtrConfigProviderContext) => { const xPackApiIntegrationTestsConfig = await readConfigFile( require.resolve('../../../api_integration/config.ts') ); + const svlSharedConfig = await readConfigFile( + require.resolve('../../../../test_serverless/shared/config.base.ts') + ); + const svlSharedServices = svlSharedConfig.get('services'); + return { ...xPackApiIntegrationTestsConfig.getAll(), testFiles: options.testFiles, services: { ...services, - ...options.services, + sloApi: svlSharedServices.sloApi, + alertingApi: svlSharedServices.alertingApi, }, junit: { reportName: 'X-Pack Οbservability Solution API Integration Tests', diff --git a/x-pack/test/observability_solution_api_integration/config/serverless/config.base.ts b/x-pack/test/observability_solution_api_integration/config/serverless/config.base.ts index f31dbdd641709..d332fc3f7ae18 100644 --- a/x-pack/test/observability_solution_api_integration/config/serverless/config.base.ts +++ b/x-pack/test/observability_solution_api_integration/config/serverless/config.base.ts @@ -23,7 +23,6 @@ export function createTestConfig(options: CreateTestConfigOptions) { ...svlSharedConfig.getAll(), services: { ...services, - ...options.services, }, kbnTestServer: { ...svlSharedConfig.get('kbnTestServer'), diff --git a/x-pack/test/observability_solution_api_integration/test_suites/alerting/burn_rate/configs/ess.config.ts b/x-pack/test/observability_solution_api_integration/test_suites/alerting/burn_rate/configs/ess.config.ts index 48995010ec191..faeda1a68fc2e 100644 --- a/x-pack/test/observability_solution_api_integration/test_suites/alerting/burn_rate/configs/ess.config.ts +++ b/x-pack/test/observability_solution_api_integration/test_suites/alerting/burn_rate/configs/ess.config.ts @@ -6,14 +6,8 @@ */ import { createTestConfig } from '../../../../config/ess/config.base'; -import { AlertingApiProvider } from '../../../../services/alerting_api'; -import { SloApiProvider } from '../../../../services/slo_api'; export default createTestConfig({ - services: { - alertingApi: AlertingApiProvider, - sloApi: SloApiProvider, - }, testFiles: [require.resolve('..')], junit: { reportName: 'SLO - Burn rate Integration Tests - ESS Env', diff --git a/x-pack/test/observability_solution_api_integration/test_suites/alerting/burn_rate/configs/serverless.config.ts b/x-pack/test/observability_solution_api_integration/test_suites/alerting/burn_rate/configs/serverless.config.ts index 388a6b0449e6f..b8cf174c8b69f 100644 --- a/x-pack/test/observability_solution_api_integration/test_suites/alerting/burn_rate/configs/serverless.config.ts +++ b/x-pack/test/observability_solution_api_integration/test_suites/alerting/burn_rate/configs/serverless.config.ts @@ -6,14 +6,8 @@ */ import { createTestConfig } from '../../../../config/serverless/config.base'; -import { AlertingApiProvider } from '../../../../services/alerting_api'; -import { SloApiProvider } from '../../../../services/slo_api'; export default createTestConfig({ - services: { - alertingApi: AlertingApiProvider, - sloApi: SloApiProvider, - }, testFiles: [require.resolve('..')], junit: { reportName: 'SLO - Integration Tests - Serverless Env', diff --git a/x-pack/test_serverless/api_integration/services/alerting_api.ts b/x-pack/test_serverless/api_integration/services/alerting_api.ts deleted file mode 100644 index 534d8bf156a87..0000000000000 --- a/x-pack/test_serverless/api_integration/services/alerting_api.ts +++ /dev/null @@ -1,162 +0,0 @@ -/* - * 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 type { - AggregationsAggregate, - SearchResponse, -} from '@elastic/elasticsearch/lib/api/typesWithBodyKey'; - -import { MetricThresholdParams } from '@kbn/infra-plugin/common/alerting/metrics'; -import { ThresholdParams } from '@kbn/observability-plugin/common/custom_threshold_rule/types'; -import { SloBurnRateRuleParams } from './slo_api'; -import { FtrProviderContext } from '../ftr_provider_context'; - -export function AlertingApiProvider({ getService }: FtrProviderContext) { - const retry = getService('retry'); - const supertest = getService('supertest'); - const es = getService('es'); - const requestTimeout = 30 * 1000; - const retryTimeout = 120 * 1000; - const logger = getService('log'); - - return { - async waitForRuleStatus({ - ruleId, - expectedStatus, - }: { - ruleId: string; - expectedStatus: string; - }) { - if (!ruleId) { - throw new Error(`'ruleId' is undefined`); - } - return await retry.tryForTime(retryTimeout, async () => { - const response = await supertest - .get(`/api/alerting/rule/${ruleId}`) - .set('kbn-xsrf', 'foo') - .set('x-elastic-internal-origin', 'foo') - .timeout(requestTimeout); - const { execution_status: executionStatus } = response.body || {}; - const { status } = executionStatus || {}; - if (status !== expectedStatus) { - throw new Error(`waitForStatus(${expectedStatus}): got ${status}`); - } - return executionStatus?.status; - }); - }, - - async waitForDocumentInIndex({ - indexName, - docCountTarget = 1, - }: { - indexName: string; - docCountTarget?: number; - }): Promise>> { - return await retry.tryForTime(retryTimeout, async () => { - const response = await es.search({ - index: indexName, - rest_total_hits_as_int: true, - }); - logger.debug(`Found ${response.hits.total} docs, looking for atleast ${docCountTarget}.`); - if (!response.hits.total || response.hits.total < docCountTarget) { - throw new Error('No hits found'); - } - return response; - }); - }, - - async waitForAlertInIndex({ - indexName, - ruleId, - }: { - indexName: string; - ruleId: string; - }): Promise>> { - if (!ruleId) { - throw new Error(`'ruleId' is undefined`); - } - return await retry.tryForTime(retryTimeout, async () => { - const response = await es.search({ - index: indexName, - body: { - query: { - term: { - 'kibana.alert.rule.uuid': ruleId, - }, - }, - }, - }); - if (response.hits.hits.length === 0) { - throw new Error('No hits found'); - } - return response; - }); - }, - - async createIndexConnector({ name, indexName }: { name: string; indexName: string }) { - const { body } = await supertest - .post(`/api/actions/connector`) - .set('kbn-xsrf', 'foo') - .set('x-elastic-internal-origin', 'foo') - .send({ - name, - config: { - index: indexName, - refresh: true, - }, - connector_type_id: '.index', - }); - return body.id as string; - }, - - async createRule({ - name, - ruleTypeId, - params, - actions = [], - tags = [], - schedule, - consumer, - }: { - ruleTypeId: string; - name: string; - params: MetricThresholdParams | ThresholdParams | SloBurnRateRuleParams; - actions?: any[]; - tags?: any[]; - schedule?: { interval: string }; - consumer: string; - }) { - const { body } = await supertest - .post(`/api/alerting/rule`) - .set('kbn-xsrf', 'foo') - .set('x-elastic-internal-origin', 'foo') - .send({ - params, - consumer, - schedule: schedule || { - interval: '5m', - }, - tags, - name, - rule_type_id: ruleTypeId, - actions, - }); - return body; - }, - - async findRule(ruleId: string) { - if (!ruleId) { - throw new Error(`'ruleId' is undefined`); - } - const response = await supertest - .get('/api/alerting/rules/_find') - .set('kbn-xsrf', 'foo') - .set('x-elastic-internal-origin', 'foo'); - return response.body.data.find((obj: any) => obj.id === ruleId); - }, - }; -} diff --git a/x-pack/test_serverless/api_integration/services/index.ts b/x-pack/test_serverless/api_integration/services/index.ts index 844c9339cf8ac..5ff5a2942a4f4 100644 --- a/x-pack/test_serverless/api_integration/services/index.ts +++ b/x-pack/test_serverless/api_integration/services/index.ts @@ -8,11 +8,8 @@ import { GenericFtrProviderContext } from '@kbn/test'; import { services as deploymentAgnosticSharedServices } from '../../shared/services/deployment_agnostic_services'; import { services as svlSharedServices } from '../../shared/services'; - -import { AlertingApiProvider } from './alerting_api'; import { SamlToolsProvider } from './saml_tools'; import { SvlCasesServiceProvider } from './svl_cases'; -import { SloApiProvider } from './slo_api'; import { TransformProvider } from './transform'; export const services = { @@ -21,10 +18,8 @@ export const services = { // serverless FTR services ...svlSharedServices, - alertingApi: AlertingApiProvider, samlTools: SamlToolsProvider, svlCases: SvlCasesServiceProvider, - sloApi: SloApiProvider, transform: TransformProvider, }; diff --git a/x-pack/test_serverless/api_integration/services/slo_api.ts b/x-pack/test_serverless/api_integration/services/slo_api.ts deleted file mode 100644 index 5a5c9eb5c5ff7..0000000000000 --- a/x-pack/test_serverless/api_integration/services/slo_api.ts +++ /dev/null @@ -1,185 +0,0 @@ -/* - * 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 { FtrProviderContext } from '../ftr_provider_context'; - -type DurationUnit = 'm' | 'h' | 'd' | 'w' | 'M'; - -interface Duration { - value: number; - unit: DurationUnit; -} - -interface WindowSchema { - id: string; - burnRateThreshold: number; - maxBurnRateThreshold: number; - longWindow: Duration; - shortWindow: Duration; - actionGroup: string; -} - -interface Dependency { - ruleId: string; - actionGroupsToSuppressOn: string[]; -} - -export interface SloBurnRateRuleParams { - sloId: string; - windows: WindowSchema[]; - dependencies?: Dependency[]; -} - -interface SloParams { - id?: string; - name: string; - description: string; - indicator: { - type: 'sli.kql.custom'; - params: { - index: string; - good: string; - total: string; - timestampField: string; - }; - }; - timeWindow: { - duration: string; - type: string; - }; - budgetingMethod: string; - objective: { - target: number; - }; - groupBy: string; -} - -export function SloApiProvider({ getService }: FtrProviderContext) { - const es = getService('es'); - const supertest = getService('supertest'); - const retry = getService('retry'); - const requestTimeout = 30 * 1000; - const retryTimeout = 180 * 1000; - - return { - async create(slo: SloParams) { - const { body } = await supertest - .post(`/api/observability/slos`) - .set('kbn-xsrf', 'foo') - .set('x-elastic-internal-origin', 'foo') - .send(slo); - - return body; - }, - - async delete(sloId: string) { - const response = await supertest - .delete(`/api/observability/slos/${sloId}`) - .set('kbn-xsrf', 'foo') - .set('x-elastic-internal-origin', 'foo'); - return response; - }, - - async waitForSloToBeDeleted(sloId: string) { - if (!sloId) { - throw new Error(`sloId is undefined`); - } - return await retry.tryForTime(retryTimeout, async () => { - const response = await supertest - .delete(`/api/observability/slos/${sloId}`) - .set('kbn-xsrf', 'foo') - .set('x-elastic-internal-origin', 'foo') - .timeout(requestTimeout); - if (!response.ok) { - throw new Error(`slodId [${sloId}] was not deleted`); - } - return response; - }); - }, - - async waitForSloCreated({ sloId }: { sloId: string }) { - if (!sloId) { - throw new Error(`'sloId is undefined`); - } - return await retry.tryForTime(retryTimeout, async () => { - const response = await supertest - .get(`/api/observability/slos/${sloId}`) - .set('kbn-xsrf', 'foo') - .set('x-elastic-internal-origin', 'foo') - .timeout(requestTimeout); - if (response.body.id === undefined) { - throw new Error(`No slo with id ${sloId} found`); - } - return response.body; - }); - }, - - async waitForSloSummaryTempIndexToExist(index: string) { - if (!index) { - throw new Error(`index is undefined`); - } - - return await retry.tryForTime(retryTimeout, async () => { - const indexExists = await es.indices.exists({ index, allow_no_indices: false }); - if (!indexExists) { - throw new Error(`index ${index} should exist`); - } - return indexExists; - }); - }, - - async getSloData({ sloId, indexName }: { sloId: string; indexName: string }) { - const response = await es.search({ - index: indexName, - body: { - query: { - bool: { - filter: [{ term: { 'slo.id': sloId } }], - }, - }, - }, - }); - return response; - }, - async waitForSloData({ sloId, indexName }: { sloId: string; indexName: string }) { - return await retry.tryForTime(retryTimeout, async () => { - const response = await es.search({ - index: indexName, - body: { - query: { - bool: { - filter: [{ term: { 'slo.id': sloId } }], - }, - }, - }, - }); - if (response.hits.hits.length === 0) { - throw new Error(`No hits found at index [${indexName}] for slo [${sloId}] `); - } - return response; - }); - }, - async deleteAllSLOs() { - const response = await supertest - .get(`/api/observability/slos/_definitions`) - .set('kbn-xsrf', 'true') - .set('x-elastic-internal-origin', 'foo') - .send() - .expect(200); - await Promise.all( - response.body.results.map(({ id }: { id: string }) => { - return supertest - .delete(`/api/observability/slos/${id}`) - .set('kbn-xsrf', 'true') - .set('x-elastic-internal-origin', 'foo') - .send() - .expect(204); - }) - ); - }, - }; -} diff --git a/x-pack/test/observability_solution_api_integration/services/alerting_api.ts b/x-pack/test_serverless/shared/services/alerting_api.ts similarity index 98% rename from x-pack/test/observability_solution_api_integration/services/alerting_api.ts rename to x-pack/test_serverless/shared/services/alerting_api.ts index 534d8bf156a87..041c637461417 100644 --- a/x-pack/test/observability_solution_api_integration/services/alerting_api.ts +++ b/x-pack/test_serverless/shared/services/alerting_api.ts @@ -13,7 +13,7 @@ import type { import { MetricThresholdParams } from '@kbn/infra-plugin/common/alerting/metrics'; import { ThresholdParams } from '@kbn/observability-plugin/common/custom_threshold_rule/types'; import { SloBurnRateRuleParams } from './slo_api'; -import { FtrProviderContext } from '../ftr_provider_context'; +import { FtrProviderContext } from '../../functional/ftr_provider_context'; export function AlertingApiProvider({ getService }: FtrProviderContext) { const retry = getService('retry'); diff --git a/x-pack/test_serverless/shared/services/index.ts b/x-pack/test_serverless/shared/services/index.ts index 98a6eee96514b..b14df717e4b02 100644 --- a/x-pack/test_serverless/shared/services/index.ts +++ b/x-pack/test_serverless/shared/services/index.ts @@ -10,7 +10,8 @@ import { SvlCommonApiServiceProvider } from './svl_common_api'; import { SvlReportingServiceProvider } from './svl_reporting'; import { SvlUserManagerProvider } from './svl_user_manager'; import { DataViewApiProvider } from './data_view_api'; - +import { AlertingApiProvider } from './alerting_api'; +import { SloApiProvider } from './slo_api'; export type { RoleCredentials } from './svl_user_manager'; export const services = { @@ -20,4 +21,6 @@ export const services = { svlReportingApi: SvlReportingServiceProvider, svlUserManager: SvlUserManagerProvider, dataViewApi: DataViewApiProvider, + alertingApi: AlertingApiProvider, + sloApi: SloApiProvider, }; diff --git a/x-pack/test/observability_solution_api_integration/services/slo_api.ts b/x-pack/test_serverless/shared/services/slo_api.ts similarity index 100% rename from x-pack/test/observability_solution_api_integration/services/slo_api.ts rename to x-pack/test_serverless/shared/services/slo_api.ts