Skip to content

Commit

Permalink
[7.17] Updates test file wrapper to deterministically detect file wri…
Browse files Browse the repository at this point in the history
…te completion (elastic#176115) (elastic#176167)

# Backport

This will backport the following commits from `main` to `7.17`:
- [Updates test file wrapper to deterministically detect file write
completion (elastic#176115)](elastic#176115)

<!--- Backport version: 8.9.8 -->

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

<!--BACKPORT [{"author":{"name":"Jeramy
Soucy","email":"[email protected]"},"sourceCommit":{"committedDate":"2024-02-02T14:57:37Z","message":"Updates
test file wrapper to deterministically detect file write completion
(elastic#176115)\n\nCloses elastic#119267\r\n\r\n## Summary\r\n\r\nAttempts to
deterministically detect when a file is written in entirety\r\nin order
to resolve flaky test issues where parsed JSON is
incomplete.\r\n\r\nFlaky Test
Runner:\r\nhttps://buildkite.com/elastic/kibana-flaky-test-suite-runner/builds/5015","sha":"f9125ba079aeaa31fcd07e442cf6789c344452ec","branchLabelMapping":{"^v8.13.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["Team:Security","release_note:skip","backport:all-open","v8.13.0"],"number":176115,"url":"https://github.com/elastic/kibana/pull/176115","mergeCommit":{"message":"Updates
test file wrapper to deterministically detect file write completion
(elastic#176115)\n\nCloses elastic#119267\r\n\r\n## Summary\r\n\r\nAttempts to
deterministically detect when a file is written in entirety\r\nin order
to resolve flaky test issues where parsed JSON is
incomplete.\r\n\r\nFlaky Test
Runner:\r\nhttps://buildkite.com/elastic/kibana-flaky-test-suite-runner/builds/5015","sha":"f9125ba079aeaa31fcd07e442cf6789c344452ec"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v8.13.0","labelRegex":"^v8.13.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/176115","number":176115,"mergeCommit":{"message":"Updates
test file wrapper to deterministically detect file write completion
(elastic#176115)\n\nCloses elastic#119267\r\n\r\n## Summary\r\n\r\nAttempts to
deterministically detect when a file is written in entirety\r\nin order
to resolve flaky test issues where parsed JSON is
incomplete.\r\n\r\nFlaky Test
Runner:\r\nhttps://buildkite.com/elastic/kibana-flaky-test-suite-runner/builds/5015","sha":"f9125ba079aeaa31fcd07e442cf6789c344452ec"}},{"url":"https://github.com/elastic/kibana/pull/176161","number":176161,"branch":"8.12","state":"OPEN"}]}]
BACKPORT-->
  • Loading branch information
jeramysoucy authored Feb 2, 2024
1 parent d965187 commit caa3916
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions x-pack/test/security_api_integration/tests/audit/audit_log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { RetryService } from '../../../../../test/common/services/retry';
import { FtrProviderContext } from '../../ftr_provider_context';

class FileWrapper {
delay = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
constructor(private readonly path: string, private readonly retry: RetryService) {}
async reset() {
// "touch" each file to ensure it exists and is empty before each test
Expand All @@ -35,10 +36,15 @@ class FileWrapper {
});
}
// writing in a file is an async operation. we use this method to make sure logs have been written.
async isNotEmpty() {
const content = await this.read();
const line = content[0];
return line.length > 0;
async isWritten() {
// attempt at determinism - wait for the size of the file to stop changing.
await this.retry.waitForWithTimeout(`file '${this.path}' to be written`, 5000, async () => {
const sizeBefore = Fs.statSync(this.path).size;
await this.delay(500);
const sizeAfter = Fs.statSync(this.path).size;
return sizeAfter === sizeBefore;
});
return Fs.statSync(this.path).size > 0;
}
}

Expand All @@ -57,8 +63,7 @@ export default function ({ getService }: FtrProviderContext) {

it('logs audit events when reading and writing saved objects', async () => {
await supertest.get('/audit_log?query=param').set('kbn-xsrf', 'foo').expect(204);
await retry.waitFor('logs event in the dest file', async () => await logFile.isNotEmpty());

await logFile.isWritten();
const content = await logFile.readJSON();

const httpEvent = content.find((c) => c.event.action === 'http_request');
Expand Down Expand Up @@ -94,8 +99,7 @@ export default function ({ getService }: FtrProviderContext) {
params: { username, password },
})
.expect(200);
await retry.waitFor('logs event in the dest file', async () => await logFile.isNotEmpty());

await logFile.isWritten();
const content = await logFile.readJSON();

const loginEvent = content.find((c) => c.event.action === 'user_login');
Expand All @@ -116,8 +120,7 @@ export default function ({ getService }: FtrProviderContext) {
params: { username, password: 'invalid_password' },
})
.expect(401);
await retry.waitFor('logs event in the dest file', async () => await logFile.isNotEmpty());

await logFile.isWritten();
const content = await logFile.readJSON();

const loginEvent = content.find((c) => c.event.action === 'user_login');
Expand Down

0 comments on commit caa3916

Please sign in to comment.