Skip to content

Commit

Permalink
[Security Solution ] Fix flake in `bundled_prebuilt_rules_package/pre…
Browse files Browse the repository at this point in the history
…release_packages.ts` API Integration test (elastic#169780)

Fixes: elastic#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]
  • Loading branch information
jpdjere authored Oct 30, 2023
1 parent 3ea90b3 commit d1b7b7f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,20 @@ 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
* @param version Semver version of the `security_detection_engine` package to install
* @returns Fleet install package response
*/

export const installPrebuiltRulesPackageByVersion = async (
export const installPrebuiltRulesPackageViaFleetAPI = async (
es: Client,
supertest: SuperTest.SuperTest<SuperTest.Test>,
version: string
supertest: SuperTest.SuperTest<SuperTest.Test>
): Promise<InstallPackageResponse> => {
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 })
Expand All @@ -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<SuperTest.Test>,
version: string
): Promise<InstallPackageResponse> => {
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;
};

0 comments on commit d1b7b7f

Please sign in to comment.