Skip to content

Commit

Permalink
only make one attempt in tests to avoid timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
spalger committed Jun 15, 2020
1 parent e3ba5e5 commit a171a7e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
30 changes: 16 additions & 14 deletions packages/kbn-es/src/utils/native_realm.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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' });
Expand All @@ -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);
}
}
};
2 changes: 1 addition & 1 deletion packages/kbn-es/src/utils/native_realm.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ describe('setPassword', () => {
});

await expect(
nativeRealm.setPassword('kibana_system', 'foo')
nativeRealm.setPassword('kibana_system', 'foo', { maxAttempts: 1 })
).rejects.toThrowErrorMatchingInlineSnapshot(`"SomeError"`);
});
});

0 comments on commit a171a7e

Please sign in to comment.