blob: 4f3c7d68b0f15f840b466d699ee3f30ea75b3011 [file] [log] [blame]
Matteo Scandoloa4285862020-12-01 18:10:10 -08001# Copyright 2020-present Open Networking Foundation
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15
16# set default shell
17SHELL = bash -e -o pipefail
18
19# Variables
20VERSION ?= $(shell cat ./VERSION)
21
22DOCKER_LABEL_VCS_DIRTY = false
23ifneq ($(shell git ls-files --others --modified --exclude-standard 2>/dev/null | wc -l | sed -e 's/ //g'),0)
24 DOCKER_LABEL_VCS_DIRTY = true
25endif
26## Docker related
27DOCKER_EXTRA_ARGS ?=
28DOCKER_REGISTRY ?=
29DOCKER_REPOSITORY ?=
30DOCKER_TAG ?= ${VERSION}$(shell [[ ${DOCKER_LABEL_VCS_DIRTY} == "true" ]] && echo "-dirty" || true)
31IMAGENAME := ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}bbsim-sadis-server
32TYPE ?= minimal
33
34## Docker labels. Only set ref and commit date if committed
35DOCKER_LABEL_VCS_URL ?= $(shell git remote get-url $(shell git remote))
36DOCKER_LABEL_VCS_REF = $(shell git rev-parse HEAD)
37DOCKER_LABEL_BUILD_DATE ?= $(shell date -u "+%Y-%m-%dT%H:%M:%SZ")
38DOCKER_LABEL_COMMIT_DATE = $(shell git show -s --format=%cd --date=iso-strict HEAD)
39
40DOCKER_BUILD_ARGS ?= \
41 ${DOCKER_EXTRA_ARGS} \
42 --build-arg org_label_schema_version="${VERSION}" \
43 --build-arg org_label_schema_vcs_url="${DOCKER_LABEL_VCS_URL}" \
44 --build-arg org_label_schema_vcs_ref="${DOCKER_LABEL_VCS_REF}" \
45 --build-arg org_label_schema_build_date="${DOCKER_LABEL_BUILD_DATE}" \
46 --build-arg org_opencord_vcs_commit_date="${DOCKER_LABEL_COMMIT_DATE}" \
47 --build-arg org_opencord_vcs_dirty="${DOCKER_LABEL_VCS_DIRTY}"
48
49DOCKER_BUILD_ARGS_LOCAL ?= ${DOCKER_BUILD_ARGS} \
50 --build-arg LOCAL_PROTOS=${LOCAL_PROTOS}
51
52# tool containers
53VOLTHA_TOOLS_VERSION ?= 2.3.1
54
55GO = 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
56GO_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
57GOCOVER_COBERTURA = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app/src/github.com/opencord/voltha-go -i voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-gocover-cobertura gocover-cobertura
58GOLANGCI_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
59HADOLINT = 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
60
61help:
62 @echo "TODO write the help"
63
64build:
65 @${GO} build -mod=vendor ./cmd/bbsim-sadis-server.go
66
67build-local:
68 @go build -mod=vendor ./cmd/bbsim-sadis-server.go
69
70## Docker targets
71docker-build:
72 docker build $(DOCKER_BUILD_ARGS) -t ${IMAGENAME}:${DOCKER_TAG} -f build/package/Dockerfile .
73
74docker-push:
75 docker push ${IMAGENAME}:${DOCKER_TAG}
76
77docker-kind-load:
78 @if [ "`kind get clusters | grep voltha-$(TYPE)`" = '' ]; then echo "no voltha-$(TYPE) cluster found" && exit 1; fi
79 kind load docker-image ${IMAGENAME}:${DOCKER_TAG} --name=voltha-$(TYPE) --nodes $(shell kubectl get nodes --template='{{range .items}}{{.metadata.name}},{{end}}' | rev | cut -c 2- | rev)
80
81## lint and unit tests
82
83lint-dockerfile:
84 @echo "Running Dockerfile lint check..."
85 @${HADOLINT} $$(find ./build -name "Dockerfile*")
86 @echo "Dockerfile lint check OK"
87
88lint-mod:
89 @echo "Running dependency check..."
90 @${GO} mod verify
91 @echo "Dependency check OK. Running vendor check..."
92 @git status > /dev/null
93 @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)
94 @[[ `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)
95 ${GO} mod tidy
96 ${GO} mod vendor
97 @git status > /dev/null
98 @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)
99 @[[ `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)
100 @echo "Vendor check OK."
101
102
103lint: lint-mod lint-dockerfile
104
105sca:
106 @rm -rf ./sca-report
107 @mkdir -p ./sca-report
108 @echo "Running static code analysis..."
109 @${GOLANGCI_LINT} run --deadline=6m --out-format junit-xml ./... | tee ./sca-report/sca-report.xml
110 @echo ""
111 @echo "Static code analysis OK"
112
113test:
114 @mkdir -p ./tests/results
115 @${GO} test -mod=vendor -v -coverprofile ./tests/results/go-test-coverage.out -covermode count ./... 2>&1 | tee ./tests/results/go-test-results.out ;\
116 RETURN=$$? ;\
117 ${GO_JUNIT_REPORT} < ./tests/results/go-test-results.out > ./tests/results/go-test-results.xml ;\
118 ${GOCOVER_COBERTURA} < ./tests/results/go-test-coverage.out > ./tests/results/go-test-coverage.xml ;\
119 exit $$RETURN
120
121distclean:
122 rm -rf ./sca-report
123
124mod-update:
125 ${GO} mod tidy
126 ${GO} mod vendor