|
| 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 | +``` |
0 commit comments