GetMongoDBContainer creates a MongoDB container for testing
(ctx context.Context)
| 566 | |
| 567 | // GetMongoDBContainer creates a MongoDB container for testing |
| 568 | func GetMongoDBContainer(ctx context.Context) (*MongoDBContainer, error) { |
| 569 | req := testcontainers.ContainerRequest{ |
| 570 | Image: "mongo:5", |
| 571 | Env: map[string]string{ |
| 572 | "MONGO_INITDB_ROOT_USERNAME": "testuser", |
| 573 | "MONGO_INITDB_ROOT_PASSWORD": "testpass", |
| 574 | }, |
| 575 | ExposedPorts: []string{"27017/tcp"}, |
| 576 | WaitingFor: wait.ForLog("Waiting for connections").WithStartupTimeout(3 * time.Minute), |
| 577 | } |
| 578 | |
| 579 | c, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{ |
| 580 | ContainerRequest: req, |
| 581 | Started: true, |
| 582 | }) |
| 583 | if err != nil { |
| 584 | return nil, err |
| 585 | } |
| 586 | |
| 587 | host, err := c.Host(ctx) |
| 588 | if err != nil { |
| 589 | return nil, err |
| 590 | } |
| 591 | port, err := c.MappedPort(ctx, "27017/tcp") |
| 592 | if err != nil { |
| 593 | return nil, err |
| 594 | } |
| 595 | |
| 596 | return &MongoDBContainer{ |
| 597 | container: c, |
| 598 | host: host, |
| 599 | port: port.Port(), |
| 600 | username: "testuser", |
| 601 | password: "testpass", |
| 602 | }, nil |
| 603 | } |
| 604 | |
| 605 | // GetTestMongoDBContainer is a helper function for tests that creates a MongoDB container |
| 606 | // and handles the error by failing the test if container creation fails |
no test coverage detected