|
1 | | -all: build |
| 1 | +.DEFAULT_GOAL := help |
| 2 | +BIN_DIR := $(CURDIR)/bin |
2 | 3 |
|
3 | | -init: ## Installs development tools |
| 4 | +# --- Go-related variables ---------------------------------------------------------------- |
| 5 | +GO_VERSION := $(shell grep '^go ' $(CURDIR)/../go.mod | awk '{print $$2}') |
| 6 | + |
| 7 | +# --- Git variables ------------------------------------------------------------------------ |
| 8 | +GIT_VERSION := $(shell git describe --tags) |
| 9 | +GIT_COMMIT := $(shell git rev-parse --short HEAD) |
| 10 | +BUILD_TIME := $(shell date -u +%Y-%m-%dT%H:%M:%SZ) |
| 11 | + |
| 12 | +# --- Test-related variables -------------------------------------------------------------- |
| 13 | +TEST_DIRS := $(shell find . -type f -name '*_test.go' -not -path '*/.*' -exec dirname {} + | sort -u) |
| 14 | +TEST_BINARY_TARGETS := $(subst /,-,$(subst ./,,$(TEST_DIRS))) |
| 15 | +RUN_TEST_TARGETS := $(addprefix test-,$(TEST_BINARY_TARGETS)) |
| 16 | +# Exclude server-settings tests from this list, as they require special handling (Grafana restart). |
| 17 | +RUN_TEST_TARGETS := $(filter-out test-server-settings%,$(RUN_TEST_TARGETS)) |
| 18 | +REPORT_OUT := pmm-api-tests-junit-report.xml |
| 19 | +TEST_OUTPUT_PREFIX := pmm-api-tests-output |
| 20 | +TEST_FLAGS := -test.count=1 -test.v |
| 21 | + |
| 22 | +# --- Docker related variables ----------------------------------------------------------- |
| 23 | +DOCKER_IMAGE := local/pmm-api-tests |
| 24 | +PMM_SERVER_INSECURE_TLS ?= 1 |
| 25 | +PMM_RUN_UPDATE_TEST ?= 0 |
| 26 | +PMM_RUN_ADVISOR_TESTS ?= 0 |
| 27 | + |
| 28 | +.PHONY: help guard-% init $(TEST_BINARY_TARGETS) $(RUN_TEST_TARGETS) |
| 29 | +.PHONY: test-server-settings test test-report clean docker-build-image docker-run-tests |
| 30 | + |
| 31 | +help: ## Display this help message |
| 32 | + @echo "Please use \`make <target>\`, where <target> is one of the following:" |
| 33 | + @grep -h '^[a-zA-Z]' $(MAKEFILE_LIST) | awk -F ':.*## ' 'NF==2 {printf " %-26s%s\n", $$1, $$2}' |
| 34 | + |
| 35 | +guard-%: |
| 36 | + @[ -n "${$*}" ] || (echo "ERROR: $* is not set"; exit 1) |
| 37 | + |
| 38 | +$(TEST_BINARY_TARGETS): ## Build test binaries |
| 39 | + @dir="$(subst -,/,$@)"; \ |
| 40 | + echo "-> Building test binary for dir: $$dir"; \ |
| 41 | + go test -c -v -race -o $(BIN_DIR)/$@ $(CURDIR)/$$dir |
| 42 | + |
| 43 | +init: $(TEST_BINARY_TARGETS) ## Build tests and install development tools |
| 44 | + @echo "-> Installing tools MAKEFLAGS: $(MAKEFLAGS)" |
4 | 45 | cd tools && go generate -x -tags=tools |
5 | 46 |
|
6 | | -build: |
7 | | - go install -v ./... |
8 | | - go test -c -v ./alerting |
9 | | - go test -c -v ./backup |
10 | | - go test -c -v ./inventory |
11 | | - go test -c -v ./management |
12 | | - go test -c -v ./server |
13 | | - go test -c -v ./user |
14 | | - |
15 | | -run: |
16 | | - go test -count=1 -p 1 -v ./... 2>&1 | tee pmm-api-tests-output.txt |
17 | | - cat pmm-api-tests-output.txt | bin/go-junit-report > pmm-api-tests-junit-report.xml |
18 | | - |
19 | | -run-dev: |
20 | | - go test -count=1 -p 1 -v ./... |
21 | | - |
22 | | -run-race: |
23 | | - go test -count=1 -p 1 -v -race ./... 2>&1 | tee pmm-api-tests-output.txt |
24 | | - cat pmm-api-tests-output.txt | bin/go-junit-report > pmm-api-tests-junit-report.xml |
25 | | - |
26 | | -clean: |
27 | | - rm -f ./pmm-api-tests-output.txt |
28 | | - rm -f ./pmm-api-tests-junit-report.xml |
| 47 | +$(RUN_TEST_TARGETS): |
| 48 | + @binary="$(subst test-,,$@)"; \ |
| 49 | + echo "🚀 Run $$binary tests"; \ |
| 50 | + $(BIN_DIR)/$$binary $(TEST_FLAGS) 2>&1 | tee $(TEST_OUTPUT_PREFIX)-$@.txt |
| 51 | + |
| 52 | +# This target shall be running separately from the rest of tests, |
| 53 | +# as it triggers Grafana restart that may cause other tests to fail. |
| 54 | +test-server-settings: guard-PMM_SERVER_URL ## Run server-settings tests (requires Grafana restart) |
| 55 | + @binary="$(subst test-,,$@)"; \ |
| 56 | + echo "🚀 Run $$binary tests"; \ |
| 57 | + $(BIN_DIR)/$$binary $(TEST_FLAGS) 2>&1 | tee $(TEST_OUTPUT_PREFIX)-$@.txt |
| 58 | + |
| 59 | +test: guard-PMM_SERVER_URL $(RUN_TEST_TARGETS) ## Run tests |
| 60 | + $(MAKE) test-server-settings |
| 61 | + @echo "✅ All tests completed." |
| 62 | + |
| 63 | +test-report: test ## Run tests and generate JUnit report |
| 64 | + cat $(CURDIR)/$(TEST_OUTPUT_PREFIX)-*.txt | $(BIN_DIR)/go-junit-report > $(REPORT_OUT) |
| 65 | + @echo "✅ Report generated: $(REPORT_OUT)." |
| 66 | + |
| 67 | +clean: ## Cleanup reports |
| 68 | + rm -f $(CURDIR)/pmm-api-tests-*.txt |
| 69 | + rm -f $(CURDIR)/$(REPORT_OUT) |
| 70 | + @echo "✅ Cleanup completed." |
| 71 | + |
| 72 | +## Docker-related targets |
| 73 | + |
| 74 | +docker-build-image: ## Build Docker image with tests |
| 75 | + DOCKER_BUILDKIT=1 docker build \ |
| 76 | + -f Dockerfile \ |
| 77 | + --build-arg GO_VERSION=$(GO_VERSION) \ |
| 78 | + --build-arg VERSION=$(GIT_VERSION) \ |
| 79 | + --build-arg GIT_COMMIT=$(GIT_COMMIT) \ |
| 80 | + --build-arg BUILD_TIME=$(BUILD_TIME) \ |
| 81 | + -t $(DOCKER_IMAGE) ../ |
| 82 | + @echo "✅ Docker image '$(DOCKER_IMAGE)' built successfully." |
| 83 | + |
| 84 | +docker-run-tests: guard-PMM_SERVER_URL ## Run tests inside Docker container |
| 85 | + docker run \ |
| 86 | + -e PMM_SERVER_URL=$(PMM_SERVER_URL) \ |
| 87 | + -e PMM_SERVER_INSECURE_TLS=$(PMM_SERVER_INSECURE_TLS) \ |
| 88 | + -e PMM_RUN_UPDATE_TEST=$(PMM_RUN_UPDATE_TEST) \ |
| 89 | + -e PMM_RUN_ADVISOR_TESTS=$(PMM_RUN_ADVISOR_TESTS) \ |
| 90 | + --name api-tests \ |
| 91 | + --network host \ |
| 92 | + $(DOCKER_IMAGE) |
| 93 | + @echo "✅ Tests executed successfully inside Docker container." |
0 commit comments