...

Text file src/cloud.google.com/go/internal/kokoro/continuous.sh

Documentation: cloud.google.com/go/internal/kokoro

     1#!/bin/bash
     2# Copyright 2019 Google LLC
     3#
     4# Licensed under the Apache License, Version 2.0 (the "License");
     5# you may not use this file except in compliance with the License.
     6# You may obtain a copy of the License at
     7#
     8#      http://www.apache.org/licenses/LICENSE-2.0
     9#
    10# Unless required by applicable law or agreed to in writing, software
    11# distributed under the License is distributed on an "AS IS" BASIS,
    12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13# See the License for the specific language governing permissions and
    14# limitations under the License.
    15
    16##
    17# continuous.sh
    18# Runs CI checks for entire repository.
    19#
    20# Jobs types
    21#
    22# Continuous: Runs root tests & tests in submodules changed by a PR. Triggered by PR merges.
    23# Nightly: Runs root tests & tests in all modules. Triggered nightly.
    24# Nightly/$MODULE: Runs tests in a specified module. Triggered nightly.
    25##
    26
    27export GOOGLE_APPLICATION_CREDENTIALS=$KOKORO_KEYSTORE_DIR/72523_go_integration_service_account
    28# Removing the GCLOUD_TESTS_GOLANG_PROJECT_ID setting may make some integration
    29# tests (like profiler's) silently skipped, so make sure you know what you are
    30# doing when changing / removing the next line.
    31
    32export GCLOUD_TESTS_GOLANG_PROJECT_ID=dulcet-port-762
    33export GCLOUD_TESTS_GOLANG_SECONDARY_BIGTABLE_PROJECT_ID=gcloud-golang-firestore-tests
    34export GCLOUD_TESTS_GOLANG_KEY=$GOOGLE_APPLICATION_CREDENTIALS
    35export GCLOUD_TESTS_GOLANG_DATASTORE_DATABASES=database-01
    36export GCLOUD_TESTS_GOLANG_FIRESTORE_PROJECT_ID=gcloud-golang-firestore-tests
    37export GCLOUD_TESTS_GOLANG_FIRESTORE_KEY=$KOKORO_KEYSTORE_DIR/72523_go_firestore_integration_service_account
    38export GCLOUD_TESTS_GOLANG_FIRESTORE_DATABASES=database-02
    39export GCLOUD_TESTS_API_KEY=$(cat $KOKORO_KEYSTORE_DIR/72523_go_gcloud_tests_api_key)
    40export GCLOUD_TESTS_GOLANG_KEYRING=projects/dulcet-port-762/locations/us/keyRings/go-integration-test
    41export GCLOUD_TESTS_GOLANG_PROFILER_ZONE="us-west1-b"
    42export GCLOUD_TESTS_IMPERSONATE_READER_KEY="${KOKORO_GFILE_DIR}/secret_manager/go-cloud-integration-impersonate-reader-service-account"
    43export GCLOUD_TESTS_IMPERSONATE_READER_EMAIL="impersonate-reader@${GCLOUD_TESTS_GOLANG_PROJECT_ID}.iam.gserviceaccount.com"
    44export GCLOUD_TESTS_IMPERSONATE_WRITER_EMAIL="impersonate-writer@${GCLOUD_TESTS_GOLANG_PROJECT_ID}.iam.gserviceaccount.com"
    45export GCLOUD_TESTS_GOLANG_PROJECT_NUMBER=$(cat ${KOKORO_GFILE_DIR}/secret_manager/go-cloud-integration-project-number)
    46export GCLOUD_TESTS_GOLANG_SERVICE_ACCOUNT_CLIENT_ID=$(cat ${KOKORO_GFILE_DIR}/secret_manager/go-cloud-integration-byoid-client-id)
    47export GCLOUD_TESTS_GOLANG_AWS_ACCOUNT_ID=$(cat ${KOKORO_GFILE_DIR}/secret_manager/go-cloud-integration-byoid-aws-acc-id)
    48export GCLOUD_TESTS_GOLANG_AWS_ROLE_NAME=$(cat ${KOKORO_GFILE_DIR}/secret_manager/go-cloud-integration-byoid-aws-role-name)
    49export GCLOUD_TESTS_GOLANG_AWS_ROLE_ID="arn:aws:iam::$GCLOUD_TESTS_GOLANG_AWS_ACCOUNT_ID:role/$GCLOUD_TESTS_GOLANG_AWS_ROLE_NAME"
    50export GCLOUD_TESTS_GOLANG_AUDIENCE_OIDC=$(cat ${KOKORO_GFILE_DIR}/secret_manager/go-cloud-integration-byoid-aud-oidc)
    51export GCLOUD_TESTS_GOLANG_AUDIENCE_AWS=$(cat ${KOKORO_GFILE_DIR}/secret_manager/go-cloud-integration-byoid-aud-aws)
    52export GOOGLE_EXTERNAL_ACCOUNT_ALLOW_EXECUTABLES="1"
    53export GOOGLE_API_GO_EXPERIMENTAL_ENABLE_NEW_AUTH_LIB="true"
    54
    55# Bigtable integration tests expect an existing instance and cluster
    56#  ❯ cbt createinstance gc-bt-it-instance gc-bt-it-instance \
    57#    gc-bt-it-cluster us-west1-b 1 SSD
    58export GCLOUD_TESTS_BIGTABLE_KEYRING=projects/dulcet-port-762/locations/us-central1/keyRings/go-integration-test-regional
    59export GCLOUD_TESTS_BIGTABLE_CLUSTER="gc-bt-it-cluster"
    60export GCLOUD_TESTS_BIGTABLE_PRI_PROJ_SEC_CLUSTER="gc-bt-it-cluster-02"
    61export GCLOUD_TESTS_BIGTABLE_INSTANCE="gc-bt-it-instance"
    62
    63# TODO: Remove this env after OMG/43748 is fixed
    64# Spanner integration tests for backup/restore is flaky https://github.com/googleapis/google-cloud-go/issues/5037
    65# to fix the flaky test Spanner need to run on us-west1 region.
    66export GCLOUD_TESTS_GOLANG_SPANNER_INSTANCE_CONFIG="regional-us-west1"
    67
    68# Fail on any error
    69set -eo pipefail
    70
    71# Display commands being run
    72set -x
    73
    74# cd to project dir on Kokoro instance
    75cd github/google-cloud-go
    76
    77go version
    78
    79export GOCLOUD_HOME=$KOKORO_ARTIFACTS_DIR/google-cloud-go/
    80export PATH="$GOPATH/bin:$PATH"
    81export GO111MODULE=on
    82export GOPROXY=https://proxy.golang.org
    83
    84# Move code into artifacts dir
    85mkdir -p $GOCLOUD_HOME
    86git clone . $GOCLOUD_HOME
    87cd $GOCLOUD_HOME
    88
    89try3() { eval "$*" || eval "$*" || eval "$*"; }
    90
    91# All packages, including +build tools, are fetched.
    92try3 go mod download
    93
    94# runDirectoryTests runs all tests in the current directory.
    95# If a PATH argument is specified, it runs `go test [PATH]`.
    96runDirectoryTests() {
    97  if { [[ $PWD == *"/internal/"* ]] ||
    98    [[ $PWD == *"/third_party/"* ]]; } &&
    99    [[ $KOKORO_JOB_NAME == *"earliest"* ]]; then
   100    # internal tools only expected to work with latest go version
   101    return
   102  fi
   103  GOWORK=off go test -race -v -timeout 45m "${1:-./...}" 2>&1 |
   104    tee sponge_log.log
   105  # Takes the kokoro output log (raw stdout) and creates a machine-parseable
   106  # xUnit XML file.
   107  cat sponge_log.log |
   108    go-junit-report -set-exit-code >sponge_log.xml
   109  # Add the exit codes together so we exit non-zero if any module fails.
   110  exit_code=$(($exit_code + $?))
   111}
   112
   113# runEmulatorTests runs emulator tests in the current directory.
   114runEmulatorTests() {
   115  if [ -f "emulator_test.sh" ]; then
   116    ./emulator_test.sh
   117    # Takes the kokoro output log (raw stdout) and creates a machine-parseable
   118    # xUnit XML file.
   119    cat sponge_log.log |
   120      go-junit-report -set-exit-code >sponge_log.xml
   121    # Add the exit codes together so we exit non-zero if any module fails.
   122    exit_code=$(($exit_code + $?))
   123  fi
   124}
   125
   126# testAllModules runs all modules' tests, including emulator tests.
   127testAllModules() {
   128  echo "Testing all modules"
   129  for i in $(find . -name go.mod); do
   130    pushd "$(dirname "$i")" >/dev/null
   131    runDirectoryTests
   132    # Run integration tests against an emulator.
   133    runEmulatorTests
   134    popd >/dev/null
   135  done
   136}
   137
   138# testChangedModules runs tests in changed modules only.
   139testChangedModules() {
   140  for d in $CHANGED_DIRS; do
   141    goDirectories="$(find "$d" -name "*.go" -printf "%h\n" | sort -u)"
   142    if [[ -n "$goDirectories" ]]; then
   143      for gd in $goDirectories; do
   144        pushd "$gd" >/dev/null
   145        runDirectoryTests .
   146        popd >/dev/null
   147      done
   148    fi
   149  done
   150}
   151
   152set +e # Run all tests, don't stop after the first failure.
   153exit_code=0
   154
   155if [[ $KOKORO_JOB_NAME == *"continuous"* ]]; then
   156  # Continuous jobs only run root tests & tests in submodules changed by the PR.
   157  SIGNIFICANT_CHANGES=$(git --no-pager diff --name-only $KOKORO_GIT_COMMIT^..$KOKORO_GIT_COMMIT | grep -Ev '(\.md$|^\.github|\.json$|\.yaml$)' || true)
   158
   159  if [ -z $SIGNIFICANT_CHANGES ]; then
   160    echo "No changes detected, skipping tests"
   161    exit 0
   162  fi
   163
   164  # CHANGED_DIRS is the list of significant top-level directories that changed,
   165  # but weren't deleted by the current PR. CHANGED_DIRS will be empty when run on main.
   166  CHANGED_DIRS=$(echo "$SIGNIFICANT_CHANGES" | tr ' ' '\n' | grep "/" | cut -d/ -f1 | sort -u | tr '\n' ' ' | xargs ls -d 2>/dev/null || true)
   167  if [[ -n $TARGET_MODULE ]]; then
   168    pushd $TARGET_MODULE >/dev/null
   169    runDirectoryTests
   170    popd >/dev/null
   171  elif [[ -z $SIGNIFICANT_CHANGES ]] || echo "$SIGNIFICANT_CHANGES" | tr ' ' '\n' | grep "^go.mod$" || [[ $CHANGED_DIRS =~ "internal" ]]; then
   172    # If PR changes affect all submodules, then run all tests.
   173    testAllModules
   174  else
   175    runDirectoryTests . # Always run base tests.
   176    echo "Running tests only in changed submodules: $CHANGED_DIRS"
   177    testChangedModules
   178  fi
   179elif [[ $KOKORO_JOB_NAME == *"nightly"* ]]; then
   180  # Expected job name format: ".../nightly/[OPTIONAL_MODULE_NAME]/[OPTIONAL_JOB_NAMES...]"
   181  ARR=(${KOKORO_JOB_NAME//// }) # Splits job name by "/" where ARR[0] is expected to be "nightly".
   182  SUBMODULE_NAME=${ARR[5]}      # Gets the token after "nightly/".
   183  if [[ -n $SUBMODULE_NAME ]] && [[ -d "./$SUBMODULE_NAME" ]]; then
   184    # Only run tests in the submodule designated in the Kokoro job name.
   185    # Expected format example: ...google-cloud-go/nightly/logging.
   186    runDirectoryTests . # Always run base tests
   187    echo "Running tests in one submodule: $SUBMODULE_NAME"
   188    pushd $SUBMODULE_NAME >/dev/null
   189    runDirectoryTests
   190    popd >/dev/null
   191  else
   192    # Run all tests if it is a regular nightly job.
   193    testAllModules
   194  fi
   195else
   196  testAllModules
   197fi
   198
   199if [[ $KOKORO_BUILD_ARTIFACTS_SUBDIR = *"continuous"* ]] || [[ $KOKORO_BUILD_ARTIFACTS_SUBDIR = *"nightly"* ]]; then
   200  chmod +x $KOKORO_GFILE_DIR/linux_amd64/flakybot
   201  $KOKORO_GFILE_DIR/linux_amd64/flakybot -logs_dir=$GOCLOUD_HOME \
   202    -repo=googleapis/google-cloud-go \
   203    -commit_hash=$KOKORO_GITHUB_COMMIT_URL_google_cloud_go
   204fi
   205
   206exit $exit_code

View as plain text