Skip to content

Commit 13f2640

Browse files
committed
Refactor testing and CI
Signed-off-by: Stefan Prodan <stefan.prodan@gmail.com>
1 parent 42cbfec commit 13f2640

4 files changed

Lines changed: 72 additions & 173 deletions

File tree

.github/workflows/main.yml

Lines changed: 0 additions & 37 deletions
This file was deleted.

.github/workflows/test.yml

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
# Copyright 2023 The Flux Authors.
2-
# SPDX-License-Identifier: Apache-2.0
3-
41
name: test
52

63
on:
@@ -16,27 +13,21 @@ jobs:
1613
permissions:
1714
contents: read
1815
steps:
19-
-
20-
name: Checkout
21-
uses: actions/checkout@v4
22-
-
23-
name: Set up Go
24-
uses: actions/setup-go@v4
16+
- name: Checkout
17+
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
18+
- name: Setup Go
19+
uses: actions/setup-go@4a3601121dd01d1626a1e23e37211e3254c1c06c # v6.4.0
2520
with:
26-
go-version: 1.25.x
21+
go-version: 1.26.x
2722
cache-dependency-path: |
2823
**/go.sum
2924
**/go.mod
30-
-
31-
name: Test
32-
run: |
33-
make tidy
34-
make test
35-
-
36-
name: Check if working tree is dirty
25+
- name: Test
26+
run: make
27+
- name: Check if working tree is dirty
3728
run: |
3829
if [[ $(git diff --stat) != '' ]]; then
3930
git diff
40-
echo 'run make test and commit changes'
31+
echo 'run make and commit changes'
4132
exit 1
4233
fi

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,6 @@ vendor/
1414

1515
# go artifacts
1616
cover.out
17+
18+
# local binaries
19+
bin/

Makefile

Lines changed: 60 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -1,139 +1,81 @@
1-
# Copyright 2019 The Kubernetes Authors.
1+
# Copyright 2026 The Flux Authors.
22
# SPDX-License-Identifier: Apache-2.0
33

4-
GOPATH := $(shell go env GOPATH)
5-
MYGOBIN := $(shell go env GOPATH)/bin
6-
SHELL := /bin/bash
7-
export PATH := $(MYGOBIN):$(PATH)
4+
# Get the currently used golang install path
5+
# (in GOPATH/bin, unless GOBIN is set).
6+
ifeq (,$(shell go env GOBIN))
7+
GOBIN=$(shell go env GOPATH)/bin
8+
else
9+
GOBIN=$(shell go env GOBIN)
10+
endif
811

9-
.PHONY: all
10-
all: generate license fix vet fmt test lint tidy
11-
12-
"$(MYGOBIN)/stringer":
13-
go install golang.org/x/tools/cmd/stringer@v0.12.0
14-
15-
"$(MYGOBIN)/addlicense":
16-
go install github.com/google/addlicense@v1.0.0
17-
18-
"$(MYGOBIN)/golangci-lint":
19-
go install github.com/golangci/golangci-lint/v2/cmd/golangci-lint@v2.7.2
20-
21-
"$(MYGOBIN)/deepcopy-gen":
22-
go install k8s.io/code-generator/cmd/deepcopy-gen@v0.25.2
23-
24-
"$(MYGOBIN)/ginkgo":
25-
go install github.com/onsi/ginkgo/v2/ginkgo@v2.2.0
26-
27-
"$(MYGOBIN)/mdrip":
28-
go install github.com/monopole/mdrip@v1.0.2
29-
30-
"$(MYGOBIN)/kind":
31-
go install sigs.k8s.io/kind@v0.16.0
32-
33-
# The following target intended for reference by a file in
34-
# https://github.com/kubernetes/test-infra/tree/master/config/jobs/kubernetes-sigs/cli-utils
35-
.PHONY: prow-presubmit-check
36-
prow-presubmit-check: \
37-
test lint verify-license
38-
39-
.PHONY: prow-presubmit-check-e2e
40-
prow-presubmit-check-e2e: \
41-
install-column-apt test-e2e verify-kapply-e2e
42-
43-
.PHONY: prow-presubmit-check-stress
44-
prow-presubmit-check-stress: \
45-
test-stress
12+
# Setting SHELL to bash allows bash commands to be executed by recipes.
13+
# Options are set to exit when a recipe line exits non-zero or a piped command fails.
14+
SHELL = /usr/bin/env bash -o pipefail
15+
.SHELLFLAGS = -ec
4616

47-
.PHONY: fix
48-
fix:
49-
go fix ./...
17+
.PHONY: all
18+
all: tidy vet fmt lint test
5019

5120
.PHONY: fmt
5221
fmt:
5322
go fmt ./...
5423

55-
# Install column (required by verify-kapply-e2e)
56-
# Update is included because the kubekins-e2e container build strips out the package cache.
57-
# In newer versions of debian, column is in the bsdextrautils package,
58-
# but in buster (used by kubekins-e2e) it's in bsdmainutils.
59-
.PHONY: install-column-apt
60-
install-column-apt:
61-
apt-get update
62-
apt-get install -y bsdmainutils
63-
64-
.PHONY: generate-deepcopy
65-
generate-deepcopy: "$(MYGOBIN)/deepcopy-gen"
66-
hack/run-in-gopath.sh deepcopy-gen --input-dirs ./pkg/apis/... -O zz_generated.deepcopy --go-header-file ./LICENSE_TEMPLATE_GO
67-
68-
.PHONY: generate
69-
generate: "$(MYGOBIN)/stringer" generate-deepcopy
70-
go generate ./...
71-
72-
.PHONY: license
73-
license: "$(MYGOBIN)/addlicense"
74-
"$(MYGOBIN)/addlicense" -v -y 2021 -c "The Kubernetes Authors." -f LICENSE_TEMPLATE .
75-
76-
.PHONY: verify-license
77-
verify-license: "$(MYGOBIN)/addlicense"
78-
"$(MYGOBIN)/addlicense" -check .
24+
.PHONY: lint
25+
lint: golangci-lint ## Run golangci linters and ESLint.
26+
$(GOLANGCI_LINT) run
7927

8028
.PHONY: tidy
8129
tidy:
8230
go mod tidy
8331

84-
.PHONY: lint
85-
lint: "$(MYGOBIN)/golangci-lint"
86-
"$(MYGOBIN)/golangci-lint" run ./...
87-
8832
.PHONY: test
8933
test:
90-
go test -race -cover ./cmd/... ./pkg/...
91-
92-
.PHONY: test-e2e
93-
test-e2e: "$(MYGOBIN)/ginkgo" "$(MYGOBIN)/kind"
94-
kind delete cluster --name=cli-utils-e2e && kind create cluster --name=cli-utils-e2e --wait 5m
95-
"$(MYGOBIN)/ginkgo" -v ./test/e2e/... -- -v 3
96-
97-
.PHONY: test-e2e-focus
98-
test-e2e-focus: "$(MYGOBIN)/ginkgo" "$(MYGOBIN)/kind"
99-
kind delete cluster --name=cli-utils-e2e && kind create cluster --name=cli-utils-e2e --wait 5m
100-
"$(MYGOBIN)"/ginkgo -v -focus ".*$(FOCUS).*" ./test/e2e/... -- -v 5
101-
102-
.PHONY: test-stress
103-
test-stress: "$(MYGOBIN)/ginkgo" "$(MYGOBIN)/kind"
104-
kind delete cluster --name=cli-utils-e2e && kind create cluster --name=cli-utils-e2e --wait 5m \
105-
--config=./test/stress/kind-cluster.yaml
106-
kubectl wait nodes --for=condition=ready --all --timeout=5m
107-
"$(MYGOBIN)/ginkgo" -v ./test/stress/... -- -v 3
34+
go test -race -cover ./pkg/...
10835

10936
.PHONY: vet
11037
vet:
11138
go vet ./...
11239

113-
.PHONY: build
114-
build:
115-
go build -o bin/kapply github.com/fluxcd/cli-utils/cmd;
116-
mv bin/kapply "$(MYGOBIN)"
117-
118-
.PHONY: build-with-race-detector
119-
build-with-race-detector:
120-
go build -race -o bin/kapply github.com/fluxcd/cli-utils/cmd;
121-
mv bin/kapply "$(MYGOBIN)"
122-
123-
.PHONY: verify-kapply-e2e
124-
verify-kapply-e2e: test-examples-e2e-kapply
125-
126-
.PHONY: test-examples-e2e-kapply
127-
test-examples-e2e-kapply: "$(MYGOBIN)/mdrip" "$(MYGOBIN)/kind"
128-
( \
129-
set -e; \
130-
/bin/rm -f bin/kapply; \
131-
/bin/rm -f "$(MYGOBIN)/kapply"; \
132-
echo "Installing kapply from ."; \
133-
make build-with-race-detector; \
134-
./hack/testExamplesE2EAgainstKapply.sh .; \
135-
)
136-
137-
.PHONY: nuke
138-
nuke:
139-
sudo rm -rf "$(GOPATH)/pkg/mod/sigs.k8s.io"
40+
##@ Dependencies
41+
42+
## Location to install dependencies to
43+
LOCALBIN ?= $(shell pwd)/bin
44+
$(LOCALBIN):
45+
mkdir -p $(LOCALBIN)
46+
47+
## Tool Binaries
48+
GOLANGCI_LINT = $(LOCALBIN)/golangci-lint-$(GOLANGCI_LINT_VERSION)
49+
GOVULNCHECK ?= $(LOCALBIN)/govulncheck
50+
51+
GOLANGCI_LINT_VERSION ?= v2.11.4
52+
.PHONY: golangci-lint
53+
golangci-lint: $(GOLANGCI_LINT) ## Download golangci-lint locally if necessary.
54+
$(GOLANGCI_LINT): $(LOCALBIN)
55+
$(call go-install-tool,$(GOLANGCI_LINT),github.com/golangci/golangci-lint/v2/cmd/golangci-lint,$(GOLANGCI_LINT_VERSION))
56+
57+
.PHONY: govulncheck
58+
govulncheck: $(GOVULNCHECK) ## Run govulncheck.
59+
$(GOVULNCHECK): $(LOCALBIN)
60+
$(call go-install-tool,$(GOVULNCHECK),golang.org/x/vuln/cmd/govulncheck,latest)
61+
@$(GOVULNCHECK) ./...
62+
63+
# go-install-tool will 'go install' any package with custom target and name of binary, if it doesn't exist
64+
# $1 - target path with name of binary (ideally with version)
65+
# $2 - package url which can be installed
66+
# $3 - specific version of package
67+
define go-install-tool
68+
@[ -f $(1) ] || { \
69+
set -e; \
70+
package=$(2)@$(3) ;\
71+
echo "Downloading $${package}" ;\
72+
GOBIN=$(LOCALBIN) go install $${package} ;\
73+
mv "$$(echo "$(1)" | sed "s/-$(3)$$//")" $(1) ;\
74+
}
75+
endef
76+
77+
##@ General
78+
79+
.PHONY: help
80+
help: ## Display this help.
81+
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)

0 commit comments

Comments
 (0)