Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 2 additions & 10 deletions .github/workflows/api-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,7 @@ jobs:
- name: Build the test image
shell: bash
run: |
echo "Make sure to set the context to the root of the repository."
docker buildx build -f api-tests/Dockerfile -t percona/pmm-api-tests .
make docker-build-image

- name: Run compose up for test DBs
shell: bash
Expand All @@ -98,14 +97,7 @@ jobs:
- name: Run API tests
shell: bash
run: |
docker run \
-e PMM_SERVER_URL=${{env.PMM_URL}} \
-e PMM_RUN_UPDATE_TEST=0 \
-e PMM_RUN_ADVISOR_TESTS=0 \
-e PMM_SERVER_INSECURE_TLS=1 \
--name pmm-api-tests \
--network host \
percona/pmm-api-tests
PMM_SERVER_URL=${{env.PMM_URL}} make docker-run-tests

- name: Get PMM logs
if: ${{ failure() }}
Expand Down
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ compose.yml
build.log
ci.yml

api-tests/pmm-api-tests-output.txt
api-tests/pmm-api-tests-junit-report.xml
api-tests/*.txt
api-tests/*.xml

packer.log
encryption.key
Expand Down
24 changes: 19 additions & 5 deletions api-tests/Dockerfile
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'
Comment thread
maxkondr marked this conversation as resolved.

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"]
115 changes: 90 additions & 25 deletions api-tests/Makefile
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: ## Display this help message
@echo "Please use \`make <target>\`, where <target> is one of the following:"
@grep -h '^[a-zA-Z]' $(MAKEFILE_LIST) | awk -F ':.*## ' 'NF==2 {printf " %-26s%s\n", $$1, $$2}'

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) ## Build tests and install development tools
@echo "-> Installing tools MAKEFLAGS: $(MAKEFLAGS)"
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 generated: $(REPORT_OUT)."

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_BUILDKIT=1 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."
13 changes: 9 additions & 4 deletions api-tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `https://admin:admin@127.0.0.1`.

# Usage

Precompile tests using the following command:
```
make init
```

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.
Expand Down
Loading
Loading