k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/hack/verify-test-code.sh (about)

     1  #!/usr/bin/env bash
     2  # Copyright 2019 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  # This script checks whether e2e test code which contains `Expect()` but not use
    17  # the e2e framework exists or not.
    18  # Usage: `hack/verify-test-code.sh`.
    19  
    20  set -o errexit
    21  set -o nounset
    22  set -o pipefail
    23  
    24  KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
    25  source "${KUBE_ROOT}/hack/lib/init.sh"
    26  cd "${KUBE_ROOT}"
    27  
    28  all_e2e_files=()
    29  # NOTE: This checks e2e test code without the e2e framework which contains Expect().To(HaveOccurred())
    30  kube::util::read-array all_e2e_files < <(find test/e2e{,_node,_kubeadm} -name '*.go' | grep -v 'test/e2e/framework/')
    31  errors_expect_no_error=()
    32  for file in "${all_e2e_files[@]}"
    33  do
    34      if grep -E "Expect\(.*\)\.(NotTo|ToNot)\(.*HaveOccurred\(\)" "${file}" > /dev/null
    35      then
    36          errors_expect_no_error+=( "${file}" )
    37      fi
    38      if grep -E "Expect\(err\)\.To\(gomega\.BeNil\(\)\)" "${file}" > /dev/null
    39      then
    40          errors_expect_no_error+=( "${file}" )
    41      fi
    42  done
    43  
    44  all_e2e_framework_files=()
    45  kube::util::read-array all_e2e_framework_files < <(find test/e2e/framework/ -name '*.go' | grep -v "_test.go")
    46  errors_framework_contains_tests=()
    47  for file in "${all_e2e_framework_files[@]}"
    48  do
    49      if grep -E "(ConformanceIt\(.*, func\(\) {|ginkgo.It\(.*, func\(\) {)" "${file}" > /dev/null
    50      then
    51          errors_framework_contains_tests+=( "${file}" )
    52      fi
    53  done
    54  
    55  if [ ${#errors_expect_no_error[@]} -ne 0 ]; then
    56    {
    57      echo "Errors:"
    58      for err in "${errors_expect_no_error[@]}"; do
    59        echo "$err"
    60      done
    61      echo
    62      echo 'The above files need to use framework.ExpectNoError(err) instead of '
    63      echo 'Expect(err).NotTo(HaveOccurred()) or gomega.Expect(err).NotTo(gomega.HaveOccurred())'
    64      echo
    65    } >&2
    66    exit 1
    67  fi
    68  
    69  if [ ${#errors_framework_contains_tests[@]} -ne 0 ]; then
    70    {
    71      echo "Errors:"
    72      for err in "${errors_framework_contains_tests[@]}"; do
    73        echo "$err"
    74      done
    75      echo
    76      echo 'The above e2e framework files should not contain any e2e tests which are implemented '
    77      echo 'with framework.ConformanceIt() or ginkgo.It()'
    78      echo
    79    } >&2
    80    exit 1
    81  fi
    82  
    83  echo 'Congratulations!  All e2e test source files are valid.'