From 40a456d63834fbd1085835203655b777fd8a7056 Mon Sep 17 00:00:00 2001 From: restrry Date: Thu, 2 Jul 2020 10:41:05 +0200 Subject: [PATCH] more robust tests --- .../server/client/audit_trail_client.ts | 1 + .../test_suites/audit_trail/index.ts | 27 ++++++++++++++++--- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/x-pack/plugins/audit_trail/server/client/audit_trail_client.ts b/x-pack/plugins/audit_trail/server/client/audit_trail_client.ts index 638d4190e03771..fe624821f5639a 100644 --- a/x-pack/plugins/audit_trail/server/client/audit_trail_client.ts +++ b/x-pack/plugins/audit_trail/server/client/audit_trail_client.ts @@ -32,6 +32,7 @@ export class AuditTrailClient implements Auditor { public add(event: AuditableEvent) { const user = this.deps.getCurrentUser(this.request); + // doesn't use getSpace since it's async operation calling ES const spaceId = this.deps.getSpaceId(this.request); this.event$.next({ diff --git a/x-pack/test/plugin_functional/test_suites/audit_trail/index.ts b/x-pack/test/plugin_functional/test_suites/audit_trail/index.ts index af52b6022a9e98..fb66f0dffc12a8 100644 --- a/x-pack/test/plugin_functional/test_suites/audit_trail/index.ts +++ b/x-pack/test/plugin_functional/test_suites/audit_trail/index.ts @@ -22,10 +22,17 @@ class FileWrapper { const content = await this.read(); return content.map((l) => JSON.parse(l)); } + // 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; + } } export default function ({ getPageObjects, getService }: FtrProviderContext) { const supertest = getService('supertest'); + const retry = getService('retry'); describe('Audit trail service', function () { this.tags('ciGroup7'); @@ -45,8 +52,11 @@ export default function ({ getPageObjects, getService }: FtrProviderContext) { .set('kbn-xsrf', 'foo') .expect(204); - const content = await logFile.readJSON(); + await retry.waitFor('logs event in the dest file', async () => { + return await logFile.isNotEmpty(); + }); + const content = await logFile.readJSON(); const pingCall = content.find( (c) => c.meta.scope === 'audit_trail_test/context/as_current_user' ); @@ -62,8 +72,11 @@ export default function ({ getPageObjects, getService }: FtrProviderContext) { .set('kbn-xsrf', 'foo') .expect(204); - const content = await logFile.readJSON(); + await retry.waitFor('logs event in the dest file', async () => { + return await logFile.isNotEmpty(); + }); + const content = await logFile.readJSON(); const pingCall = content.find( (c) => c.meta.scope === 'audit_trail_test/context/as_internal_user' ); @@ -79,8 +92,11 @@ export default function ({ getPageObjects, getService }: FtrProviderContext) { .set('kbn-xsrf', 'foo') .expect(204); - const content = await logFile.readJSON(); + await retry.waitFor('logs event in the dest file', async () => { + return await logFile.isNotEmpty(); + }); + const content = await logFile.readJSON(); const pingCall = content.find( (c) => c.meta.scope === 'audit_trail_test/contract/as_current_user' ); @@ -96,8 +112,11 @@ export default function ({ getPageObjects, getService }: FtrProviderContext) { .set('kbn-xsrf', 'foo') .expect(204); - const content = await logFile.readJSON(); + await retry.waitFor('logs event in the dest file', async () => { + return await logFile.isNotEmpty(); + }); + const content = await logFile.readJSON(); const pingCall = content.find( (c) => c.meta.scope === 'audit_trail_test/contract/as_internal_user' );