github.com/zppinho/prow@v0.0.0-20240510014325-1738badeb017/test/integration/integration-test.sh (about)

     1  #!/usr/bin/env bash
     2  # Copyright 2021 The Kubernetes 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 -o errexit
    17  set -o nounset
    18  set -o pipefail
    19  
    20  SCRIPT_ROOT="$(cd "$(dirname "$0")" && pwd)"
    21  # shellcheck disable=SC1091
    22  source "${SCRIPT_ROOT}"/lib.sh
    23  
    24  # shellcheck disable=SC1091
    25  source "${REPO_ROOT}/hack/build/setup-go.sh"
    26  
    27  function usage() {
    28    >&2 cat <<EOF
    29  Run Prow's integration tests.
    30  
    31  Usage: $0 [options]
    32  
    33  Examples:
    34    # Bring up the KIND cluster and Prow components, but only run the
    35    # "TestClonerefs/postsubmit" test.
    36    $0 -run=Clonerefs/post
    37  
    38    # Only run the "TestClonerefs/postsubmit" test, with increased verbosity.
    39    $0 -verbose -no-setup -run=Clonerefs/post
    40  
    41    # Recompile and redeploy the Prow components that use the "fakegitserver" and
    42    # "fakegerritserver" images, then only run the "TestClonerefs/postsubmit"
    43    # test, but also
    44    $0 -verbose -build=fakegitserver,fakegerritserver -run=Clonerefs/post
    45  
    46    # Recompile "deck" image, redeploy "deck" and "deck-tenanted" Prow components,
    47    # then only run the "TestDeck" tests. The test knows that "deck" and
    48    # "deck-tenanted" components both depend on the "deck" image in lib.sh (grep
    49    # for PROW_IMAGES_TO_COMPONENTS).
    50    $0 -verbose -build=deck -run=Clonerefs/post
    51  
    52    # Recompile all Prow components, redeploy them, and then only run the
    53    # "TestClonerefs/postsubmit" test.
    54    $0 -verbose -no-setup-kind-cluster -run=Clonerefs/post
    55  
    56    # Before running the "TestClonerefs/postsubmit" test, delete all ProwJob
    57    # Custom Resources and test pods from test-pods namespace.
    58    $0 -verbose -no-setup-kind-cluster -run=Clonerefs/post -clear=ALL
    59  
    60  Options:
    61      -no-setup:
    62          Skip setup of the KIND cluster and Prow installation. That is, only run
    63          gotestsum. This is useful if you already have the cluster and components
    64          set up, and only want to run some tests without setting up the cluster
    65          or recompiling Prow images.
    66  
    67      -no-setup-kind-cluster:
    68          Skip setup of the KIND cluster, but still (re-)install Prow to the
    69          cluster. Flag "-build=..." implies this flag. This is useful if you want
    70          to skip KIND setup. Most of the time, you will want to use this flag
    71          when rerunning tests after initially setting up the cluster (because
    72          most of the time your changes will not impact the KIND cluster itself).
    73  
    74      -build='':
    75          Build only the comma-separated list of Prow components with
    76          "${REPO_ROOT}"/hack/prowimagebuilder. Useful when developing a fake
    77          service that needs frequent recompilation. The images are a
    78          comma-separated string. Also results in only redeploying certain entries
    79          in PROW_COMPONENTS, by way of PROW_IMAGES_TO_COMPONENTS in lib.sh.
    80  
    81          The value "ALL" for this falg is an alias for all images (PROW_IMAGES in
    82          lib.sh).
    83  
    84          By default, "-build=ALL" is assumed, so that users do not have to
    85          provide any arguments to this script to run all tests.
    86  
    87          Implies -no-setup-kind-cluster.
    88  
    89      -clear='':
    90          Delete the comma-separated list of Kubernetes resources from the KIND
    91          cluster before running the test. Possible values: "ALL", "prowjobs",
    92          "test-pods". ALL is an alias for prowjobs and test-pods.
    93  
    94          This makes it easier to see the exact ProwJob Custom Resource ("watch
    95          kubectl get prowjobs.prow.k8s.io") or associated test pod ("watch
    96          kubectl get pods -n test-pods") that is created by the test being run.
    97  
    98      -run='':
    99          Run only those tests that match the given pattern. The format is
   100          "TestName/testcasename". E.g., "TestClonerefs/postsubmit" will only run
   101          1 test. Due to fuzzy matching, "Clonerefs/post" is equivalent.
   102  
   103      -save-logs='':
   104          Export all cluster logs to the given directory (directory will be
   105          created if it doesn't exist).
   106  
   107      -teardown:
   108          Delete the KIND cluster and also the local Docker registry used by the
   109          cluster.
   110  
   111      -verbose:
   112          Make tests run more verbosely.
   113  
   114      -help:
   115          Display this help message.
   116  EOF
   117  }
   118  
   119  function main() {
   120    declare -a tests_to_run
   121    declare -a setup_args
   122    declare -a clear_args
   123    declare -a teardown_args
   124    setup_args=(-setup-kind-cluster -setup-prow-components -build=ALL)
   125    local summary_format
   126    summary_format=pkgname
   127    local setup_kind_cluster
   128    local setup_prow_components
   129    local build_images
   130    local resource
   131    local resources_val
   132    local fakepubsub_node_port
   133    setup_kind_cluster=0
   134    setup_prow_components=0
   135  
   136    for arg in "$@"; do
   137      case "${arg}" in
   138        -no-setup)
   139          unset 'setup_args[0]'
   140          unset 'setup_args[1]'
   141          unset 'setup_args[2]'
   142          ;;
   143        -no-setup-kind-cluster)
   144          unset 'setup_args[0]'
   145          ;;
   146        -build=*)
   147          # Imply -no-setup-kind-cluster.
   148          unset 'setup_args[0]'
   149          # Because we specified a "-build=..." flag explicitly, drop the default
   150          # "-build=ALL" option.
   151          unset 'setup_args[2]'
   152          setup_args+=("${arg}")
   153          ;;
   154        -clear=*)
   155          resources_val="${arg#-clear=}"
   156          for resource in ${resources_val//,/ }; do
   157            case "${resource}" in
   158              ALL)
   159                clear_args=(-prowjobs -test-pods)
   160              ;;
   161              prowjobs|test-pods)
   162                clear_args+=("${resource}")
   163              ;;
   164              *)
   165                echo >&2 "unrecognized argument to -clear: ${resource}"
   166                return 1
   167              ;;
   168            esac
   169          done
   170          ;;
   171        -run=*)
   172          tests_to_run+=("${arg}")
   173          ;;
   174        -save-logs=*)
   175          teardown_args+=("${arg}")
   176          ;;
   177        -teardown)
   178          teardown_args+=(-all)
   179          ;;
   180        -verbose)
   181          summary_format=standard-verbose
   182          ;;
   183        -help)
   184          usage
   185          return
   186          ;;
   187        --*)
   188          echo >&2 "cannot use flags with two leading dashes ('--...'), use single dashes instead ('-...')"
   189          return 1
   190          ;;
   191      esac
   192    done
   193  
   194    # By default use 30303 for fakepubsub.
   195    fakepubsub_node_port="30303"
   196  
   197    # If in CI (pull-test-infra-integration presubmit job), do some things slightly differently.
   198    if [[ -n "${ARTIFACTS:-}" ]]; then
   199      # Use the ARTIFACTS variable to save log output.
   200      teardown_args+=(-save-logs="${ARTIFACTS}/kind_logs")
   201      # Randomize the node port used for the fakepubsub service.
   202      fakepubsub_node_port="$(get_random_node_port)"
   203      log "Using randomized port ${fakepubsub_node_port} for fakepubsub"
   204    fi
   205  
   206    if [[ -n "${teardown_args[*]}" ]]; then
   207      # shellcheck disable=SC2064
   208      trap "${SCRIPT_ROOT}/teardown.sh ${teardown_args[*]}" EXIT
   209    fi
   210  
   211    for arg in "${setup_args[@]}"; do
   212      case "${arg}" in
   213        -setup-kind-cluster) setup_kind_cluster=1 ;;
   214        -setup-prow-components) setup_prow_components=1 ;;
   215        -build=*)
   216          build_images="${arg#-build=}"
   217          ;;
   218      esac
   219    done
   220  
   221    if ((setup_kind_cluster)); then
   222      "${SCRIPT_ROOT}"/setup-kind-cluster.sh \
   223        -fakepubsub-node-port="${fakepubsub_node_port}"
   224    fi
   225  
   226    if ((setup_prow_components)); then
   227      "${SCRIPT_ROOT}"/setup-prow-components.sh \
   228        ${build_images:+"-build=${build_images}"} \
   229        -fakepubsub-node-port="${fakepubsub_node_port}"
   230    fi
   231  
   232    build_gotestsum
   233  
   234    if [[ -n "${clear_args[*]}" ]]; then
   235      "${SCRIPT_ROOT}/clear.sh" "${clear_args[@]}"
   236    fi
   237  
   238    log "Finished preparing environment; running integration test"
   239  
   240    JUNIT_RESULT_DIR="${REPO_ROOT}/_output"
   241    # If we are in CI, copy to the artifact upload location.
   242    if [[ -n "${ARTIFACTS:-}" ]]; then
   243      JUNIT_RESULT_DIR="${ARTIFACTS}"
   244    fi
   245  
   246    # Run integration tests with junit output.
   247    mkdir -p "${JUNIT_RESULT_DIR}"
   248    "${REPO_ROOT}/_bin/gotestsum" \
   249      --format "${summary_format}" \
   250      --junitfile="${JUNIT_RESULT_DIR}/junit-integration.xml" \
   251      -- "${SCRIPT_ROOT}/test" \
   252      --run-integration-test ${tests_to_run[@]:+"${tests_to_run[@]}"} \
   253      --fakepubsub-node-port "${fakepubsub_node_port}"
   254  }
   255  
   256  function build_gotestsum() {
   257    log "Building gotestsum"
   258    set -x
   259    pushd "${REPO_ROOT}/hack/tools"
   260    go build -o "${REPO_ROOT}/_bin/gotestsum" gotest.tools/gotestsum
   261    popd
   262    set +x
   263  }
   264  
   265  main "$@"