blob: 1d21cd94c17e21a5220fddd9ad1d751f505425ff [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
Tinoj Josephc2ccd6b2022-07-19 04:32:15 +053081 @rm -rf ./sca-report
82 @mkdir -p ./sca-report
Naveen Sampath04696f72022-06-13 15:19:14 +053083 @echo "Running static code analysis..."
Tinoj Josephc2ccd6b2022-07-19 04:32:15 +053084 @${GOLANGCI_LINT} run -vv --deadline=6m --out-format junit-xml ./... | tee ./sca-report/sca-report.xml
85 @echo ""
86 @echo "Static code analysis OK"
Naveen Sampath04696f72022-06-13 15:19:14 +053087
88clean: ## Removes any local filesystem artifacts generated by a build
89 rm -f voltha-go-controller/voltha-go-controller
90 rm -f voltha-go-controller/cli/vgcctl
91
92mock-gen:
93 mockery -dir intf/ -name DatabaseIntf -structname MockDb -filename MockDatabase.go
Matteo Scandolo9061e432022-05-10 13:56:29 -070094
95## lint and unit tests
Naveen Sampath04696f72022-06-13 15:19:14 +053096lint-dockerfile: ## Perform static analysis on Dockerfile
97 @echo "Running Dockerfile lint check ..."
98 @${HADOLINT} $$(find . -name "Dockerfile.*")
99 @echo "Dockerfile lint check OK"
Matteo Scandolo9061e432022-05-10 13:56:29 -0700100
Naveen Sampath04696f72022-06-13 15:19:14 +0530101lint-mod: ## Verify the Go dependencies
102 @echo "Running dependency check..."
103 @${GOCMD} mod verify
104 @echo "Dependency check OK. Running vendor check..."
105 @git status > /dev/null
106 @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)
107 @[[ `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)
108 ${GOCMD} mod tidy
109 ${GOCMD} mod vendor
110 @git status > /dev/null
111 @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)
112 @[[ `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)
113 @echo "Vendor check OK."
Matteo Scandolo9061e432022-05-10 13:56:29 -0700114
Naveen Sampath04696f72022-06-13 15:19:14 +0530115lint: local-lib-go lint-mod lint-dockerfile ## Run all lint targets
Matteo Scandolo9061e432022-05-10 13:56:29 -0700116
Naveen Sampath04696f72022-06-13 15:19:14 +0530117mod-update: ## Update go mod files
118 ${GOCMD} mod tidy
119 ${GOCMD} mod vendor
120
121test: ## Run unit tests
122 @mkdir -p ./tests/results
123 @${GOCMD} test -mod=vendor -v -coverprofile ./tests/results/go-test-coverage.out -covermode count ./... 2>&1 | tee ./tests/results/go-test-results.out ;\
124 RETURN=$$? ;\
125 ${GO_JUNIT_REPORT} < ./tests/results/go-test-results.out > ./tests/results/go-test-results.xml ;\
126 ${GOCOVER_COBERTURA} < ./tests/results/go-test-coverage.out > ./tests/results/go-test-coverage.xml ;\
127 exit $$RETURN