Skip to content

Commit

Permalink
[Entity Analytics]Integration test cleanup (#168869)
Browse files Browse the repository at this point in the history
## Summary

Followup to some recent work to improve test reliability (namely
#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
  • Loading branch information
rylnd authored Nov 8, 2023
1 parent 2f038e1 commit ed9956d
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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', () => {
Expand All @@ -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;
Expand Down Expand Up @@ -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 () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
}
};

Expand Down Expand Up @@ -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<void> => {
await deleteRiskEngineTask({ es, log });
await cleanRiskEngineConfig({ kibanaServer });
await clearTransforms({ es, log });
await deleteRiskScoreIndices({ log, es });
};

export const updateRiskEngineConfigSO = async ({
attributes,
kibanaServer,
Expand Down Expand Up @@ -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}`);
}
};

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

Expand Down Expand Up @@ -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}`);
}
};

Expand Down

0 comments on commit ed9956d

Please sign in to comment.