diff --git a/.circleci/local_publish_helpers.sh b/.circleci/local_publish_helpers.sh index 51719408dae..b9f79a9eb5b 100644 --- a/.circleci/local_publish_helpers.sh +++ b/.circleci/local_publish_helpers.sh @@ -255,7 +255,7 @@ function setAwsAccountCredentials { export AWS_SECRET_ACCESS_KEY_ORIG=$AWS_SECRET_ACCESS_KEY export AWS_SESSION_TOKEN_ORIG=$AWS_SESSION_TOKEN # introduce a delay of up to 1 minute to allow for more even spread aws list-accounts calls due to throttling - sleep $[ ( $RANDOM % 60 ) + 1 ]s + # sleep $ ( $RANDOM % 60 ) + 1 ]s if [[ "$OSTYPE" == "msys" ]]; then # windows provided by circleci has this OSTYPE useChildAccountCredentials diff --git a/codebuild_specs/build_image.yml b/codebuild_specs/build_image.yml new file mode 100644 index 00000000000..5363c47c0d3 --- /dev/null +++ b/codebuild_specs/build_image.yml @@ -0,0 +1,26 @@ +version: 0.2 +phases: + pre_build: + commands: + - echo Logging in to Amazon ECR... + - aws ecr get-login-password --region $AWS_DEFAULT_REGION | docker login --username AWS --password-stdin $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com + build: + commands: + - export BRANCH_TAG=${CODEBUILD_WEBHOOK_TRIGGER#branch/codebuild-image/*} + - export IMAGE_TAG=latest + - echo $IMAGE_TAG + - echo $PKG_BINARY_CACHE_BUCKET_NAME + - mkdir pkg-cache + - aws s3 sync s3://$PKG_BINARY_CACHE_BUCKET_NAME pkg-cache + - /bin/bash ./rename_to_fetched.sh + - ls pkg-cache/* + - echo Build started on `date` + - echo Building the Docker image... + - echo $CODEBUILD_SRC_DIR + - docker build --tag $IMAGE_REPO_NAME:$IMAGE_TAG . + - docker tag $IMAGE_REPO_NAME:$IMAGE_TAG $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/$IMAGE_REPO_NAME:$IMAGE_TAG + post_build: + commands: + - echo Build completed on `date` + - echo Pushing the Docker image... + - docker push $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/$IMAGE_REPO_NAME:$IMAGE_TAG diff --git a/codebuild_specs/cleanup_resources.yml b/codebuild_specs/cleanup_resources.yml index c9249fc7446..b5e01cb0e05 100644 --- a/codebuild_specs/cleanup_resources.yml +++ b/codebuild_specs/cleanup_resources.yml @@ -1,7 +1,9 @@ version: 0.2 env: shell: bash + variables: + NODE_OPTIONS: --max-old-space-size=5120 phases: build: commands: - - echo cleanup running + - source ./shared-scripts.sh && _cleanupResources diff --git a/codebuild_specs/e2e_workflow.yml b/codebuild_specs/e2e_workflow.yml index 8ca58071fb6..21589333552 100644 --- a/codebuild_specs/e2e_workflow.yml +++ b/codebuild_specs/e2e_workflow.yml @@ -18,14 +18,12 @@ batch: buildspec: codebuild_specs/build_linux.yml env: compute-type: BUILD_GENERAL1_LARGE - - identifier: build_windows - buildspec: codebuild_specs/build_windows.yml - env: - type: WINDOWS_SERVER_2019_CONTAINER - compute-type: BUILD_GENERAL1_LARGE - image: aws/codebuild/windows-base:2019-2.0 - - identifier: test - buildspec: codebuild_specs/test.yml + - identifier: cleanup_resources + buildspec: codebuild_specs/cleanup_resources.yml + depend-on: + - build_linux + - identifier: integration_test + buildspec: codebuild_specs/integration_test.yml env: compute-type: BUILD_GENERAL1_LARGE depend-on: @@ -36,8 +34,22 @@ batch: compute-type: BUILD_GENERAL1_LARGE depend-on: - build_linux + - identifier: publish_to_local_registry + buildspec: codebuild_specs/publish_to_local_registry.yml + env: + compute-type: BUILD_GENERAL1_LARGE + depend-on: + - build_linux + - identifier: test + buildspec: codebuild_specs/test.yml + env: + compute-type: BUILD_GENERAL1_LARGE + depend-on: + - build_linux - identifier: validate_cdk_version buildspec: codebuild_specs/validate_cdk_version.yml + env: + compute-type: BUILD_GENERAL1_LARGE depend-on: - build_linux - identifier: verify_api_extract @@ -50,12 +62,20 @@ batch: buildspec: codebuild_specs/verify_yarn_lock.yml depend-on: - build_linux - - identifier: publish_to_local_registry - buildspec: codebuild_specs/publish_to_local_registry.yml + + - identifier: build_windows + buildspec: codebuild_specs/build_windows.yml + env: + type: WINDOWS_SERVER_2019_CONTAINER + compute-type: BUILD_GENERAL1_LARGE + image: aws/codebuild/windows-base:2019-2.0 + - identifier: build_pkg_binaries_macos + buildspec: codebuild_specs/build_pkg_binaries_macos.yml env: compute-type: BUILD_GENERAL1_LARGE depend-on: - - build_linux + - publish_to_local_registry + - identifier: build_pkg_binaries_arm buildspec: codebuild_specs/build_pkg_binaries_arm.yml env: @@ -68,18 +88,15 @@ batch: compute-type: BUILD_GENERAL1_LARGE depend-on: - publish_to_local_registry - - identifier: build_pkg_binaries_macos - buildspec: codebuild_specs/build_pkg_binaries_macos.yml - env: - compute-type: BUILD_GENERAL1_LARGE - depend-on: - - publish_to_local_registry - identifier: build_pkg_binaries_win buildspec: codebuild_specs/build_pkg_binaries_win.yml env: + type: WINDOWS_SERVER_2019_CONTAINER compute-type: BUILD_GENERAL1_LARGE + image: aws/codebuild/windows-base:2019-2.0 depend-on: - publish_to_local_registry + - identifier: upload_pkg_binaries buildspec: codebuild_specs/upload_pkg_binaries.yml env: @@ -89,22 +106,22 @@ batch: - build_pkg_binaries_linux - build_pkg_binaries_macos - build_pkg_binaries_win + - identifier: verify_versions_match buildspec: codebuild_specs/verify_versions_match.yml depend-on: - publish_to_local_registry - - identifier: run_e2e_tests_linux - buildspec: codebuild_specs/run_e2e_tests_linux.yml - env: - compute-type: BUILD_GENERAL1_LARGE - debug-session: true - depend-on: - - upload_pkg_binaries - - identifier: run_e2e_tests_windows - buildspec: codebuild_specs/run_e2e_tests_windows.yml - env: - type: WINDOWS_SERVER_2019_CONTAINER - compute-type: BUILD_GENERAL1_LARGE - image: aws/codebuild/windows-base:2019-2.0 - depend-on: - - upload_pkg_binaries + # - identifier: run_e2e_tests_linux + # buildspec: codebuild_specs/run_e2e_tests_linux.yml + # env: + # compute-type: BUILD_GENERAL1_LARGE + # depend-on: + # - upload_pkg_binaries + # - identifier: run_e2e_tests_windows + # buildspec: codebuild_specs/run_e2e_tests_windows.yml + # env: + # type: WINDOWS_SERVER_2019_CONTAINER + # compute-type: BUILD_GENERAL1_LARGE + # image: aws/codebuild/windows-base:2019-2.0 + # depend-on: + # - upload_pkg_binaries diff --git a/codebuild_specs/integration_test.yml b/codebuild_specs/integration_test.yml index d2e6df4b7a0..e8ca39bb4ed 100644 --- a/codebuild_specs/integration_test.yml +++ b/codebuild_specs/integration_test.yml @@ -4,4 +4,4 @@ env: phases: build: commands: - - echo integration testing + - source ./circleci && ./shared-scripts.sh && _integrationTests diff --git a/codebuild_specs/lint.yml b/codebuild_specs/lint.yml index 7363c6c10d0..9cfa007b5fb 100644 --- a/codebuild_specs/lint.yml +++ b/codebuild_specs/lint.yml @@ -1,6 +1,8 @@ version: 0.2 env: shell: bash + variables: + NODE_OPTIONS: --max-old-space-size=5121 phases: build: commands: diff --git a/codebuild_specs/mock_e2e_tests.yml b/codebuild_specs/mock_e2e_tests.yml index 5b2d812cb89..fd1859594e0 100644 --- a/codebuild_specs/mock_e2e_tests.yml +++ b/codebuild_specs/mock_e2e_tests.yml @@ -3,8 +3,9 @@ env: shell: bash variables: JEST_JUNIT_OUTPUT: 'reports/junit/js-test-results.xml' - NODE_OPTIONS: --max-old-space-size=14000 + NODE_OPTIONS: --max-old-space-size=5120 phases: build: commands: + - codebuild-breakpoint - source ./shared-scripts.sh && _mockE2ETests diff --git a/codebuild_specs/pr_workflow.yml b/codebuild_specs/pr_workflow.yml index bffa844977e..80a653f476e 100644 --- a/codebuild_specs/pr_workflow.yml +++ b/codebuild_specs/pr_workflow.yml @@ -18,64 +18,74 @@ batch: buildspec: codebuild_specs/build_linux.yml env: compute-type: BUILD_GENERAL1_LARGE + - identifier: lint + buildspec: codebuild_specs/lint.yml + env: + compute-type: BUILD_GENERAL1_LARGE + depend-on: + - build_linux + - identifier: mock_e2e_tests + buildspec: codebuild_specs/mock_e2e_tests.yml + env: + compute-type: BUILD_GENERAL1_LARGE + debug-session: true + depend-on: + - build_linux - identifier: test buildspec: codebuild_specs/test.yml env: compute-type: BUILD_GENERAL1_LARGE - debug-session: true depend-on: - build_linux - # - identifier: mock_e2e_tests - # buildspec: codebuild_specs/mock_e2e_tests.yml - # env: - # compute-type: BUILD_GENERAL1_LARGE - # depend-on: - # - build_linux - # - identifier: validate_cdk_version - # buildspec: codebuild_specs/validate_cdk_version.yml - # depend-on: - # - build_linux - # - identifier: verify_api_extract - # buildspec: codebuild_specs/verify_api_extract.yml - # env: - # compute-type: BUILD_GENERAL1_LARGE - # depend-on: - # - build_linux - # - identifier: verify_yarn_lock - # buildspec: codebuild_specs/verify_yarn_lock.yml - # depend-on: - # - build_linux - # - identifier: publish_to_local_registry - # buildspec: codebuild_specs/publish_to_local_registry.yml - # env: - # compute-type: BUILD_GENERAL1_LARGE - # depend-on: - # - build_linux - # - identifier: build_pkg_binaries_arm - # buildspec: codebuild_specs/build_pkg_binaries_arm.yml - # env: - # compute-type: BUILD_GENERAL1_LARGE - # depend-on: - # - publish_to_local_registry - # - identifier: build_pkg_binaries_linux - # buildspec: codebuild_specs/build_pkg_binaries_linux.yml - # env: - # compute-type: BUILD_GENERAL1_LARGE - # depend-on: - # - publish_to_local_registry - # - identifier: build_pkg_binaries_macos - # buildspec: codebuild_specs/build_pkg_binaries_macos.yml - # env: - # compute-type: BUILD_GENERAL1_LARGE - # depend-on: - # - publish_to_local_registry - # - identifier: build_pkg_binaries_win - # buildspec: codebuild_specs/build_pkg_binaries_win.yml - # env: - # compute-type: BUILD_GENERAL1_LARGE - # depend-on: - # - publish_to_local_registry - # - identifier: verify_versions_match - # buildspec: codebuild_specs/verify_versions_match.yml - # depend-on: - # - publish_to_local_registry + - identifier: publish_to_local_registry + buildspec: codebuild_specs/publish_to_local_registry.yml + env: + compute-type: BUILD_GENERAL1_LARGE + depend-on: + - build_linux + - identifier: validate_cdk_version + buildspec: codebuild_specs/validate_cdk_version.yml + env: + compute-type: BUILD_GENERAL1_LARGE + depend-on: + - build_linux + - identifier: verify_api_extract + buildspec: codebuild_specs/verify_api_extract.yml + env: + compute-type: BUILD_GENERAL1_LARGE + depend-on: + - build_linux + - identifier: verify_yarn_lock + buildspec: codebuild_specs/verify_yarn_lock.yml + env: + compute-type: BUILD_GENERAL1_LARGE + depend-on: + - build_linux + - identifier: build_pkg_binaries_arm + buildspec: codebuild_specs/build_pkg_binaries_arm.yml + env: + compute-type: BUILD_GENERAL1_LARGE + depend-on: + - publish_to_local_registry + - identifier: build_pkg_binaries_linux + buildspec: codebuild_specs/build_pkg_binaries_linux.yml + env: + compute-type: BUILD_GENERAL1_LARGE + depend-on: + - publish_to_local_registry + - identifier: build_pkg_binaries_macos + buildspec: codebuild_specs/build_pkg_binaries_macos.yml + env: + compute-type: BUILD_GENERAL1_LARGE + depend-on: + - publish_to_local_registry + - identifier: build_pkg_binaries_win + buildspec: codebuild_specs/build_pkg_binaries_win.yml + env: + compute-type: BUILD_GENERAL1_LARGE + depend-on: + - publish_to_local_registry + - identifier: verify_versions_match + buildspec: codebuild_specs/verify_versions_match.yml + depend-on: + - publish_to_local_registry diff --git a/codebuild_specs/test.yml b/codebuild_specs/test.yml index 00a2eec803a..bb1a381745b 100644 --- a/codebuild_specs/test.yml +++ b/codebuild_specs/test.yml @@ -2,9 +2,13 @@ version: 0.2 env: shell: bash variables: - NODE_OPTIONS: --max-old-space-size=14000 + NODE_OPTIONS: --max-old-space-size=12000 phases: build: commands: - codebuild-breakpoint + - node -e "console.log(require('v8').getHeapStatistics().total_available_size / 1024 / 1024)" - source ./shared-scripts.sh && _testLinux + post_build: + commands: + - echo Finished unit testing diff --git a/package.json b/package.json index 8fed261980e..cadcc523f28 100644 --- a/package.json +++ b/package.json @@ -46,7 +46,9 @@ "setup-dev": "yarn dev-build && yarn rm-dev-link && yarn link-dev && yarn rm-aa-dev-link && yarn link-aa-dev", "split-e2e-tests": "yarn ts-node ./scripts/split-e2e-tests.ts && git add .circleci/config.yml", "test-changed": "nx affected --target=test", - "test-ci": "nx run-many --parallel=4 --target=test --all -- --ci --runInBand", + "test-ci": "nx run-many --parallel=3 --target=test --all --ci --runInBand", + "test-ci-1": "nx run-many --parallel=1 --target=test --all --exclude @aws-amplify/amplify-category-function:test --ci --runInBand", + "test-ci-2": "nx run --parallel=3 @aws-amplify/amplify-category-function:test --ci --runInBand", "test": "nx run-many --target=test --all -- --runInBand", "update-data-packages": "./scripts/update-data-dependencies.sh && yarn", "update-test-timing-data": "ts-node ./scripts/cci-get-job-metrics.ts && ts-node ./scripts/cci-extract-test-timings-from-job-metrics.ts", diff --git a/packages/amplify-category-function/src/__tests__/provider-utils/awscloudformation/index.test.ts b/packages/amplify-category-function/src/__tests__/provider-utils/awscloudformation/index.test.ts index 2a410148006..b38beb9ee87 100644 --- a/packages/amplify-category-function/src/__tests__/provider-utils/awscloudformation/index.test.ts +++ b/packages/amplify-category-function/src/__tests__/provider-utils/awscloudformation/index.test.ts @@ -31,6 +31,7 @@ jest.mock('../../../provider-utils/awscloudformation/utils/buildFunction', () => })); const buildFunction_mock = buildFunction as jest.MockedFunction; +jest.retryTimes(2); describe('awscloudformation function provider', () => { beforeEach(() => jest.clearAllMocks()); it('opens the correct service console', () => { diff --git a/shared-scripts.sh b/shared-scripts.sh index 8cd10e7020e..153f1603b64 100644 --- a/shared-scripts.sh +++ b/shared-scripts.sh @@ -70,6 +70,9 @@ function loadCacheFile { } function _loadTestAccountCredentials { echo ASSUMING PARENT TEST ACCOUNT credentials + echo $TEST_ACCOUNT_ROLE + echo $RANDOM + session_id=$((1 + $RANDOM % 10000)) creds=$(aws sts assume-role --role-arn $TEST_ACCOUNT_ROLE --role-session-name testSession${session_id} --duration-seconds 3600) if [ -z $(echo $creds | jq -c -r '.AssumedRoleUser.Arn') ]; then @@ -81,10 +84,6 @@ function _loadTestAccountCredentials { export AWS_SECRET_ACCESS_KEY=$(echo $creds | jq -c -r ".Credentials.SecretAccessKey") export AWS_SESSION_TOKEN=$(echo $creds | jq -c -r ".Credentials.SessionToken") } - - - - function _setShell { echo Setting Shell yarn config set script-shell $(which bash) @@ -92,8 +91,10 @@ function _setShell { function _buildLinux { _setShell echo Linux Build + yarn run production-build yarn build-tests + storeCache $CODEBUILD_SRC_DIR repo storeCache $HOME/.cache .cache } @@ -105,13 +106,29 @@ function _buildWindows { storeCache $CODEBUILD_SRC_DIR repo-windows storeCache $HOME/.cache .cache-windows } +function _unitTests { + FAILED_TEST_REGEX_FILE=./amplify-unit-tests/failed_unit_tests.txt + if [ -f $FAILED_TEST_REGEX_FILE ]; then + # read the content of failed tests + failedTests=$(<$FAILED_TEST_REGEX_FILE) + yarn test-ci --forceExit --no-cache --maxWorkers=4 $TEST_SUITE -t "$failedTests" + else + yarn test-ci --forceExit --no-cache --maxWorkers=4 $TEST_SUITE + fi +} function _testLinux { echo Run Test + # download [repo, .cache from s3] loadCache repo $CODEBUILD_SRC_DIR loadCache .cache $HOME/.cache - # run tests + + # node --expose-gc node_modules/.bin/jest --logHeapUsage yarn test-ci + + # yarn test-ci-1 + # yarn test-ci-2 + # echo collecting coverage # yarn coverage } @@ -163,8 +180,11 @@ function _mockE2ETests { loadCache repo $CODEBUILD_SRC_DIR loadCache .cache $HOME/.cache + sudo apt-get -y install lsof + source .circleci/local_publish_helpers.sh cd packages/amplify-util-mock/ + yarn e2e } function _publishToLocalRegistry { @@ -349,3 +369,106 @@ function _runE2ETestsWindows { retry runE2eTest } +function _integrationTests { + #restore cache + loadCache repo $CODEBUILD_SRC_DIR + loadCache .cache $HOME/.cache + + echo $AUTH_CLONE_URL + echo $API_CLONE_URL + + source .circleci/ + # make file executable + cd .circleci/ && chmod +x aws.sh + + # don't know what this does + expect .circleci/aws_configure.exp + + # configuring Amplify CLI + yarn rm-dev-link && yarn link-dev && yarn rm-aa-dev-link && yarn link-aa-dev + echo 'export PATH="$(yarn global bin):$PATH"' >> $BASH_ENV + amplify-dev + + # cloning auth test package + cd .. + git clone $AUTH_CLONE_URL + cd aws-amplify-cypress-auth + yarn --cache-folder ~/.cache/yarn + yarn add cypress@6.8.0 --save + + cd .circleci/ && chmod +x auth.sh && chmod +x amplify_init.sh && chmod +x amplify_init.exp + expect .circleci/amplify_init.exp ../aws-amplify-cypress-auth + expect .circleci/enable_auth.exp + cd ../aws-amplify-cypress-auth + yarn --frozen-lockfile --cache-folder ~/.cache/yarn + cd ../aws-amplify-cypress-auth/src && cat $(find . -type f -name 'aws-exports*') + + # Start auth test server in the background + cd ../aws-amplify-cypress-auth + pwd + yarn start + + # Run cypress test for auth + cd ../aws-amplify-cypress-auth + cp ../repo/cypress.json . + cp -R ../repo/cypress . + yarn cypress run --spec $(find . -type f -name 'auth_spec*') + + # cleanup scripts + sudo kill -9 $(lsof -t -i:3000) + cd .circleci/ && chmod +x delete_auth.sh + expect .circleci/delete_auth.exp + + # clone API Test Package + cd .. + git clone $API_CLONE_URL #TODO: Define this + cd aws-amplify-cypress-api + yarn --cache-folder ~/.cache/yarn + + cd .circleci/ && chmod +x api.sh + expect .circleci/amplify_init.exp ../aws-amplify-cypress-api + expect .circleci/enable_api.exp + cd ../aws-amplify-cypress-api + yarn --frozen-lockfile --cache-folder ~/.cache/yarn + cd ../aws-amplify-cypress-api/src && cat $(find . -type f -name 'aws-exports*') + + # Start API test server in background + cd ../aws-amplify-cypress-api + pwd + yarn start + + # Run cypress tests for API + cd ../aws-amplify-cypress-api + yarn add cypress@6.8.0 --save + cp ../repo/cypress.json . + cp -R ../repo/cypress . + yarn cypress run --spec $(find . -type f -name 'api_spec*') + + cd .circleci/ && chmod +x delete_api.sh + expect .circleci/delete_api.exp + + _scanE2eTestArtifacts + + # storing in cache for auth tests + storeCache $HOME/aws-amplify-cypress-auth/cypress/videos ~/aws-amplify-cypress-auth/cypress/videos + storeCache $HOME/aws-amplify-cypress-auth/cypress/screenshots ~/aws-amplify-cypress-auth/cypress/screenshots + + # storing in cache for api tests + storeCache $HOME/aws-amplify-cypress-api/cypress/videos ~aws-amplify-cypress-api/cypress/videos + storeCache $HOME/aws-amplify-cypress-api/cypress/screenshots ~aws-amplify-cypress-api/cypress/screenshots + + echo integration tests completed +} +function _cleanupResources { + #restore cache + loadCache repo $CODEBUILD_SRC_DIR + loadCache .cache $HOME/.cache + + #loading test account credentials + _loadTestAccountCredentials + + cd packages/amplify-e2e-tests + yarn clean-e2e-resources + + storeCache $HOME/packages/amplify-e2e-tests/amplify-e2e-reports repo +}