google.golang.org/grpc@v1.62.1/test/kokoro/xds_url_map.sh (about)

     1  #!/usr/bin/env bash
     2  # Copyright 2021 gRPC authors.
     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  set -eo pipefail
    17  
    18  # Constants
    19  readonly GITHUB_REPOSITORY_NAME="grpc-go"
    20  readonly TEST_DRIVER_INSTALL_SCRIPT_URL="https://raw.githubusercontent.com/${TEST_DRIVER_REPO_OWNER:-grpc}/psm-interop/${TEST_DRIVER_BRANCH:-main}/.kokoro/psm_interop_kokoro_lib.sh"
    21  ## xDS test client Docker images
    22  readonly CLIENT_IMAGE_NAME="gcr.io/grpc-testing/xds-interop/go-client"
    23  readonly FORCE_IMAGE_BUILD="${FORCE_IMAGE_BUILD:-0}"
    24  
    25  #######################################
    26  # Builds test app Docker images and pushes them to GCR
    27  # Globals:
    28  #   CLIENT_IMAGE_NAME: Test client Docker image name
    29  #   GIT_COMMIT: SHA-1 of git commit being built
    30  # Arguments:
    31  #   None
    32  # Outputs:
    33  #   Writes the output of `gcloud builds submit` to stdout, stderr
    34  #######################################
    35  build_test_app_docker_images() {
    36    echo "Building Go xDS interop test app Docker images"
    37    docker build -f "${SRC_DIR}/interop/xds/client/Dockerfile" -t "${CLIENT_IMAGE_NAME}:${GIT_COMMIT}" "${SRC_DIR}"
    38    gcloud -q auth configure-docker
    39    docker push "${CLIENT_IMAGE_NAME}:${GIT_COMMIT}"
    40    if is_version_branch "${TESTING_VERSION}"; then
    41      tag_and_push_docker_image "${CLIENT_IMAGE_NAME}" "${GIT_COMMIT}" "${TESTING_VERSION}"
    42    fi
    43  }
    44  
    45  #######################################
    46  # Builds test app and its docker images unless they already exist
    47  # Globals:
    48  #   CLIENT_IMAGE_NAME: Test client Docker image name
    49  #   GIT_COMMIT: SHA-1 of git commit being built
    50  #   FORCE_IMAGE_BUILD
    51  # Arguments:
    52  #   None
    53  # Outputs:
    54  #   Writes the output to stdout, stderr
    55  #######################################
    56  build_docker_images_if_needed() {
    57    # Check if images already exist
    58    client_tags="$(gcloud_gcr_list_image_tags "${CLIENT_IMAGE_NAME}" "${GIT_COMMIT}")"
    59    printf "Client image: %s:%s\n" "${CLIENT_IMAGE_NAME}" "${GIT_COMMIT}"
    60    echo "${client_tags:-Client image not found}"
    61  
    62    # Build if any of the images are missing, or FORCE_IMAGE_BUILD=1
    63    if [[ "${FORCE_IMAGE_BUILD}" == "1" || -z "${client_tags}" ]]; then
    64      build_test_app_docker_images
    65    else
    66      echo "Skipping Go test app build"
    67    fi
    68  }
    69  
    70  #######################################
    71  # Executes the test case
    72  # Globals:
    73  #   TEST_DRIVER_FLAGFILE: Relative path to test driver flagfile
    74  #   KUBE_CONTEXT: The name of kubectl context with GKE cluster access
    75  #   TEST_XML_OUTPUT_DIR: Output directory for the test xUnit XML report
    76  #   CLIENT_IMAGE_NAME: Test client Docker image name
    77  #   GIT_COMMIT: SHA-1 of git commit being built
    78  #   TESTING_VERSION: version branch under test: used by the framework to determine the supported PSM
    79  #                    features.
    80  # Arguments:
    81  #   Test case name
    82  # Outputs:
    83  #   Writes the output of test execution to stdout, stderr
    84  #   Test xUnit report to ${TEST_XML_OUTPUT_DIR}/${test_name}/sponge_log.xml
    85  #######################################
    86  run_test() {
    87    # Test driver usage:
    88    # https://github.com/grpc/grpc/tree/master/tools/run_tests/xds_k8s_test_driver#basic-usage
    89    local test_name="${1:?Usage: run_test test_name}"
    90    local out_dir="${TEST_XML_OUTPUT_DIR}/${test_name}"
    91    mkdir -pv "${out_dir}"
    92    set -x
    93    python -m "tests.${test_name}" \
    94      --flagfile="${TEST_DRIVER_FLAGFILE}" \
    95      --flagfile="config/url-map.cfg" \
    96      --kube_context="${KUBE_CONTEXT}" \
    97      --client_image="${CLIENT_IMAGE_NAME}:${GIT_COMMIT}" \
    98      --testing_version="${TESTING_VERSION}" \
    99      --collect_app_logs \
   100      --log_dir="${out_dir}" \
   101      --xml_output_file="${out_dir}/sponge_log.xml" \
   102      |& tee "${out_dir}/sponge_log.log"
   103  }
   104  
   105  #######################################
   106  # Main function: provision software necessary to execute tests, and run them
   107  # Globals:
   108  #   KOKORO_ARTIFACTS_DIR
   109  #   GITHUB_REPOSITORY_NAME
   110  #   SRC_DIR: Populated with absolute path to the source repo
   111  #   TEST_DRIVER_REPO_DIR: Populated with the path to the repo containing
   112  #                         the test driver
   113  #   TEST_DRIVER_FULL_DIR: Populated with the path to the test driver source code
   114  #   TEST_DRIVER_FLAGFILE: Populated with relative path to test driver flagfile
   115  #   TEST_XML_OUTPUT_DIR: Populated with the path to test xUnit XML report
   116  #   GIT_ORIGIN_URL: Populated with the origin URL of git repo used for the build
   117  #   GIT_COMMIT: Populated with the SHA-1 of git commit being built
   118  #   GIT_COMMIT_SHORT: Populated with the short SHA-1 of git commit being built
   119  #   KUBE_CONTEXT: Populated with name of kubectl context with GKE cluster access
   120  # Arguments:
   121  #   None
   122  # Outputs:
   123  #   Writes the output of test execution to stdout, stderr
   124  #######################################
   125  main() {
   126    local script_dir
   127    script_dir="$(dirname "$0")"
   128  
   129    # Source the test driver from the master branch.
   130    echo "Sourcing test driver install script from: ${TEST_DRIVER_INSTALL_SCRIPT_URL}"
   131    source /dev/stdin <<< "$(curl -s "${TEST_DRIVER_INSTALL_SCRIPT_URL}")"
   132  
   133    activate_gke_cluster GKE_CLUSTER_PSM_BASIC
   134  
   135    set -x
   136    if [[ -n "${KOKORO_ARTIFACTS_DIR}" ]]; then
   137      kokoro_setup_test_driver "${GITHUB_REPOSITORY_NAME}"
   138    else
   139      local_setup_test_driver "${script_dir}"
   140    fi
   141    build_docker_images_if_needed
   142    # Run tests
   143    cd "${TEST_DRIVER_FULL_DIR}"
   144    run_test url_map || echo "Failed url_map test"
   145  }
   146  
   147  main "$@"