Skip to content

Commit 7cd1e4f

Browse files
committed
local testing simpler
1 parent 4bcf159 commit 7cd1e4f

File tree

4 files changed

+328
-1
lines changed

4 files changed

+328
-1
lines changed

Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ check-envtest: get-pgmonitor get-external-snapshotter
215215
$(GO_TEST) -count=1 -cover -tags=envtest ./...
216216

217217
# The "PGO_TEST_TIMEOUT_SCALE" environment variable (default: 1) can be set to a
218-
# positive number that extends test timeouts. The following runs tests with
218+
# positive number that extends test timeouts. The following runs tests with
219219
# timeouts that are 20% longer than normal:
220220
# make check-envtest-existing PGO_TEST_TIMEOUT_SCALE=1.2
221221
.PHONY: check-envtest-existing
@@ -234,6 +234,10 @@ check-kuttl: ## example command: make check-kuttl KUTTL_TEST='
234234
${KUTTL_TEST} \
235235
--config testing/kuttl/kuttl-test.yaml
236236

237+
.PHONY: test-docker
238+
test-docker: ## Run tests in Docker environment (use TEST_MODE=ci|all|specific, TEST_NAME=<test>, TEST_PACKAGE=<package>)
239+
@./hack/test-docker.sh $(if $(TEST_MODE),-m $(TEST_MODE)) $(if $(TEST_NAME),-t $(TEST_NAME)) $(if $(TEST_PACKAGE),-p $(TEST_PACKAGE)) $(if $(VERBOSE),-v)
240+
237241
.PHONY: generate-kuttl
238242
generate-kuttl: export KUTTL_PG_UPGRADE_FROM_VERSION ?= 15
239243
generate-kuttl: export KUTTL_PG_UPGRADE_TO_VERSION ?= 16

TESTING.md

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
# Testing Guide
2+
3+
This document describes how to run tests for the Percona PostgreSQL Operator locally using the Docker-based testing environment.
4+
5+
## Overview
6+
7+
The operator has several types of tests:
8+
9+
- **CI Tests**: Basic tests that run in CI/CD pipelines
10+
- **Full Test Suite**: Complete test coverage including envtest
11+
- **Specific Tests**: Individual test cases for debugging
12+
13+
All tests can be run locally using Docker to ensure a consistent testing environment.
14+
15+
## Quick Start
16+
17+
### Prerequisites
18+
19+
- Docker installed and running
20+
- Bash shell (macOS/Linux)
21+
22+
### Basic Usage
23+
24+
```bash
25+
# Run CI tests (default)
26+
./test.sh
27+
28+
# Run all tests
29+
./test.sh -m all
30+
31+
# Run a specific test
32+
./test.sh -m specific -t TestReconcilePostgresClusterDataSource
33+
34+
# Show help
35+
./test.sh -h
36+
```
37+
38+
## Detailed Usage
39+
40+
### Test Modes
41+
42+
The test script supports three modes:
43+
44+
1. **CI Mode** (`-m ci`): Runs the same tests as CI/CD pipelines
45+
2. **All Mode** (`-m all`): Runs the complete test suite with envtest
46+
3. **Specific Mode** (`-m specific`): Runs individual test cases
47+
48+
### Command Line Options
49+
50+
```bash
51+
./test.sh [OPTIONS]
52+
53+
OPTIONS:
54+
-m, --mode MODE Test mode: ci, all, or specific (default: ci)
55+
-t, --test TEST Specific test to run (for specific mode)
56+
-p, --package PACKAGE Specific package to test (default: ./internal/controller/postgrescluster)
57+
-v, --verbose Enable verbose output
58+
-b, --build-only Only build the Docker image, don't run tests
59+
-h, --help Show help message
60+
```
61+
62+
### Examples
63+
64+
```bash
65+
# Run CI tests
66+
./test.sh
67+
68+
# Run full test suite with verbose output
69+
./test.sh -m all -v
70+
71+
# Run specific test in default package
72+
./test.sh -m specific -t TestReconcilePostgresClusterDataSource
73+
74+
# Run specific test in custom package
75+
./test.sh -m specific -t TestSomeFunction -p ./pkg/some/package
76+
77+
# Just build the test environment (useful for debugging)
78+
./test.sh -b
79+
80+
# Run test with verbose output
81+
./test.sh -m specific -t TestReconcilePostgresClusterDataSource -v
82+
```
83+
84+
## Using Make
85+
86+
You can also run tests using Make targets:
87+
88+
```bash
89+
# Run CI tests
90+
make test-docker
91+
92+
# Run all tests
93+
make test-docker TEST_MODE=all
94+
95+
# Run specific test
96+
make test-docker TEST_MODE=specific TEST_NAME=TestReconcilePostgresClusterDataSource
97+
98+
# Run with verbose output
99+
make test-docker TEST_MODE=specific TEST_NAME=TestReconcilePostgresClusterDataSource VERBOSE=1
100+
101+
# Run test in specific package
102+
make test-docker TEST_MODE=specific TEST_NAME=TestSomeTest TEST_PACKAGE=./pkg/some/package
103+
```
104+
105+
## Test Environment
106+
107+
The Docker test environment includes:
108+
109+
- Ubuntu latest base image
110+
- Go 1.24.3
111+
- All required dependencies (build tools, Git, curl, etc.)
112+
- Pre-configured envtest with Kubernetes 1.32
113+
- All necessary Go modules and tools
114+
115+
The environment is built from `Dockerfile.test` and provides:
116+
117+
- Consistent testing environment across different machines
118+
- Isolated test execution
119+
- All dependencies pre-installed
120+
- Environment variables properly configured
121+
122+
## Debugging Failed Tests
123+
124+
When a test fails, you can:
125+
126+
1. **Run with verbose output**:
127+
```bash
128+
./test.sh -m specific -t TestFailingTest -v
129+
```
130+
131+
2. **Build the environment and run interactively**:
132+
```bash
133+
./test.sh -b
134+
docker run --rm -it pgo-test bash
135+
```
136+
137+
3. **Run the test manually inside the container**:
138+
```bash
139+
source <(/workspace/hack/tools/setup-envtest --bin-dir=/workspace/hack/tools/envtest use 1.32 --print=env)
140+
PGO_NAMESPACE='postgres-operator' \
141+
QUERIES_CONFIG_DIR='/workspace/hack/tools/queries' \
142+
CGO_ENABLED=1 go test -v -count=1 -tags=envtest ./internal/controller/postgrescluster -run TestFailingTest
143+
```
144+
145+
## Running Tests Natively (Without Docker)
146+
147+
If you prefer to run tests natively without Docker:
148+
149+
```bash
150+
# Set up envtest
151+
make tools/setup-envtest
152+
make get-pgmonitor get-external-snapshotter
153+
154+
# Run basic tests
155+
make check
156+
157+
# Run tests with envtest
158+
make check-envtest
159+
160+
# Run specific test natively
161+
source <(hack/tools/setup-envtest --bin-dir=hack/tools/envtest use 1.32 --print=env)
162+
PGO_NAMESPACE='postgres-operator' \
163+
QUERIES_CONFIG_DIR='hack/tools/queries' \
164+
CGO_ENABLED=1 go test -v -count=1 -tags=envtest ./internal/controller/postgrescluster -run TestSpecificTest
165+
```

hack/test-docker.sh

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
# Default values
6+
TEST_MODE="all"
7+
SPECIFIC_TEST=""
8+
SPECIFIC_PACKAGE=""
9+
VERBOSE=""
10+
BUILD_ONLY=false
11+
12+
# Colors for output
13+
RED='\033[0;31m'
14+
GREEN='\033[0;32m'
15+
YELLOW='\033[1;33m'
16+
BLUE='\033[0;34m'
17+
NC='\033[0m' # No Color
18+
19+
usage() {
20+
echo "Usage: $0 [OPTIONS]"
21+
echo ""
22+
echo "Run tests in Docker environment"
23+
echo ""
24+
echo "OPTIONS:"
25+
echo " -m, --mode MODE Test mode: all, or specific (default: all)"
26+
echo " -t, --test TEST Specific test to run (for specific mode)"
27+
echo " -p, --package PACKAGE Specific package to test (default: ./internal/controller/postgrescluster)"
28+
echo " -v, --verbose Enable verbose output"
29+
echo " -b, --build-only Only build the Docker image, don't run tests"
30+
echo " -h, --help Show this help message"
31+
echo ""
32+
echo "EXAMPLES:"
33+
echo " $0 # Run all tests"
34+
echo " $0 -m specific -t TestReconcilePostgresClusterDataSource"
35+
echo " $0 -m specific -t TestSomeOtherTest -p ./pkg/some/package"
36+
echo " $0 -b # Just build the test image"
37+
echo ""
38+
}
39+
40+
log() {
41+
echo -e "${BLUE}[$(date +'%Y-%m-%d %H:%M:%S')]${NC} $1"
42+
}
43+
44+
error() {
45+
echo -e "${RED}[ERROR]${NC} $1" >&2
46+
}
47+
48+
success() {
49+
echo -e "${GREEN}[SUCCESS]${NC} $1"
50+
}
51+
52+
warning() {
53+
echo -e "${YELLOW}[WARNING]${NC} $1"
54+
}
55+
56+
# Parse command line arguments
57+
while [[ $# -gt 0 ]]; do
58+
case $1 in
59+
-m|--mode)
60+
TEST_MODE="$2"
61+
shift 2
62+
;;
63+
-t|--test)
64+
SPECIFIC_TEST="$2"
65+
shift 2
66+
;;
67+
-p|--package)
68+
SPECIFIC_PACKAGE="$2"
69+
shift 2
70+
;;
71+
-v|--verbose)
72+
VERBOSE="-v"
73+
shift
74+
;;
75+
-b|--build-only)
76+
BUILD_ONLY=true
77+
shift
78+
;;
79+
-h|--help)
80+
usage
81+
exit 0
82+
;;
83+
*)
84+
error "Unknown option: $1"
85+
usage
86+
exit 1
87+
;;
88+
esac
89+
done
90+
91+
# Validate test mode
92+
if [[ ! "$TEST_MODE" =~ ^(ci|all|specific)$ ]]; then
93+
error "Invalid test mode: $TEST_MODE. Must be 'ci', 'all', or 'specific'"
94+
exit 1
95+
fi
96+
97+
# Validate specific test requirements
98+
if [[ "$TEST_MODE" == "specific" && -z "$SPECIFIC_TEST" ]]; then
99+
error "Specific test name is required when using 'specific' mode"
100+
echo "Use -t or --test to specify the test name"
101+
exit 1
102+
fi
103+
104+
# Set default package for specific tests
105+
if [[ "$TEST_MODE" == "specific" && -z "$SPECIFIC_PACKAGE" ]]; then
106+
SPECIFIC_PACKAGE="./internal/controller/postgrescluster"
107+
fi
108+
109+
# Build Docker image
110+
log "Building Docker test environment..."
111+
if ! docker build -t pgo-test -f Dockerfile.test .; then
112+
error "Failed to build Docker test environment"
113+
exit 1
114+
fi
115+
116+
success "Docker test environment built successfully"
117+
118+
# Exit if build-only mode
119+
if [[ "$BUILD_ONLY" == true ]]; then
120+
success "Build completed. Use '$0 -m <mode>' to run tests."
121+
exit 0
122+
fi
123+
124+
# Run tests based on mode
125+
case $TEST_MODE in
126+
"ci" | "all")
127+
log "Running CI tests in Docker..."
128+
docker run --rm -it pgo-test bash -c "
129+
source <(/workspace/hack/tools/setup-envtest --bin-dir=/workspace/hack/tools/envtest use 1.32 --print=env) && \
130+
PGO_NAMESPACE='postgres-operator' \
131+
QUERIES_CONFIG_DIR='/workspace/hack/tools/queries' \
132+
make check
133+
make check-envtest
134+
"
135+
;;
136+
"specific")
137+
log "Running specific test: $SPECIFIC_TEST in package: $SPECIFIC_PACKAGE"
138+
docker run --rm -it pgo-test bash -c "
139+
source <(/workspace/hack/tools/setup-envtest --bin-dir=/workspace/hack/tools/envtest use 1.32 --print=env) && \
140+
PGO_NAMESPACE='postgres-operator' \
141+
QUERIES_CONFIG_DIR='/workspace/hack/tools/queries' \
142+
CGO_ENABLED=1 go test $VERBOSE -count=1 -tags=envtest \
143+
$SPECIFIC_PACKAGE \
144+
-run $SPECIFIC_TEST
145+
"
146+
;;
147+
esac
148+
149+
if [[ $? -eq 0 ]]; then
150+
success "Tests completed successfully!"
151+
else
152+
error "Tests failed!"
153+
exit 1
154+
fi

test.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/bash
2+
3+
# Wrapper script to run the consolidated test script
4+
exec "$(dirname "$0")/hack/test-docker.sh" "$@"

0 commit comments

Comments
 (0)