forked from caraml-dev/xp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
204 lines (162 loc) · 7.27 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
export
MANAGEMENT_SVC_PATH=management-service
TREATMENT_SVC_PATH=treatment-service
MANAGEMENT_SVC_BIN_NAME=$(if $(MANAGEMENT_APP_NAME),$(MANAGEMENT_APP_NAME),xp-management)
TREATMENT_SVC_BIN_NAME=$(if $(TREATMENT_APP_NAME),$(TREATMENT_APP_NAME),xp-treatment)
VERSION_NUMBER=$(if $(VERSION),$(VERSION),$(shell ./scripts/vertagen/vertagen.sh -f docker))
UNAME_S := $(shell uname -s)
ifeq ($(UNAME_S),Linux)
PLATFROM=linux-x86_64
endif
ifeq ($(UNAME_S),Darwin)
PLATFROM=osx-x86_64
endif
PB_URL="https://github.com/protocolbuffers/protobuf/releases"
PB_VERSION=3.19.4
PROTOC_VERSION=1.5.2
protoc_dir=${PWD}/.protoc
OPENAPI_VERSION=1.8.1
# ==================================
# General
# ==================================
.PHONY: format
format: format-python format-go
.PHONY: lint
lint: lint-python lint-go
.PHONY: vendor
vendor:
@echo "Fetching dependencies..."
cd common && go mod vendor
cd clients && go mod vendor
cd management-service && go mod vendor
.PHONY: version
version:
$(eval VERSION=$(if $(OVERWRITE_VERSION),$(OVERWRITE_VERSION),v$(shell scripts/vertagen/vertagen.sh)))
@echo "xp-api version:" $(VERSION)
generate-api:
test -x ${GOPATH}/bin/oapi-codegen || go install github.com/deepmap/oapi-codegen/cmd/oapi-codegen@v${OPENAPI_VERSION}
oapi-codegen -config api/common/schema.conf api/schema.yaml
oapi-codegen -config api/clients/management.conf api/experiments.yaml
oapi-codegen -config api/clients/treatment.conf api/treatment.yaml
oapi-codegen -templates api/templates/chi -config api/management/server.conf api/experiments.yaml
oapi-codegen -config api/mockmanagement/server.conf api/experiments.yaml
oapi-codegen -config api/treatment/server.conf api/treatment.yaml
cd clients/management/ && mockery --name=ClientInterface --output=../../clients/testutils/mocks/management --filename=ManagementClientInterface.go
cd clients/treatment/ && mockery --name=ClientInterface --output=../../clients/testutils/mocks/treatment --filename=TreatmentClientInterface.go
# ==================================
# Setup Management & Treatment Services
# ==================================
local-authz-server:
@docker compose up -d postgres-auth && docker compose run keto-server migrate sql -e
@docker compose up -d keto-server
@docker compose run keto-server-bootstrap-policies engines acp ory policies import glob /policies/example_policy.json
local-db:
@docker compose up -d postgres
local-pubsub:
@docker compose up -d pubsub
.PHONY: mgmt-svc
mgmt-svc: local-authz-server local-db local-pubsub
cd management-service && PUBSUB_EMULATOR_HOST="localhost:8085" go run main.go serve --config="config/example.yaml"
.PHONY: treatment-svc
treatment-svc: local-pubsub
source .env.development
cd treatment-service && go run main.go serve
swagger-ui:
@docker compose up -d swagger-ui
@xdg-open 2>/dev/null http://localhost:8081 || open http://localhost:8081
$(protoc_dir):
curl -LO ${PB_URL}/download/v${PB_VERSION}/protoc-${PB_VERSION}-${PLATFROM}.zip
unzip protoc-${PB_VERSION}-${PLATFROM}.zip -d ${protoc_dir}
compile-protos: | $(protoc_dir)
go install github.com/golang/protobuf/protoc-gen-go@v${PROTOC_VERSION}
${protoc_dir}/bin/protoc --proto_path=. -I=api/proto/ --go_out=treatment-service api/proto/logs.proto
${protoc_dir}/bin/protoc --proto_path=. -I=api/proto/ --go_out=common/segmenters --go_opt=module=github.com/caraml-dev/xp/common/segmenters api/proto/segmenters.proto
${protoc_dir}/bin/protoc --proto_path=. -I=api/proto/ --go_out=common api/proto/message.proto
${protoc_dir}/bin/protoc --proto_path=. -I=api/proto/ --go_out=common api/proto/experiment.proto
${protoc_dir}/bin/protoc --proto_path=. -I=api/proto/ --go_out=common api/proto/settings.proto
# ==================================
# Code dependencies recipes
# ==================================
.PHONY: setup
setup:
@echo "> Initializing dependencies ..."
@test -x ${GOPATH}/bin/golangci-lint || go install github.com/golangci/golangci-lint/cmd/[email protected]
@echo "Setting up dev tools..."
@test -x "$(which pre-commit)" || pip install pre-commit
@pre-commit install
@pre-commit install-hooks
tidy-management-service:
cd ${MANAGEMENT_SVC_PATH} && go mod tidy
tidy-treatment-service:
cd ${TREATMENT_SVC_PATH} && go mod tidy
tidy: tidy-management-service tidy-treatment-service
format-go:
@echo "> Formatting code"
gofmt -s -w ${MANAGEMENT_SVC_PATH}
gofmt -s -w ${TREATMENT_SVC_PATH}
# ==================================
# Linting recipes
# ==================================
lint-management-service:
@echo "> Linting Management Service code..."
cd ${MANAGEMENT_SVC_PATH} && golangci-lint run --timeout 5m
lint-treatment-service:
@echo "> Linting Treatment Service code..."
cd ${TREATMENT_SVC_PATH} && golangci-lint run --timeout 5m
lint-go: lint-management-service lint-treatment-service
# ==================================
# Test recipes
# ==================================
test-management-service: tidy-management-service compile-protos
@cd ${MANAGEMENT_SVC_PATH} && go mod vendor
@echo "> Running Management Service tests ..."
# Set -gcflags=-l to disable inlining for the tests, to have Monkey patching work reliably
@cd ${MANAGEMENT_SVC_PATH} && go test -v ./... -coverpkg ./... -gcflags=-l -race -coverprofile cover.out.tmp -tags unit,integration ${API_ALL_PACKAGES}
@cd ${MANAGEMENT_SVC_PATH} && cat cover.out.tmp | grep -v "api/api.go\|cmd\|.pb.go\|mock\|testutils\|server" > cover.out
@cd ${MANAGEMENT_SVC_PATH} && go tool cover -func cover.out
test-treatment-service: tidy-treatment-service compile-protos
@cd ${TREATMENT_SVC_PATH} && go mod vendor
@echo "> Running Fetch Treatment Service tests..."
cd ${TREATMENT_SVC_PATH} && go test -v ./... -coverpkg ./... -race -coverprofile cover.out.tmp -tags unit,integration ${API_ALL_PACKAGES}
cd ${TREATMENT_SVC_PATH} && cat cover.out.tmp | grep -v "api/api.go\|cmd\|.pb.go\|testhelper\|server\|internal" > cover.out
cd ${TREATMENT_SVC_PATH} && go tool cover -func cover.out
test: test-management-service test-treatment-service
# ==================================
# Build recipes
# ==================================
build-management-service: version
@echo "Building binary..."
@cd ${MANAGEMENT_SVC_PATH} && go build -o ./bin/${MANAGEMENT_SVC_BIN_NAME}
@echo "Copying OpenAPI specs..."
@cp api/experiments.yaml ${MANAGEMENT_SVC_PATH}/bin
@cp api/schema.yaml ${MANAGEMENT_SVC_PATH}/bin
build-treatment-service: version
@echo "Building binary..."
@cd ${TREATMENT_SVC_PATH} && go build -o ./bin/${TREATMENT_SVC_BIN_NAME}
@cp api/treatment.yaml ${TREATMENT_SVC_PATH}/bin
@cp api/schema.yaml ${TREATMENT_SVC_PATH}/bin
build: build-management-service build-treatment-service
.PHONY: build-image
build-image: version vendor
@$(eval IMAGE_TAG = $(if $(DOCKER_REGISTRY),$(DOCKER_REGISTRY)/,)${BIN_NAME}:${VERSION})
@echo "Building docker image: ${IMAGE_TAG}"
docker build --tag ${IMAGE_TAG} .
# ==================================
# Python E2E tests
# ==================================
install-python-ci-dependencies:
pip install -r tests/requirements.txt
e2e: build
docker compose down
docker compose up -d postgres pubsub
cd tests/e2e; python -m pytest -s -v
e2e-ci:
cd tests/e2e; python -m pytest -s -v
format-python:
cd tests ; isort e2e/
cd tests ; flake8 e2e/
cd tests ; black e2e/
lint-python:
cd tests ; isort e2e/ --check-only
cd tests ; flake8 e2e/
cd tests ; black e2e/ --check