GetMSSQLContainer creates a Microsoft SQL Server container for testing
(ctx context.Context)
| 404 | |
| 405 | // GetMSSQLContainer creates a Microsoft SQL Server container for testing |
| 406 | func GetMSSQLContainer(ctx context.Context) (retC *Container, retErr error) { |
| 407 | req := testcontainers.ContainerRequest{ |
| 408 | Image: "mcr.microsoft.com/mssql/server:2022-latest", |
| 409 | Env: map[string]string{ |
| 410 | "ACCEPT_EULA": "Y", |
| 411 | "SA_PASSWORD": "Test123!", |
| 412 | "MSSQL_PID": "Express", |
| 413 | }, |
| 414 | ExposedPorts: []string{"1433/tcp"}, |
| 415 | WaitingFor: wait.ForLog("SQL Server is now ready for client connections"). |
| 416 | WithStartupTimeout(3 * time.Minute), |
| 417 | } |
| 418 | |
| 419 | c, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{ |
| 420 | ContainerRequest: req, |
| 421 | Started: true, |
| 422 | }) |
| 423 | if err != nil { |
| 424 | return nil, err |
| 425 | } |
| 426 | |
| 427 | host, err := c.Host(ctx) |
| 428 | if err != nil { |
| 429 | return nil, err |
| 430 | } |
| 431 | port, err := c.MappedPort(ctx, "1433/tcp") |
| 432 | if err != nil { |
| 433 | return nil, err |
| 434 | } |
| 435 | |
| 436 | // MSSQL connection string format |
| 437 | dsn := fmt.Sprintf("sqlserver://sa:Test123!@%s:%s?database=master", host, port.Port()) |
| 438 | db, err := sql.Open("sqlserver", dsn) |
| 439 | if err != nil { |
| 440 | return nil, err |
| 441 | } |
| 442 | defer func() { |
| 443 | if retErr != nil { |
| 444 | db.Close() |
| 445 | } |
| 446 | }() |
| 447 | |
| 448 | if err := waitDBPing(ctx, db); err != nil { |
| 449 | return nil, err |
| 450 | } |
| 451 | |
| 452 | return &Container{ |
| 453 | container: c, |
| 454 | host: host, |
| 455 | port: port.Port(), |
| 456 | db: db, |
| 457 | }, nil |
| 458 | } |
| 459 | |
| 460 | // GetTestMSSQLContainer is a helper function for tests that creates a MSSQL container |
| 461 | // and handles the error by failing the test if container creation fails |
no test coverage detected