Skip to content

Commit

Permalink
feat(mongodb): Make the replica set name configurable (#2538)
Browse files Browse the repository at this point in the history
* Make replicaset name configurable

* Updated test

* fix: quote replica set name

---------

Co-authored-by: Manuel de la Peña <mdelapenya@gmail.com>
Co-authored-by: Manuel de la Peña <social.mdelapenya@gmail.com>
  • Loading branch information
3 people authored Jun 14, 2024
1 parent bd9ac42 commit 5d68a52
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
6 changes: 3 additions & 3 deletions modules/mongodb/mongodb.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ func WithPassword(password string) testcontainers.CustomizeRequestOption {

// WithReplicaSet configures the container to run a single-node MongoDB replica set named "rs".
// It will wait until the replica set is ready.
func WithReplicaSet() testcontainers.CustomizeRequestOption {
func WithReplicaSet(replSetName string) testcontainers.CustomizeRequestOption {
return func(req *testcontainers.GenericContainerRequest) error {
req.Cmd = append(req.Cmd, "--replSet", "rs")
req.Cmd = append(req.Cmd, "--replSet", replSetName)
req.LifecycleHooks = append(req.LifecycleHooks, testcontainers.ContainerLifecycleHooks{
PostStarts: []testcontainers.ContainerHook{
func(ctx context.Context, c testcontainers.Container) error {
Expand All @@ -92,7 +92,7 @@ func WithReplicaSet() testcontainers.CustomizeRequestOption {
return err
}

cmd := eval("rs.initiate({ _id: 'rs', members: [ { _id: 0, host: '%s:27017' } ] })", ip)
cmd := eval("rs.initiate({ _id: '%s', members: [ { _id: 0, host: '%s:27017' } ] })", replSetName, ip)
return wait.ForExec(cmd).WaitUntilReady(ctx, c)
},
},
Expand Down
4 changes: 2 additions & 2 deletions modules/mongodb/mongodb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ func TestMongoDB(t *testing.T) {
name: "With Replica set and mongo:4",
opts: []testcontainers.ContainerCustomizer{
testcontainers.WithImage("mongo:4"),
mongodb.WithReplicaSet(),
mongodb.WithReplicaSet("rs"),
},
},
{
name: "With Replica set and mongo:6",
opts: []testcontainers.ContainerCustomizer{
testcontainers.WithImage("mongo:6"),
mongodb.WithReplicaSet(),
mongodb.WithReplicaSet("rs"),
},
},
}
Expand Down

0 comments on commit 5d68a52

Please sign in to comment.