Skip to content

Commit 646879d

Browse files
Merge branch 'v3' into pmm-autodiscover
2 parents 5404dce + 64d3111 commit 646879d

49 files changed

Lines changed: 3542 additions & 3597 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/api-tests.yml

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,7 @@ jobs:
8181
- name: Build the test image
8282
shell: bash
8383
run: |
84-
echo "Make sure to set the context to the root of the repository."
85-
docker buildx build -f api-tests/Dockerfile -t percona/pmm-api-tests .
84+
make docker-build-image
8685
8786
- name: Run compose up for test DBs
8887
shell: bash
@@ -98,14 +97,7 @@ jobs:
9897
- name: Run API tests
9998
shell: bash
10099
run: |
101-
docker run \
102-
-e PMM_SERVER_URL=${{env.PMM_URL}} \
103-
-e PMM_RUN_UPDATE_TEST=0 \
104-
-e PMM_RUN_ADVISOR_TESTS=0 \
105-
-e PMM_SERVER_INSECURE_TLS=1 \
106-
--name pmm-api-tests \
107-
--network host \
108-
percona/pmm-api-tests
100+
PMM_SERVER_URL=${{env.PMM_URL}} make docker-run-tests
109101
110102
- name: Get PMM logs
111103
if: ${{ failure() }}

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ compose.yml
3838
build.log
3939
ci.yml
4040

41-
api-tests/pmm-api-tests-output.txt
42-
api-tests/pmm-api-tests-junit-report.xml
41+
api-tests/*.txt
42+
api-tests/*.xml
4343

4444
packer.log
4545
encryption.key

api-tests/Dockerfile

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,25 @@
11
# This Dockerfile is used only for API tests.
2+
ARG VERSION=dev
3+
ARG GIT_COMMIT=unknown
4+
ARG BUILD_TIME=unknown
5+
ARG GO_VERSION="1.25"
6+
ARG GO_BASE_IMAGE="golang:${GO_VERSION}"
27

3-
FROM golang:1.25
8+
FROM $GO_BASE_IMAGE AS builder
9+
ENV GONOPROXY='github.com/percona,github.com/Percona-Lab'
10+
ENV GONOSUMDB='github.com/percona,github.com/Percona-Lab'
11+
ENV GOPRIVATE='github.com/percona,github.com/Percona-Lab'
412

5-
RUN export GOPATH=$(go env GOPATH) && \
6-
mkdir -p $GOPATH/pmm
13+
WORKDIR $GOPATH/pmm
714

8-
COPY . $GOPATH/pmm/
15+
COPY go.mod go.sum ./
16+
RUN --mount=type=cache,target=/go/pkg/mod \
17+
go mod download
18+
19+
COPY . $GOPATH/pmm
920
WORKDIR $GOPATH/pmm/api-tests/
21+
RUN --mount=type=cache,target=/go/pkg/mod \
22+
--mount=type=cache,target=/root/.cache/go-build \
23+
make -j4 init
1024

11-
CMD ["make", "init", "run-race"]
25+
CMD ["make", "test-report"]

api-tests/Makefile

Lines changed: 90 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,93 @@
1-
all: build
1+
.DEFAULT_GOAL := help
2+
BIN_DIR := $(CURDIR)/bin
23

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)"
445
cd tools && go generate -x -tags=tools
546

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."

api-tests/README.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,33 @@ Make sure you have the latest Go version installed on your systems, execute the
88
to set up API-tests in your local systems.
99

1010
1. Run PMM Server. This can be done by running `make env-up` in the root (`pmm`) directory.
11-
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`.
11+
2. Replace `$PMM_SERVER_URL` with a URL in format `https://USERNAME:PASSWORD@HOST`. For local development it's usually `https://admin:admin@127.0.0.1`.
1212

1313
# Usage
1414

15+
Precompile tests using the following command:
16+
```
17+
make init
18+
```
19+
1520
Run the tests using the following command:
1621

1722
```
18-
go test ./... -pmm.server-url $PMM_SERVER_URL -v
23+
PMM_SERVER_URL=$PMM_SERVER_URL make test
1924
```
2025

2126
# Docker
2227

2328
Build Docker image using the following command:
2429

2530
```
26-
docker build -t IMAGENAME .
31+
make docker-build-image
2732
```
2833

2934
Run Docker container using the following command:
3035

3136
```
32-
docker run -e PMM_SERVER_URL=**pmm-server-url** IMAGENAME
37+
PMM_SERVER_URL=$PMM_SERVER_URL make docker-run-tests
3338
```
3439

3540
where `PMM_SERVER_URL` should be pointing to a running PMM Server.

0 commit comments

Comments
 (0)