Skip to content

Commit

Permalink
feat(mongodb): add WithReplicaSet option
Browse files Browse the repository at this point in the history
The `WithReplicaSet` option configures the MongoDB container to run a a
single-node replica set named "rs." The container will wait until the
replica set is ready.
  • Loading branch information
heiytor committed May 7, 2024
1 parent b5541e6 commit 504cd35
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 12 deletions.
24 changes: 24 additions & 0 deletions modules/mongodb/mongodb.go
Expand Up @@ -79,6 +79,18 @@ func WithPassword(password string) testcontainers.CustomizeRequestOption {
}
}

// WithReplicaSet configures the container to run a single-node MongoDB replica set named "rs".
func WithReplicaSet() testcontainers.CustomizeRequestOption {
return func(req *testcontainers.GenericContainerRequest) error {
req.Cmd = append(req.Cmd, "--replSet", "rs")

cmd := eval("rs.initiate({ _id: 'rs', members: [ { _id: 0, host: '%s:27017' } ] })", "name-here")
req.WaitingFor = wait.ForAll(req.WaitingFor, wait.ForExec(cmd))

return nil
}
}

// ConnectionString returns the connection string for the MongoDB container.
// If you provide a username and a password, the connection string will also include them.
func (c *MongoDBContainer) ConnectionString(ctx context.Context) (string, error) {
Expand All @@ -95,3 +107,15 @@ func (c *MongoDBContainer) ConnectionString(ctx context.Context) (string, error)
}
return c.Endpoint(ctx, "mongodb")
}

// eval builds an mongosh|mongo eval command.
func eval(command string, args ...any) []string {
command = "\"" + fmt.Sprintf(command, args...) + "\""

return []string{
"sh",
"-c",
// In previous versions, the binary "mongosh" was named "mongo".
"mongosh --quiet --eval " + command + " || mongo --quiet --eval " + command,
}
}
44 changes: 32 additions & 12 deletions modules/mongodb/mongodb_test.go
Expand Up @@ -14,32 +14,52 @@ import (

func TestMongoDB(t *testing.T) {
type tests struct {
name string
image string
name string
opts []testcontainers.ContainerCustomizer
}
testCases := []tests{
{
name: "From Docker Hub",
image: "mongo:6",
name: "From Docker Hub",
opts: []testcontainers.ContainerCustomizer{
testcontainers.WithImage("mongo:6"),
},
},
{
name: "Community Server",
image: "mongodb/mongodb-community-server:7.0.2-ubi8",
name: "Community Server",
opts: []testcontainers.ContainerCustomizer{
testcontainers.WithImage("mongodb/mongodb-community-server:7.0.2-ubi8"),
},
},
{
name: "Enterprise Server",
image: "mongodb/mongodb-enterprise-server:7.0.0-ubi8",
name: "Enterprise Server",
opts: []testcontainers.ContainerCustomizer{
testcontainers.WithImage("mongodb/mongodb-enterprise-server:7.0.0-ubi8"),
},
},
{
name: "With Replica set and mongo:4",
opts: []testcontainers.ContainerCustomizer{
testcontainers.WithImage("mongo:4"),
mongodb.WithReplicaSet(),
},
},
{
name: "With Replica set and mongo:6",
opts: []testcontainers.ContainerCustomizer{
testcontainers.WithImage("mongo:6"),
mongodb.WithReplicaSet(),
},
},
}

for _, tc := range testCases {
image := tc.image
t.Run(image, func(t *testing.T) {
for _, tt := range testCases {
tc := tt
t.Run(tc.name, func(t *testing.T) {
t.Parallel()

ctx := context.Background()

mongodbContainer, err := mongodb.RunContainer(ctx, testcontainers.WithImage(image))
mongodbContainer, err := mongodb.RunContainer(ctx, tc.opts...)
if err != nil {
t.Fatalf("failed to start container: %s", err)
}
Expand Down

0 comments on commit 504cd35

Please sign in to comment.