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 |
| 81 | @echo "Running static code analysis..." |
| 82 | sudo docker run --rm --user $$(id -u):$$(id -g) -v $(CURDIR):/go/src ciena/go-lint |
| 83 | |
| 84 | clean: ## 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 | |
| 88 | mock-gen: |
| 89 | mockery -dir intf/ -name DatabaseIntf -structname MockDb -filename MockDatabase.go |
Matteo Scandolo | 9061e43 | 2022-05-10 13:56:29 -0700 | [diff] [blame] | 90 | |
| 91 | ## lint and unit tests |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame^] | 92 | lint-dockerfile: ## Perform static analysis on Dockerfile |
| 93 | @echo "Running Dockerfile lint check ..." |
| 94 | @${HADOLINT} $$(find . -name "Dockerfile.*") |
| 95 | @echo "Dockerfile lint check OK" |
Matteo Scandolo | 9061e43 | 2022-05-10 13:56:29 -0700 | [diff] [blame] | 96 | |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame^] | 97 | lint-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 Scandolo | 9061e43 | 2022-05-10 13:56:29 -0700 | [diff] [blame] | 110 | |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame^] | 111 | lint: local-lib-go lint-mod lint-dockerfile ## Run all lint targets |
Matteo Scandolo | 9061e43 | 2022-05-10 13:56:29 -0700 | [diff] [blame] | 112 | |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame^] | 113 | mod-update: ## Update go mod files |
| 114 | ${GOCMD} mod tidy |
| 115 | ${GOCMD} mod vendor |
| 116 | |
| 117 | test: ## 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 | |