Helper functions for integration tests createTestDatabase creates a test database instance with container
()
| 238 | |
| 239 | // createTestDatabase creates a test database instance with container |
| 240 | func createTestDatabase() (*Postgres, testcontainers.Container, error) { |
| 241 | ctx := context.Background() |
| 242 | |
| 243 | image := "postgres:15-alpine" |
| 244 | |
| 245 | container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{ |
| 246 | ContainerRequest: testcontainers.ContainerRequest{ |
| 247 | Image: image, |
| 248 | ExposedPorts: []string{"5432/tcp"}, |
| 249 | Env: map[string]string{ |
| 250 | "POSTGRES_USER": "postgres", |
| 251 | "POSTGRES_PASSWORD": "postgres", |
| 252 | "POSTGRES_DB": "permify", |
| 253 | }, |
| 254 | WaitingFor: wait.ForAll( |
| 255 | wait.ForLog("database system is ready to accept connections"), |
| 256 | wait.ForListeningPort("5432/tcp"), |
| 257 | ), |
| 258 | }, |
| 259 | Started: true, |
| 260 | }) |
| 261 | if err != nil { |
| 262 | return nil, nil, err |
| 263 | } |
| 264 | |
| 265 | // Enable track_commit_timestamp for XID tracking |
| 266 | _, _, execErr := container.Exec(ctx, []string{"psql", "-U", "postgres", "-c", "ALTER SYSTEM SET track_commit_timestamp = on;"}) |
| 267 | if execErr != nil { |
| 268 | container.Terminate(ctx) |
| 269 | return nil, nil, execErr |
| 270 | } |
| 271 | |
| 272 | // Restart container to apply the setting |
| 273 | stopTimeout := 2 * time.Second |
| 274 | err = container.Stop(context.Background(), &stopTimeout) |
| 275 | if err != nil { |
| 276 | container.Terminate(ctx) |
| 277 | return nil, nil, err |
| 278 | } |
| 279 | |
| 280 | err = container.Start(context.Background()) |
| 281 | if err != nil { |
| 282 | container.Terminate(ctx) |
| 283 | return nil, nil, err |
| 284 | } |
| 285 | |
| 286 | // Get connection details |
| 287 | host, err := container.Host(ctx) |
| 288 | if err != nil { |
| 289 | container.Terminate(ctx) |
| 290 | return nil, nil, err |
| 291 | } |
| 292 | |
| 293 | port, err := container.MappedPort(ctx, "5432") |
| 294 | if err != nil { |
| 295 | container.Terminate(ctx) |
| 296 | return nil, nil, err |
| 297 | } |
no test coverage detected