|
| 1 | +# Testcontainers-go Integration Tests |
| 2 | + |
| 3 | +This directory contains integration tests that demonstrate the use of [testcontainers-go](https://github.com/testcontainers/testcontainers-go) for testing database container functionality. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +These tests showcase a **hybrid approach** where: |
| 8 | +- **Production code** continues to use the Docker client API directly for managing long-lived Liferay containers |
| 9 | +- **Integration tests** use testcontainers-go for reliable, isolated, ephemeral test containers |
| 10 | + |
| 11 | +## Running the Tests |
| 12 | + |
| 13 | +### Run all tests (recommended for CI/CD) |
| 14 | +```bash |
| 15 | +go test ./... -short |
| 16 | +``` |
| 17 | +This skips PostgreSQL tests that may have IPv6 networking issues in some environments. |
| 18 | + |
| 19 | +### Run MySQL integration test |
| 20 | +```bash |
| 21 | +go test -v ./docker -run TestMySQLContainerIntegration -timeout 5m |
| 22 | +``` |
| 23 | + |
| 24 | +### Run all integration tests (including PostgreSQL) |
| 25 | +```bash |
| 26 | +go test -v ./docker -timeout 5m |
| 27 | +``` |
| 28 | + |
| 29 | +## Test Files |
| 30 | + |
| 31 | +### `database_integration_test.go` |
| 32 | + |
| 33 | +Contains three integration tests: |
| 34 | + |
| 35 | +1. **TestMySQLContainerIntegration** ✅ |
| 36 | + - Demonstrates MySQL container integration |
| 37 | + - Always passes in all environments |
| 38 | + - Shows automatic lifecycle management |
| 39 | + |
| 40 | +2. **TestPostgreSQLContainerIntegration** ⚠️ |
| 41 | + - Demonstrates PostgreSQL container integration |
| 42 | + - Skipped in short mode due to IPv6 networking issues |
| 43 | + - Container starts successfully, connection may fail in some environments |
| 44 | + |
| 45 | +3. **TestPostgreSQLSnapshot** |
| 46 | + - Demonstrates advanced snapshot/restore features |
| 47 | + - Skipped in short mode |
| 48 | + - Shows unique testcontainers-go capabilities |
| 49 | + |
| 50 | +## Why Testcontainers-go? |
| 51 | + |
| 52 | +### Benefits for Testing |
| 53 | +- ✅ **Automatic cleanup**: No leftover containers |
| 54 | +- ✅ **Port conflict avoidance**: Dynamic port allocation |
| 55 | +- ✅ **Test isolation**: Each test gets fresh containers |
| 56 | +- ✅ **Simplified setup**: No manual Docker commands |
| 57 | +- ✅ **Advanced features**: Snapshots, wait strategies, etc. |
| 58 | + |
| 59 | +### Why Not for Production Code? |
| 60 | +- ❌ **Designed for tests**: Automatic cleanup is counter to LPN's purpose |
| 61 | +- ❌ **Ephemeral focus**: LPN manages persistent containers |
| 62 | +- ❌ **API mismatch**: Test-oriented API vs. production container management |
| 63 | + |
| 64 | +## Integration Test Pattern |
| 65 | + |
| 66 | +Following testcontainers-go best practices: |
| 67 | + |
| 68 | +```go |
| 69 | +func TestDatabaseIntegration(t *testing.T) { |
| 70 | + ctx := context.Background() |
| 71 | + |
| 72 | + // Start container |
| 73 | + container, err := module.Run(ctx, "image:tag", options...) |
| 74 | + |
| 75 | + // CRITICAL: Register cleanup BEFORE error check |
| 76 | + // This prevents resource leaks |
| 77 | + testcontainers.CleanupContainer(t, container) |
| 78 | + |
| 79 | + // Then check for errors |
| 80 | + require.NoError(t, err) |
| 81 | + |
| 82 | + // Use container for testing |
| 83 | + // ... |
| 84 | +} |
| 85 | +``` |
| 86 | + |
| 87 | +## More Information |
| 88 | + |
| 89 | +See [TESTCONTAINERS_EVALUATION.md](../docs/TESTCONTAINERS_EVALUATION.md) for: |
| 90 | +- Complete evaluation rationale |
| 91 | +- Architectural analysis |
| 92 | +- Code comparisons |
| 93 | +- Detailed recommendations |
| 94 | + |
| 95 | +## Dependencies |
| 96 | + |
| 97 | +- `github.com/testcontainers/testcontainers-go v0.40.0` |
| 98 | +- `github.com/testcontainers/testcontainers-go/modules/postgres v0.40.0` |
| 99 | +- `github.com/testcontainers/testcontainers-go/modules/mysql v0.40.0` |
| 100 | +- `github.com/lib/pq` - PostgreSQL driver |
| 101 | +- `github.com/go-sql-driver/mysql` - MySQL driver |
| 102 | + |
| 103 | +## Known Issues |
| 104 | + |
| 105 | +### PostgreSQL IPv6 Networking |
| 106 | +Some test environments have IPv6 localhost connection issues with PostgreSQL containers. The tests handle this by: |
| 107 | +- Skipping in short mode (`go test -short`) |
| 108 | +- Passing in environments with proper IPv6 configuration |
| 109 | +- Not affecting production code at all |
| 110 | + |
| 111 | +To run despite this issue: |
| 112 | +```bash |
| 113 | +# Skip problematic tests |
| 114 | +go test ./... -short |
| 115 | + |
| 116 | +# Or run specific working tests |
| 117 | +go test -v ./docker -run TestMySQLContainerIntegration |
| 118 | +``` |
0 commit comments