Skip to content

Commit

Permalink
[8.x] fix flaky test with timestamp (#195681) (#195695)
Browse files Browse the repository at this point in the history
# Backport

This will backport the following commits from `main` to `8.x`:
- [fix flaky test with timestamp
(#195681)](#195681)

<!--- Backport version: 9.4.3 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Paulo
Silva","email":"[email protected]"},"sourceCommit":{"committedDate":"2024-10-10T00:36:33Z","message":"fix
flaky test with timestamp (#195681)\n\n## Summary\r\n\r\nIt fixes the
flaky test raised on #195634 by adding the possibility to\r\npass the
timestamp to the function. That helps to eliminate flakiness,\r\nby
passing the same `currentTimestamp` to both the test and
the\r\nfunction. Also, it's a simpler approach that doesn't require
mocking\r\nglobal objects or using Jest's fake timers, keeping your
test\r\nstraightforward and easy to
understand.","sha":"b51ba0a27c852f967b922130d01ac7cf2ec11d64","branchLabelMapping":{"^v9.0.0$":"main","^v8.16.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","v9.0.0","Team:Cloud
Security","v8.16.0","backport:version"],"title":"Fix flaky Jest test on
generateVulnerabilitiesRuleQuery","number":195681,"url":"https://github.com/elastic/kibana/pull/195681","mergeCommit":{"message":"fix
flaky test with timestamp (#195681)\n\n## Summary\r\n\r\nIt fixes the
flaky test raised on #195634 by adding the possibility to\r\npass the
timestamp to the function. That helps to eliminate flakiness,\r\nby
passing the same `currentTimestamp` to both the test and
the\r\nfunction. Also, it's a simpler approach that doesn't require
mocking\r\nglobal objects or using Jest's fake timers, keeping your
test\r\nstraightforward and easy to
understand.","sha":"b51ba0a27c852f967b922130d01ac7cf2ec11d64"}},"sourceBranch":"main","suggestedTargetBranches":["8.x"],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/195681","number":195681,"mergeCommit":{"message":"fix
flaky test with timestamp (#195681)\n\n## Summary\r\n\r\nIt fixes the
flaky test raised on #195634 by adding the possibility to\r\npass the
timestamp to the function. That helps to eliminate flakiness,\r\nby
passing the same `currentTimestamp` to both the test and
the\r\nfunction. Also, it's a simpler approach that doesn't require
mocking\r\nglobal objects or using Jest's fake timers, keeping your
test\r\nstraightforward and easy to
understand.","sha":"b51ba0a27c852f967b922130d01ac7cf2ec11d64"}},{"branch":"8.x","label":"v8.16.0","branchLabelMappingKey":"^v8.16.0$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->

Co-authored-by: Paulo Silva <[email protected]>
  • Loading branch information
kibanamachine and opauloh authored Oct 10, 2024
1 parent 20c0c9c commit d9490ab
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ describe('CreateDetectionRuleFromVulnerability', () => {
} as Vulnerability;
const currentTimestamp = new Date().toISOString();

const query = generateVulnerabilitiesRuleQuery(mockVulnerability);
const query = generateVulnerabilitiesRuleQuery(mockVulnerability, currentTimestamp);
expect(query).toEqual(
`vulnerability.id: "CVE-2024-00005" AND event.ingested >= "${currentTimestamp}"`
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,11 @@ export const getVulnerabilityRuleName = (vulnerability: Vulnerability) => {
});
};

export const generateVulnerabilitiesRuleQuery = (vulnerability: Vulnerability) => {
const currentTimestamp = new Date().toISOString();

return `vulnerability.id: "${vulnerability.id}" AND event.ingested >= "${currentTimestamp}"`;
export const generateVulnerabilitiesRuleQuery = (
vulnerability: Vulnerability,
startTimestamp = new Date().toISOString()
) => {
return `vulnerability.id: "${vulnerability.id}" AND event.ingested >= "${startTimestamp}"`;
};

const CSP_RULE_TAG = 'Cloud Security';
Expand Down

0 comments on commit d9490ab

Please sign in to comment.