Skip to content

Commit b8df7e6

Browse files
authored
fix: opt in region test to correct account (#936)
* fix: opt in region test to correct account Signed-off-by: Kevin Shan <[email protected]> * chore: reset credentials Signed-off-by: Kevin Shan <[email protected]> * chore: revert changes Signed-off-by: Kevin Shan <[email protected]> * fix: generate correct ios test build spec Signed-off-by: Kevin Shan <[email protected]> * chore: unassume account credentials after ios test Signed-off-by: Kevin Shan <[email protected]> * chore: consolite region constants Signed-off-by: Kevin Shan <[email protected]> * chore: update error messages Signed-off-by: Kevin Shan <[email protected]> --------- Signed-off-by: Kevin Shan <[email protected]>
1 parent f1c23f4 commit b8df7e6

File tree

7 files changed

+74
-53
lines changed

7 files changed

+74
-53
lines changed

.codebuild/ios_canary_workflow.yml

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ batch:
2727
env:
2828
compute-type: BUILD_GENERAL1_LARGE
2929
variables:
30-
TEST_SUITE: src/__tests__/build-app-ios.test.ts
30+
TEST_SUITE: src/__tests__/build-app-swift.test.ts
3131
CLI_REGION: ap-east-1
3232
CANARY_METRIC_NAME: IosAppBuildCodegenSuccessRate
3333
depend-on:
@@ -37,7 +37,7 @@ batch:
3737
env:
3838
compute-type: BUILD_GENERAL1_LARGE
3939
variables:
40-
TEST_SUITE: src/__tests__/build-app-ios.test.ts
40+
TEST_SUITE: src/__tests__/build-app-swift.test.ts
4141
CLI_REGION: ap-northeast-1
4242
CANARY_METRIC_NAME: IosAppBuildCodegenSuccessRate
4343
depend-on:
@@ -47,7 +47,7 @@ batch:
4747
env:
4848
compute-type: BUILD_GENERAL1_LARGE
4949
variables:
50-
TEST_SUITE: src/__tests__/build-app-ios.test.ts
50+
TEST_SUITE: src/__tests__/build-app-swift.test.ts
5151
CLI_REGION: ap-northeast-2
5252
CANARY_METRIC_NAME: IosAppBuildCodegenSuccessRate
5353
depend-on:
@@ -57,7 +57,7 @@ batch:
5757
env:
5858
compute-type: BUILD_GENERAL1_LARGE
5959
variables:
60-
TEST_SUITE: src/__tests__/build-app-ios.test.ts
60+
TEST_SUITE: src/__tests__/build-app-swift.test.ts
6161
CLI_REGION: ap-northeast-3
6262
CANARY_METRIC_NAME: IosAppBuildCodegenSuccessRate
6363
depend-on:
@@ -67,7 +67,7 @@ batch:
6767
env:
6868
compute-type: BUILD_GENERAL1_LARGE
6969
variables:
70-
TEST_SUITE: src/__tests__/build-app-ios.test.ts
70+
TEST_SUITE: src/__tests__/build-app-swift.test.ts
7171
CLI_REGION: ap-south-1
7272
CANARY_METRIC_NAME: IosAppBuildCodegenSuccessRate
7373
depend-on:
@@ -77,7 +77,7 @@ batch:
7777
env:
7878
compute-type: BUILD_GENERAL1_LARGE
7979
variables:
80-
TEST_SUITE: src/__tests__/build-app-ios.test.ts
80+
TEST_SUITE: src/__tests__/build-app-swift.test.ts
8181
CLI_REGION: ap-southeast-1
8282
CANARY_METRIC_NAME: IosAppBuildCodegenSuccessRate
8383
depend-on:
@@ -87,7 +87,7 @@ batch:
8787
env:
8888
compute-type: BUILD_GENERAL1_LARGE
8989
variables:
90-
TEST_SUITE: src/__tests__/build-app-ios.test.ts
90+
TEST_SUITE: src/__tests__/build-app-swift.test.ts
9191
CLI_REGION: ap-southeast-2
9292
CANARY_METRIC_NAME: IosAppBuildCodegenSuccessRate
9393
depend-on:
@@ -97,7 +97,7 @@ batch:
9797
env:
9898
compute-type: BUILD_GENERAL1_LARGE
9999
variables:
100-
TEST_SUITE: src/__tests__/build-app-ios.test.ts
100+
TEST_SUITE: src/__tests__/build-app-swift.test.ts
101101
CLI_REGION: ca-central-1
102102
CANARY_METRIC_NAME: IosAppBuildCodegenSuccessRate
103103
depend-on:
@@ -107,7 +107,7 @@ batch:
107107
env:
108108
compute-type: BUILD_GENERAL1_LARGE
109109
variables:
110-
TEST_SUITE: src/__tests__/build-app-ios.test.ts
110+
TEST_SUITE: src/__tests__/build-app-swift.test.ts
111111
CLI_REGION: eu-central-1
112112
CANARY_METRIC_NAME: IosAppBuildCodegenSuccessRate
113113
depend-on:
@@ -117,7 +117,7 @@ batch:
117117
env:
118118
compute-type: BUILD_GENERAL1_LARGE
119119
variables:
120-
TEST_SUITE: src/__tests__/build-app-ios.test.ts
120+
TEST_SUITE: src/__tests__/build-app-swift.test.ts
121121
CLI_REGION: eu-north-1
122122
CANARY_METRIC_NAME: IosAppBuildCodegenSuccessRate
123123
depend-on:
@@ -127,7 +127,7 @@ batch:
127127
env:
128128
compute-type: BUILD_GENERAL1_LARGE
129129
variables:
130-
TEST_SUITE: src/__tests__/build-app-ios.test.ts
130+
TEST_SUITE: src/__tests__/build-app-swift.test.ts
131131
CLI_REGION: eu-south-1
132132
CANARY_METRIC_NAME: IosAppBuildCodegenSuccessRate
133133
depend-on:
@@ -137,7 +137,7 @@ batch:
137137
env:
138138
compute-type: BUILD_GENERAL1_LARGE
139139
variables:
140-
TEST_SUITE: src/__tests__/build-app-ios.test.ts
140+
TEST_SUITE: src/__tests__/build-app-swift.test.ts
141141
CLI_REGION: eu-west-1
142142
CANARY_METRIC_NAME: IosAppBuildCodegenSuccessRate
143143
depend-on:
@@ -147,7 +147,7 @@ batch:
147147
env:
148148
compute-type: BUILD_GENERAL1_LARGE
149149
variables:
150-
TEST_SUITE: src/__tests__/build-app-ios.test.ts
150+
TEST_SUITE: src/__tests__/build-app-swift.test.ts
151151
CLI_REGION: eu-west-2
152152
CANARY_METRIC_NAME: IosAppBuildCodegenSuccessRate
153153
depend-on:
@@ -157,7 +157,7 @@ batch:
157157
env:
158158
compute-type: BUILD_GENERAL1_LARGE
159159
variables:
160-
TEST_SUITE: src/__tests__/build-app-ios.test.ts
160+
TEST_SUITE: src/__tests__/build-app-swift.test.ts
161161
CLI_REGION: eu-west-3
162162
CANARY_METRIC_NAME: IosAppBuildCodegenSuccessRate
163163
depend-on:
@@ -167,7 +167,7 @@ batch:
167167
env:
168168
compute-type: BUILD_GENERAL1_LARGE
169169
variables:
170-
TEST_SUITE: src/__tests__/build-app-ios.test.ts
170+
TEST_SUITE: src/__tests__/build-app-swift.test.ts
171171
CLI_REGION: me-south-1
172172
CANARY_METRIC_NAME: IosAppBuildCodegenSuccessRate
173173
depend-on:
@@ -177,7 +177,7 @@ batch:
177177
env:
178178
compute-type: BUILD_GENERAL1_LARGE
179179
variables:
180-
TEST_SUITE: src/__tests__/build-app-ios.test.ts
180+
TEST_SUITE: src/__tests__/build-app-swift.test.ts
181181
CLI_REGION: sa-east-1
182182
CANARY_METRIC_NAME: IosAppBuildCodegenSuccessRate
183183
depend-on:
@@ -187,7 +187,7 @@ batch:
187187
env:
188188
compute-type: BUILD_GENERAL1_LARGE
189189
variables:
190-
TEST_SUITE: src/__tests__/build-app-ios.test.ts
190+
TEST_SUITE: src/__tests__/build-app-swift.test.ts
191191
CLI_REGION: us-east-1
192192
CANARY_METRIC_NAME: IosAppBuildCodegenSuccessRate
193193
depend-on:
@@ -197,7 +197,7 @@ batch:
197197
env:
198198
compute-type: BUILD_GENERAL1_LARGE
199199
variables:
200-
TEST_SUITE: src/__tests__/build-app-ios.test.ts
200+
TEST_SUITE: src/__tests__/build-app-swift.test.ts
201201
CLI_REGION: us-east-2
202202
CANARY_METRIC_NAME: IosAppBuildCodegenSuccessRate
203203
depend-on:
@@ -207,7 +207,7 @@ batch:
207207
env:
208208
compute-type: BUILD_GENERAL1_LARGE
209209
variables:
210-
TEST_SUITE: src/__tests__/build-app-ios.test.ts
210+
TEST_SUITE: src/__tests__/build-app-swift.test.ts
211211
CLI_REGION: us-west-1
212212
CANARY_METRIC_NAME: IosAppBuildCodegenSuccessRate
213213
depend-on:
@@ -217,7 +217,7 @@ batch:
217217
env:
218218
compute-type: BUILD_GENERAL1_LARGE
219219
variables:
220-
TEST_SUITE: src/__tests__/build-app-ios.test.ts
220+
TEST_SUITE: src/__tests__/build-app-swift.test.ts
221221
CLI_REGION: us-west-2
222222
CANARY_METRIC_NAME: IosAppBuildCodegenSuccessRate
223223
depend-on:

.codebuild/run_ios_modelgen_e2e_test.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ phases:
2323
- cd $CODEBUILD_SRC_DIR && ./.codebuild/scripts/run-ios-modelgen-e2e-test.sh
2424
post_build:
2525
commands:
26+
- source ./shared-scripts.sh && _unassumeTestAccountCredentials
2627
- aws sts get-caller-identity
2728
- source ./shared-scripts.sh && _scanArtifacts
2829

scripts/e2e-test-regions.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
[
2+
{ "name": "ap-east-1", "optIn": true },
3+
{ "name": "ap-northeast-1", "optIn": false },
4+
{ "name": "ap-northeast-2", "optIn": false },
5+
{ "name": "ap-northeast-3", "optIn": false },
6+
{ "name": "ap-south-1", "optIn": false },
7+
{ "name": "ap-southeast-1", "optIn": false },
8+
{ "name": "ap-southeast-2", "optIn": false },
9+
{ "name": "ca-central-1", "optIn": false },
10+
{ "name": "eu-central-1", "optIn": false },
11+
{ "name": "eu-north-1", "optIn": false },
12+
{ "name": "eu-south-1", "optIn": true },
13+
{ "name": "eu-west-1", "optIn": false },
14+
{ "name": "eu-west-2", "optIn": false },
15+
{ "name": "eu-west-3", "optIn": false },
16+
{ "name": "me-south-1", "optIn": true },
17+
{ "name": "sa-east-1", "optIn": false },
18+
{ "name": "us-east-1", "optIn": false },
19+
{ "name": "us-east-2", "optIn": false },
20+
{ "name": "us-west-1", "optIn": false },
21+
{ "name": "us-west-2", "optIn": false }
22+
]

scripts/split-canary-tests.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,16 @@ const CODEBUILD_CONFIG_BASE_PATH: string = join(CODEBUILD_CONFIG_PATH, 'canary_w
1616
* - amplify-codegen/scripts/split-e2e-tests.ts
1717
* - amplify-codegen/packages/amplify-codegen-e2e-tests/src/cleanup-e2e-resources.ts
1818
*/
19-
const SUPPORTED_REGIONS_PATH: string = join(REPO_ROOT, 'scripts', 'support-test-regions.json');
20-
const AWS_REGIONS_TO_RUN_TESTS: string[] = JSON.parse(fs.readFileSync(SUPPORTED_REGIONS_PATH, 'utf-8'));
19+
const SUPPORTED_REGIONS_PATH: string = join(REPO_ROOT, 'scripts', 'e2e-test-regions.json');
20+
const AWS_REGIONS_TO_RUN_TESTS: string[] = JSON.parse(fs.readFileSync(SUPPORTED_REGIONS_PATH, 'utf-8')).map(region => region.name);
2121

2222
enum ComputeType {
2323
MEDIUM = 'BUILD_GENERAL1_MEDIUM',
2424
LARGE = 'BUILD_GENERAL1_LARGE',
2525
}
2626
type PlatformConfig = {
2727
platform: string;
28+
testName: string;
2829
metric: string;
2930
outputPath: string;
3031
}
@@ -66,16 +67,19 @@ const getPlatformsConfig = (): PlatformConfig[] => {
6667
return [
6768
{
6869
platform: 'ts',
70+
testName: 'ts',
6971
metric: 'TsAppBuildCodegenSuccessRate',
7072
outputPath: getOutputPath('ts_canary_workflow.yml')
7173
},
7274
{
7375
platform: 'ios',
76+
testName: 'swift',
7477
metric: 'IosAppBuildCodegenSuccessRate',
7578
outputPath: getOutputPath('ios_canary_workflow.yml')
7679
},
7780
{
7881
platform: 'android',
82+
testName: 'android',
7983
metric: 'AndroidAppBuildCodegenSuccessRate',
8084
outputPath: getOutputPath('android_canary_workflow.yml')
8185
},
@@ -129,7 +133,7 @@ const getBuildspec = (jobConfig: JobConfig): string => {
129133
};
130134

131135
const getTestSuite = (jobConfig: JobConfig): string => {
132-
return `src/__tests__/build-app-${jobConfig.platformConfig.platform}.test.ts`;
136+
return `src/__tests__/build-app-${jobConfig.platformConfig.testName}.test.ts`;
133137
};
134138

135139
const main = (): void => {

scripts/split-e2e-tests.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ const DEBUG_FLAG = '--debug';
2525
*
2626
* NOTE: The list of supported regions must be kept in sync amongst all of:
2727
* - the internal pipeline that publishes new lambda layer versions
28-
* - amplify-codegen/scripts/split-e2e-tests.ts
28+
* - amplify-codegen/scripts/split-canary-tests.ts
2929
* - amplify-codegen/packages/amplify-codegen-e2e-tests/src/cleanup-e2e-resources.ts
3030
*/
31-
const SUPPORTED_REGIONS_PATH: string = join(REPO_ROOT, 'scripts', 'support-test-regions.json');
32-
const AWS_REGIONS_TO_RUN_TESTS: string[] = JSON.parse(fs.readFileSync(SUPPORTED_REGIONS_PATH, 'utf-8'));
31+
const SUPPORTED_REGIONS_PATH: string = join(REPO_ROOT, 'scripts', 'e2e-test-regions.json');
32+
const AWS_REGIONS_TO_RUN_TESTS: string[] = JSON.parse(fs.readFileSync(SUPPORTED_REGIONS_PATH, 'utf-8')).map(region => region.name);
3333

3434
export function loadConfigBase() {
3535
return yaml.load(fs.readFileSync(CODEBUILD_CONFIG_BASE_PATH, 'utf8'));

scripts/support-test-regions.json

Lines changed: 0 additions & 22 deletions
This file was deleted.

shared-scripts.sh

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -339,19 +339,35 @@ function _unassumeTestAccountCredentials {
339339
function useChildAccountCredentials {
340340
if [ -z "$USE_PARENT_ACCOUNT" ]; then
341341
export AWS_PAGER=""
342+
export AWS_MAX_ATTEMPTS=5
343+
export AWS_STS_REGIONAL_ENDPOINTS=regional
342344
parent_acct=$(aws sts get-caller-identity | jq -cr '.Account')
343345
child_accts=$(aws organizations list-accounts | jq -c "[.Accounts[].Id | select(. != \"$parent_acct\")]")
344346
org_size=$(echo $child_accts | jq 'length')
345-
pick_acct=$(echo $child_accts | jq -cr ".[$RANDOM % $org_size]")
347+
opt_in_regions=$(jq -r '.[] | select(.optIn == true) | .name' $CODEBUILD_SRC_DIR/scripts/e2e-test-regions.json)
348+
if echo "$opt_in_regions" | grep -qw "$CLI_REGION"; then
349+
child_accts=$(echo $child_accts | jq -cr '.[]')
350+
for child_acct in $child_accts; do
351+
# Get enabled opt-in regions for the child account
352+
enabled_regions=$(aws account list-regions --account-id $child_acct --region-opt-status-contains ENABLED)
353+
# Check if given opt-in region is enabled for the child account
354+
if echo "$enabled_regions" | jq -e ".Regions[].RegionName == \"$CLI_REGION\""; then
355+
pick_acct=$child_acct
356+
break
357+
fi
358+
done
359+
else
360+
pick_acct=$(echo $child_accts | jq -cr ".[$RANDOM % $org_size]")
361+
fi
346362
session_id=$((1 + $RANDOM % 10000))
347363
if [[ -z "$pick_acct" || -z "$session_id" ]]; then
348-
echo "Unable to find a child account. Falling back to parent AWS account"
349-
return
364+
echo "Unable to find a child account. Fatal error and test run aborted"
365+
exit 1
350366
fi
351367
creds=$(aws sts assume-role --role-arn arn:aws:iam::${pick_acct}:role/OrganizationAccountAccessRole --role-session-name testSession${session_id} --duration-seconds 3600)
352368
if [ -z $(echo $creds | jq -c -r '.AssumedRoleUser.Arn') ]; then
353-
echo "Unable to assume child account role. Falling back to parent AWS account"
354-
return
369+
echo "Unable to assume child account role. Fatal error and test run aborted"
370+
exit 1
355371
fi
356372
export ORGANIZATION_SIZE=$org_size
357373
export CREDS=$creds

0 commit comments

Comments
 (0)