diff --git a/hack/tools/go.mod b/hack/tools/go.mod index 6ac59436bf6..95a1098b8a7 100644 --- a/hack/tools/go.mod +++ b/hack/tools/go.mod @@ -6,6 +6,7 @@ require ( github.com/aojea/sloppy-netparser v0.0.0-20210819225411-1b3bd8b3b975 github.com/cespare/prettybench v0.0.0-20150116022406-03b8cfe5406c github.com/client9/misspell v0.3.4 + github.com/golang/mock v1.4.4 github.com/golangci/golangci-lint v1.41.1 github.com/google/go-flow-levee v0.1.5 gotest.tools/gotestsum v1.6.4 diff --git a/hack/tools/go.sum b/hack/tools/go.sum index 6da395b72fd..d2fb94975ab 100644 --- a/hack/tools/go.sum +++ b/hack/tools/go.sum @@ -236,6 +236,7 @@ github.com/golang/mock v1.3.1/go.mod h1:sBzyDLLjw3U8JLTeZvSv8jJB+tU5PVekmnlKIyFU github.com/golang/mock v1.4.0/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt3cw= github.com/golang/mock v1.4.1/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt3cw= github.com/golang/mock v1.4.3/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt3cw= +github.com/golang/mock v1.4.4 h1:l75CXGRSwbaYNpl/Z2X1XIIAMSCquvXgpVZDhwEIJsc= github.com/golang/mock v1.4.4/go.mod h1:l3mdAwkq5BuhzHwde/uurv3sEJeZMXNpwsxVWU71h+4= github.com/golang/protobuf v1.1.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= diff --git a/hack/tools/tools.go b/hack/tools/tools.go index edb46b6fb9e..26d8e7b4d3f 100644 --- a/hack/tools/tools.go +++ b/hack/tools/tools.go @@ -33,4 +33,7 @@ import ( // dependencies _ "sigs.k8s.io/zeitgeist" + + // mockgen + _ "github.com/golang/mock/mockgen" ) diff --git a/hack/update-mocks.sh b/hack/update-mocks.sh new file mode 100755 index 00000000000..f32feb97e03 --- /dev/null +++ b/hack/update-mocks.sh @@ -0,0 +1,141 @@ +#!/usr/bin/env bash + +# Copyright 2021 The Kubernetes Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# This script generates mock files using mockgen. +# Usage: `hack/update-mocks.sh`. + +set -o errexit +set -o nounset +set -o pipefail + +KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/.. +source "${KUBE_ROOT}/hack/lib/init.sh" +# Explicitly opt into go modules, even though we're inside a GOPATH directory +export GO111MODULE=on + +_tmp="${KUBE_ROOT}/_tmp_build_tag_files" +mkdir -p "${_tmp}" + +function cleanup { + rm -rf "$_tmp" + rm -f "tempfile" +} + +trap cleanup EXIT + +kube::golang::verify_go_version + +echo 'installing mockgen' +pushd "${KUBE_ROOT}/hack/tools" >/dev/null + go install github.com/golang/mock/mockgen +popd >/dev/null + +find_files() { + find . -not \( \ + \( \ + -wholename './output' \ + -o -wholename './.git' \ + -o -wholename './_output' \ + -o -wholename './_gopath' \ + -o -wholename './release' \ + -o -wholename './target' \ + -o -wholename '*/third_party/*' \ + -o -wholename '*/vendor/*' \ + -o -wholename './staging/src/k8s.io/client-go/*vendor/*' \ + -o -wholename '*/bindata.go' \ + \) -prune \ + \) -name '*.go' +} + +cd "${KUBE_ROOT}" + +echo 'executing go generate command on below files' + +for IFILE in $(find_files | xargs grep --files-with-matches -e '//go:generate mockgen'); do + temp_file_name=$(mktemp --tmpdir="${_tmp}") + # serach for build tag used in file + build_tag_string=$(grep -o '+build.*$' "$IFILE") || true + + # if the file does not have build string + if [ -n "$build_tag_string" ] + then + # write the build tag in the temp file + echo -n "$build_tag_string" > "$temp_file_name" + + # if +build tag is defined in interface file + BUILD_TAG_FILE=$temp_file_name go generate -v "$IFILE" + else + # if no +build tag is defined in interface file + go generate -v "$IFILE" + fi +done + + +# get the changed mock files +files=$(git diff --name-only) +for file in $files; do + if [ "$file" == "hack/update-mocks.sh" ]; then + continue + fi + + # serach for build tags used in file + # //go:build !providerless + # // +build !providerless + go_build_tag_string=$(grep -o 'go:build.*$' "$file") || true + build_tag_string=$(grep -o '+build.*$' "$file") || true + new_header='' + + # if the file has both headers + if [ -n "$build_tag_string" ] && [ -n "$go_build_tag_string" ] + then + # create a new header with the build string and the copyright text + new_header=$(echo -e "//""$go_build_tag_string""\n""//" "$build_tag_string""\n" | cat - hack/boilerplate/boilerplate.generatego.txt) + + # ignore the first line (build tag) from the file + tail -n +3 "$file" > tempfile + fi + + # if the file has only // +build !providerless header + if [ -n "$build_tag_string" ] && [ -z "$go_build_tag_string" ] + then + # create a new header with the build string and the copyright text + new_header=$(echo -e "//" "$build_tag_string""\n" | cat - hack/boilerplate/boilerplate.generatego.txt) + + # ignore the first line (build tag) from the file + tail -n +2 "$file" > tempfile + fi + + # if the file has only //go:build !providerless header + if [ -z "$build_tag_string" ] && [ -n "$go_build_tag_string" ] + then + # create a new header with the build string and the copyright text + new_header=$(echo -e "//""$go_build_tag_string""\n" | cat - hack/boilerplate/boilerplate.generatego.txt) + + # ignore the first line (build tag) from the file + tail -n +2 "$file" > tempfile + fi + + # if the header if generted + if [ -n "$new_header" ] + then + # write the newly generated header file to the original file + echo -e "$new_header" | cat - tempfile > "$file" + else + # if no build string insert at the top + cat hack/boilerplate/boilerplate.generatego.txt "$file" > tempfile && \ + mv tempfile "$file" + fi +done diff --git a/hack/verify-mocks.sh b/hack/verify-mocks.sh new file mode 100755 index 00000000000..15ed64c16e5 --- /dev/null +++ b/hack/verify-mocks.sh @@ -0,0 +1,86 @@ +#!/usr/bin/env bash + +# Copyright 2021 The Kubernetes Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# This script checks whether updating of Mock files generated from Interfaces +# is needed or not. We should run `hack/update-mocks.sh` +# if Mock files are out of date. +# Usage: `hack/verify-mocks.sh`. + + +set -o errexit +set -o nounset +set -o pipefail + +KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/.. +export KUBE_ROOT +source "${KUBE_ROOT}/hack/lib/init.sh" + +# Explicitly opt into go modules, even though we're inside a GOPATH directory +export GO111MODULE=on + +_tmp="${KUBE_ROOT}/_tmp" +mkdir -p "${_tmp}" + +"${KUBE_ROOT}/hack/update-mocks.sh" + +# If there are untracked generated mock files which needed to be committed +if git_status=$(git status --porcelain --untracked=normal 2>/dev/null) && [[ -n "${git_status}" ]]; then + echo "!!! You may have untracked mock files." +fi + +# get the changed mock files +files=$(git diff --name-only) + +# copy the changed files to the _tmp directory for later comparison +mock_files=() +for file in $files; do + if [ "$file" == "hack/verify-mocks.sh" ]; then + continue + fi + + # create the directory in _tmp + mkdir -p "$_tmp/""$(dirname "$file")" + cp "$file" "$_tmp/""$file" + mock_files+=("$file") + + # reset the current file + git checkout "$file" +done + +echo "diffing process started for ${#mock_files[@]} files" +ret=0 +for file in "${mock_files[@]}"; do + diff -Naupr -B \ + -I '^/\*' \ + -I 'Copyright The Kubernetes Authors.' \ + -I 'Licensed under the Apache License, Version 2.0 (the "License");' \ + -I 'you may not use this file except in compliance with the License.' \ + -I 'You may obtain a copy of the License at' \ + -I 'http://www.apache.org/licenses/LICENSE-2.0' \ + -I 'Unless required by applicable law or agreed to in writing, software' \ + -I 'distributed under the License is distributed on an "AS IS" BASIS,' \ + -I 'WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.' \ + -I 'See the License for the specific language governing permissions and' \ + -I 'limitations under the License.' \ + -I '^\*/' \ + "$file" "$_tmp/""$file" || ret=$? + + if [[ $ret -ne 0 ]]; then + echo "Mock files are out of date. Please run hack/update-mocks.sh" >&2 + exit 1 + fi +done +echo "up to date" diff --git a/pkg/kubelet/dockershim/libdocker/client.go b/pkg/kubelet/dockershim/libdocker/client.go index 8f7e5b53970..892a57ad261 100644 --- a/pkg/kubelet/dockershim/libdocker/client.go +++ b/pkg/kubelet/dockershim/libdocker/client.go @@ -1,5 +1,6 @@ //go:build !dockerless // +build !dockerless +//go:generate mockgen -source client.go -destination testing/mock_client.go -package testing /* Copyright 2014 The Kubernetes Authors. diff --git a/pkg/kubelet/dockershim/network/plugins.go b/pkg/kubelet/dockershim/network/plugins.go index aa6b1bb1aae..88f1a5c0c9d 100644 --- a/pkg/kubelet/dockershim/network/plugins.go +++ b/pkg/kubelet/dockershim/network/plugins.go @@ -1,5 +1,6 @@ //go:build !dockerless // +build !dockerless +//go:generate mockgen -source plugins.go -destination testing/mock_network_plugin.go -package testing /* Copyright 2014 The Kubernetes Authors.