1- # Copyright 2019 Iguazio
1+ # Copyright 2025 The v3io-go Authors.
22#
33# Licensed under the Apache License, Version 2.0 (the "License");
44# you may not use this file except in compliance with the License.
55# You may obtain a copy of the License at
66#
7- # http://www.apache.org/licenses/LICENSE-2.0
7+ # http://www.apache.org/licenses/LICENSE-2.0
88#
99# Unless required by applicable law or agreed to in writing, software
1010# distributed under the License is distributed on an "AS IS" BASIS,
1111# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212# See the License for the specific language governing permissions and
1313# limitations under the License.
14+
15+ GO_VERSION := $(shell go version | cut -d " " -f 3)
16+ GOPATH ?= $(shell go env GOPATH)
17+ SHELL := /bin/bash
18+
19+ # Get default os / arch from go env
20+ V3IO_DEFAULT_OS := $(shell go env GOOS)
21+ V3IO_DEFAULT_ARCH := $(shell go env GOARCH || echo amd64)
22+
23+ V3IO_OS := $(if $(V3IO_OS ) ,$(V3IO_OS ) ,$(V3IO_DEFAULT_OS ) )
24+ V3IO_ARCH := $(if $(V3IO_ARCH ) ,$(V3IO_ARCH ) ,$(V3IO_DEFAULT_ARCH ) )
25+
26+ # Version information
27+ V3IO_VERSION_GIT_COMMIT = $(shell git rev-parse HEAD)
28+ V3IO_PATH ?= $(shell pwd)
29+
30+ # Link flags
31+ GO_LINK_FLAGS ?= -s -w
32+
33+ # Go test timeout
34+ V3IO_GO_TEST_TIMEOUT ?= "30m"
35+
36+ #
37+ # Must be first target
38+ #
39+ .PHONY : all
40+ all :
41+ $(error "Please pick a target (run 'make targets' to view targets) " )
42+
43+ #
44+ # Linting and formatting
45+ #
46+ .PHONY : fmt
47+ fmt : ensure-golangci-linter
48+ gofmt -s -w .
49+ $(GOLANGCI_LINT_BIN ) run --fix
50+
51+ .PHONY : lint
52+ lint : modules ensure-test-files-annotated ensure-golangci-linter
53+ @echo Linting...
54+ $(GOLANGCI_LINT_BIN ) run -v
55+ @echo Done.
56+
57+ .PHONY : ensure-test-files-annotated
58+ ensure-test-files-annotated :
59+ $(eval test_files_missing_build_annotations=$(strip $(shell find . -type f -name '* _test.go' -exec bash -c "grep -m 1 -L '//go:build ' {} | grep go" \;) ) )
60+ @if [ -n " $( test_files_missing_build_annotations) " ]; then \
61+ echo " Found go test files without build annotations: " ; \
62+ echo $(test_files_missing_build_annotations ) ; \
63+ echo " !!! Go test files must be annotated with '//go:build test_<x>' !!!" ; \
64+ exit 1; \
65+ fi
66+ @echo " All go test files have //go:build test_X annotation"
67+ @exit $(.SHELLSTATUS )
68+
69+ GOLANGCI_LINT_VERSION := 2.3.0
70+ GOLANGCI_LINT_BIN := $(CURDIR ) /.bin/golangci-lint
71+ GOLANGCI_LINT_INSTALL_COMMAND := GOBIN=$(CURDIR ) /.bin go install github.com/golangci/golangci-lint/v2/cmd/golangci-lint@v$(GOLANGCI_LINT_VERSION )
72+
73+ .PHONY : ensure-golangci-linter
74+ ensure-golangci-linter :
75+ @if ! command -v $(GOLANGCI_LINT_BIN ) > /dev/null 2>&1 ; then \
76+ echo " golangci-lint not found. Installing..." ; \
77+ $(GOLANGCI_LINT_INSTALL_COMMAND ) ; \
78+ else \
79+ installed_version=$$($(GOLANGCI_LINT_BIN ) version | awk '/version/ {gsub(/^v/, "", $$4) ; print $$ 4}' ); \
80+ if [ " $$ installed_version" != " $( GOLANGCI_LINT_VERSION) " ]; then \
81+ echo " golangci-lint version mismatch ($$ installed_version != $( GOLANGCI_LINT_VERSION) ). Reinstalling..." ; \
82+ $(GOLANGCI_LINT_INSTALL_COMMAND ) ; \
83+ fi \
84+ fi
85+
86+ #
87+ # Environment validation
1488#
15- # To setup env locally on your dev box, run:
16- # $ export $(grep -v '^#' ./hack/test.env | xargs)
1789.PHONY : check-env
1890check-env :
1991ifndef V3IO_DATAPLANE_URL
@@ -39,45 +111,70 @@ ifndef V3IO_CONTROLPLANE_IGZ_ADMIN_PASSWORD
39111endif
40112 @echo "All required env vars populated"
41113
114+ #
115+ # Schema generation
116+ #
42117.PHONY : generate-capnp
43118generate-capnp :
44- pushd ./pkg/dataplane/schemas/; ./build; popd
119+ @echo " Generating Cap'n Proto schemas..."
120+ @cd ./pkg/dataplane/schemas/; ./build
121+ @echo " Schema generation complete."
45122
46- .PHONY : clean
47- clean :
48- pushd ./pkg/dataplane/schemas/; ./clean; popd
123+ .PHONY : clean-schemas
124+ clean-schemas :
125+ @echo " Cleaning generated schemas..."
126+ @cd ./pkg/dataplane/schemas/; ./clean
127+ @echo " Schema cleanup complete."
49128
50- .PHONY : fmt
51- fmt :
52- gofmt -s -w .
129+ #
130+ # Building
131+ #
132+ .PHONY : build-lib
133+ build-lib : modules
134+ @echo " Building v3io-go library..."
135+ @go build ./pkg/...
136+ @echo " Library build successful."
53137
54- .PHONY : lint
55- lint :
56- ./hack/lint/install.sh
57- ./hack/lint/run.sh
138+ .PHONY : build
139+ build : clean-schemas generate-capnp build-lib lint test
140+ @echo " Full build pipeline complete."
58141
59- .PHONY : test
60- test : check-env
61- go test -race -tags unit -count 1 ./...
142+ #
143+ # Testing
144+ #
145+ .PHONY : test-unit
146+ test-unit : modules ensure-gopath
147+ go test -race -tags=test_unit -v ./pkg/... -short
148+
149+ .PHONY : test-integration
150+ test-integration : modules ensure-gopath
151+ go test -race -tags=test_integration -v ./pkg/... --timeout $(V3IO_GO_TEST_TIMEOUT )
152+
153+ .PHONY : test-coverage
154+ test-coverage :
155+ go test -tags=test_unit -coverprofile=coverage.out ./pkg/... || go tool cover -html=coverage.out
62156
63157.PHONY : test-controlplane
64- test-controlplane : check-env
65- go test -test.v=true -race -tags unit -count 1 ./pkg/controlplane/...
158+ test-controlplane : modules ensure-gopath check-env
159+ go test -test.v=true -race -tags=test_unit -count= 1 ./pkg/controlplane/...
66160
67161.PHONY : test-dataplane
68- test-dataplane : check-env
69- go test -test.v=true -race -tags unit -count 1 ./pkg/dataplane/...
162+ test-dataplane : modules ensure-gopath check-env
163+ go test -test.v=true -race -tags=test_unit -count= 1 ./pkg/dataplane/...
70164
71165.PHONY : test-dataplane-simple
72- test-dataplane-simple : check-env
73- go test -test.v=true -tags unit -count 1 ./pkg/dataplane/...
166+ test-dataplane-simple : modules ensure-gopath check-env
167+ go test -test.v=true -tags=test_unit -count= 1 ./pkg/dataplane/...
74168
75169.PHONY : test-system
76170test-system : test-controlplane test-dataplane-simple
77171
172+ .PHONY : test
173+ test : test-unit
174+
78175.PHONY : build-test-container
79176build-test-container :
80- @echo Building test container...
177+ @echo " Building test container..."
81178 docker build \
82179 --file hack/test/docker/Dockerfile \
83180 --tag v3io-go-test:latest \
@@ -87,15 +184,50 @@ build-test-container:
87184test-system-in-docker : build-test-container
88185 @echo " Running system test in docker container..."
89186 docker run --rm \
90- --env V3IO_DATAPLANE_URL=" ${ V3IO_DATAPLANE_URL} " \
91- --env V3IO_DATAPLANE_USERNAME=" ${ V3IO_DATAPLANE_USERNAME} " \
92- --env V3IO_DATAPLANE_ACCESS_KEY=" ${ V3IO_DATAPLANE_ACCESS_KEY} " \
93- --env V3IO_CONTROLPLANE_URL=" ${ V3IO_CONTROLPLANE_URL} " \
94- --env V3IO_CONTROLPLANE_USERNAME=" ${ V3IO_CONTROLPLANE_USERNAME} " \
95- --env V3IO_CONTROLPLANE_PASSWORD=" ${ V3IO_CONTROLPLANE_PASSWORD} " \
96- --env V3IO_CONTROLPLANE_IGZ_ADMIN_PASSWORD=" ${ V3IO_CONTROLPLANE_IGZ_ADMIN_PASSWORD} " \
187+ --env V3IO_DATAPLANE_URL=" $( V3IO_DATAPLANE_URL) " \
188+ --env V3IO_DATAPLANE_USERNAME=" $( V3IO_DATAPLANE_USERNAME) " \
189+ --env V3IO_DATAPLANE_ACCESS_KEY=" $( V3IO_DATAPLANE_ACCESS_KEY) " \
190+ --env V3IO_CONTROLPLANE_URL=" $( V3IO_CONTROLPLANE_URL) " \
191+ --env V3IO_CONTROLPLANE_USERNAME=" $( V3IO_CONTROLPLANE_USERNAME) " \
192+ --env V3IO_CONTROLPLANE_PASSWORD=" $( V3IO_CONTROLPLANE_PASSWORD) " \
193+ --env V3IO_CONTROLPLANE_IGZ_ADMIN_PASSWORD=" $( V3IO_CONTROLPLANE_IGZ_ADMIN_PASSWORD) " \
97194 v3io-go-test:latest make test-system
98- @echo Done.
195+ @echo " Docker test complete. "
99196
100- .PHONY : build
101- build : clean generate-capnp lint test
197+ #
198+ # Go modules and environment
199+ #
200+ .PHONY : ensure-gopath
201+ ensure-gopath :
202+ ifndef GOPATH
203+ $(error GOPATH must be set)
204+ endif
205+
206+ .PHONY : modules
207+ modules : ensure-gopath
208+ @go mod download
209+
210+ .PHONY : tidy
211+ tidy :
212+ @go mod tidy
213+
214+ .PHONY : clean
215+ clean : clean-schemas
216+ @echo " Cleaning build artifacts..."
217+ @go clean -cache
218+ @rm -rf .bin/
219+ @rm -rf coverage.out
220+ @echo " Clean complete."
221+
222+ .PHONY : targets
223+ targets :
224+ @awk -F: ' /^[^ \t="]+:/ && !/PHONY/ {print $$1}' Makefile | sort -u
225+
226+ #
227+ # Versioning
228+ #
229+ .PHONY : version
230+ version :
231+ @echo " Git commit: $( V3IO_VERSION_GIT_COMMIT) "
232+ @echo " Go version: $( GO_VERSION) "
233+ @echo " OS/Arch: $( V3IO_OS) /$( V3IO_ARCH) "
0 commit comments