...
1#!/usr/bin/env bash
2
3# Copyright 2018 The Kubernetes Authors.
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17# This script checks whether mutable global feature gate is invocated correctly
18# in `*_test.go` files.
19# Usage: `hack/verify-test-featuregates.sh`.
20
21set -o errexit
22set -o nounset
23set -o pipefail
24
25KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
26source "${KUBE_ROOT}/hack/lib/init.sh"
27
28cd "${KUBE_ROOT}"
29
30rc=0
31
32# find test files accessing the mutable global feature gate or interface
33direct_sets=$(git grep MutableFeatureGate -- '*_test.go') || true
34if [[ -n "${direct_sets}" ]]; then
35 echo "Test files may not access mutable global feature gates directly:" >&2
36 echo "${direct_sets}" >&2
37 echo >&2
38 echo "Use this invocation instead:" >&2
39 echo " defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.<FeatureName>, <value>)()" >&2
40 echo >&2
41 rc=1
42fi
43
44# find test files calling SetFeatureGateDuringTest and not calling the result
45missing_defers=$(git grep "\\.SetFeatureGateDuringTest" -- '*_test.go' | grep -E -v "defer .*\\)\\(\\)$") || true
46if [[ -n "${missing_defers}" ]]; then
47 echo "Invalid invocations of featuregatetesting.SetFeatureGateDuringTest():" >&2
48 echo "${missing_defers}" >&2
49 echo >&2
50 echo "Always make a deferred call to the returned function to ensure the feature gate is reset:" >&2
51 echo " defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.<FeatureName>, <value>)()" >&2
52 echo >&2
53 rc=1
54fi
55
56
57# ensure all generic features are added in alphabetic order
58lines=$(git grep 'genericfeatures[.].*:' -- pkg/features/kube_features.go)
59sorted_lines=$(echo "$lines" | sort -f)
60if [[ "$lines" != "$sorted_lines" ]]; then
61 echo "Generic features in pkg/features/kube_features.go not sorted" >&2
62 echo >&2
63 echo "Expected:" >&2
64 echo "$sorted_lines" >&2
65 echo >&2
66 echo "Got:" >&2
67 echo "$lines" >&2
68 rc=1
69fi
70
71exit $rc
View as plain text