-
Notifications
You must be signed in to change notification settings - Fork 207
PMM-7 API tests rework #5299
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: v3
Are you sure you want to change the base?
PMM-7 API tests rework #5299
Changes from 3 commits
5f166d9
ef171ec
8bbe1ca
4fc661c
bb594be
ff0ab8f
0d997ce
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| /bin/ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,25 @@ | ||
| # This Dockerfile is used only for API tests. | ||
| ARG VERSION=dev | ||
| ARG GIT_COMMIT=unknown | ||
| ARG BUILD_TIME=unknown | ||
| ARG GO_VERSION="1.25" | ||
| ARG GO_BASE_IMAGE="golang:${GO_VERSION}" | ||
|
|
||
| FROM golang:1.25 | ||
| FROM $GO_BASE_IMAGE AS builder | ||
| ENV GONOPROXY='github.com/percona,github.com/Percona-Lab' | ||
| ENV GONOSUMDB='github.com/percona,github.com/Percona-Lab' | ||
| ENV GOPRIVATE='github.com/percona,github.com/Percona-Lab' | ||
|
|
||
| RUN export GOPATH=$(go env GOPATH) && \ | ||
| mkdir -p $GOPATH/pmm | ||
| WORKDIR $GOPATH/pmm | ||
|
|
||
| COPY . $GOPATH/pmm/ | ||
| COPY go.mod go.sum ./ | ||
| RUN --mount=type=cache,target=/go/pkg/mod \ | ||
| go mod download | ||
|
|
||
| COPY . $GOPATH/pmm | ||
| WORKDIR $GOPATH/pmm/api-tests/ | ||
| RUN --mount=type=cache,target=/go/pkg/mod \ | ||
| --mount=type=cache,target=/root/.cache/go-build \ | ||
| make -j4 init | ||
|
|
||
| CMD ["make", "init", "run-race"] | ||
| CMD ["make", "test-report"] | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,28 +1,93 @@ | ||||||
| all: build | ||||||
| .DEFAULT_GOAL := help | ||||||
| BIN_DIR := $(CURDIR)/bin | ||||||
|
|
||||||
| init: ## Installs development tools | ||||||
| # --- Go-related variables ---------------------------------------------------------------- | ||||||
| GO_VERSION := $(shell grep '^go ' $(CURDIR)/../go.mod | awk '{print $$2}') | ||||||
|
|
||||||
| # --- Git variables ------------------------------------------------------------------------ | ||||||
| GIT_VERSION := $(shell git describe --tags) | ||||||
| GIT_COMMIT := $(shell git rev-parse --short HEAD) | ||||||
| BUILD_TIME := $(shell date -u +%Y-%m-%dT%H:%M:%SZ) | ||||||
|
|
||||||
| # --- Test-related variables -------------------------------------------------------------- | ||||||
| TEST_DIRS := $(shell find . -type f -name '*_test.go' -not -path '*/.*' -exec dirname {} + | sort -u) | ||||||
| TEST_BINARY_TARGETS := $(subst /,-,$(subst ./,,$(TEST_DIRS))) | ||||||
| RUN_TEST_TARGETS := $(addprefix test-,$(TEST_BINARY_TARGETS)) | ||||||
| # Exclude server-settings tests from this list, as they require special handling (Grafana restart). | ||||||
| RUN_TEST_TARGETS := $(filter-out test-server-settings%,$(RUN_TEST_TARGETS)) | ||||||
| REPORT_OUT := pmm-api-tests-junit-report.xml | ||||||
| TEST_OUTPUT_PREFIX := pmm-api-tests-output | ||||||
| TEST_FLAGS := -test.count=1 -test.v | ||||||
|
|
||||||
| # --- Docker related variables ----------------------------------------------------------- | ||||||
| DOCKER_IMAGE := local/pmm-api-tests | ||||||
| PMM_SERVER_INSECURE_TLS ?= 1 | ||||||
| PMM_RUN_UPDATE_TEST ?= 0 | ||||||
| PMM_RUN_ADVISOR_TESTS ?= 0 | ||||||
|
|
||||||
| .PHONY: help guard-% init $(TEST_BINARY_TARGETS) $(RUN_TEST_TARGETS) | ||||||
| .PHONY: test-server-settings test test-report clean docker-build-image docker-run-tests | ||||||
|
|
||||||
| help: ## Show available targets | ||||||
| @echo "Available targets: $(TARGETS)" | ||||||
| @grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf " \033[36m%-20s\033[0m %s\n", $$1, $$2}' | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's leave out colors please, don't work well in all environments, especially in the logs (not human-readable). |
||||||
|
|
||||||
| guard-%: | ||||||
| @[ -n "${$*}" ] || (echo "ERROR: $* is not set"; exit 1) | ||||||
|
|
||||||
| $(TEST_BINARY_TARGETS): ## Build test binaries | ||||||
| @dir="$(subst -,/,$@)"; \ | ||||||
| echo "-> Building test binary for dir: $$dir"; \ | ||||||
| go test -c -v -race -o $(BIN_DIR)/$@ $(CURDIR)/$$dir | ||||||
|
|
||||||
| init: $(TEST_BINARY_TARGETS) ## Installs development tools | ||||||
| @echo "-> Installing tools" | ||||||
| cd tools && go generate -x -tags=tools | ||||||
|
|
||||||
| build: | ||||||
| go install -v ./... | ||||||
| go test -c -v ./alerting | ||||||
| go test -c -v ./backup | ||||||
| go test -c -v ./inventory | ||||||
| go test -c -v ./management | ||||||
| go test -c -v ./server | ||||||
| go test -c -v ./user | ||||||
|
|
||||||
| run: | ||||||
| go test -count=1 -p 1 -v ./... 2>&1 | tee pmm-api-tests-output.txt | ||||||
| cat pmm-api-tests-output.txt | bin/go-junit-report > pmm-api-tests-junit-report.xml | ||||||
|
|
||||||
| run-dev: | ||||||
| go test -count=1 -p 1 -v ./... | ||||||
|
|
||||||
| run-race: | ||||||
| go test -count=1 -p 1 -v -race ./... 2>&1 | tee pmm-api-tests-output.txt | ||||||
| cat pmm-api-tests-output.txt | bin/go-junit-report > pmm-api-tests-junit-report.xml | ||||||
|
|
||||||
| clean: | ||||||
| rm -f ./pmm-api-tests-output.txt | ||||||
| rm -f ./pmm-api-tests-junit-report.xml | ||||||
| $(RUN_TEST_TARGETS): | ||||||
| @binary="$(subst test-,,$@)"; \ | ||||||
| echo "🚀 Run $$binary tests"; \ | ||||||
| $(BIN_DIR)/$$binary $(TEST_FLAGS) 2>&1 | tee $(TEST_OUTPUT_PREFIX)-$@.txt | ||||||
|
|
||||||
| # This target shall be running separately from the rest of tests, | ||||||
| # as it triggers Grafana restart that may cause other tests to fail. | ||||||
| test-server-settings: guard-PMM_SERVER_URL ## Run server-settings tests (requires Grafana restart) | ||||||
| @binary="$(subst test-,,$@)"; \ | ||||||
| echo "🚀 Run $$binary tests"; \ | ||||||
| $(BIN_DIR)/$$binary $(TEST_FLAGS) 2>&1 | tee $(TEST_OUTPUT_PREFIX)-$@.txt | ||||||
|
|
||||||
| test: guard-PMM_SERVER_URL $(RUN_TEST_TARGETS) ## Run tests | ||||||
| $(MAKE) test-server-settings | ||||||
| @echo "✅ All tests completed." | ||||||
|
|
||||||
| test-report: test ## Run tests and generate JUnit report | ||||||
| cat $(CURDIR)/$(TEST_OUTPUT_PREFIX)-*.txt | $(BIN_DIR)/go-junit-report > $(REPORT_OUT) | ||||||
| @echo "✅ Report is generated: $(REPORT_OUT)." | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Just to make it consistent with the rest. |
||||||
|
|
||||||
| clean: ## Cleanup reports | ||||||
| rm -f $(CURDIR)/pmm-api-tests-*.txt | ||||||
| rm -f $(CURDIR)/$(REPORT_OUT) | ||||||
| @echo "✅ Cleanup completed." | ||||||
|
|
||||||
| ## Docker-related targets | ||||||
|
|
||||||
| docker-build-image: ## Build Docker image with tests | ||||||
| docker build \ | ||||||
| -f Dockerfile \ | ||||||
| --build-arg GO_VERSION=$(GO_VERSION) \ | ||||||
| --build-arg VERSION=$(GIT_VERSION) \ | ||||||
| --build-arg GIT_COMMIT=$(GIT_COMMIT) \ | ||||||
| --build-arg BUILD_TIME=$(BUILD_TIME) \ | ||||||
| -t $(DOCKER_IMAGE) ../ | ||||||
| @echo "✅ Docker image '$(DOCKER_IMAGE)' built successfully." | ||||||
|
|
||||||
| docker-run-tests: guard-PMM_SERVER_URL ## Run tests inside Docker container | ||||||
| docker run \ | ||||||
| -e PMM_SERVER_URL=$(PMM_SERVER_URL) \ | ||||||
| -e PMM_SERVER_INSECURE_TLS=$(PMM_SERVER_INSECURE_TLS) \ | ||||||
| -e PMM_RUN_UPDATE_TEST=$(PMM_RUN_UPDATE_TEST) \ | ||||||
| -e PMM_RUN_ADVISOR_TESTS=$(PMM_RUN_ADVISOR_TESTS) \ | ||||||
| --name api-tests \ | ||||||
| --network host \ | ||||||
| $(DOCKER_IMAGE) | ||||||
| @echo "✅ Tests executed successfully inside Docker container." | ||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -8,28 +8,33 @@ Make sure you have the latest Go version installed on your systems, execute the | |||||
| to set up API-tests in your local systems. | ||||||
|
|
||||||
| 1. Run PMM Server. This can be done by running `make env-up` in the root (`pmm`) directory. | ||||||
| 2. Replace `$PMM_SERVER_URL` with a URL in format `http://USERNAME:PASSWORD@HOST`. For local development it's usually `http://admin:admin@127.0.0.1`. | ||||||
| 2. Replace `$PMM_SERVER_URL` with a URL in format `https://USERNAME:PASSWORD@HOST`. For local development it's usually `http://admin:admin@127.0.0.1`. | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
I think we don't really want to test http. |
||||||
|
|
||||||
| # Usage | ||||||
|
|
||||||
| Precompile tests using the following command: | ||||||
| ``` | ||||||
| make -j4 init | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||
| ``` | ||||||
|
|
||||||
| Run the tests using the following command: | ||||||
|
|
||||||
| ``` | ||||||
| go test ./... -pmm.server-url $PMM_SERVER_URL -v | ||||||
| PMM_SERVER_URL=$PMM_SERVER_URL make test | ||||||
| ``` | ||||||
|
|
||||||
| # Docker | ||||||
|
|
||||||
| Build Docker image using the following command: | ||||||
|
|
||||||
| ``` | ||||||
| docker build -t IMAGENAME . | ||||||
| make docker-build-image | ||||||
| ``` | ||||||
|
|
||||||
| Run Docker container using the following command: | ||||||
|
|
||||||
| ``` | ||||||
| docker run -e PMM_SERVER_URL=**pmm-server-url** IMAGENAME | ||||||
| PMM_SERVER_URL=$PMM_SERVER_URL make docker-run-tests | ||||||
| ``` | ||||||
|
|
||||||
| where `PMM_SERVER_URL` should be pointing to a running PMM Server. | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need all of these? It worked without it before, looks like bloat.