Skip to content

Commit 6e93a1d

Browse files
committed
Refactor MongoDB replica set initiation and remove unused localNonLoopbackIP function
1 parent deb1fbd commit 6e93a1d

File tree

2 files changed

+4
-62
lines changed

2 files changed

+4
-62
lines changed

modules/mongodb/mongodb.go

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -191,26 +191,15 @@ func initiateReplicaSet(req *testcontainers.GenericContainerRequest, cli mongoCl
191191
req.LifecycleHooks, testcontainers.ContainerLifecycleHooks{
192192
PostStarts: []testcontainers.ContainerHook{
193193
func(ctx context.Context, c testcontainers.Container) error {
194-
// Wait for MongoDB to be ready
195-
if err := waitForMongoReady(ctx, c, cli); err != nil {
196-
return fmt.Errorf("wait for mongo: %w", err)
197-
}
198-
199-
// Initiate replica set
200-
host, err := c.Host(ctx)
201-
if err != nil {
202-
return fmt.Errorf("get host: %w", err)
203-
}
204-
mappedPort, err := c.MappedPort(ctx, "27017/tcp")
194+
ip, err := c.ContainerIP(ctx)
205195
if err != nil {
206-
return fmt.Errorf("get mapped port: %w", err)
196+
return fmt.Errorf("container ip: %w", err)
207197
}
208198

209199
cmd := cli.eval(
210-
"rs.initiate({ _id: '%s', members: [ { _id: 0, host: '%s:%s' } ] })",
200+
"rs.initiate({ _id: '%s', members: [ { _id: 0, host: '%s:27017' } ] })",
211201
replSetName,
212-
host,
213-
mappedPort.Port(),
202+
ip,
214203
)
215204
return wait.ForExec(cmd).WaitUntilReady(ctx, c)
216205
},

modules/mongodb/mongodb_test.go

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ package mongodb_test
22

33
import (
44
"context"
5-
"errors"
65
"fmt"
7-
"net"
86
"net/url"
97
"testing"
108

@@ -17,52 +15,7 @@ import (
1715
"github.com/testcontainers/testcontainers-go/modules/mongodb"
1816
)
1917

20-
func localNonLoopbackIP() (string, error) {
21-
interfaces, err := net.Interfaces()
22-
if err != nil {
23-
return "", fmt.Errorf("list network interfaces: %w", err)
24-
}
25-
26-
for _, iface := range interfaces {
27-
// Skip down or loopback interfaces.
28-
if iface.Flags&net.FlagUp == 0 || iface.Flags&net.FlagLoopback != 0 {
29-
continue
30-
}
31-
32-
addrs, err := iface.Addrs()
33-
if err != nil {
34-
continue // try next interface
35-
}
36-
for _, addr := range addrs {
37-
var ip net.IP
38-
switch v := addr.(type) {
39-
case *net.IPNet:
40-
ip = v.IP
41-
case *net.IPAddr:
42-
ip = v.IP
43-
default:
44-
continue
45-
}
46-
// Check if it's a valid IPv4 and not loopback.
47-
if ip.IsLoopback() {
48-
continue
49-
}
50-
ip = ip.To4()
51-
if ip == nil {
52-
continue // not IPv4
53-
}
54-
return ip.String(), nil
55-
}
56-
}
57-
return "", errors.New("no non-loopback IPv4 address found")
58-
}
59-
6018
func TestMongoDB(t *testing.T) {
61-
host, err := localNonLoopbackIP()
62-
if err != nil {
63-
host = "host.docker.internal"
64-
}
65-
t.Setenv("TESTCONTAINERS_HOST_OVERRIDE", host)
6619
type tests struct {
6720
name string
6821
img string

0 commit comments

Comments
 (0)