Skip to content

Commit ebff5da

Browse files
committed
fix(e2e): unset expired credentials before CDK test retry and add rotator cleanup
1 parent dc4fb04 commit ebff5da

2 files changed

Lines changed: 54 additions & 3 deletions

File tree

packages/amplify-e2e-core/src/utils/credentials-rotator.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ const tryRefreshCredentials = async (parentRoleArn: string, childRoleArn?: strin
5050
};
5151

5252
let isRotationBackgroundTaskAlreadyScheduled = false;
53+
let credentialRefreshTimer: ReturnType<typeof setInterval> | undefined;
5354

5455
/**
5556
* Schedules a background task that attempts to refresh test account credentials
@@ -66,20 +67,34 @@ export const tryScheduleCredentialRefresh = () => {
6667

6768
if (process.env.CHILD_ACCOUNT_ROLE) {
6869
// Attempts to refresh credentials in background every 10 minutes.
69-
setInterval(() => {
70+
credentialRefreshTimer = setInterval(() => {
7071
void tryRefreshCredentials(process.env.TEST_ACCOUNT_ROLE, process.env.CHILD_ACCOUNT_ROLE);
7172
}, 10 * 60 * 1000);
73+
credentialRefreshTimer.unref();
7274

7375
console.log('Test profile credentials refresh was scheduled for child account');
7476
} else {
7577
// CDK tests and tests with USE_PARENT_ACCOUNT only use the parent account role.
7678
// Refresh the parent account credentials to prevent expiration during long-running tests.
77-
setInterval(() => {
79+
credentialRefreshTimer = setInterval(() => {
7880
void tryRefreshCredentials(process.env.TEST_ACCOUNT_ROLE);
7981
}, 10 * 60 * 1000);
82+
credentialRefreshTimer.unref();
8083

8184
console.log('Test profile credentials refresh was scheduled for parent account');
8285
}
8386

8487
isRotationBackgroundTaskAlreadyScheduled = true;
8588
};
89+
90+
/**
91+
* Stops the credential refresh timer. Call this in afterAll to prevent
92+
* "Cannot log after tests are done" warnings from Jest.
93+
*/
94+
export const stopCredentialRefresh = () => {
95+
if (credentialRefreshTimer) {
96+
clearInterval(credentialRefreshTimer);
97+
credentialRefreshTimer = undefined;
98+
}
99+
isRotationBackgroundTaskAlreadyScheduled = false;
100+
};

shared-scripts.sh

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,43 @@ function _runE2ETestsLinux {
364364

365365
function _runCDKTestsLinux {
366366
echo "RUN CDK Tests Linux"
367-
retry runCDKTest
367+
MAX_ATTEMPTS=2
368+
SLEEP_DURATION=5
369+
FIRST_RUN=true
370+
RUN_INDEX=0
371+
FAILED_TEST_REGEX_FILE="./amplify-e2e-reports/amplify-e2e-failed-test.txt"
372+
if [ -f $FAILED_TEST_REGEX_FILE ]; then
373+
rm -f $FAILED_TEST_REGEX_FILE
374+
fi
375+
until [ $RUN_INDEX -ge $MAX_ATTEMPTS ]
376+
do
377+
echo "Attempting runCDKTest with max retries $MAX_ATTEMPTS"
378+
if [ "$FIRST_RUN" != "true" ]; then
379+
# Unset expired assumed-role credentials so _loadTestAccountCredentials
380+
# can re-assume from the CodeBuild instance profile (not nested AssumeRole)
381+
unset AWS_ACCESS_KEY_ID
382+
unset AWS_SECRET_ACCESS_KEY
383+
unset AWS_SESSION_TOKEN
384+
unset AWS_DEFAULT_REGION
385+
_loadTestAccountCredentials
386+
fi
387+
setAwsAccountCredentials
388+
RUN_INDEX="$RUN_INDEX" runCDKTest && break
389+
RUN_INDEX=$[$RUN_INDEX+1]
390+
FIRST_RUN=false
391+
echo "Attempt $RUN_INDEX completed."
392+
sleep $SLEEP_DURATION
393+
done
394+
if [ $RUN_INDEX -ge $MAX_ATTEMPTS ]; then
395+
echo "failed: runCDKTest" >&2
396+
exit 1
397+
fi
398+
399+
resetAwsAccountCredentials
400+
TEST_SUITE=${TEST_SUITE:-"TestSuiteNotSet"}
401+
aws cloudwatch put-metric-data --metric-name FlakyE2ETests --namespace amplify-category-api-e2e-tests --unit Count --value $RUN_INDEX --dimensions testFile=$TEST_SUITE --profile amplify-integ-test-user || true
402+
echo "Attempt $RUN_INDEX succeeded."
403+
exit 0 # don't fail the step if putting the metric fails
368404
}
369405

370406
function _runGqlE2ETests {

0 commit comments

Comments
 (0)