...

Text file src/k8s.io/kubernetes/hack/verify-test-code.sh

Documentation: k8s.io/kubernetes/hack

     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
    20set -o errexit
    21set -o nounset
    22set -o pipefail
    23
    24KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
    25source "${KUBE_ROOT}/hack/lib/init.sh"
    26cd "${KUBE_ROOT}"
    27
    28all_e2e_files=()
    29# NOTE: This checks e2e test code without the e2e framework which contains Expect().To(HaveOccurred())
    30kube::util::read-array all_e2e_files < <(find test/e2e{,_node,_kubeadm} -name '*.go' | grep -v 'test/e2e/framework/')
    31errors_expect_no_error=()
    32for file in "${all_e2e_files[@]}"
    33do
    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
    42done
    43
    44all_e2e_framework_files=()
    45kube::util::read-array all_e2e_framework_files < <(find test/e2e/framework/ -name '*.go' | grep -v "_test.go")
    46errors_framework_contains_tests=()
    47for file in "${all_e2e_framework_files[@]}"
    48do
    49    if grep -E "(ConformanceIt\(.*, func\(\) {|ginkgo.It\(.*, func\(\) {)" "${file}" > /dev/null
    50    then
    51        errors_framework_contains_tests+=( "${file}" )
    52    fi
    53done
    54
    55if [ ${#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
    67fi
    68
    69if [ ${#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
    81fi
    82
    83echo 'Congratulations!  All e2e test source files are valid.'

View as plain text