From ed9956daea8304c53f701c93e93bfba24807b3a5 Mon Sep 17 00:00:00 2001 From: Ryland Herrick Date: Wed, 8 Nov 2023 13:10:11 -0600 Subject: [PATCH] [Entity Analytics]Integration test cleanup (#168869) ## Summary Followup to some recent work to improve test reliability (namely https://github.com/elastic/kibana/pull/168469): * Consolidates teardown of risk engine artifacts into a single helper function * Removes references to flaky test issues in comments * Log utils errors at the warning level --- .../risk_engine/init_and_status_apis.ts | 33 +++---------------- .../risk_scoring_task_execution.ts | 15 ++------- .../group10/risk_engine/telemetry_usage.ts | 16 ++------- .../group10/risk_engine/utils.ts | 26 ++++++++++++--- 4 files changed, 33 insertions(+), 57 deletions(-) diff --git a/x-pack/test/detection_engine_api_integration/security_and_spaces/group10/risk_engine/init_and_status_apis.ts b/x-pack/test/detection_engine_api_integration/security_and_spaces/group10/risk_engine/init_and_status_apis.ts index 480ebd9d9c845..ecf413fcfed70 100644 --- a/x-pack/test/detection_engine_api_integration/security_and_spaces/group10/risk_engine/init_and_status_apis.ts +++ b/x-pack/test/detection_engine_api_integration/security_and_spaces/group10/risk_engine/init_and_status_apis.ts @@ -9,17 +9,14 @@ import expect from '@kbn/expect'; import { riskEngineConfigurationTypeName } from '@kbn/security-solution-plugin/server/lib/risk_engine/saved_object'; import { FtrProviderContext } from '../../../common/ftr_provider_context'; import { - cleanRiskEngineConfig, legacyTransformIds, createLegacyTransforms, clearLegacyTransforms, riskEngineRouteHelpersFactory, - clearTransforms, installLegacyRiskScore, getLegacyRiskScoreDashboards, clearLegacyDashboards, - deleteRiskEngineTask, - deleteAllRiskScores, + cleanRiskEngine, } from './utils'; // eslint-disable-next-line import/no-default-export @@ -32,35 +29,15 @@ export default ({ getService }: FtrProviderContext) => { describe('Risk Engine', () => { beforeEach(async () => { - await cleanRiskEngineConfig({ kibanaServer }); - await deleteRiskEngineTask({ es, log }); - await deleteAllRiskScores(log, es); - await clearTransforms({ - es, - log, - }); + await cleanRiskEngine({ kibanaServer, es, log }); }); afterEach(async () => { - await cleanRiskEngineConfig({ - kibanaServer, - }); - await clearLegacyTransforms({ - es, - log, - }); - await clearTransforms({ - es, - log, - }); - await clearLegacyDashboards({ - supertest, - log, - }); - await deleteRiskEngineTask({ es, log }); + await cleanRiskEngine({ kibanaServer, es, log }); + await clearLegacyTransforms({ es, log }); + await clearLegacyDashboards({ supertest, log }); }); - // FLAKY: https://github.com/elastic/kibana/issues/168376 describe('init api', () => { it('should return response with success status', async () => { const response = await riskEngineRoutes.init(); diff --git a/x-pack/test/detection_engine_api_integration/security_and_spaces/group10/risk_engine/risk_scoring_task_execution.ts b/x-pack/test/detection_engine_api_integration/security_and_spaces/group10/risk_engine/risk_scoring_task_execution.ts index 6a95d236a9d0a..4985bede5dd6f 100644 --- a/x-pack/test/detection_engine_api_integration/security_and_spaces/group10/risk_engine/risk_scoring_task_execution.ts +++ b/x-pack/test/detection_engine_api_integration/security_and_spaces/group10/risk_engine/risk_scoring_task_execution.ts @@ -13,18 +13,15 @@ import { dataGeneratorFactory } from '../../../utils/data_generator'; import { buildDocument, createAndSyncRuleAndAlertsFactory, - deleteRiskEngineTask, - deleteAllRiskScores, readRiskScores, waitForRiskScoresToBePresent, normalizeScores, riskEngineRouteHelpersFactory, updateRiskEngineConfigSO, getRiskEngineTask, - cleanRiskEngineConfig, waitForRiskEngineTaskToBeGone, deleteRiskScoreIndices, - clearTransforms, + cleanRiskEngine, } from './utils'; // eslint-disable-next-line import/no-default-export @@ -57,21 +54,15 @@ export default ({ getService }: FtrProviderContext): void => { }); beforeEach(async () => { - await cleanRiskEngineConfig({ kibanaServer }); - await deleteRiskEngineTask({ es, log }); - await deleteAllRiskScores(log, es); + await cleanRiskEngine({ kibanaServer, es, log }); await deleteAllAlerts(supertest, log, es); await deleteAllRules(supertest, log); - await clearTransforms({ es, log }); }); afterEach(async () => { - await cleanRiskEngineConfig({ kibanaServer }); - await deleteRiskEngineTask({ es, log }); - await deleteAllRiskScores(log, es); + await cleanRiskEngine({ kibanaServer, es, log }); await deleteAllAlerts(supertest, log, es); await deleteAllRules(supertest, log); - await clearTransforms({ es, log }); }); describe('with some alerts containing hosts', () => { diff --git a/x-pack/test/detection_engine_api_integration/security_and_spaces/group10/risk_engine/telemetry_usage.ts b/x-pack/test/detection_engine_api_integration/security_and_spaces/group10/risk_engine/telemetry_usage.ts index 2e7888fe00591..75e91eb1a6fc4 100644 --- a/x-pack/test/detection_engine_api_integration/security_and_spaces/group10/risk_engine/telemetry_usage.ts +++ b/x-pack/test/detection_engine_api_integration/security_and_spaces/group10/risk_engine/telemetry_usage.ts @@ -12,12 +12,9 @@ import { deleteAllRules, deleteAllAlerts, getRiskEngineStats } from '../../../ut import { buildDocument, createAndSyncRuleAndAlertsFactory, - deleteRiskEngineTask, - deleteRiskScoreIndices, waitForRiskScoresToBePresent, riskEngineRouteHelpersFactory, - cleanRiskEngineConfig, - clearTransforms, + cleanRiskEngine, } from './utils'; import { dataGeneratorFactory } from '../../../utils/data_generator'; @@ -49,12 +46,9 @@ export default ({ getService }: FtrProviderContext) => { }); beforeEach(async () => { - await cleanRiskEngineConfig({ kibanaServer }); - await deleteRiskEngineTask({ es, log }); - await deleteRiskScoreIndices({ log, es }); + await cleanRiskEngine({ kibanaServer, es, log }); await deleteAllAlerts(supertest, log, es); await deleteAllRules(supertest, log); - await clearTransforms({ es, log }); }); describe('Risk engine not enabled', () => { @@ -67,7 +61,6 @@ export default ({ getService }: FtrProviderContext) => { }); }); - // FLAKY: https://github.com/elastic/kibana/issues/168429 describe('Risk engine enabled', () => { let hostId: string; let userId: string; @@ -105,12 +98,9 @@ export default ({ getService }: FtrProviderContext) => { }); afterEach(async () => { - await cleanRiskEngineConfig({ kibanaServer }); - await deleteRiskEngineTask({ es, log }); - await deleteRiskScoreIndices({ log, es }); + await cleanRiskEngine({ kibanaServer, es, log }); await deleteAllAlerts(supertest, log, es); await deleteAllRules(supertest, log); - await clearTransforms({ es, log }); }); it('should return riskEngineMetrics with expected values', async () => { diff --git a/x-pack/test/detection_engine_api_integration/security_and_spaces/group10/risk_engine/utils.ts b/x-pack/test/detection_engine_api_integration/security_and_spaces/group10/risk_engine/utils.ts index 7af59dfab7cf8..3911f55d01631 100644 --- a/x-pack/test/detection_engine_api_integration/security_and_spaces/group10/risk_engine/utils.ts +++ b/x-pack/test/detection_engine_api_integration/security_and_spaces/group10/risk_engine/utils.ts @@ -119,7 +119,7 @@ export const deleteRiskScoreIndices = async ({ }), ]); } catch (e) { - log.error(`Error deleting risk score indices: ${e.message}`); + log.warning(`Error deleting risk score indices: ${e.message}`); } }; @@ -302,6 +302,24 @@ export const cleanRiskEngineConfig = async ({ } }; +/** + * General helper for cleaning up risk engine artifacts. This should be used before and after any risk engine tests so as not to pollute the test environment. + */ +export const cleanRiskEngine = async ({ + es, + kibanaServer, + log, +}: { + es: Client; + kibanaServer: KbnClient; + log: ToolingLog; +}): Promise => { + await deleteRiskEngineTask({ es, log }); + await cleanRiskEngineConfig({ kibanaServer }); + await clearTransforms({ es, log }); + await deleteRiskScoreIndices({ log, es }); +}; + export const updateRiskEngineConfigSO = async ({ attributes, kibanaServer, @@ -344,7 +362,7 @@ export const clearTransforms = async ({ force: true, }); } catch (e) { - log.error(`Error deleting risk_score_latest_transform_default: ${e.message}`); + log.warning(`Error deleting risk_score_latest_transform_default: ${e.message}`); } }; @@ -364,7 +382,7 @@ export const clearLegacyTransforms = async ({ try { await Promise.all(transforms); } catch (e) { - log.error(`Error deleting legacy transforms: ${e.message}`); + log.warning(`Error deleting legacy transforms: ${e.message}`); } }; @@ -392,7 +410,7 @@ export const clearLegacyDashboards = async ({ .send() .expect(200); } catch (e) { - log.error(`Error deleting legacy dashboards: ${e.message}`); + log.warning(`Error deleting legacy dashboards: ${e.message}`); } };