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 committed Jun 14, 2024
1 parent 9d32f8e commit f331866
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 @@ -77,9 +77,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.Request) error {
req.Cmd = append(req.Cmd, "--replSet", "rs")
req.Cmd = append(req.Cmd, "--replSet", replSetName)
req.LifecycleHooks = append(req.LifecycleHooks, testcontainers.LifecycleHooks{
PostStarts: []testcontainers.StartedContainerHook{
func(ctx context.Context, c testcontainers.StartedContainer) error {
Expand All @@ -88,7 +88,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.RequestCustomizer{
testcontainers.WithImage("mongo:4"),
mongodb.WithReplicaSet(),
mongodb.WithReplicaSet("rs"),
},
},
{
name: "With Replica set and mongo:6",
opts: []testcontainers.RequestCustomizer{
testcontainers.WithImage("mongo:6"),
mongodb.WithReplicaSet(),
mongodb.WithReplicaSet("rs"),
},
},
}
Expand Down

0 comments on commit f331866

Please sign in to comment.