Skip to content

Commit 83b47ca

Browse files
fix(influxdb): Respect custom waitStrategy (#2845)
* Respect custom waitStrategy for InfluxDB (#1) fix(influxdb): Respect passed waitStrategy * Refactor test to use require for container state assertion Co-authored-by: Steven Hartland <stevenmhartland@gmail.com> * Change default wait strategy from checking logs to health check for influxdb2 (#2) * /health check waitStrategy for influx1 and influx2 * Undo health endpoint test after default strategy was changed * Update http health check to verify JSON status value (#3) * Move Shutdown check to WithInitDb (#4) Move Shutdown check to WithInitDb * Remove slice Co-authored-by: Steven Hartland <stevenmhartland@gmail.com> * Simplify comment Co-authored-by: Steven Hartland <stevenmhartland@gmail.com> * More precise WitInitDb domment --------- Co-authored-by: Steven Hartland <stevenmhartland@gmail.com>
1 parent 1b26907 commit 83b47ca

File tree

1 file changed

+24
-37
lines changed

1 file changed

+24
-37
lines changed

modules/influxdb/influxdb.go

Lines changed: 24 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ package influxdb
22

33
import (
44
"context"
5+
"encoding/json"
56
"fmt"
7+
"io"
68
"path"
7-
"strings"
89

910
"github.com/testcontainers/testcontainers-go"
1011
"github.com/testcontainers/testcontainers-go/wait"
@@ -34,7 +35,7 @@ func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustom
3435
"INFLUXDB_HTTP_HTTPS_ENABLED": "false",
3536
"INFLUXDB_HTTP_AUTH_ENABLED": "false",
3637
},
37-
WaitingFor: wait.ForListeningPort("8086/tcp"),
38+
WaitingFor: waitForHttpHealth(),
3839
}
3940
genericContainerReq := testcontainers.GenericContainerRequest{
4041
ContainerRequest: req,
@@ -47,38 +48,6 @@ func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustom
4748
}
4849
}
4950

50-
hasInitDb := false
51-
52-
for _, f := range genericContainerReq.Files {
53-
if f.ContainerFilePath == "/" && strings.HasSuffix(f.HostFilePath, "docker-entrypoint-initdb.d") {
54-
// Init service in container will start influxdb, run scripts in docker-entrypoint-initdb.d and then
55-
// terminate the influxdb server, followed by restart of influxdb. This is tricky to wait for, and
56-
// in this case, we are assuming that data was added by init script, so we then look for an
57-
// "Open shard" which is the last thing that happens before the server is ready to accept connections.
58-
// This is probably different for InfluxDB 2.x, but that is left as an exercise for the reader.
59-
strategies := []wait.Strategy{
60-
genericContainerReq.WaitingFor,
61-
wait.ForLog("influxdb init process in progress..."),
62-
wait.ForLog("Server shutdown completed"),
63-
wait.ForLog("Opened shard"),
64-
}
65-
genericContainerReq.WaitingFor = wait.ForAll(strategies...)
66-
hasInitDb = true
67-
break
68-
}
69-
}
70-
71-
if !hasInitDb {
72-
if lastIndex := strings.LastIndex(genericContainerReq.Image, ":"); lastIndex != -1 {
73-
tag := genericContainerReq.Image[lastIndex+1:]
74-
if tag == "latest" || tag[0] == '2' {
75-
genericContainerReq.WaitingFor = wait.ForLog(`Listening log_id=[0-9a-zA-Z_~]+ service=tcp-listener transport=http`).AsRegexp()
76-
}
77-
} else {
78-
genericContainerReq.WaitingFor = wait.ForLog("Listening for signals")
79-
}
80-
}
81-
8251
container, err := testcontainers.GenericContainer(ctx, genericContainerReq)
8352
var c *InfluxDbContainer
8453
if container != nil {
@@ -147,9 +116,8 @@ func WithConfigFile(configFile string) testcontainers.CustomizeRequestOption {
147116
}
148117
}
149118

150-
// WithInitDb will copy a 'docker-entrypoint-initdb.d' directory to the container.
151-
// The secPath is the path to the directory on the host machine.
152-
// The directory will be copied to the root of the container.
119+
// WithInitDb returns a request customizer that initialises the database using the file `docker-entrypoint-initdb.d`
120+
// located in `srcPath` directory.
153121
func WithInitDb(srcPath string) testcontainers.CustomizeRequestOption {
154122
return func(req *testcontainers.GenericContainerRequest) error {
155123
cf := testcontainers.ContainerFile{
@@ -158,6 +126,25 @@ func WithInitDb(srcPath string) testcontainers.CustomizeRequestOption {
158126
FileMode: 0o755,
159127
}
160128
req.Files = append(req.Files, cf)
129+
130+
req.WaitingFor = wait.ForAll(
131+
wait.ForLog("Server shutdown completed"),
132+
waitForHttpHealth(),
133+
)
161134
return nil
162135
}
163136
}
137+
138+
func waitForHttpHealth() *wait.HTTPStrategy {
139+
return wait.ForHTTP("/health").
140+
WithResponseMatcher(func(body io.Reader) bool {
141+
decoder := json.NewDecoder(body)
142+
r := struct {
143+
Status string `json:"status"`
144+
}{}
145+
if err := decoder.Decode(&r); err != nil {
146+
return false
147+
}
148+
return r.Status == "pass"
149+
})
150+
}

0 commit comments

Comments
 (0)