github.com/cilium/controller-tools@v0.3.1-0.20230329170030-f2b7ff866fde/test.sh (about) 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 set -e 18 19 # Enable tracing in this script off by setting the TRACE variable in your 20 # environment to any value: 21 # 22 # $ TRACE=1 test.sh 23 TRACE=${TRACE:-""} 24 if [ -n "$TRACE" ]; then 25 set -x 26 fi 27 28 # Turn colors in this script off by setting the NO_COLOR variable in your 29 # environment to any value: 30 # 31 # $ NO_COLOR=1 test.sh 32 NO_COLOR=${NO_COLOR:-""} 33 if [ -z "$NO_COLOR" ]; then 34 header=$'\e[1;33m' 35 reset=$'\e[0m' 36 else 37 header='' 38 reset='' 39 fi 40 41 k8s_version=1.11.0 42 # keeping older version around to reproduce any issue (just in case) 43 #k8s_version=1.10.1 44 goarch=amd64 45 goos="unknown" 46 47 if [[ "$OSTYPE" == "linux-gnu" ]]; then 48 goos="linux" 49 elif [[ "$OSTYPE" == "darwin"* ]]; then 50 goos="darwin" 51 fi 52 53 if [[ "$goos" == "unknown" ]]; then 54 echo "OS '$OSTYPE' not supported. Aborting." >&2 55 exit 1 56 fi 57 58 tmp_root=/tmp 59 kb_root_dir=$tmp_root/kubebuilder 60 61 function header_text { 62 echo "$header$*$reset" 63 } 64 65 # Skip fetching and untaring the tools by setting the SKIP_FETCH_TOOLS variable 66 # in your environment to any value: 67 # 68 # $ SKIP_FETCH_TOOLS=1 ./test.sh 69 # 70 # If you skip fetching tools, this script will use the tools already on your 71 # machine, but rebuild the kubebuilder and kubebuilder-bin binaries. 72 SKIP_FETCH_TOOLS=${SKIP_FETCH_TOOLS:-""} 73 74 # fetch k8s API gen tools and make it available under kb_root_dir/bin. 75 function fetch_kb_tools { 76 if [ -n "$SKIP_FETCH_TOOLS" ]; then 77 return 0 78 fi 79 80 header_text "fetching tools" 81 kb_tools_archive_name="kubebuilder-tools-$k8s_version-$goos-$goarch.tar.gz" 82 kb_tools_download_url="https://storage.googleapis.com/kubebuilder-tools/$kb_tools_archive_name" 83 84 kb_tools_archive_path="$tmp_root/$kb_tools_archive_name" 85 if [ ! -f $kb_tools_archive_path ]; then 86 curl -sL ${kb_tools_download_url} -o "$kb_tools_archive_path" 87 fi 88 tar -zvxf "$kb_tools_archive_path" -C "$tmp_root/" 89 } 90 91 function setup_envs { 92 header_text "setting up env vars" 93 94 # Setup env vars 95 export TEST_ASSET_KUBECTL=$kb_root_dir/bin/kubectl 96 export TEST_ASSET_KUBE_APISERVER=$kb_root_dir/bin/kube-apiserver 97 export TEST_ASSET_ETCD=$kb_root_dir/bin/etcd 98 } 99 100 header_text "using tools" 101 102 which golangci-lint 103 104 # fetch the testing binaries - e.g. apiserver and etcd 105 fetch_kb_tools 106 107 # setup testing env 108 setup_envs 109 110 header_text "running golangci-lint" 111 112 header_text "generating marker help" 113 pushd cmd/controller-gen > /dev/null 114 go generate 115 popd > /dev/null 116 117 golangci-lint run --disable-all \ 118 --enable=misspell \ 119 --enable=golint \ 120 --enable=govet \ 121 --enable=deadcode \ 122 --enable=goimports \ 123 --enable=errcheck \ 124 --enable=varcheck \ 125 --enable=unparam \ 126 --enable=ineffassign \ 127 --enable=nakedret \ 128 --enable=misspell \ 129 --enable=gocyclo \ 130 --enable=gosec \ 131 --deadline=5m \ 132 ./pkg/... ./cmd/... 133 134 # --enable=structcheck \ # doesn't understand embedded structs 135 # --enable=goconst \ # complains about constants that shouldn't be constants 136 # --enable=interfacer \ # just kinda strange, deprecated, has bad suggestions that defeat self-documenting code 137 138 header_text "running go test" 139 140 go test -race ./pkg/... ./cmd/... -parallel 4