blob: b46c24357a907210af3b9f2557af51956eeff2d3 [file] [log] [blame]
onkarkundargi72cfd362020-02-27 12:34:37 +05301#
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
17help:
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
28SHELL=bash -e -o pipefail
29
30# Variables
31VERSION ?= $(shell cat ./VERSION)
32
33DOCKER_LABEL_VCS_DIRTY = false
34ifneq ($(shell git ls-files --others --modified --exclude-standard 2>/dev/null | wc -l | sed -e 's/ //g'),0)
35 DOCKER_LABEL_VCS_DIRTY = true
36endif
37## Docker related
38DOCKER_EXTRA_ARGS ?=
39DOCKER_REGISTRY ?=
40DOCKER_REPOSITORY ?=
41DOCKER_TAG ?= ${VERSION}$(shell [[ ${DOCKER_LABEL_VCS_DIRTY} == "true" ]] && echo "-dirty" || true)
42DOCKER_IMAGENAME := ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}nem-ondemand-proxy
43
44## Docker labels. Only set ref and commit date if committed
45DOCKER_LABEL_VCS_URL ?= $(shell git remote get-url $(shell git remote))
46DOCKER_LABEL_VCS_REF = $(shell git rev-parse HEAD)
47DOCKER_LABEL_BUILD_DATE ?= $(shell date -u "+%Y-%m-%dT%H:%M:%SZ")
48DOCKER_LABEL_COMMIT_DATE = $(shell git show -s --format=%cd --date=iso-strict HEAD)
49
50DOCKER_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
60VOLTHA_TOOLS_VERSION ?= 2.0.0
61
62GO = 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
63GO_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
64GO_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
65GOCOVER_COBERTURA = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app -i voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-gocover-cobertura gocover-cobertura
66GOLANGCI_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
67PROTOC = 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
68PROTOC_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
70PROTO_FILES := $(sort $(wildcard protos/nem_ondemand_api/*.proto))
71
72## Local Development Helpers
73local-protos:
74ifdef 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
77endif
78
79protos:
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
88build: docker-build
89
90docker-build: local-protos protos
91 docker build $(DOCKER_BUILD_ARGS) -t ${DOCKER_IMAGENAME}:${DOCKER_TAG} -t ${DOCKER_IMAGENAME}:latest -f docker/Dockerfile .
92
93docker-push:
94 docker push ${DOCKER_IMAGENAME}:${DOCKER_TAG}
95
96lint-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
110lint: lint-mod
111
112sca: 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
120test: 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
128view-coverage:
129 go tool cover -html ./tests/results/go-test-coverage.out
130
131check: lint sca test
132
133clean:
134 rm -rf release sca-report
135
136mod-update:
137 ${GO} mod tidy
138 ${GO} mod vendor
139
140.PHONY: protos