Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 1 | DOCKER_NAME := voltha-go-controller |
| 2 | DOCKER_TAG := latest |
| 3 | GOLINT := golint |
| 4 | GOCMD = 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 |
| 5 | GOBUILD=$(GOCMD) build |
| 6 | GOCLEAN=$(GOCMD) clean |
| 7 | GOTEST=$(GOCMD) test |
| 8 | GOTOOL=$(GOCMD) tool |
| 9 | |
| 10 | # Static code analyzers |
| 11 | GOIMPORTS := goimports |
| 12 | GO_FILES := `find . -path vendor -prune -o -name '*.go'` |
| 13 | STATIC_TOOLS := $(GOLINT) $(GOFMT) $(GOIMPORTS) |
| 14 | #STATIC_TOOLS := $(GOFMT) |
Matteo Scandolo | 9061e43 | 2022-05-10 13:56:29 -0700 | [diff] [blame] | 15 | |
| 16 | # set default shell |
| 17 | SHELL = bash -e -o pipefail |
| 18 | |
| 19 | # Variables |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 20 | VERSION ?= $(shell head -n 1 ./VERSION) |
| 21 | # DOCKER_TAG ?= ${VERSION} |
| 22 | IMAGENAME ?= ${DOCKER_NAME}:${DOCKER_TAG} |
Matteo Scandolo | 9061e43 | 2022-05-10 13:56:29 -0700 | [diff] [blame] | 23 | |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 24 | COVERAGE_DIR = ./tests/results |
| 25 | COVERAGE_PROFILE = $(COVERAGE_DIR)/profile.out |
| 26 | TEST_TARGETS := test-default test-verbose test-short |
| 27 | test-short: ARGS=-short |
| 28 | test-verbose: ARGS=-v |
Matteo Scandolo | 9061e43 | 2022-05-10 13:56:29 -0700 | [diff] [blame] | 29 | # tool containers |
| 30 | VOLTHA_TOOLS_VERSION ?= 2.4.0 |
| 31 | |
Matteo Scandolo | 9061e43 | 2022-05-10 13:56:29 -0700 | [diff] [blame] | 32 | HADOLINT = 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 Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 33 | GOLANGCI_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 |
| 34 | GO_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 |
| 35 | GOCOVER_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 |
| 38 | local-protos: ## Copies a local version of the voltha-protos dependency into the vendor directory |
| 39 | ifdef 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 |
| 44 | endif |
| 45 | |
| 46 | local-lib-go: ## Copies a local version of the voltha-lib-go dependency into the vendor directory |
| 47 | ifdef 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/ |
| 50 | endif |
| 51 | |
| 52 | # This should to be the first and default target in this Makefile |
| 53 | help: ## 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 | |
| 61 | exe: |
| 62 | @echo Building voltha-go-controller.... |
| 63 | @cd voltha-go-controller && go build |
| 64 | |
| 65 | vgcctl: |
| 66 | @echo Building vgcctl.... |
| 67 | @cd voltha-go-controller/cli && go build -o vgcctl |
| 68 | |
| 69 | docker: exe vgcctl |
| 70 | @echo Building Docker $(DOCKER_NAME).... |
| 71 | sudo docker build -t $(IMAGENAME) -f docker/Dockerfile.voltha-go-controller . |
| 72 | |
| 73 | ## Docker targets |
| 74 | build: local-protos local-lib-go docker ## Build voltha-go-controller image |
| 75 | |
| 76 | build-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 | |
| 80 | sca: ## Runs static code analysis with the golangci-lint tool |
Tinoj Joseph | c2ccd6b | 2022-07-19 04:32:15 +0530 | [diff] [blame] | 81 | @rm -rf ./sca-report |
| 82 | @mkdir -p ./sca-report |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 83 | @echo "Running static code analysis..." |
Tinoj Joseph | c2ccd6b | 2022-07-19 04:32:15 +0530 | [diff] [blame] | 84 | @${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 Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 87 | |
| 88 | clean: ## 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 | |
| 92 | mock-gen: |
| 93 | mockery -dir intf/ -name DatabaseIntf -structname MockDb -filename MockDatabase.go |
Matteo Scandolo | 9061e43 | 2022-05-10 13:56:29 -0700 | [diff] [blame] | 94 | |
| 95 | ## lint and unit tests |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 96 | lint-dockerfile: ## Perform static analysis on Dockerfile |
| 97 | @echo "Running Dockerfile lint check ..." |
| 98 | @${HADOLINT} $$(find . -name "Dockerfile.*") |
| 99 | @echo "Dockerfile lint check OK" |
Matteo Scandolo | 9061e43 | 2022-05-10 13:56:29 -0700 | [diff] [blame] | 100 | |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 101 | lint-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 Scandolo | 9061e43 | 2022-05-10 13:56:29 -0700 | [diff] [blame] | 114 | |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 115 | lint: local-lib-go lint-mod lint-dockerfile ## Run all lint targets |
Matteo Scandolo | 9061e43 | 2022-05-10 13:56:29 -0700 | [diff] [blame] | 116 | |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 117 | mod-update: ## Update go mod files |
| 118 | ${GOCMD} mod tidy |
| 119 | ${GOCMD} mod vendor |
| 120 | |
| 121 | test: ## 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 |
| 128 | |