Matteo Scandolo | a428586 | 2020-12-01 18:10:10 -0800 | [diff] [blame] | 1 | # 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 |
| 17 | SHELL = bash -e -o pipefail |
| 18 | |
| 19 | # Variables |
| 20 | VERSION ?= $(shell cat ./VERSION) |
| 21 | |
| 22 | DOCKER_LABEL_VCS_DIRTY = false |
| 23 | ifneq ($(shell git ls-files --others --modified --exclude-standard 2>/dev/null | wc -l | sed -e 's/ //g'),0) |
| 24 | DOCKER_LABEL_VCS_DIRTY = true |
| 25 | endif |
| 26 | ## Docker related |
| 27 | DOCKER_EXTRA_ARGS ?= |
| 28 | DOCKER_REGISTRY ?= |
| 29 | DOCKER_REPOSITORY ?= |
| 30 | DOCKER_TAG ?= ${VERSION}$(shell [[ ${DOCKER_LABEL_VCS_DIRTY} == "true" ]] && echo "-dirty" || true) |
| 31 | IMAGENAME := ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}bbsim-sadis-server |
| 32 | TYPE ?= minimal |
| 33 | |
| 34 | ## Docker labels. Only set ref and commit date if committed |
| 35 | DOCKER_LABEL_VCS_URL ?= $(shell git remote get-url $(shell git remote)) |
| 36 | DOCKER_LABEL_VCS_REF = $(shell git rev-parse HEAD) |
| 37 | DOCKER_LABEL_BUILD_DATE ?= $(shell date -u "+%Y-%m-%dT%H:%M:%SZ") |
| 38 | DOCKER_LABEL_COMMIT_DATE = $(shell git show -s --format=%cd --date=iso-strict HEAD) |
| 39 | |
| 40 | DOCKER_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 | |
| 49 | DOCKER_BUILD_ARGS_LOCAL ?= ${DOCKER_BUILD_ARGS} \ |
| 50 | --build-arg LOCAL_PROTOS=${LOCAL_PROTOS} |
| 51 | |
| 52 | # tool containers |
| 53 | VOLTHA_TOOLS_VERSION ?= 2.3.1 |
| 54 | |
| 55 | 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 |
| 56 | 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 |
Matteo Scandolo | abf872d | 2020-12-14 08:22:06 -1000 | [diff] [blame^] | 57 | GOCOVER_COBERTURA = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app/src/github.com/opencord/bbsim-sadis-server -i voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-gocover-cobertura gocover-cobertura |
Matteo Scandolo | a428586 | 2020-12-01 18:10:10 -0800 | [diff] [blame] | 58 | 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 |
| 59 | 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 |
| 60 | |
| 61 | help: |
| 62 | @echo "TODO write the help" |
| 63 | |
| 64 | build: |
| 65 | @${GO} build -mod=vendor ./cmd/bbsim-sadis-server.go |
| 66 | |
| 67 | build-local: |
| 68 | @go build -mod=vendor ./cmd/bbsim-sadis-server.go |
| 69 | |
| 70 | ## Docker targets |
| 71 | docker-build: |
| 72 | docker build $(DOCKER_BUILD_ARGS) -t ${IMAGENAME}:${DOCKER_TAG} -f build/package/Dockerfile . |
| 73 | |
| 74 | docker-push: |
| 75 | docker push ${IMAGENAME}:${DOCKER_TAG} |
| 76 | |
| 77 | docker-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 | |
| 83 | lint-dockerfile: |
| 84 | @echo "Running Dockerfile lint check..." |
| 85 | @${HADOLINT} $$(find ./build -name "Dockerfile*") |
| 86 | @echo "Dockerfile lint check OK" |
| 87 | |
| 88 | lint-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 | |
| 103 | lint: lint-mod lint-dockerfile |
| 104 | |
| 105 | sca: |
| 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 | |
| 113 | test: |
| 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 | |
| 121 | distclean: |
| 122 | rm -rf ./sca-report |
| 123 | |
| 124 | mod-update: |
| 125 | ${GO} mod tidy |
| 126 | ${GO} mod vendor |