blob: c79c276ce087d57ff7926d97a4958252611b1e86 [file] [log] [blame]
Naveen Sampath04696f72022-06-13 15:19:14 +05301DOCKER_NAME := voltha-go-controller
2DOCKER_TAG := latest
3GOLINT := golint
4GOCMD = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app $(shell test -t 0 && echo "-it") -v gocache:/.cache -v gocache-${VOLTHA_TOOLS_VERSION}:/go/pkg voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-golang go
5GOBUILD=$(GOCMD) build
6GOCLEAN=$(GOCMD) clean
7GOTEST=$(GOCMD) test
8GOTOOL=$(GOCMD) tool
9
10# Static code analyzers
11GOIMPORTS := goimports
12GO_FILES := `find . -path vendor -prune -o -name '*.go'`
13STATIC_TOOLS := $(GOLINT) $(GOFMT) $(GOIMPORTS)
14#STATIC_TOOLS := $(GOFMT)
Matteo Scandolo9061e432022-05-10 13:56:29 -070015
16# set default shell
17SHELL = bash -e -o pipefail
18
19# Variables
Naveen Sampath04696f72022-06-13 15:19:14 +053020VERSION ?= $(shell head -n 1 ./VERSION)
21# DOCKER_TAG ?= ${VERSION}
22IMAGENAME ?= ${DOCKER_NAME}:${DOCKER_TAG}
Matteo Scandolo9061e432022-05-10 13:56:29 -070023
Naveen Sampath04696f72022-06-13 15:19:14 +053024COVERAGE_DIR = ./tests/results
25COVERAGE_PROFILE = $(COVERAGE_DIR)/profile.out
26TEST_TARGETS := test-default test-verbose test-short
27test-short: ARGS=-short
28test-verbose: ARGS=-v
Matteo Scandolo9061e432022-05-10 13:56:29 -070029# tool containers
30VOLTHA_TOOLS_VERSION ?= 2.4.0
31
Matteo Scandolo9061e432022-05-10 13:56:29 -070032HADOLINT = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app $(shell test -t 0 && echo "-it") voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-hadolint hadolint
Naveen Sampath04696f72022-06-13 15:19:14 +053033GOLANGCI_LINT = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app $(shell test -t 0 && echo "-it") -v gocache:/.cache -v gocache-${VOLTHA_TOOLS_VERSION}:/go/pkg voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-golangci-lint golangci-lint
34GO_JUNIT_REPORT = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app -i voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-go-junit-report go-junit-report
35GOCOVER_COBERTURA = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app/src/github.com/opencord/voltha-openolt-adapter -i voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-gocover-cobertura gocover-cobertura
36
37## Local Development Helpers
38local-protos: ## Copies a local version of the voltha-protos dependency into the vendor directory
39ifdef LOCAL_PROTOS
40 rm -rf vendor/github.com/opencord/voltha-protos/v5/go
41 mkdir -p vendor/github.com/opencord/voltha-protos/v5/go
42 cp -r ${LOCAL_PROTOS}/go/* vendor/github.com/opencord/voltha-protos/v5/go
43 rm -rf vendor/github.com/opencord/voltha-protos/v5/go/vendor
44endif
45
46local-lib-go: ## Copies a local version of the voltha-lib-go dependency into the vendor directory
47ifdef LOCAL_LIB_GO
48 mkdir -p vendor/github.com/opencord/voltha-lib-go/v7/pkg
49 cp -r ${LOCAL_LIB_GO}/pkg/* vendor/github.com/opencord/voltha-lib-go/v7/pkg/
50endif
51
52# This should to be the first and default target in this Makefile
53help: ## Print help for each Makefile target
54 @echo "Usage: make [<target>]"
55 @echo "where available targets are:"
56 @echo
57 @grep '^[[:alpha:]_-]*:.* ##' $(MAKEFILE_LIST) \
58 | sort | awk 'BEGIN {FS=":.* ## "}; {printf "%-25s : %s\n", $$1, $$2};'
59
60
61exe:
62 @echo Building voltha-go-controller....
63 @cd voltha-go-controller && go build
64
65vgcctl:
66 @echo Building vgcctl....
67 @cd voltha-go-controller/cli && go build -o vgcctl
68
69docker: exe vgcctl
70 @echo Building Docker $(DOCKER_NAME)....
71 sudo docker build -t $(IMAGENAME) -f docker/Dockerfile.voltha-go-controller .
72
73## Docker targets
74build: local-protos local-lib-go docker ## Build voltha-go-controller image
75
76build-docker-profile: sca exe-profile vgcctl
77 @echo Building Docker $(DOCKER_NAME)....
78 sudo docker build -t $(IMAGENAME)-profile -f docker/Dockerfile.voltha-go-controller .
79
80sca: ## Runs static code analysis with the golangci-lint tool
81 @echo "Running static code analysis..."
82 sudo docker run --rm --user $$(id -u):$$(id -g) -v $(CURDIR):/go/src ciena/go-lint
83
84clean: ## Removes any local filesystem artifacts generated by a build
85 rm -f voltha-go-controller/voltha-go-controller
86 rm -f voltha-go-controller/cli/vgcctl
87
88mock-gen:
89 mockery -dir intf/ -name DatabaseIntf -structname MockDb -filename MockDatabase.go
Matteo Scandolo9061e432022-05-10 13:56:29 -070090
91## lint and unit tests
Naveen Sampath04696f72022-06-13 15:19:14 +053092lint-dockerfile: ## Perform static analysis on Dockerfile
93 @echo "Running Dockerfile lint check ..."
94 @${HADOLINT} $$(find . -name "Dockerfile.*")
95 @echo "Dockerfile lint check OK"
Matteo Scandolo9061e432022-05-10 13:56:29 -070096
Naveen Sampath04696f72022-06-13 15:19:14 +053097lint-mod: ## Verify the Go dependencies
98 @echo "Running dependency check..."
99 @${GOCMD} mod verify
100 @echo "Dependency check OK. Running vendor check..."
101 @git status > /dev/null
102 @git diff-index --quiet HEAD -- go.mod go.sum vendor || (echo "ERROR: Staged or modified files must be committed before running this test" && git status -- go.mod go.sum vendor && exit 1)
103 @[[ `git ls-files --exclude-standard --others go.mod go.sum vendor` == "" ]] || (echo "ERROR: Untracked files must be cleaned up before running this test" && git status -- go.mod go.sum vendor && exit 1)
104 ${GOCMD} mod tidy
105 ${GOCMD} mod vendor
106 @git status > /dev/null
107 @git diff-index --quiet HEAD -- go.mod go.sum vendor || (echo "ERROR: Modified files detected after running go mod tidy / go mod vendor" && git status -- go.mod go.sum vendor && git checkout -- go.mod go.sum vendor && exit 1)
108 @[[ `git ls-files --exclude-standard --others go.mod go.sum vendor` == "" ]] || (echo "ERROR: Untracked files detected after running go mod tidy / go mod vendor" && git status -- go.mod go.sum vendor && git checkout -- go.mod go.sum vendor && exit 1)
109 @echo "Vendor check OK."
Matteo Scandolo9061e432022-05-10 13:56:29 -0700110
Naveen Sampath04696f72022-06-13 15:19:14 +0530111lint: local-lib-go lint-mod lint-dockerfile ## Run all lint targets
Matteo Scandolo9061e432022-05-10 13:56:29 -0700112
Naveen Sampath04696f72022-06-13 15:19:14 +0530113mod-update: ## Update go mod files
114 ${GOCMD} mod tidy
115 ${GOCMD} mod vendor
116
117test: ## Run unit tests
118 @mkdir -p ./tests/results
119 @${GOCMD} test -mod=vendor -v -coverprofile ./tests/results/go-test-coverage.out -covermode count ./... 2>&1 | tee ./tests/results/go-test-results.out ;\
120 RETURN=$$? ;\
121 ${GO_JUNIT_REPORT} < ./tests/results/go-test-results.out > ./tests/results/go-test-results.xml ;\
122 ${GOCOVER_COBERTURA} < ./tests/results/go-test-coverage.out > ./tests/results/go-test-coverage.xml ;\
123 exit $$RETURN
124