From 632836f62f2a0c0a741663872f49a0e0ac694392 Mon Sep 17 00:00:00 2001 From: Spencer Date: Tue, 16 Jun 2020 12:13:24 -0700 Subject: [PATCH] [7.x] [kbn/es] only make one attempt in tests to avoid timeout (#69197) (#69298) Co-authored-by: spalger Co-authored-by: Elastic Machine Co-authored-by: spalger Co-authored-by: Elastic Machine --- packages/kbn-es/src/utils/native_realm.js | 30 ++++++++++--------- .../kbn-es/src/utils/native_realm.test.js | 10 +++---- 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/packages/kbn-es/src/utils/native_realm.js b/packages/kbn-es/src/utils/native_realm.js index 573944a8cc6d0..5075cfb965c08 100644 --- a/packages/kbn-es/src/utils/native_realm.js +++ b/packages/kbn-es/src/utils/native_realm.js @@ -37,13 +37,8 @@ exports.NativeRealm = class NativeRealm { this._log = log; } - async setPassword(username, password = this._elasticPassword, { attempt = 1 } = {}) { - await this._autoRetry(async () => { - this._log.info( - (attempt > 1 ? `attempt ${attempt}: ` : '') + - `setting ${chalk.bold(username)} password to ${chalk.bold(password)}` - ); - + async setPassword(username, password = this._elasticPassword, retryOpts = {}) { + await this._autoRetry(retryOpts, async () => { try { await this._client.security.changePassword({ username, @@ -83,8 +78,8 @@ exports.NativeRealm = class NativeRealm { ); } - async getReservedUsers() { - return await this._autoRetry(async () => { + async getReservedUsers(retryOpts = {}) { + return await this._autoRetry(retryOpts, async () => { const resp = await this._client.security.getUser(); const usernames = Object.keys(resp.body).filter( (user) => resp.body[user].metadata._reserved === true @@ -98,9 +93,9 @@ exports.NativeRealm = class NativeRealm { }); } - async isSecurityEnabled() { + async isSecurityEnabled(retryOpts = {}) { try { - return await this._autoRetry(async () => { + return await this._autoRetry(retryOpts, async () => { const { body: { features }, } = await this._client.xpack.info({ categories: 'features' }); @@ -115,18 +110,25 @@ exports.NativeRealm = class NativeRealm { } } - async _autoRetry(fn, attempt = 1) { + async _autoRetry(opts, fn) { + const { attempt = 1, maxAttempts = 3 } = opts; + try { return await fn(attempt); } catch (error) { - if (attempt >= 3) { + if (attempt >= maxAttempts) { throw error; } const sec = 1.5 * attempt; this._log.warning(`assuming ES isn't initialized completely, trying again in ${sec} seconds`); await new Promise((resolve) => setTimeout(resolve, sec * 1000)); - return await this._autoRetry(fn, attempt + 1); + + const nextOpts = { + ...opts, + attempt: attempt + 1, + }; + return await this._autoRetry(nextOpts, fn); } } }; diff --git a/packages/kbn-es/src/utils/native_realm.test.js b/packages/kbn-es/src/utils/native_realm.test.js index 54732f7136fcc..cd124c97dfdd4 100644 --- a/packages/kbn-es/src/utils/native_realm.test.js +++ b/packages/kbn-es/src/utils/native_realm.test.js @@ -85,7 +85,7 @@ describe('isSecurityEnabled', () => { throw error; }); - expect(await nativeRealm.isSecurityEnabled()).toBe(false); + expect(await nativeRealm.isSecurityEnabled({ maxAttempts: 1 })).toBe(false); }); test('rejects if unexpected error is thrown', async () => { @@ -97,9 +97,9 @@ describe('isSecurityEnabled', () => { throw error; }); - await expect(nativeRealm.isSecurityEnabled()).rejects.toThrowErrorMatchingInlineSnapshot( - `"ResponseError"` - ); + await expect( + nativeRealm.isSecurityEnabled({ maxAttempts: 1 }) + ).rejects.toThrowErrorMatchingInlineSnapshot(`"ResponseError"`); }); }); @@ -226,7 +226,7 @@ describe('setPassword', () => { }); await expect( - nativeRealm.setPassword('kibana_system', 'foo') + nativeRealm.setPassword('kibana_system', 'foo', { maxAttempts: 1 }) ).rejects.toThrowErrorMatchingInlineSnapshot(`"SomeError"`); }); });