Skip to content

Commit

Permalink
[Security Solutions][Detection Engine] Adds number of signals to wait…
Browse files Browse the repository at this point in the history
…For() within integration tests to increase determinism (#81203)

## Summary

* Fixes #81119
* Fixes #81186
* Adds an additional parameter to the waitFor and tweaks the different tests to pass in the number of signals to waitFor.

### Checklist

- [x] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios
  • Loading branch information
FrankHassanabad authored Oct 20, 2020
1 parent e33fd71 commit 20872b6
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export default ({ getService }: FtrProviderContext) => {
it('should be able to execute and get 10 signals', async () => {
const rule = { ...getSimpleRule(), from: '1900-01-01T00:00:00.000Z', query: '*:*' };
await createRule(supertest, rule);
await waitForSignalsToBePresent(supertest);
await waitForSignalsToBePresent(supertest, 10);
const signalsOpen = await getAllSignals(supertest);
expect(signalsOpen.hits.hits.length).equal(10);
});
Expand All @@ -102,7 +102,7 @@ export default ({ getService }: FtrProviderContext) => {
it('should be able to get a count of 10 closed signals when closing 10', async () => {
const rule = { ...getSimpleRule(), from: '1900-01-01T00:00:00.000Z', query: '*:*' };
await createRule(supertest, rule);
await waitForSignalsToBePresent(supertest);
await waitForSignalsToBePresent(supertest, 10);
const signalsOpen = await getAllSignals(supertest);
const signalIds = signalsOpen.hits.hits.map((signal) => signal._id);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ export default ({ getService }: FtrProviderContext) => {
],
};
await createRule(supertest, ruleWithException);
await waitForSignalsToBePresent(supertest);
await waitForSignalsToBePresent(supertest, 10);
const signalsOpen = await getAllSignals(supertest);
expect(signalsOpen.hits.hits.length).equal(10);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export default ({ getService }: FtrProviderContext) => {
};

await createRule(supertest, rule);
await waitForSignalsToBePresent(supertest);
await waitForSignalsToBePresent(supertest, 10);
const signalsOpen = await getAllSignals(supertest);
expect(signalsOpen.hits.hits.length).equal(10);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export default ({ getService }: FtrProviderContext) => {
it('should be able to execute and get 10 signals', async () => {
const rule = { ...getSimpleRule(), from: '1900-01-01T00:00:00.000Z', query: '*:*' };
await createRule(supertest, rule);
await waitForSignalsToBePresent(supertest);
await waitForSignalsToBePresent(supertest, 10);
const signalsOpen = await getAllSignals(supertest);
expect(signalsOpen.hits.hits.length).equal(10);
});
Expand All @@ -101,7 +101,7 @@ export default ({ getService }: FtrProviderContext) => {
it('should be able to get a count of 10 closed signals when closing 10', async () => {
const rule = { ...getSimpleRule(), from: '1900-01-01T00:00:00.000Z', query: '*:*' };
await createRule(supertest, rule);
await waitForSignalsToBePresent(supertest);
await waitForSignalsToBePresent(supertest, 10);
const signalsOpen = await getAllSignals(supertest);
const signalIds = signalsOpen.hits.hits.map((signal) => signal._id);

Expand All @@ -124,7 +124,7 @@ export default ({ getService }: FtrProviderContext) => {
expect(signalsClosed.hits.hits.length).to.equal(10);
});

it('should be able close 10 signals immediately and they all should be closed', async () => {
it('should be able close signals immediately and they all should be closed', async () => {
const rule = { ...getSimpleRule(), from: '1900-01-01T00:00:00.000Z', query: '*:*' };
await createRule(supertest, rule);
await waitForSignalsToBePresent(supertest);
Expand Down
9 changes: 6 additions & 3 deletions x-pack/test/detection_engine_api_integration/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -794,11 +794,14 @@ export const waitForRuleSuccess = async (
};

/**
* Waits for the signal hits to be greater than zero before continuing
* Waits for the signal hits to be greater than the supplied number
* before continuing with a default of at least one signal
* @param supertest Deps
* @param numberOfSignals The number of signals to wait for, default is 1
*/
export const waitForSignalsToBePresent = async (
supertest: SuperTest<supertestAsPromised.Test>
supertest: SuperTest<supertestAsPromised.Test>,
numberOfSignals = 1
): Promise<void> => {
await waitFor(async () => {
const {
Expand All @@ -808,7 +811,7 @@ export const waitForSignalsToBePresent = async (
.set('kbn-xsrf', 'true')
.send(getQueryAllSignals())
.expect(200);
return signalsOpen.hits.hits.length > 0;
return signalsOpen.hits.hits.length >= numberOfSignals;
});
};

Expand Down

0 comments on commit 20872b6

Please sign in to comment.