From d1b7b7fd411e30f087cc6a5764d37206aec0ad16 Mon Sep 17 00:00:00 2001 From: Juan Pablo Djeredjian Date: Mon, 30 Oct 2023 13:42:23 +0100 Subject: [PATCH] [Security Solution ] Fix flake in `bundled_prebuilt_rules_package/prerelease_packages.ts` API Integration test (#169780) Fixes: https://github.com/elastic/kibana/issues/162192 ## Summary Removes flake by explicitly calling the Fleet endpoint to install the latest package, and doing assertions before actually installing the rules. Previously we were calling directly `installPrebuiltRules` without having explicitly installed the package before. The old installation endpoint would check that the package was installed before proceeding, but the new install method doesn't. So the explicit installation is required. ## Flaky test runs - ~https://buildkite.com/elastic/kibana-flaky-test-suite-runner/builds/3721~ - ~https://buildkite.com/elastic/kibana-flaky-test-suite-runner/builds/3730 [CONTROL - NO CHANGES]~ - https://buildkite.com/elastic/kibana-flaky-test-suite-runner/builds/3773 [After refactor] --- .../prerelease_packages.ts | 16 ++++++--- .../install_fleet_package_by_url.ts | 35 ++++++++++++++++--- 2 files changed, 42 insertions(+), 9 deletions(-) diff --git a/x-pack/test/detection_engine_api_integration/security_and_spaces/bundled_prebuilt_rules_package/prerelease_packages.ts b/x-pack/test/detection_engine_api_integration/security_and_spaces/bundled_prebuilt_rules_package/prerelease_packages.ts index fd69e3128c3e7..7348b596d1404 100644 --- a/x-pack/test/detection_engine_api_integration/security_and_spaces/bundled_prebuilt_rules_package/prerelease_packages.ts +++ b/x-pack/test/detection_engine_api_integration/security_and_spaces/bundled_prebuilt_rules_package/prerelease_packages.ts @@ -9,6 +9,7 @@ import { FtrProviderContext } from '../../common/ftr_provider_context'; import { deleteAllPrebuiltRuleAssets, deleteAllRules } from '../../utils'; import { getInstalledRules } from '../../utils/prebuilt_rules/get_installed_rules'; import { getPrebuiltRulesStatus } from '../../utils/prebuilt_rules/get_prebuilt_rules_status'; +import { installPrebuiltRulesPackageViaFleetAPI } from '../../utils/prebuilt_rules/install_fleet_package_by_url'; import { installPrebuiltRules } from '../../utils/prebuilt_rules/install_prebuilt_rules'; // eslint-disable-next-line import/no-default-export @@ -37,14 +38,21 @@ export default ({ getService }: FtrProviderContext): void => { expect(statusBeforePackageInstallation.stats.num_prebuilt_rules_to_install).toBe(0); expect(statusBeforePackageInstallation.stats.num_prebuilt_rules_to_upgrade).toBe(0); - await installPrebuiltRules(es, supertest); + await installPrebuiltRulesPackageViaFleetAPI(es, supertest); - // Verify that status is updated after package installation const statusAfterPackageInstallation = await getPrebuiltRulesStatus(supertest); - expect(statusAfterPackageInstallation.stats.num_prebuilt_rules_installed).toBe(1); // 1 rule in package 99.0.0 - expect(statusAfterPackageInstallation.stats.num_prebuilt_rules_to_install).toBe(0); + expect(statusAfterPackageInstallation.stats.num_prebuilt_rules_installed).toBe(0); + expect(statusAfterPackageInstallation.stats.num_prebuilt_rules_to_install).toBe(1); // 1 rule in package 99.0.0 expect(statusAfterPackageInstallation.stats.num_prebuilt_rules_to_upgrade).toBe(0); + await installPrebuiltRules(es, supertest); + + // Verify that status is updated after package installation + const statusAfterRulesInstallation = await getPrebuiltRulesStatus(supertest); + expect(statusAfterRulesInstallation.stats.num_prebuilt_rules_installed).toBe(1); // 1 rule in package 99.0.0 + expect(statusAfterRulesInstallation.stats.num_prebuilt_rules_to_install).toBe(0); + expect(statusAfterRulesInstallation.stats.num_prebuilt_rules_to_upgrade).toBe(0); + // Get installed rules const rulesResponse = await getInstalledRules(supertest); diff --git a/x-pack/test/detection_engine_api_integration/utils/prebuilt_rules/install_fleet_package_by_url.ts b/x-pack/test/detection_engine_api_integration/utils/prebuilt_rules/install_fleet_package_by_url.ts index 802626881b8e6..bccdf28906e23 100644 --- a/x-pack/test/detection_engine_api_integration/utils/prebuilt_rules/install_fleet_package_by_url.ts +++ b/x-pack/test/detection_engine_api_integration/utils/prebuilt_rules/install_fleet_package_by_url.ts @@ -10,7 +10,7 @@ import { ALL_SAVED_OBJECT_INDICES } from '@kbn/core-saved-objects-server'; import { InstallPackageResponse } from '@kbn/fleet-plugin/common/types'; /** - * Installs prebuilt rules package `security_detection_engine` by version. + * Installs latest available non-prerelease prebuilt rules package `security_detection_engine`. * * @param es Elasticsearch client * @param supertest SuperTest instance @@ -18,13 +18,12 @@ import { InstallPackageResponse } from '@kbn/fleet-plugin/common/types'; * @returns Fleet install package response */ -export const installPrebuiltRulesPackageByVersion = async ( +export const installPrebuiltRulesPackageViaFleetAPI = async ( es: Client, - supertest: SuperTest.SuperTest, - version: string + supertest: SuperTest.SuperTest ): Promise => { const fleetResponse = await supertest - .post(`/api/fleet/epm/packages/security_detection_engine/${version}`) + .post(`/api/fleet/epm/packages/security_detection_engine`) .set('kbn-xsrf', 'xxxx') .type('application/json') .send({ force: true }) @@ -48,3 +47,29 @@ export const installPrebuiltRulesPackageByVersion = async ( return fleetResponse.body as InstallPackageResponse; }; +/** + * Installs prebuilt rules package `security_detection_engine`, passing in the version + * of the package as a parameter to the utl. + * + * @param es Elasticsearch client + * @param supertest SuperTest instance + * @param version Semver version of the `security_detection_engine` package to install + * @returns Fleet install package response + */ + +export const installPrebuiltRulesPackageByVersion = async ( + es: Client, + supertest: SuperTest.SuperTest, + version: string +): Promise => { + const fleetResponse = await supertest + .post(`/api/fleet/epm/packages/security_detection_engine/${version}`) + .set('kbn-xsrf', 'xxxx') + .type('application/json') + .send({ force: true }) + .expect(200); + + await es.indices.refresh({ index: ALL_SAVED_OBJECT_INDICES }); + + return fleetResponse.body as InstallPackageResponse; +};