Skip to content

Commit d306724

Browse files
Spencerspalgerelasticmachine
authored
[7.8] [kbn/es] only make one attempt in tests to avoid timeout (#69197) (#69299)
Co-authored-by: spalger <spalger@users.noreply.github.com> Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
1 parent 1b0f29a commit d306724

File tree

2 files changed

+21
-19
lines changed

2 files changed

+21
-19
lines changed

packages/kbn-es/src/utils/native_realm.js

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,8 @@ exports.NativeRealm = class NativeRealm {
3737
this._log = log;
3838
}
3939

40-
async setPassword(username, password = this._elasticPassword, { attempt = 1 } = {}) {
41-
await this._autoRetry(async () => {
42-
this._log.info(
43-
(attempt > 1 ? `attempt ${attempt}: ` : '') +
44-
`setting ${chalk.bold(username)} password to ${chalk.bold(password)}`
45-
);
46-
40+
async setPassword(username, password = this._elasticPassword, retryOpts = {}) {
41+
await this._autoRetry(retryOpts, async () => {
4742
try {
4843
await this._client.security.changePassword({
4944
username,
@@ -83,8 +78,8 @@ exports.NativeRealm = class NativeRealm {
8378
);
8479
}
8580

86-
async getReservedUsers() {
87-
return await this._autoRetry(async () => {
81+
async getReservedUsers(retryOpts = {}) {
82+
return await this._autoRetry(retryOpts, async () => {
8883
const resp = await this._client.security.getUser();
8984
const usernames = Object.keys(resp.body).filter(
9085
(user) => resp.body[user].metadata._reserved === true
@@ -98,9 +93,9 @@ exports.NativeRealm = class NativeRealm {
9893
});
9994
}
10095

101-
async isSecurityEnabled() {
96+
async isSecurityEnabled(retryOpts = {}) {
10297
try {
103-
return await this._autoRetry(async () => {
98+
return await this._autoRetry(retryOpts, async () => {
10499
const {
105100
body: { features },
106101
} = await this._client.xpack.info({ categories: 'features' });
@@ -115,18 +110,25 @@ exports.NativeRealm = class NativeRealm {
115110
}
116111
}
117112

118-
async _autoRetry(fn, attempt = 1) {
113+
async _autoRetry(opts, fn) {
114+
const { attempt = 1, maxAttempts = 3 } = opts;
115+
119116
try {
120117
return await fn(attempt);
121118
} catch (error) {
122-
if (attempt >= 3) {
119+
if (attempt >= maxAttempts) {
123120
throw error;
124121
}
125122

126123
const sec = 1.5 * attempt;
127124
this._log.warning(`assuming ES isn't initialized completely, trying again in ${sec} seconds`);
128125
await new Promise((resolve) => setTimeout(resolve, sec * 1000));
129-
return await this._autoRetry(fn, attempt + 1);
126+
127+
const nextOpts = {
128+
...opts,
129+
attempt: attempt + 1,
130+
};
131+
return await this._autoRetry(nextOpts, fn);
130132
}
131133
}
132134
};

packages/kbn-es/src/utils/native_realm.test.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ describe('isSecurityEnabled', () => {
8585
throw error;
8686
});
8787

88-
expect(await nativeRealm.isSecurityEnabled()).toBe(false);
88+
expect(await nativeRealm.isSecurityEnabled({ maxAttempts: 1 })).toBe(false);
8989
});
9090

9191
test('rejects if unexpected error is thrown', async () => {
@@ -97,9 +97,9 @@ describe('isSecurityEnabled', () => {
9797
throw error;
9898
});
9999

100-
await expect(nativeRealm.isSecurityEnabled()).rejects.toThrowErrorMatchingInlineSnapshot(
101-
`"ResponseError"`
102-
);
100+
await expect(
101+
nativeRealm.isSecurityEnabled({ maxAttempts: 1 })
102+
).rejects.toThrowErrorMatchingInlineSnapshot(`"ResponseError"`);
103103
});
104104
});
105105

@@ -226,7 +226,7 @@ describe('setPassword', () => {
226226
});
227227

228228
await expect(
229-
nativeRealm.setPassword('kibana_system', 'foo')
229+
nativeRealm.setPassword('kibana_system', 'foo', { maxAttempts: 1 })
230230
).rejects.toThrowErrorMatchingInlineSnapshot(`"SomeError"`);
231231
});
232232
});

0 commit comments

Comments
 (0)