Girish Gowdra | 6450343 | 2020-01-07 10:59:10 +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 | # set default shell |
| 18 | SHELL = bash -e -o pipefail |
| 19 | |
| 20 | # Variables |
| 21 | VERSION ?= $(shell cat ./VERSION) |
| 22 | |
| 23 | DOCKER_EXTRA_ARGS ?= |
| 24 | DOCKER_REGISTRY ?= |
| 25 | DOCKER_REPOSITORY ?= |
| 26 | DOCKER_TAG ?= ${VERSION} |
| 27 | IMAGENAME := ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}openolt-scale-tester:${DOCKER_TAG} |
| 28 | |
| 29 | # Default is GO111MODULE=auto, which will refuse to use go mod if running |
| 30 | # go less than 1.13.0 and this repository is checked out in GOPATH. For now, |
| 31 | # force module usage. This affects commands executed from this Makefile, but |
| 32 | # not the environment inside the Docker build (which does not build from |
| 33 | # inside a GOPATH). |
| 34 | export GO111MODULE=on |
| 35 | |
| 36 | DOCKER_BUILD_ARGS ?= \ |
| 37 | ${DOCKER_EXTRA_ARGS} \ |
| 38 | |
| 39 | .PHONY: docker-build local-protos local-lib-go |
| 40 | |
| 41 | # This should to be the first and default target in this Makefile |
| 42 | help: |
| 43 | @echo "Usage: make [<target>]" |
| 44 | @echo "where available targets are:" |
| 45 | @echo "" |
| 46 | @echo "clean : Removes any local filesystem artifacts generated by a build" |
| 47 | @echo "distclean : Removes any local filesystem artifacts generated by a build or test run" |
| 48 | @echo "build : Build all openolt adapter artifacts" |
| 49 | @echo "docker-build : Build openolt adapter docker image" |
| 50 | @echo "docker-push : Push the docker images to an external repository" |
| 51 | @echo "help : Print this help" |
| 52 | @echo "lint : Run all lint targets" |
| 53 | @echo "lint-mod : Verify the Go dependencies" |
| 54 | @echo "lint-sanity : Run the Go language sanity tests (vet)" |
| 55 | @echo "lint-style : Verify the Go standard format of the source" |
| 56 | @echo "sca : Runs various SCA through golangci-lint tool" |
| 57 | @echo "test : Run unit tests, if any" |
| 58 | @echo |
| 59 | |
| 60 | ## Docker targets |
| 61 | |
| 62 | build: docker-build |
| 63 | |
| 64 | docker-build: local-protos local-lib-go |
Orhan Kupusoglu | 66b00d8 | 2020-03-13 12:06:33 +0300 | [diff] [blame^] | 65 | go mod vendor |
| 66 | go build -mod=vendor |
Girish Gowdra | 6450343 | 2020-01-07 10:59:10 +0530 | [diff] [blame] | 67 | docker build $(DOCKER_BUILD_ARGS) -t ${IMAGENAME} -f docker/Dockerfile . |
| 68 | |
| 69 | docker-push: |
| 70 | docker push ${IMAGENAME} |
| 71 | |
| 72 | lint-style: |
| 73 | ifeq (,$(shell which gofmt)) |
| 74 | go get -u github.com/golang/go/src/cmd/gofmt |
| 75 | endif |
| 76 | @echo "Running style check..." |
| 77 | @gofmt_out="$$(gofmt -l $$(find . -name '*.go' -not -path './vendor/*'))" ;\ |
| 78 | if [ ! -z "$$gofmt_out" ]; then \ |
| 79 | echo "$$gofmt_out" ;\ |
| 80 | echo "Style check failed on one or more files ^, run 'go fmt' to fix." ;\ |
| 81 | exit 1 ;\ |
| 82 | fi |
| 83 | @echo "Style check OK" |
| 84 | |
| 85 | lint-sanity: |
| 86 | @echo "Running sanity check..." |
| 87 | @go vet -mod=vendor ./... |
| 88 | @echo "Sanity check OK" |
| 89 | |
| 90 | lint-mod: |
| 91 | @echo "Running dependency check..." |
| 92 | @go mod verify |
| 93 | @echo "Dependency check OK. Running vendor check..." |
| 94 | @git status > /dev/null |
| 95 | @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) |
| 96 | @[[ `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) |
| 97 | go mod tidy |
| 98 | go mod vendor |
| 99 | @git status > /dev/null |
| 100 | @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) |
| 101 | @[[ `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) |
| 102 | @echo "Vendor check OK." |
| 103 | |
| 104 | lint: lint-style lint-sanity lint-mod |
| 105 | |
| 106 | __GOPATH=$(shell go env GOPATH) |
| 107 | |
| 108 | # Rules to automatically install golangci-lint |
| 109 | GOLANGCI_LINT_TOOL?=$(shell which golangci-lint) |
| 110 | ifeq (,$(GOLANGCI_LINT_TOOL)) |
| 111 | GOLANGCI_LINT_TOOL=$(__GOPATH)/bin/golangci-lint |
| 112 | golangci_lint_tool_install: |
| 113 | # Same version as installed by Jenkins ci-management |
| 114 | # Note that install using `go get` is not recommended as per https://github.com/golangci/golangci-lint |
| 115 | curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh| sh -s -- -b $(__GOPATH)/bin v1.17.0 |
| 116 | else |
| 117 | golangci_lint_tool_install: |
| 118 | endif |
| 119 | |
| 120 | # Rules to automatically install go-junit-report |
| 121 | GO_JUNIT_REPORT?=$(shell which go-junit-report) |
| 122 | ifeq (,$(GO_JUNIT_REPORT)) |
| 123 | GO_JUNIT_REPORT=$(__GOPATH)/bin/go-junit-report |
| 124 | go_junit_install: |
| 125 | go get -u github.com/jstemmer/go-junit-report |
| 126 | else |
| 127 | go_junit_install: |
| 128 | endif |
| 129 | |
| 130 | # Rules to automatically install gocover-covertura |
| 131 | GOCOVER_COBERTURA?=$(shell which gocover-cobertura) |
| 132 | ifeq (,$(GOCOVER_COBERTURA)) |
| 133 | @GOCOVER_COBERTURA=$(__GOPATH)/bin/gocover-cobertura |
| 134 | gocover_cobertura_install: |
| 135 | go get -u github.com/t-yuki/gocover-cobertura |
| 136 | else |
| 137 | gocover_cobertura_install: |
| 138 | endif |
| 139 | |
| 140 | test: go_junit_install gocover_cobertura_install |
| 141 | @mkdir -p ./tests/results |
| 142 | |
Orhan Kupusoglu | 66b00d8 | 2020-03-13 12:06:33 +0300 | [diff] [blame^] | 143 | go mod vendor |
| 144 | go build -mod=vendor |
| 145 | |
Girish Gowdra | 6450343 | 2020-01-07 10:59:10 +0530 | [diff] [blame] | 146 | @go test -mod=vendor -v -coverprofile ./tests/results/go-test-coverage.out -covermode count ./... 2>&1 | tee ./tests/results/go-test-results.out ;\ |
| 147 | RETURN=$$? ;\ |
| 148 | $(GO_JUNIT_REPORT) < ./tests/results/go-test-results.out > ./tests/results/go-test-results.xml ;\ |
| 149 | $(GOCOVER_COBERTURA) < ./tests/results/go-test-coverage.out > ./tests/results/go-test-coverage.xml ;\ |
| 150 | exit $$RETURN |
| 151 | |
| 152 | GOLANGCI_LINT_TOOL:=$(shell which golangci-lint) |
| 153 | sca: golangci_lint_tool_install |
| 154 | @mkdir -p ./sca-report |
| 155 | golangci-lint run --out-format junit-xml ./... 2>&1 | tee ./sca-report/sca-report.xml |
| 156 | |
| 157 | clean: |
| 158 | |
| 159 | distclean: clean |
| 160 | rm -rf ./sca_report |
| 161 | |
| 162 | # end file |