Skip to content

Commit 918e153

Browse files
authored
Merge branch 'main' into db/dolt
2 parents 606c5aa + 83ae8bf commit 918e153

File tree

2 files changed

+63
-17
lines changed

2 files changed

+63
-17
lines changed

wait/http.go

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"net/http"
1212
"net/url"
1313
"strconv"
14+
"strings"
1415
"time"
1516

1617
"github.com/docker/go-connections/nat"
@@ -27,17 +28,18 @@ type HTTPStrategy struct {
2728
timeout *time.Duration
2829

2930
// additional properties
30-
Port nat.Port
31-
Path string
32-
StatusCodeMatcher func(status int) bool
33-
ResponseMatcher func(body io.Reader) bool
34-
UseTLS bool
35-
AllowInsecure bool
36-
TLSConfig *tls.Config // TLS config for HTTPS
37-
Method string // http method
38-
Body io.Reader // http request body
39-
PollInterval time.Duration
40-
UserInfo *url.Userinfo
31+
Port nat.Port
32+
Path string
33+
StatusCodeMatcher func(status int) bool
34+
ResponseMatcher func(body io.Reader) bool
35+
UseTLS bool
36+
AllowInsecure bool
37+
TLSConfig *tls.Config // TLS config for HTTPS
38+
Method string // http method
39+
Body io.Reader // http request body
40+
PollInterval time.Duration
41+
UserInfo *url.Userinfo
42+
ForceIPv4LocalHost bool
4143
}
4244

4345
// NewHTTPStrategy constructs a HTTP strategy waiting on port 80 and status code 200
@@ -119,6 +121,13 @@ func (ws *HTTPStrategy) WithPollInterval(pollInterval time.Duration) *HTTPStrate
119121
return ws
120122
}
121123

124+
// WithForcedIPv4LocalHost forces usage of localhost to be ipv4 127.0.0.1
125+
// to avoid ipv6 docker bugs https://github.com/moby/moby/issues/42442 https://github.com/moby/moby/issues/42375
126+
func (ws *HTTPStrategy) WithForcedIPv4LocalHost() *HTTPStrategy {
127+
ws.ForceIPv4LocalHost = true
128+
return ws
129+
}
130+
122131
// ForHTTP is a convenience method similar to Wait.java
123132
// https://github.com/testcontainers/testcontainers-java/blob/1d85a3834bd937f80aad3a4cec249c027f31aeb4/core/src/main/java/org/testcontainers/containers/wait/strategy/Wait.java
124133
func ForHTTP(path string) *HTTPStrategy {
@@ -143,6 +152,10 @@ func (ws *HTTPStrategy) WaitUntilReady(ctx context.Context, target StrategyTarge
143152
if err != nil {
144153
return err
145154
}
155+
// to avoid ipv6 docker bugs https://github.com/moby/moby/issues/42442 https://github.com/moby/moby/issues/42375
156+
if ws.ForceIPv4LocalHost {
157+
ipAddress = strings.Replace(ipAddress, "localhost", "127.0.0.1", 1)
158+
}
146159

147160
var mappedPort nat.Port
148161
if ws.Port == "" {

wait/http_test.go

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func ExampleHTTPStrategy() {
3232
WaitingFor: wait.ForHTTP("/").WithStartupTimeout(10 * time.Second),
3333
}
3434

35-
gogs, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
35+
c, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
3636
ContainerRequest: req,
3737
Started: true,
3838
})
@@ -42,12 +42,12 @@ func ExampleHTTPStrategy() {
4242
// }
4343

4444
defer func() {
45-
if err := gogs.Terminate(ctx); err != nil {
45+
if err := c.Terminate(ctx); err != nil {
4646
log.Fatalf("failed to terminate container: %s", err)
4747
}
4848
}()
4949

50-
state, err := gogs.State(ctx)
50+
state, err := c.State(ctx)
5151
if err != nil {
5252
panic(err)
5353
}
@@ -67,7 +67,7 @@ func ExampleHTTPStrategy_WithPort() {
6767
WaitingFor: wait.ForHTTP("/").WithPort("80/tcp"),
6868
}
6969

70-
gogs, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
70+
c, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
7171
ContainerRequest: req,
7272
Started: true,
7373
})
@@ -77,12 +77,45 @@ func ExampleHTTPStrategy_WithPort() {
7777
// }
7878

7979
defer func() {
80-
if err := gogs.Terminate(ctx); err != nil {
80+
if err := c.Terminate(ctx); err != nil {
8181
log.Fatalf("failed to terminate container: %s", err)
8282
}
8383
}()
8484

85-
state, err := gogs.State(ctx)
85+
state, err := c.State(ctx)
86+
if err != nil {
87+
panic(err)
88+
}
89+
90+
fmt.Println(state.Running)
91+
92+
// Output:
93+
// true
94+
}
95+
96+
func ExampleHTTPStrategy_WithForcedIPv4LocalHost() {
97+
ctx := context.Background()
98+
req := testcontainers.ContainerRequest{
99+
Image: "nginx:latest",
100+
ExposedPorts: []string{"8080/tcp", "80/tcp"},
101+
WaitingFor: wait.ForHTTP("/").WithForcedIPv4LocalHost(),
102+
}
103+
104+
c, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
105+
ContainerRequest: req,
106+
Started: true,
107+
})
108+
if err != nil {
109+
panic(err)
110+
}
111+
112+
defer func() {
113+
if err := c.Terminate(ctx); err != nil {
114+
log.Fatalf("failed to terminate container: %s", err)
115+
}
116+
}()
117+
118+
state, err := c.State(ctx)
86119
if err != nil {
87120
panic(err)
88121
}

0 commit comments

Comments
 (0)