onkarkundargi | 72cfd36 | 2020-02-27 12:34:37 +0530 | [diff] [blame^] | 1 | # |
| 2 | # Copyright 2016 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 | help: |
| 18 | @echo "build - build the proxy" |
| 19 | @echo "lint-style - Verify code is properly gofmt-ed" |
| 20 | @echo "lint-sanity - Verify that 'go vet' doesn't report any issues" |
| 21 | @echo "lint-mod - Verify the integrity of the 'mod' files" |
| 22 | @echo "lint - run static code analysis" |
| 23 | @echo "sca - Runs various SCA through golangci-lint tool" |
| 24 | @echo "test - run unity tests" |
| 25 | @echo "check - runs targets that should be run before a commit" |
| 26 | @echo "clean - remove temporary and generated files" |
| 27 | |
| 28 | SHELL=bash -e -o pipefail |
| 29 | |
| 30 | # Variables |
| 31 | VERSION ?= $(shell cat ./VERSION) |
| 32 | |
| 33 | DOCKER_LABEL_VCS_DIRTY = false |
| 34 | ifneq ($(shell git ls-files --others --modified --exclude-standard 2>/dev/null | wc -l | sed -e 's/ //g'),0) |
| 35 | DOCKER_LABEL_VCS_DIRTY = true |
| 36 | endif |
| 37 | ## Docker related |
| 38 | DOCKER_EXTRA_ARGS ?= |
| 39 | DOCKER_REGISTRY ?= |
| 40 | DOCKER_REPOSITORY ?= |
| 41 | DOCKER_TAG ?= ${VERSION}$(shell [[ ${DOCKER_LABEL_VCS_DIRTY} == "true" ]] && echo "-dirty" || true) |
| 42 | DOCKER_IMAGENAME := ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}nem-ondemand-proxy |
| 43 | |
| 44 | ## Docker labels. Only set ref and commit date if committed |
| 45 | DOCKER_LABEL_VCS_URL ?= $(shell git remote get-url $(shell git remote)) |
| 46 | DOCKER_LABEL_VCS_REF = $(shell git rev-parse HEAD) |
| 47 | DOCKER_LABEL_BUILD_DATE ?= $(shell date -u "+%Y-%m-%dT%H:%M:%SZ") |
| 48 | DOCKER_LABEL_COMMIT_DATE = $(shell git show -s --format=%cd --date=iso-strict HEAD) |
| 49 | |
| 50 | DOCKER_BUILD_ARGS ?= \ |
| 51 | ${DOCKER_EXTRA_ARGS} \ |
| 52 | --build-arg org_label_schema_version="${VERSION}" \ |
| 53 | --build-arg org_label_schema_vcs_url="${DOCKER_LABEL_VCS_URL}" \ |
| 54 | --build-arg org_label_schema_vcs_ref="${DOCKER_LABEL_VCS_REF}" \ |
| 55 | --build-arg org_label_schema_build_date="${DOCKER_LABEL_BUILD_DATE}" \ |
| 56 | --build-arg org_opencord_vcs_commit_date="${DOCKER_LABEL_COMMIT_DATE}" \ |
| 57 | --build-arg org_opencord_vcs_dirty="${DOCKER_LABEL_VCS_DIRTY}" |
| 58 | |
| 59 | # tool containers |
| 60 | VOLTHA_TOOLS_VERSION ?= 2.0.0 |
| 61 | |
| 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_SH = 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 sh -c |
| 64 | GO_JUNIT_REPORT = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/appecho -i voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-go-junit-report go-junit-report |
| 65 | 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 |
| 66 | 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 -e GOGC=10 voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-golangci-lint golangci-lint |
| 67 | PROTOC = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app $(shell test -t 0 && echo "-it") voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-protoc protoc |
| 68 | PROTOC_SH = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/go/src/github.com/opencord/nem-ondemand-proxy $(shell test -t 0 && echo "-it") --workdir=/go/src/github.com/opencord/nem-ondemand-proxy voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-protoc sh -c |
| 69 | |
| 70 | PROTO_FILES := $(sort $(wildcard protos/nem_ondemand_api/*.proto)) |
| 71 | |
| 72 | ## Local Development Helpers |
| 73 | local-protos: |
| 74 | ifdef LOCAL_PROTOS |
| 75 | mkdir -p vendor/github.com/opencord/voltha-protos/v3/go |
| 76 | cp -r ${LOCAL_PROTOS}/go/* vendor/github.com/opencord/voltha-protos/v3/go |
| 77 | endif |
| 78 | |
| 79 | protos: |
| 80 | @echo "Creating *.go.pb files" |
| 81 | @${PROTOC_SH} " \ |
| 82 | set -e -o pipefail; \ |
| 83 | for x in ${PROTO_FILES}; do \ |
| 84 | echo \$$x; \ |
| 85 | protoc --go_out=plugins=grpc:/go/src -I protos \$$x; \ |
| 86 | done" |
| 87 | |
| 88 | build: docker-build |
| 89 | |
| 90 | docker-build: local-protos protos |
| 91 | docker build $(DOCKER_BUILD_ARGS) -t ${DOCKER_IMAGENAME}:${DOCKER_TAG} -t ${DOCKER_IMAGENAME}:latest -f docker/Dockerfile . |
| 92 | |
| 93 | docker-push: |
| 94 | docker push ${DOCKER_IMAGENAME}:${DOCKER_TAG} |
| 95 | |
| 96 | lint-mod: |
| 97 | @echo "Running dependency check..." |
| 98 | @${GO} mod verify |
| 99 | @echo "Dependency check OK. Running vendor check..." |
| 100 | @git status > /dev/null |
| 101 | @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) |
| 102 | @[[ `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) |
| 103 | ${GO} mod tidy |
| 104 | ${GO} mod vendor |
| 105 | @git status > /dev/null |
| 106 | @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) |
| 107 | @[[ `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) |
| 108 | @echo "Vendor check OK." |
| 109 | |
| 110 | lint: lint-mod |
| 111 | |
| 112 | sca: protos |
| 113 | @rm -rf ./sca-report |
| 114 | @mkdir -p ./sca-report |
| 115 | @echo "Running static code analysis..." |
| 116 | @${GOLANGCI_LINT} run --deadline=20m --out-format junit-xml ./... | tee ./sca-report/sca-report.xml |
| 117 | @echo "" |
| 118 | @echo "Static code analysis OK" |
| 119 | |
| 120 | test: protos |
| 121 | @mkdir -p ./tests/results |
| 122 | @${GO} test -mod=vendor -v -coverprofile ./tests/results/go-test-coverage.out -covermode count ./... 2>&1 | tee ./tests/results/go-test-results.out ;\ |
| 123 | RETURN=$$? ;\ |
| 124 | ${GO_JUNIT_REPORT} < ./tests/results/go-test-results.out > ./tests/results/go-test-results.xml ;\ |
| 125 | ${GOCOVER_COBERTURA} < ./tests/results/go-test-coverage.out > ./tests/results/go-test-coverage.xml ;\ |
| 126 | exit $$RETURN |
| 127 | |
| 128 | view-coverage: |
| 129 | go tool cover -html ./tests/results/go-test-coverage.out |
| 130 | |
| 131 | check: lint sca test |
| 132 | |
| 133 | clean: |
| 134 | rm -rf release sca-report |
| 135 | |
| 136 | mod-update: |
| 137 | ${GO} mod tidy |
| 138 | ${GO} mod vendor |
| 139 | |
| 140 | .PHONY: protos |