Skip to content

Commit

Permalink
build: assign region at runtime in e2e tests (#13513)
Browse files Browse the repository at this point in the history
* build: assign region at runtime in e2e tests

* forgot that

* use different salt

* try this

* try that

* windows $%@#T%@#%
  • Loading branch information
sobolk authored Jan 4, 2024
1 parent 2480681 commit 3c956a0
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 238 deletions.
1 change: 0 additions & 1 deletion codebuild_specs/e2e_workflow_base.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ env:
AWS_DEFAULT_REGION: us-east-1
AWS_REGION: us-east-1
CDK_DEFAULT_REGION: us-east-1
CLI_REGION: us-east-1
AMPLIFY_DIR: '$CODEBUILD_SRC_DIR/out'
AMPLIFY_PATH: '$CODEBUILD_SRC_DIR/out/amplify-pkg-linux-x64'

Expand Down
230 changes: 0 additions & 230 deletions codebuild_specs/e2e_workflow_generated.yml

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion codebuild_specs/run_e2e_tests_windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ version: 0.2
env:
shell: powershell.exe
variables:
CLI_REGION: us-east-1
TEST_SUITE: src/__tests__/auth_2a.test.ts|src/__tests__/auth_2b.test.ts|src/__tests__/auth_2d.test.ts|src/__tests__/auth_2f.test.ts
CI: true
CIRCLECI: true
Expand Down
3 changes: 3 additions & 0 deletions codebuild_specs/scripts-windows/run-e2e-windows.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ export NODE_OPTIONS=--max-old-space-size=5120
source .circleci/local_publish_helpers_codebuild.sh
source ./codebuild_specs/scripts-windows/shared-scripts-windows.sh

export CLI_REGION=$(yarn ts-node ./scripts/select-region-for-e2e-test.ts)
echo "Test will run in $CLI_REGION"

# source $BASH_ENV

amplify version
Expand Down
36 changes: 36 additions & 0 deletions scripts/select-region-for-e2e-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { AWS_REGIONS_TO_RUN_TESTS } from './cci-utils';
import * as fs from 'fs-extra';
import path from 'path';

/**
* This script prints region assignment for an e2e test job.
* The algorithm takes input via environment variable - CODEBUILD_BATCH_BUILD_IDENTIFIER
* and computes deterministic region assignment by looking at position
* of build job in 'wait_for_ids.json' file.
* In order to reshuffle regions 'offset' constant below should be modified.
*
* If region is already assigned, i.e. CLI_REGION environment variable is set this script is pass-through.
*/

// if region is specified by job honor it.
let selectedRegion = process.env.CLI_REGION;
if (!selectedRegion) {
const jobId = process.env.CODEBUILD_BATCH_BUILD_IDENTIFIER;
if (!jobId) {
throw Error('CODEBUILD_BATCH_BUILD_IDENTIFIER environment variable must be set');
}
// Offset should be changed if we want re-shuffle regions.
const offset = 0;
const waitForIdsFilePath = path.join('.', 'codebuild_specs', 'wait_for_ids.json');
const jobIds = JSON.parse(fs.readFileSync(waitForIdsFilePath, 'utf-8')) as Array<string>;
let jobPosition = jobIds.indexOf(jobId);
if (jobPosition < 0) {
// this should never happen if PR checks pass, but just in case fall back to first region.
jobPosition = 0;
}
const regionIndex = (jobPosition + offset) % AWS_REGIONS_TO_RUN_TESTS.length;

selectedRegion = AWS_REGIONS_TO_RUN_TESTS[regionIndex];
}

console.log(selectedRegion);
17 changes: 11 additions & 6 deletions scripts/split-e2e-tests-codebuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import * as glob from 'glob';
import * as fs from 'fs-extra';
import { join } from 'path';
import * as yaml from 'js-yaml';
import { AWS_REGIONS_TO_RUN_TESTS as regions } from './cci-utils';
import { REPO_ROOT } from './cci-utils';
import { FORCE_REGION_MAP, getOldJobNameWithoutSuffixes, loadTestTimings, USE_PARENT_ACCOUNT } from './cci-utils';
import { migrationFromV10Tests, migrationFromV12Tests, migrationFromV8Tests } from './split-e2e-test-filters';
Expand Down Expand Up @@ -146,17 +145,15 @@ type ConfigBase = {
const MAX_WORKERS = 3;
type OS_TYPE = 'w' | 'l';
type CandidateJob = {
region: string;
region?: string;
os: OS_TYPE;
executor: string;
tests: string[];
useParentAccount: boolean;
disableCoverage: boolean;
};
const createRandomJob = (os: OS_TYPE): CandidateJob => {
const region = regions[Math.floor(Math.random() * regions.length)];
return {
region,
os,
executor: os === 'l' ? 'l_large' : 'w_medium',
tests: [],
Expand Down Expand Up @@ -262,7 +259,11 @@ const splitTestsV3 = (
formattedJob.env.variables['compute-type'] = 'BUILD_GENERAL1_SMALL';
}
formattedJob.env.variables.TEST_SUITE = job.tests.join('|');
formattedJob.env.variables.CLI_REGION = job.region;
if (job.region) {
// Jobs with forced region are assigned one explicitly.
// Otherwise, region is assigned at runtime by select-region-for-e2e-test.ts script.
formattedJob.env.variables.CLI_REGION = job.region;
}
if (job.useParentAccount) {
formattedJob.env.variables.USE_PARENT_ACCOUNT = 1;
}
Expand All @@ -283,7 +284,11 @@ const splitTestsV3 = (
};
formattedJob.env.variables = {};
formattedJob.env.variables.TEST_SUITE = job.tests.join('|');
formattedJob.env.variables.CLI_REGION = job.region;
if (job.region) {
// Jobs with forced region are assigned one explicitly.
// Otherwise, region is assigned at runtime by select-region-for-e2e-test.ts script.
formattedJob.env.variables.CLI_REGION = job.region;
}
if (job.useParentAccount) {
formattedJob.env.variables.USE_PARENT_ACCOUNT = 1;
}
Expand Down
3 changes: 3 additions & 0 deletions shared-scripts.sh
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,9 @@ function _runE2ETestsLinux {
echo RUN E2E Tests Linux
_loadE2ECache
_install_packaged_cli_linux
# select region
export CLI_REGION=$(yarn ts-node ./scripts/select-region-for-e2e-test.ts)
echo "Test will run in $CLI_REGION"
# verify installation
which amplify
amplify version
Expand Down

0 comments on commit 3c956a0

Please sign in to comment.