Skip to content

Commit

Permalink
Add Make rules and bash for Go tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chaodaiG committed Sep 30, 2021
1 parent b2c50c2 commit 530849f
Show file tree
Hide file tree
Showing 9 changed files with 1,486 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
/bazel-*
/_artifacts
/_output
/_bin
node_modules/
.DS_Store
# Files generated by JetBrains IDEs, e.g. IntelliJ IDEA
Expand Down
1 change: 1 addition & 0 deletions .go-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1.16.2
66 changes: 66 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# 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.

################################################################################
# ========================== Capture Environment ===============================
# get the repo root and output path
REPO_ROOT:=${CURDIR}
OUT_DIR=$(REPO_ROOT)/out
################################################################################
# ========================= Setup Go ===========================================
# go1.9+ can autodetect GOROOT, but if some other tool sets it ...
GOROOT:=
# enable modules
GO111MODULE=on
export GOROOT GO111MODULE
################################################################################
# ================================= Testing ====================================
# unit tests (hermetic)
unit:
hack/make-rules/go-test/unit.sh
# integration tests
integration:
hack/make-rules/go-test/integration.sh
# all tests
test:
hack/make-rules/go-test/all.sh
################################################################################
# ================================= Cleanup ====================================
# standard cleanup target
clean:
rm -rf "$(OUT_DIR)/"
################################################################################
# ============================== Auto-Update ===================================
# update generated code, gofmt, etc.
update:
hack/make-rules/update/all.sh
# update generated code
generate:
hack/make-rules/update/generated.sh
# gofmt
gofmt:
hack/make-rules/update/gofmt.sh
################################################################################
# ================================== Linting ===================================
# run linters, ensure generated code, etc.
verify:
hack/make-rules/verify/all.sh
# code linters
lint:
hack/make-rules/verify/lint.sh
# shell linter
shellcheck:
hack/make-rules/verify/shellcheck.sh
#################################################################################
.PHONY: unit
43 changes: 43 additions & 0 deletions hack/build/setup-go.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/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.

set -o nounset
set -o errexit
set -o pipefail

# cd to the repo root and setup go
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd -P)"
cd "${REPO_ROOT}"

# read go-version file unless GO_VERSION is set
GO_VERSION="${GO_VERSION:-"$(cat .go-version)"}"

# only setup go if we haven't set FORCE_HOST_GO, or `go version` doesn't match
# go version output looks like:
# go version go1.14.5 darwin/amd64
if ! ([ -n "${FORCE_HOST_GO:-}" ] ||
(command -v go >/dev/null && [ "$(go version | cut -d' ' -f3)" = "go${GO_VERSION}" ])); then

if ! (command -v gvm >/dev/null); then
bash -c "$(curl -s -S -L https://raw.githubusercontent.com/moovweb/gvm/master/binscripts/gvm-installer)"
source /root/.gvm/scripts/gvm
fi

gvm install go${GO_VERSION}
gvm use go${GO_VERSION}
fi

# force go modules
export GO111MODULE=on
23 changes: 23 additions & 0 deletions hack/make-rules/go-test/all.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/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.

set -o nounset
set -o errexit
set -o pipefail

CUR_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
cd "${CUR_DIR}"

./unit.sh
41 changes: 41 additions & 0 deletions hack/make-rules/go-test/unit.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/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.

set -o nounset
set -o errexit
set -o pipefail

# cd to the repo root and setup go
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../../.." && pwd -P)"
cd "${REPO_ROOT}"
source hack/build/setup-go.sh

# build gotestsum
cd 'hack/tools'
go build -o "${REPO_ROOT}/_bin/gotestsum" gotest.tools/gotestsum
cd "${REPO_ROOT}"

# run unit tests with junit output
(
set -x;
mkdir -p "${REPO_ROOT}/_output"
"${REPO_ROOT}/_bin/gotestsum" --junitfile="${REPO_ROOT}/_output/unit-junit.xml" \
-- './...'
)

# if we are in CI, copy to the artifact upload location
if [[ -n "${ARTIFACTS:-}" ]]; then
cp "_output/unit-junit.xml" "${ARTIFACTS:?}/unit-junit.xml"
fi
9 changes: 9 additions & 0 deletions hack/tools/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module k8s.io/test-infra/hack/tools

go 1.16

require (
github.com/golangci/golangci-lint v1.42.1
gotest.tools/gotestsum v1.7.0
k8s.io/code-generator v0.22.2
)
Loading

0 comments on commit 530849f

Please sign in to comment.