-
Notifications
You must be signed in to change notification settings - Fork 9
/
Makefile
115 lines (94 loc) · 3.63 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
all: lint test
PHONY: test coverage lint golint clean vendor local-dev-databases docker-up docker-down integration-test unit-test
GOOS=linux
DB_STRING=host=localhost port=26257 user=root sslmode=disable
DEV_DB=${DB_STRING} dbname=serverservice
TEST_DB=${DB_STRING} dbname=serverservice_test
DOCKER_IMAGE := "ghcr.io/metal-toolbox/hollow-serverservice"
## run all tests
test: | unit-test integration-test
## run integration tests
integration-test: test-database
@echo Running integration tests...
@SERVERSERVICE_CRDB_URI="${TEST_DB}" go test -cover -tags testtools,integration -p 1 ./...
## run lint and unit tests
unit-test: | lint
@echo Running unit tests...
@go test -cover -short -tags testtools ./...
## check test coverage
coverage: | test-database
@echo Generating coverage report...
@SERVERSERVICE_CRDB_URI="${TEST_DB}" go test ./... -race -coverprofile=coverage.out -covermode=atomic -tags testtools -p 1
@go tool cover -func=coverage.out
@go tool cover -html=coverage.out
## lint
lint: golint
golint: | vendor
@echo Linting Go files...
@golangci-lint run
## clean docker files
clean: docker-clean
@echo Cleaning...
@rm -rf ./dist/
@rm -rf coverage.out
@go clean -testcache
vendor:
@go mod download
@go mod tidy
## setup docker compose test env
docker-up:
@docker-compose -f quickstart.yml up -d crdb
## stop docker compose test env
docker-down:
@docker-compose -f quickstart.yml down
## clean docker volumes
docker-clean:
@docker-compose -f quickstart.yml down --volumes
## setup devel database
dev-database: | vendor
@cockroach sql --insecure -e "drop database if exists serverservice"
@cockroach sql --insecure -e "create database serverservice"
@SERVERSERVICE_CRDB_URI="${DEV_DB}" go run main.go migrate up
## setup test database
test-database: | vendor
@cockroach sql --insecure -e "drop database if exists serverservice_test"
@cockroach sql --insecure -e "create database serverservice_test"
@SERVERSERVICE_CRDB_URI="${TEST_DB}" go run main.go migrate up
@cockroach sql --insecure -e "use serverservice_test; ALTER TABLE attributes DROP CONSTRAINT check_server_id_server_component_id; ALTER TABLE versioned_attributes DROP CONSTRAINT check_server_id_server_component_id;"
## Build linux bin
build-linux:
GOOS=linux GOARCH=amd64 go build -o serverservice
## build docker image and tag as ghcr.io/metal-toolbox/hollow-serverservice:latest
build-image: build-linux
docker build --rm=true -f Dockerfile -t ${DOCKER_IMAGE}:latest . \
--label org.label-schema.schema-version=1.0 \
--label org.label-schema.vcs-ref=$(GIT_COMMIT_FULL) \
--label org.label-schema.vcs-url=$(REPO)
## build and push devel docker image to KIND image repo used by the sandbox - https://github.com/metal-toolbox/sandbox
push-image-devel: build-image
docker tag ${DOCKER_IMAGE}:latest localhost:5001/serverservice:latest
docker push localhost:5001/serverservice:latest
kind load docker-image localhost:5001/serverservice:latest
# https://gist.github.com/prwhite/8168133
# COLORS
GREEN := $(shell tput -Txterm setaf 2)
YELLOW := $(shell tput -Txterm setaf 3)
WHITE := $(shell tput -Txterm setaf 7)
RESET := $(shell tput -Txterm sgr0)
TARGET_MAX_CHAR_NUM=20
## Show help
help:
@echo ''
@echo 'Usage:'
@echo ' ${YELLOW}make${RESET} ${GREEN}<target>${RESET}'
@echo ''
@echo 'Targets:'
@awk '/^[a-zA-Z\-\\_0-9]+:/ { \
helpMessage = match(lastLine, /^## (.*)/); \
if (helpMessage) { \
helpCommand = substr($$1, 0, index($$1, ":")-1); \
helpMessage = substr(lastLine, RSTART + 3, RLENGTH); \
printf " ${YELLOW}%-$(TARGET_MAX_CHAR_NUM)s${RESET} ${GREEN}%s${RESET}\n", helpCommand, helpMessage; \
} \
} \
{ lastLine = $$0 }' $(MAKEFILE_LIST)