-
Notifications
You must be signed in to change notification settings - Fork 28
/
Makefile
259 lines (202 loc) · 11.4 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# Copyright 2018 The Service Manager Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
all: build test-unit ## Default target that builds SM and runs unit-tests
GO ?= go
GOFMT ?= gofmt
BINDIR ?= bin
PROJECT_PKG ?= github.com/Peripli/service-manager
TEST_PROFILE_OUT= cover.out
PLATFORM ?= linux
ARCH ?= amd64
INT_TEST_PROFILE ?= $(CURDIR)/profile-int.cov
UNIT_TEST_PROFILE ?= $(CURDIR)/profile-unit.cov
INT_BROKER_TEST_PROFILE ?= $(CURDIR)/profile-int-broker.cov
INT_OSB_AND_PLUGIN_TEST_PROFILE ?= $(CURDIR)/profile-int-osb-and-plugin.cov
INT_SERVICE_INSTANCE_AND_BINDINGS_TEST_PROFILE ?= $(CURDIR)/profile-int-service-instance-and-bindings.cov
INT_OTHER_TEST_PROFILE ?= $(CURDIR)/profile-int-other.cov
TEST_PROFILE ?= $(CURDIR)/profile.cov
COVERAGE ?= $(CURDIR)/coverage.html
VERSION ?= $(shell git describe --tags --always --dirty)
DATE ?= $(shell date -u '+%Y-%m-%d-%H%M UTC')
VERSION_FLAGS ?= -X "main.Version=$(VERSION)" -X "main.BuildTime=$(DATE)"
# .go files - excludes fakes, mocks, generated files, etc...
SOURCE_FILES = $(shell find . -type f -name '*.go' ! -name '*.gen.go' ! -name '*.pb.go' ! -name '*mock*.go' \
! -name '*fake*.go' ! -path "./vendor/*" ! -path "./pkg/query/parser/*" ! -path "*/*fakes*/*" \
-exec grep -vrli 'Code generated by counterfeiter' {} \;)
# .go files with go:generate directives (currently files that contain interfaces for which counterfeiter fakes are generated)
GENERATE_PREREQ_FILES = $(shell find . -name "*.go" ! -path "./vendor/*" -exec grep "go:generate" -rli {} \;)
# GO_FLAGS - extra "go build" flags to use - e.g. -v (for verbose)
GO_BUILD = env CGO_ENABLED=0 GOOS=$(PLATFORM) GOARCH=$(ARCH) \
$(GO) build $(GO_FLAGS) -ldflags '-s -w $(BUILD_LDFLAGS) $(VERSION_FLAGS)'
# TEST_FLAGS - extra "go test" flags to use
GO_INT_TEST = $(GO) test -p 1 -timeout 30m -race -coverpkg $(shell go list ./... | egrep -v "fakes|test|cmd|parser" | paste -sd "," -) \
./test/... $(TEST_FLAGS) -coverprofile=$(INT_TEST_PROFILE)
GO_INT_TEST_OTHER = $(GO) test -p 1 -timeout 30m -race -coverpkg $(shell go list ./... | egrep -v "fakes|test|cmd|parser" | paste -sd "," -) \
$(shell go list ./test/... | egrep -v "broker_test|osb_and_plugin_test|service_instance_and_binding_test") $(TEST_FLAGS) -coverprofile=$(INT_OTHER_TEST_PROFILE)
GO_INT_TEST_BROKER = $(GO) test -p 1 -timeout 30m -race -coverpkg $(shell go list ./... | egrep -v "fakes|test|cmd|parser" | paste -sd "," -) \
./test/broker_test/... $(TEST_FLAGS) -coverprofile=$(INT_BROKER_TEST_PROFILE)
GO_INT_TEST_OSB_AND_PLUGIN = $(GO) test -p 1 -timeout 30m -race -coverpkg $(shell go list ./... | egrep -v "fakes|test|cmd|parser" | paste -sd "," -) \
./test/osb_and_plugin_test/... $(TEST_FLAGS) -coverprofile=$(INT_OSB_AND_PLUGIN_TEST_PROFILE)
GO_INT_TEST_SERVICE_INSTANCE_AND_BINDING = $(GO) test -p 1 -timeout 30m -race -coverpkg $(shell go list ./... | egrep -v "fakes|test|cmd|parser" | paste -sd "," -) \
./test/service_instance_and_binding_test/... $(TEST_FLAGS) -coverprofile=$(INT_SERVICE_INSTANCE_AND_BINDINGS_TEST_PROFILE)
GO_UNIT_TEST = $(GO) test -p 1 -race -coverpkg $(shell go list ./... | egrep -v "fakes|test|cmd|parser" | paste -sd "," -) \
$(shell go list ./... | egrep -v "test") -coverprofile=$(UNIT_TEST_PROFILE)
COUNTERFEITER ?= "v6.0.2"
#-----------------------------------------------------------------------------
# Prepare environment to be able to run other make targets
#-----------------------------------------------------------------------------
prepare-counterfeiter:
@echo "Installing counterfeiter $(COUNTERFEITER)..."
@GO111MODULE=off go get -u github.com/maxbrunsfeld/counterfeiter
@chmod a+x $(GOPATH)/bin/counterfeiter
## Installs some tools (gometalinter, cover, goveralls)
prepare: prepare-counterfeiter build-gen-binary
ifeq ($(shell which gometalinter),)
@echo "Installing gometalinter ...";\
cd $(GOPATH)/src;\
GO111MODULE=off go get -u github.com/alecthomas/gometalinter;\
GO111MODULE=off cd $(GOPATH)/src/github.com/alecthomas/gometalinter;\
GO111MODULE=off go install;\
GO111MODULE=off gometalinter -i -u
endif
ifeq ($(shell which cover),)
@echo "Installing cover tool..."
@go get -u golang.org/x/tools/cmd/cover
endif
ifeq ($(shell which goveralls),)
@echo "Installing goveralls..."
@go get github.com/mattn/goveralls
endif
ifeq ($(shell which golint),)
@echo "Installing golint... "
@go get -u golang.org/x/lint/golint
endif
#-----------------------------------------------------------------------------
# Builds and dependency management
#-----------------------------------------------------------------------------
build: .init gomod-vendor service-manager ## Downloads vendored dependecies and builds the service-manager binary
gomod-vendor:
@go mod vendor
service-manager: $(BINDIR)/service-manager
# Build serivce-manager under ./bin/service-manager
$(BINDIR)/service-manager: FORCE | .init
$(GO_BUILD) -o $@ $(PROJECT_PKG)
# init creates the bin dir
.init: $(BINDIR)
# Force can be used as a prerequisite to a target and this will cause this target to always run
FORCE:
$(BINDIR):
mkdir -p $@
clean-bin: ## Cleans up the binaries
@echo Deleting $(CURDIR)/$(BINDIR) and built binaries...
@rm -rf $(BINDIR)
clean-vendor: ## Cleans up the vendor folder and clears out the go.mod
@echo Deleting vendor folder...
@rm -rf vendor
@echo > go.sum
build-gen-binary:
@go install github.com/Peripli/service-manager/cmd/smgen
#-----------------------------------------------------------------------------
# Tests and coverage
#-----------------------------------------------------------------------------
generate: prepare-counterfeiter build-gen-binary $(GENERATE_PREREQ_FILES) ## Recreates gen files if any of the files containing go:generate directives have changed
$(GO) list ./... | xargs $(GO) generate
@touch $@
test-unit:
@echo Running unit tests:
$(GO_UNIT_TEST)
test-int: generate ## Runs the integration tests. Use TEST_FLAGS="--storage.uri=postgres://postgres:postgres@localhost:5432/postgres?sslmode=disable" to specify the DB. All other SM flags are also supported
@echo Running integration tests:
$(GO_INT_TEST)
test-int-other:
@echo Running integration tests:
$(GO_INT_TEST_OTHER)
test-int-broker:
@echo Running integration tests:
$(GO_INT_TEST_BROKER)
test-int-osb-and-plugin:
@echo Running integration tests:
$(GO_INT_TEST_OSB_AND_PLUGIN)
test-int-service-instance-and-binding:
@echo Running integration tests:
$(GO_INT_TEST_SERVICE_INSTANCE_AND_BINDING)
test-report: test-int test-unit
@$(GO) get github.com/wadey/gocovmerge
@gocovmerge $(CURDIR)/*.cov > $(TEST_PROFILE)
coverage: test-report ## Produces an HTML report containing code coverage details
@go tool cover -html=$(TEST_PROFILE) -o $(COVERAGE)
@echo Generated coverage report in $(COVERAGE).
go-deps:
set GO111MODULE=off
go get gotest.tools/gotestsum
go get github.com/t-yuki/gocover-cobertura
go install github.com/axw/gocov/gocov@latest
go get github.com/AlekSi/gocov-xml
go get -u github.com/jstemmer/go-junit-report
set GO111MODULE=on
go mod tidy
# Run tests
run-test: go-deps
rm -rf reports
mkdir -p reports
$(GOBIN)/gotestsum --junitfile reports/junit.xml -- -coverprofile=cover.out ./... -mod=mod
go tool cover -func $(TEST_PROFILE_OUT) | grep total
find . -name cover.out -execdir sh -c '$(GOBIN)/gocover-cobertura < cover.out > coverage.xml' \; ;\
GO111MODULE=on
clean-generate:
@rm -f generate
clean-test-unit: clean-generate ## Cleans up unit test artifacts
@echo Deleting $(UNIT_TEST_PROFILE)...
@rm -f $(UNIT_TEST_PROFILE)
clean-test-int: clean-generate ## Cleans up integration test artifacts
@echo Deleting $(INT_TEST_PROFILE)...
@rm -f $(INT_TEST_PROFILE)
clean-test-report: clean-test-unit clean-test-int
@echo Deleting $(TEST_PROFILE)...
@rm -f $(TEST_PROFILE)
clean-coverage: clean-test-report ## Cleans up coverage artifacts
@echo Deleting $(COVERAGE)...
@rm -f $(COVERAGE)
#-----------------------------------------------------------------------------
# Formatting, Linting, Static code checks
#-----------------------------------------------------------------------------
precommit: build coverage format-check lint-check ## Run this before commiting (builds, recreates fakes, runs tests, checks linting and formating). This also runs integration tests - check test-int target for details
precommit-integration-tests-broker: build test-int-broker ## Run this before commiting (builds, recreates fakes, runs tests, checks linting and formating). This also runs integration tests - check test-int target for details
precommit-integration-tests-osb-and-plugin: build test-int-osb-and-plugin ## Run this before commiting (builds, recreates fakes, runs tests, checks linting and formating). This also runs integration tests - check test-int target for details
precommit-integration-tests-service-instance-and-binding: build test-int-service-instance-and-binding ## Run this before commiting (builds, recreates fakes, runs tests, checks linting and formating). This also runs integration tests - check test-int target for details
precommit-integration-tests-other: build test-int-other ## Run this before commiting (builds, recreates fakes, runs tests, checks linting and formating). This also runs integration tests - check test-int target for details
precommit-unit-tests: build test-unit format-check lint-check ## Run this before commiting (builds, recreates fakes, runs tests, checks linting and formating). This also runs integration tests - check test-int target for details
precommit-new-unit-tets: prepare build test-unit format-check lint-check
precommit-new-integration-tests-broker: prepare build test-int-broker
precommit-new-integration-tests-osb-and-plugin: prepare build test-int-osb-and-plugin
precommit-new-integration-tests-service-instance-and-binding: prepare build test-int-service-instance-and-binding
format: ## Formats the source code files with gofmt
@echo The following files were reformated:
@$(GOFMT) -l -s -w $(SOURCE_FILES)
format-check: ## Checks for style violation using gofmt
@echo Checking if there are files not formatted with gofmt...
@$(GOFMT) -l -s $(SOURCE_FILES) | grep ".*\.go"; if [ "$$?" = "0" ]; then echo "Files need reformating! Run make format!" ; exit 1; fi
golangci-lint: $(BINDIR)/golangci-lint
$(BINDIR)/golangci-lint:
@curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s v1.45.2
lint-check: golangci-lint
@echo Running linter checks...
@$(BINDIR)/golangci-lint run --config .golangci.yml --issues-exit-code=0 --deadline=30m --out-format checkstyle ./... > checkstyle.xml
#-----------------------------------------------------------------------------
# Useful utility targets
#-----------------------------------------------------------------------------
clean: clean-bin clean-coverage ## Cleans up binaries, test and coverage artifacts
help: ## Displays documentation about the makefile targets
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | \
awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'