-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore(cli-testing): add a retry for test #29871
Changes from all commits
f560279
92de292
2f7e0d8
9541b9c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/** | ||
* @param maxAttempts the maximum number of attempts | ||
* @param interval interval in milliseconds to observe between attempts | ||
*/ | ||
export type EventuallyOptions = { | ||
maxAttempts?: number; | ||
interval?: number; | ||
}; | ||
|
||
const wait = (ms: number): Promise<void> => new Promise((resolve) => setTimeout(resolve, ms)); | ||
const DEFAULT_INTERVAL = 1000; | ||
const DEFAULT_MAX_ATTEMPTS = 10; | ||
|
||
/** | ||
* Runs a function on an interval until the maximum number of attempts has | ||
* been reached. | ||
* | ||
* Default interval = 1000 milliseconds | ||
* Default maxAttempts = 10 | ||
* | ||
* @param fn function to run | ||
* @param options EventuallyOptions | ||
*/ | ||
const eventually = async <T>(call: () => Promise<T>, options?: EventuallyOptions): Promise<T> => { | ||
const opts = { | ||
interval: options?.interval ? options.interval : DEFAULT_INTERVAL, | ||
maxAttempts: options?.maxAttempts ? options.maxAttempts : DEFAULT_MAX_ATTEMPTS, | ||
}; | ||
|
||
while (opts.maxAttempts-- >= 0) { | ||
try { | ||
return await call(); | ||
} catch (err) { | ||
if (opts.maxAttempts <= 0) throw err; | ||
} | ||
await wait(opts.interval); | ||
} | ||
|
||
throw new Error('An unexpected error has occurred.'); | ||
}; | ||
|
||
export default eventually; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ import * as fs from 'fs'; | |
import * as path from 'path'; | ||
import * as yaml from 'yaml'; | ||
import { integTest, randomString, withoutBootstrap } from '../../lib'; | ||
import eventually from '../../lib/eventually'; | ||
|
||
jest.setTimeout(2 * 60 * 60_000); // Includes the time to acquire locks, worst-case single-threaded runtime | ||
|
||
|
@@ -283,17 +284,25 @@ integTest('can remove customPermissionsBoundary', withoutBootstrap(async (fixtur | |
}), | ||
}); | ||
policyArn = policy.Policy?.Arn; | ||
await fixture.cdkBootstrapModern({ | ||
// toolkitStackName doesn't matter for this particular invocation | ||
toolkitStackName: bootstrapStackName, | ||
customPermissionsBoundary: policyName, | ||
}); | ||
|
||
const response = await fixture.aws.cloudFormation('describeStacks', { StackName: bootstrapStackName }); | ||
expect( | ||
response.Stacks?.[0].Parameters?.some( | ||
param => (param.ParameterKey === 'InputPermissionsBoundary' && param.ParameterValue === policyName), | ||
)).toEqual(true); | ||
// Policy creation and consistency across regions is "almost immediate" | ||
// See: https://docs.aws.amazon.com/IAM/latest/UserGuide/troubleshoot_general.html#troubleshoot_general_eventual-consistency | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great catch on this and thank you for also adding it as a comment! |
||
// We will put this in an `eventually` block to retry stack creation with a reasonable timeout | ||
const createStackWithPermissionBoundary = async (): Promise<void> => { | ||
await fixture.cdkBootstrapModern({ | ||
// toolkitStackName doesn't matter for this particular invocation | ||
toolkitStackName: bootstrapStackName, | ||
customPermissionsBoundary: policyName, | ||
}); | ||
|
||
const response = await fixture.aws.cloudFormation('describeStacks', { StackName: bootstrapStackName }); | ||
expect( | ||
response.Stacks?.[0].Parameters?.some( | ||
param => (param.ParameterKey === 'InputPermissionsBoundary' && param.ParameterValue === policyName), | ||
)).toEqual(true); | ||
}; | ||
|
||
await eventually(createStackWithPermissionBoundary, { maxAttempts: 3 }); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not blocking on this, but just curious why 3? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Arbitrarily seemed reasonable. |
||
|
||
await fixture.cdkBootstrapModern({ | ||
// toolkitStackName doesn't matter for this particular invocation | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if
eventually
is a standard naming convention but I love it.