-
Notifications
You must be signed in to change notification settings - Fork 1
/
Makefile
268 lines (209 loc) · 9.15 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
260
261
262
263
264
265
266
267
268
#
# Environment Variables
#
UNAME_S := $(shell uname -s)
PYTHON_VENV = ~/.venv/cortex-cli-test
ifeq ($(CORTEX_CLI),) ## Cortex CLI, defaults to CLI in the repository
export CORTEX_CLI := . $(PYTHON_VENV)/bin/activate; python3 ./cortexapps_cli/cortex.py -q
endif
ifeq ($(CORTEX_GH_ALIAS),) ## Github alias defined in Cortex GitHub integration, defaults to public-api-test
export CORTEX_GH_ALIAS := public-api-test
endif
# Change this once we can get WEBHOOK_URL via Cortex API
ifeq ($(CORTEX_GH_WEBHOOK_URL),) ## The GitHub webhook URL defined in Cortex
export CORTEX_GH_WEBHOOK_URL=https://api.getcortexapp.com/api/v1/github/manual-webhook/e0b77380-e7af-4e14-8563-8168651e307e/$(CORTEX_GH_ALIAS)
endif
# Should only need to change this if using enterprise GitHub.
ifeq ($(GH_URL),) ## GitHub URL, will be used to call the GitHub API to create a webhook
export GH_URL=https://api.github.com
endif
ifeq ($(GH_ORG),) ## GitHub organization used for GitHub tests
export GH_ORG=cortextests
endif
ifeq ($(GH_REPO),) ## GitHub repository used for GitHub tests
export GH_REPO=public-api-test-repo
endif
ifeq ($(CORTEX_API_KEY),) ## Required; Cortex API key with Admin permission
$(error CORTEX_API_KEY is not set)
endif
ifeq ($(CORTEX_BASE_URL),) ## Required; Cortex base URL for API, ie for cloud this would be https://api.getcortexapp.com
$(error CORTEX_BASE_URL is not set)
endif
ifeq ($(CORTEX_BASE_URL),http://api.local.getcortexapp.com:8080)
export CORTEX_GH_WEBHOOK_URL=$(shell ./scripts/ngrok.sh)/api/v1/github/manual-webhook/a4037bca-c83e-4058-8550-8393826ff642/$(CORTEX_GH_ALIAS)
ifeq ($(NGROK_PORT),)
export NGROK_PORT=8081
endif
endif
ifeq ($(CORTEX_ENV),) ## Cortex environment, defaults to 'default'; used to distinguish make build targets between environments; if not set inferred from CORTEX_BASE_URL
ifeq ($(CORTEX_BASE_URL),http://api.local.getcortexapp.com:8080)
export CORTEX_ENV=local
else ifeq ($(CORTEX_BASE_URL),https://api.staging.getcortexapp.com)
export CORTEX_ENV=staging
else ifeq ($(CORTEX_BASE_URL),https://api.getcortexapp.com)
export CORTEX_ENV=prod
else ifeq ($(CORTEX_BASE_URL),http://api.helm.getcortexapp.com)
export CORTEX_ENV=helm
else ifeq ($(CORTEX_ENV),)
export CORTEX_ENV=default
endif
endif
ifneq ($(CORTEX_TENANT),) ## Used with CORTEX_ENV, if set can help distinguish between different tenants in the same environment
export BUILD_SUBDIR=$(CORTEX_ENV)-$(CORTEX_TENANT)
else
export BUILD_SUBDIR=$(CORTEX_ENV)
endif
#
# Configuration variables
#
BUILD_DIR = build/$(BUILD_SUBDIR)
BUILD_TOOLS_DIR = $(BUILD_DIR)/tools
export FEATURE_FLAG_EXPORT=$(BUILD_DIR)/ff/feature-flags.json
DATA_DIR = data
ENTITIES := $(shell find $(DATA_DIR) -type f)
ARCHIVE_ENTITIES = robot-item-sorter inventory-scraper
ARCHIVE_TARGETS := $(ARCHIVE_ENTITIES:%=$(BUILD_DIR)/%.archive)
CATALOG_ENTITIES := $(wildcard data/catalog/*.yaml)
CATALOG_TARGETS := $(CATALOG_ENTITIES:data/catalog/%.yaml=$(BUILD_DIR)/%.yaml)
CUSTOM_RESOURCES := $(wildcard data/resource-definitions/*.json)
CUSTOM_RESOURCE_TARGETS := $(CUSTOM_RESOURCES:data/resource-definitions/%.json=$(BUILD_DIR)/%.json)
FEATURE_FLAG_VARS := $(shell env | grep CORTEX_FF | cut -d= -f1)
FEATURE_FLAGS = $(patsubst CORTEX_FF_%,%,$(FEATURE_FLAG_VARS))
FEATURE_FLAG_ENVSUBST := $(FEATURE_FLAGS:%=$(BUILD_DIR)/ff/envsubst/%)
all: info setup feature-flags-dump load-data github test-api ## Setup environment, load data and test
all-cli: all test-cli
.PHONY: info
info:
@echo "Running test for: $(BUILD_SUBDIR)"
.PHONY: setup
setup: tools venv ## Setup python virtual environment for testing
#
#
# Tools setup
#
#
.PHONY: tools
tools: brew jq python3
.PHONY: brew
brew: $(BUILD_TOOLS_DIR)/brew | $(BUILD_TOOLS_DIR)
$(BUILD_TOOLS_DIR)/brew: | $(BUILD_TOOLS_DIR)
ifeq ($(UNAME_S),Darwin)
@which brew > /dev/null || /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
endif
@touch $@
.PHONY: jq
jq: $(BUILD_TOOLS_DIR)/jq | $(BUILD_TOOLS_DIR)
$(BUILD_TOOLS_DIR)/jq:
ifeq ($(UNAME_S),Darwin)
@which jq > /dev/null || brew install jq
else
@which jq > /dev/null || (echo "jq is not installed"; exit)
endif
@touch $@
.PHONY: python3
python3: ${BUILD_TOOLS_DIR}/python3 | $(BUILD_TOOLS_DIR)
${BUILD_TOOLS_DIR}/python3:
ifeq ($(UNAME_S),Darwin)
@which python3 > /dev/null || brew install python3
else
@which python3 > /dev/null || (echo "python3 is not installed"; exit 1)
endif
@touch $@
.PHONY: venv
venv: $(PYTHON_VENV)
$(PYTHON_VENV): requirements.txt
python3 -m venv $@
. $@/bin/activate; python3 -m pip install --upgrade -r $^
touch $@
.PHONY: help
help:
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | cut -d':' -f1- | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
.PHONY: vars
vars: ## Display variables used for testing
@grep -E 'ifeq.*## .*$$' $(MAKEFILE_LIST) | grep -v grep | sort | sed 's/ifeq.*(//' | sed 's/).*)//' | awk 'BEGIN {FS = "## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
.PHONY: load-data
load-data: catalog-entities archive-entities resource-definitions ## Load data from 'data' directory into Cortex
.PHONY: archive-entities
archive-entities: $(ARCHIVE_TARGETS) | $(BUILD_DIR)
.PHONY: catalog-entities
catalog-entities: $(CATALOG_TARGETS) | $(BUILD_DIR)
$(BUILD_DIR)/%.archive: $(BUILD_TOOLS_DIR)/python3
@$(CORTEX_CLI) catalog archive -t $(notdir $(basename $@))
@touch $@
$(BUILD_DIR)/%.yaml: data/catalog/%.yaml $(CUSTOM_RESOURCE_TARGETS)
$(CORTEX_CLI) catalog create -f $<
@touch $@
.PHONY: resource-definitions
resource-definitions: $(CUSTOM_RESOURCE_TARGETS) | $(BUILD_DIR)
$(BUILD_DIR)/%.json: data/resource-definitions/%.json | $(BUILD_DIR)
$(CORTEX_CLI) catalog delete-by-type -t $(notdir $(basename $@))
($(CORTEX_CLI) resource-definitions get -t $(notdir $(basename $@)) && $(CORTEX_CLI) resource-definitions delete -t $(notdir $(basename $@)) ) || :
$(CORTEX_CLI) resource-definitions create -f $<
@touch $@
#
# This target performs token replacement of files in the feature-flags directory and checks
# if the contents of the file have changed since the last time it was built. If so, the
# feature flag is updated in the environment.
#
# This check is beneficial only in local test environments. As of now, no intent to save
# state between runs of an automated build, so all flags would need to be set each test
# cycle.
#
# If these flags can be set all at once and time isn't a concern, this target can most
# likely be removed.
#
.PHONY: feature-flags
feature-flags: feature-flags-dump $(FEATURE_FLAG_ENVSUBST)
$(BUILD_DIR)/ff/envsubst/%: | $(BUILD_DIR)/ff/envsubst $(BUILD_DIR)/ff/source
@echo "Checking if feature flag $* needs to be updated"
@envsubst < feature-flags/$*.json > $@
@diff $@ $(BUILD_DIR)/ff/source/$* 2> /dev/null || (. $(PYTHON_VENV)/bin/activate; python tests/feature_flag_set.py $*)
@cp $@ $(BUILD_DIR)/ff/source
@rm $@
test: test-api test-cli ## Run pytest for both API and CLI tests in the 'tests' directory
test-api: feature-flags ## Run pytest for API tests in the 'tests' directory
@if [ -f .coverage ]; then rm .coverage; fi
ifeq ($(CORTEX_API_KEY_VIEWER),) ## Required; Cortex API key with Viewer permission, used in RBAC tests
$(error CORTEX_API_KEY_VIEWER is not set)
endif
ifeq ($(GH_PAT),) ## GitHub Personal Access Token
$(error GH_PAT is not set)
endif
ifeq ($(GH_WEBHOOK_SECRET),) ## GitHub webhook secret; defined in the Cortex GitHub configuration and used to create GitHub webhook
$(error GH_WEBHOOK_SECRET is not set)
endif
@. $(PYTHON_VENV)/bin/activate; PYTHONPATH=cortexapps_cli:tests pytest -rA -n auto -m "not serial" --html=report.html --self-contained-html --cov=cortexapps_cli --cov-append --cov-report term-missing $(PYTEST_PARMS)
test-cli: feature-flags test-api cli-tests ## Run pytest for CLI-specific tests in the 'tests' directory
cli-tests: ## Run pytest for CLI-specific tests in the 'tests' directory
@. $(PYTHON_VENV)/bin/activate; PYTHONPATH=cortexapps_cli:tests pytest -rA -n 0 -m "serial" --cov=cortexapps_cli --cov-append --cov-report term-missing $(PYTEST_PARMS)
test-git: feature-flags github ## Run pytest for git tests in the 'tests' directory
@. $(PYTHON_VENV)/bin/activate; PYTHONPATH=cortexapps_cli:tests pytest -k test_git
.PHONY: clean
clean: clean-data
@rm -rf $(BUILD_DIR)
clean-data: $(BUILD_TOOLS_DIR)/jq ${ENTITIES}
for entity in $(shell $(CORTEX_CLI) catalog list -g public-api-test | jq -r '.entities[].tag'); do \
$(CORTEX_CLI) catalog delete -t $$entity; echo "Deleted: $$entity";\
done
.PHONY: feature-flags-dump
feature-flags-dump: $(FEATURE_FLAG_EXPORT) ## Dump current feature flags to $(FEATURE_FLAG_EXPORT)
.PHONY: feature-flags-clean
feature-flags-clean:
@rm -f $(FEATURE_FLAG_EXPORT)
$(FEATURE_FLAG_EXPORT): | $(BUILD_DIR)/ff
. $(PYTHON_VENV)/bin/activate; python3 tests/feature_flag_dump.py $@
.PHONY: github
github: $(BUILD_DIR)/github ## Configure Cortex GitHub integration, create GitHub webhook
$(BUILD_DIR)/github: | $(BUILD_DIR)
. $(PYTHON_VENV)/bin/activate; PYTHONPATH=cortexapps_cli:tests python3 tests/github_setup.py
touch $@
$(BUILD_DIR):
@mkdir -p $@
$(BUILD_DIR)/ff:
@mkdir -p $@
$(BUILD_DIR)/ff/source:
@mkdir -p $@
$(BUILD_TOOLS_DIR):
@mkdir -p $@
$(BUILD_DIR)/ff/envsubst:
@mkdir -p $@