Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 1 | # |
| 2 | # Copyright 2016-present the original author or authors. |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | # |
| 16 | |
| 17 | # set default shell |
| 18 | SHELL = bash -e -o pipefail |
| 19 | |
| 20 | # Variables |
| 21 | VERSION ?= $(shell cat ./VERSION) |
| 22 | |
| 23 | DOCKER_LABEL_VCS_DIRTY = false |
| 24 | ifneq ($(shell git ls-files --others --modified --exclude-standard 2>/dev/null | wc -l | sed -e 's/ //g'),0) |
| 25 | DOCKER_LABEL_VCS_DIRTY = true |
| 26 | endif |
| 27 | ## Docker related |
| 28 | DOCKER_EXTRA_ARGS ?= |
| 29 | DOCKER_REGISTRY ?= |
| 30 | DOCKER_REPOSITORY ?= |
| 31 | DOCKER_TAG ?= ${VERSION}$(shell [[ ${DOCKER_LABEL_VCS_DIRTY} == "true" ]] && echo "-dirty" || true) |
| 32 | SIMULATEDONU_IMAGENAME := ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}voltha-adapter-simulated-onu |
Andrea Campanella | ce307c9 | 2019-12-17 15:36:38 -0800 | [diff] [blame] | 33 | TYPE ?= minimal |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 34 | |
| 35 | ## Docker labels. Only set ref and commit date if committed |
| 36 | DOCKER_LABEL_VCS_URL ?= $(shell git remote get-url $(shell git remote)) |
| 37 | DOCKER_LABEL_VCS_REF = $(shell git rev-parse HEAD) |
| 38 | DOCKER_LABEL_BUILD_DATE ?= $(shell date -u "+%Y-%m-%dT%H:%M:%SZ") |
| 39 | DOCKER_LABEL_COMMIT_DATE = $(shell git show -s --format=%cd --date=iso-strict HEAD) |
| 40 | |
Scott Baker | 70c1609 | 2019-09-30 17:08:54 -0700 | [diff] [blame] | 41 | # Default is GO111MODULE=auto, which will refuse to use go mod if running |
| 42 | # go less than 1.13.0 and this repository is checked out in GOPATH. For now, |
| 43 | # force module usage. This affects commands executed from this Makefile, but |
| 44 | # not the environment inside the Docker build (which does not build from |
| 45 | # inside a GOPATH). |
| 46 | export GO111MODULE=on |
| 47 | |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 48 | DOCKER_BUILD_ARGS ?= \ |
| 49 | ${DOCKER_EXTRA_ARGS} \ |
| 50 | --build-arg org_label_schema_version="${VERSION}" \ |
| 51 | --build-arg org_label_schema_vcs_url="${DOCKER_LABEL_VCS_URL}" \ |
| 52 | --build-arg org_label_schema_vcs_ref="${DOCKER_LABEL_VCS_REF}" \ |
| 53 | --build-arg org_label_schema_build_date="${DOCKER_LABEL_BUILD_DATE}" \ |
| 54 | --build-arg org_opencord_vcs_commit_date="${DOCKER_LABEL_COMMIT_DATE}" \ |
| 55 | --build-arg org_opencord_vcs_dirty="${DOCKER_LABEL_VCS_DIRTY}" |
| 56 | |
| 57 | DOCKER_BUILD_ARGS_LOCAL ?= ${DOCKER_BUILD_ARGS} \ |
| 58 | --build-arg LOCAL_PYVOLTHA=${LOCAL_PYVOLTHA} \ |
| 59 | --build-arg LOCAL_PROTOS=${LOCAL_PROTOS} |
| 60 | |
Matteo Scandolo | 18f5eb1 | 2020-04-17 10:34:25 -0700 | [diff] [blame] | 61 | VOLTHA_TOOLS_VERSION ?= 2.0.0 |
| 62 | GO = 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 |
| 63 | 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 |
| 64 | GOCOVER_COBERTURA = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app -i voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-gocover-cobertura gocover-cobertura |
| 65 | |
Scott Baker | 378d25c | 2019-10-25 15:04:37 -0700 | [diff] [blame] | 66 | .PHONY: simulated_onu local-protos local-lib-go |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 67 | |
| 68 | # This should to be the first and default target in this Makefile |
| 69 | help: |
| 70 | @echo "Usage: make [<target>]" |
| 71 | @echo "where available targets are:" |
| 72 | @echo |
| 73 | @echo "build : Build the docker images." |
| 74 | @echo " - If this is the first time you are building, choose 'make build' option." |
| 75 | @echo "simulated_onu : Build the simulated_onu docker image" |
| 76 | @echo "clean : Remove files created by the build and tests" |
| 77 | @echo "docker-push : Push the docker images to an external repository" |
| 78 | @echo "lint-dockerfile : Perform static analysis on Dockerfiles" |
| 79 | @echo "lint-style : Verify code is properly gofmt-ed" |
| 80 | @echo "lint-sanity : Verify that 'go vet' doesn't report any issues" |
Scott Baker | 70c1609 | 2019-09-30 17:08:54 -0700 | [diff] [blame] | 81 | @echo "lint-mod : Verify the integrity of the 'mod' files" |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 82 | @echo "lint : Shorthand for lint-style & lint-sanity" |
Scott Baker | 378d25c | 2019-10-25 15:04:37 -0700 | [diff] [blame] | 83 | @echo "local-lib-go : Copies a local version of the VOTLHA dependencies into the vendor directory" |
| 84 | @echo "local-protos : Copies a local verison of the VOLTHA protos into the vendor directory" |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 85 | @echo "test : Generate reports for all go tests" |
| 86 | @echo |
| 87 | |
| 88 | ## Local Development Helpers |
| 89 | local-protos: |
| 90 | ifdef LOCAL_PROTOS |
Scott Baker | 378d25c | 2019-10-25 15:04:37 -0700 | [diff] [blame] | 91 | rm -rf vendor/github.com/opencord/voltha-protos/go |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 92 | mkdir -p vendor/github.com/opencord/voltha-protos/go |
Scott Baker | 378d25c | 2019-10-25 15:04:37 -0700 | [diff] [blame] | 93 | cp -r ${LOCAL_PROTOS}/go/* vendor/github.com/opencord/voltha-protos/go |
| 94 | rm -rf vendor/github.com/opencord/voltha-protos/go/vendor |
| 95 | endif |
| 96 | |
| 97 | ## Local Development Helpers |
| 98 | local-lib-go: |
| 99 | ifdef LOCAL_LIB_GO |
| 100 | rm -rf vendor/github.com/opencord/voltha-lib-go |
| 101 | mkdir -p vendor/github.com/opencord/voltha-lib-go/v2/pkg |
| 102 | cp -r ${LOCAL_LIB_GO}/pkg/* vendor/github.com/opencord/voltha-lib-go/v2/pkg/ |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 103 | endif |
| 104 | |
| 105 | ## Docker targets |
| 106 | |
| 107 | build: docker-build |
| 108 | |
| 109 | docker-build: simulated_onu |
| 110 | |
Scott Baker | 378d25c | 2019-10-25 15:04:37 -0700 | [diff] [blame] | 111 | simulated_onu: local-protos local-lib-go |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 112 | docker build $(DOCKER_BUILD_ARGS) -t ${SIMULATEDONU_IMAGENAME}:${DOCKER_TAG} -t ${SIMULATEDONU_IMAGENAME}:latest -f docker/Dockerfile.simulated_onu . |
| 113 | |
| 114 | docker-push: |
| 115 | docker push ${SIMULATEDONU_IMAGENAME}:${DOCKER_TAG} |
| 116 | |
Andrea Campanella | ce307c9 | 2019-12-17 15:36:38 -0800 | [diff] [blame] | 117 | docker-kind-load: |
| 118 | @if [ "`kind get clusters | grep voltha-$(TYPE)`" = '' ]; then echo "no voltha-$(TYPE) cluster found" && exit 1; fi |
| 119 | kind load docker-image ${SIMULATEDONU_IMAGENAME}:${DOCKER_TAG} --name=voltha-$(TYPE) --nodes $(shell kubectl get nodes --template='{{range .items}}{{.metadata.name}},{{end}}' | sed 's/,$$//') |
| 120 | |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 121 | ## lint and unit tests |
| 122 | |
| 123 | PATH:=$(GOPATH)/bin:$(PATH) |
| 124 | HADOLINT=$(shell PATH=$(GOPATH):$(PATH) which hadolint) |
| 125 | lint-dockerfile: |
| 126 | ifeq (,$(shell PATH=$(GOPATH):$(PATH) which hadolint)) |
| 127 | mkdir -p $(GOPATH)/bin |
| 128 | curl -o $(GOPATH)/bin/hadolint -sNSL https://github.com/hadolint/hadolint/releases/download/v1.17.1/hadolint-$(shell uname -s)-$(shell uname -m) |
| 129 | chmod 755 $(GOPATH)/bin/hadolint |
| 130 | endif |
| 131 | @echo "Running Dockerfile lint check ..." |
| 132 | @hadolint $$(find . -name "Dockerfile.*") |
| 133 | @echo "Dockerfile lint check OK" |
| 134 | |
| 135 | lint-style: |
| 136 | ifeq (,$(shell which gofmt)) |
| 137 | go get -u github.com/golang/go/src/cmd/gofmt |
| 138 | endif |
| 139 | @echo "Running style check..." |
| 140 | @gofmt_out="$$(gofmt -l $$(find . -name '*.go' -not -path './vendor/*'))" ;\ |
| 141 | if [ ! -z "$$gofmt_out" ]; then \ |
| 142 | echo "$$gofmt_out" ;\ |
| 143 | echo "Style check failed on one or more files ^, run 'go fmt' to fix." ;\ |
| 144 | exit 1 ;\ |
| 145 | fi |
| 146 | @echo "Style check OK" |
| 147 | |
| 148 | lint-sanity: |
| 149 | @echo "Running sanity check..." |
Scott Baker | 70c1609 | 2019-09-30 17:08:54 -0700 | [diff] [blame] | 150 | @go vet -mod=vendor ./... |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 151 | @echo "Sanity check OK" |
| 152 | |
Scott Baker | 70c1609 | 2019-09-30 17:08:54 -0700 | [diff] [blame] | 153 | lint-mod: |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 154 | @echo "Running dependency check..." |
Scott Baker | 70c1609 | 2019-09-30 17:08:54 -0700 | [diff] [blame] | 155 | @go mod verify |
Scott Baker | af51569 | 2019-11-26 08:16:21 -0800 | [diff] [blame] | 156 | @echo "Dependency check OK. Running vendor check..." |
| 157 | @git status > /dev/null |
| 158 | @git diff-index --quiet HEAD -- go.mod go.sum vendor || (echo "ERROR: Staged or modified files must be committed before running this test" && echo "`git status`" && exit 1) |
| 159 | @[[ `git ls-files --exclude-standard --others go.mod go.sum vendor` == "" ]] || (echo "ERROR: Untracked files must be cleaned up before running this test" && echo "`git status`" && exit 1) |
| 160 | go mod tidy |
| 161 | go mod vendor |
| 162 | @git status > /dev/null |
| 163 | @git diff-index --quiet HEAD -- go.mod go.sum vendor || (echo "ERROR: Modified files detected after running go mod tidy / go mod vendor" && echo "`git status`" && exit 1) |
| 164 | @[[ `git ls-files --exclude-standard --others go.mod go.sum vendor` == "" ]] || (echo "ERROR: Untracked files detected after running go mod tidy / go mod vendor" && echo "`git status`" && exit 1) |
| 165 | @echo "Vendor check OK." |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 166 | |
Scott Baker | 70c1609 | 2019-09-30 17:08:54 -0700 | [diff] [blame] | 167 | lint: lint-style lint-sanity lint-mod lint-dockerfile |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 168 | |
| 169 | GO_JUNIT_REPORT:=$(shell which go-junit-report) |
| 170 | GOCOVER_COBERTURA:=$(shell which gocover-cobertura) |
| 171 | |
| 172 | test: |
| 173 | ifeq (,$(GO_JUNIT_REPORT)) |
| 174 | go get -u github.com/jstemmer/go-junit-report |
| 175 | @GO_JUNIT_REPORT=$(GOPATH)/bin/go-junit-report |
| 176 | endif |
| 177 | ifeq (,$(GOCOVER_COBERTURA)) |
| 178 | go get -u github.com/t-yuki/gocover-cobertura |
| 179 | @GOCOVER_COBERTURA=$(GOPATH)/bin/gocover-cobertura |
| 180 | endif |
| 181 | @mkdir -p ./tests/results |
Scott Baker | 70c1609 | 2019-09-30 17:08:54 -0700 | [diff] [blame] | 182 | @go test -mod=vendor -v -coverprofile ./tests/results/go-test-coverage.out -covermode count ./... 2>&1 | tee ./tests/results/go-test-results.out ;\ |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 183 | RETURN=$$? ;\ |
| 184 | $(GO_JUNIT_REPORT) < ./tests/results/go-test-results.out > ./tests/results/go-test-results.xml ;\ |
| 185 | $(GOCOVER_COBERTURA) < ./tests/results/go-test-coverage.out > ./tests/results/go-test-coverage.xml ;\ |
| 186 | exit $$RETURN |
| 187 | |
| 188 | clean: |
| 189 | |
| 190 | distclean: clean |
| 191 | |
Matteo Scandolo | 18f5eb1 | 2020-04-17 10:34:25 -0700 | [diff] [blame] | 192 | mod-update: |
| 193 | ${GO} mod tidy |
| 194 | ${GO} mod vendor |
| 195 | |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 196 | # end file |