Skip to content

Commit d68b675

Browse files
mdelapenyaclaude
andauthored
chore(inbucket|influxdb|mongodb|k3s): use Run function (#3413)
* chore(inbucket): use Run function 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]> * chore(influxdb): use Run function 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]> * chore(mongodb): use Run function * chore(k3s): use Run function * chore(influx): use primitive options * chore: simplify --------- Co-authored-by: Claude <[email protected]>
1 parent 924dab6 commit d68b675

File tree

4 files changed

+198
-205
lines changed

4 files changed

+198
-205
lines changed

modules/inbucket/inbucket.go

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -36,35 +36,24 @@ func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomize
3636

3737
// Run creates an instance of the Inbucket container type
3838
func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustomizer) (*InbucketContainer, error) {
39-
req := testcontainers.ContainerRequest{
40-
Image: img,
41-
ExposedPorts: []string{"2500/tcp", "9000/tcp", "1100/tcp"},
42-
WaitingFor: wait.ForAll(
39+
moduleOpts := []testcontainers.ContainerCustomizer{
40+
testcontainers.WithExposedPorts("2500/tcp", "9000/tcp", "1100/tcp"),
41+
testcontainers.WithWaitStrategy(
4342
wait.ForListeningPort("2500/tcp"),
4443
wait.ForListeningPort("9000/tcp"),
4544
wait.ForListeningPort("1100/tcp"),
4645
),
4746
}
47+
moduleOpts = append(moduleOpts, opts...)
4848

49-
genericContainerReq := testcontainers.GenericContainerRequest{
50-
ContainerRequest: req,
51-
Started: true,
52-
}
53-
54-
for _, opt := range opts {
55-
if err := opt.Customize(&genericContainerReq); err != nil {
56-
return nil, err
57-
}
58-
}
59-
60-
container, err := testcontainers.GenericContainer(ctx, genericContainerReq)
49+
container, err := testcontainers.Run(ctx, img, moduleOpts...)
6150
var c *InbucketContainer
6251
if container != nil {
6352
c = &InbucketContainer{Container: container}
6453
}
6554

6655
if err != nil {
67-
return c, fmt.Errorf("generic container: %w", err)
56+
return c, fmt.Errorf("run inbucket: %w", err)
6857
}
6958

7059
return c, nil

modules/influxdb/influxdb.go

Lines changed: 50 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -28,38 +28,28 @@ func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomize
2828

2929
// Run creates an instance of the InfluxDB container type
3030
func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustomizer) (*InfluxDbContainer, error) {
31-
req := testcontainers.ContainerRequest{
32-
Image: img,
33-
ExposedPorts: []string{"8086/tcp", "8088/tcp"},
34-
Env: map[string]string{
31+
moduleOpts := []testcontainers.ContainerCustomizer{
32+
testcontainers.WithExposedPorts("8086/tcp", "8088/tcp"),
33+
testcontainers.WithEnv(map[string]string{
3534
"INFLUXDB_BIND_ADDRESS": ":8088",
3635
"INFLUXDB_HTTP_BIND_ADDRESS": ":8086",
3736
"INFLUXDB_REPORTING_DISABLED": "true",
3837
"INFLUXDB_MONITOR_STORE_ENABLED": "false",
3938
"INFLUXDB_HTTP_HTTPS_ENABLED": "false",
4039
"INFLUXDB_HTTP_AUTH_ENABLED": "false",
41-
},
42-
WaitingFor: waitForHTTPHealth(),
43-
}
44-
genericContainerReq := testcontainers.GenericContainerRequest{
45-
ContainerRequest: req,
46-
Started: true,
47-
}
48-
49-
for _, opt := range opts {
50-
if err := opt.Customize(&genericContainerReq); err != nil {
51-
return nil, err
52-
}
40+
}),
41+
testcontainers.WithWaitStrategy(waitForHTTPHealth()),
5342
}
43+
moduleOpts = append(moduleOpts, opts...)
5444

55-
container, err := testcontainers.GenericContainer(ctx, genericContainerReq)
45+
container, err := testcontainers.Run(ctx, img, moduleOpts...)
5646
var c *InfluxDbContainer
5747
if container != nil {
5848
c = &InfluxDbContainer{Container: container}
5949
}
6050

6151
if err != nil {
62-
return c, fmt.Errorf("generic container: %w", err)
52+
return c, fmt.Errorf("run influxdb: %w", err)
6353
}
6454

6555
return c, nil
@@ -80,36 +70,29 @@ func (c *InfluxDbContainer) ConnectionUrl(ctx context.Context) (string, error) {
8070
}
8171

8272
func WithUsername(username string) testcontainers.CustomizeRequestOption {
83-
return func(req *testcontainers.GenericContainerRequest) error {
84-
req.Env["INFLUXDB_USER"] = username
85-
return nil
86-
}
73+
return testcontainers.WithEnv(map[string]string{
74+
"INFLUXDB_USER": username,
75+
})
8776
}
8877

8978
func WithPassword(password string) testcontainers.CustomizeRequestOption {
90-
return func(req *testcontainers.GenericContainerRequest) error {
91-
req.Env["INFLUXDB_PASSWORD"] = password
92-
return nil
93-
}
79+
return testcontainers.WithEnv(map[string]string{
80+
"INFLUXDB_PASSWORD": password,
81+
})
9482
}
9583

9684
func WithDatabase(database string) testcontainers.CustomizeRequestOption {
97-
return func(req *testcontainers.GenericContainerRequest) error {
98-
req.Env["INFLUXDB_DATABASE"] = database
99-
return nil
100-
}
85+
return testcontainers.WithEnv(map[string]string{
86+
"INFLUXDB_DATABASE": database,
87+
})
10188
}
10289

10390
func WithConfigFile(configFile string) testcontainers.CustomizeRequestOption {
104-
return func(req *testcontainers.GenericContainerRequest) error {
105-
cf := testcontainers.ContainerFile{
106-
HostFilePath: configFile,
107-
ContainerFilePath: "/etc/influxdb/influxdb.conf",
108-
FileMode: 0o755,
109-
}
110-
req.Files = append(req.Files, cf)
111-
return nil
112-
}
91+
return testcontainers.WithFiles(testcontainers.ContainerFile{
92+
HostFilePath: configFile,
93+
ContainerFilePath: "/etc/influxdb/influxdb.conf",
94+
FileMode: 0o755,
95+
})
11396
}
11497

11598
// withV2 configures the influxdb container to be compatible with InfluxDB v2
@@ -122,21 +105,17 @@ func withV2(req *testcontainers.GenericContainerRequest, org, bucket string) err
122105
return errors.New("bucket name is required")
123106
}
124107

125-
req.Env["DOCKER_INFLUXDB_INIT_ORG"] = org
126-
req.Env["DOCKER_INFLUXDB_INIT_BUCKET"] = bucket
127-
req.Env["DOCKER_INFLUXDB_INIT_MODE"] = "setup" // Always setup, we wont be migrating from v1 to v2
128-
return nil
108+
return testcontainers.WithEnv(map[string]string{
109+
"DOCKER_INFLUXDB_INIT_ORG": org,
110+
"DOCKER_INFLUXDB_INIT_BUCKET": bucket,
111+
"DOCKER_INFLUXDB_INIT_MODE": "setup", // Always setup, we wont be migrating from v1 to v2
112+
})(req)
129113
}
130114

131115
// WithV2 configures the influxdb container to be compatible with InfluxDB v2
132116
func WithV2(org, bucket string) testcontainers.CustomizeRequestOption {
133117
return func(req *testcontainers.GenericContainerRequest) error {
134-
err := withV2(req, org, bucket)
135-
if err != nil {
136-
return err
137-
}
138-
139-
return nil
118+
return withV2(req, org, bucket)
140119
}
141120
}
142121

@@ -168,9 +147,10 @@ func WithV2Auth(org, bucket, username, password string) testcontainers.Customize
168147
return errors.New("username and password file already set, use either WithV2Auth or WithV2SecretsAuth")
169148
}
170149

171-
req.Env["DOCKER_INFLUXDB_INIT_USERNAME"] = username
172-
req.Env["DOCKER_INFLUXDB_INIT_PASSWORD"] = password
173-
return nil
150+
return testcontainers.WithEnv(map[string]string{
151+
"DOCKER_INFLUXDB_INIT_USERNAME": username,
152+
"DOCKER_INFLUXDB_INIT_PASSWORD": password,
153+
})(req)
174154
}
175155
}
176156

@@ -195,9 +175,10 @@ func WithV2SecretsAuth(org, bucket, usernameFile, passwordFile string) testconta
195175
return err
196176
}
197177

198-
req.Env["DOCKER_INFLUXDB_INIT_USERNAME_FILE"] = secretsPath(usernameFile)
199-
req.Env["DOCKER_INFLUXDB_INIT_PASSWORD_FILE"] = secretsPath(passwordFile)
200-
return nil
178+
return testcontainers.WithEnv(map[string]string{
179+
"DOCKER_INFLUXDB_INIT_USERNAME_FILE": secretsPath(usernameFile),
180+
"DOCKER_INFLUXDB_INIT_PASSWORD_FILE": secretsPath(passwordFile),
181+
})(req)
201182
}
202183
}
203184

@@ -208,9 +189,9 @@ func WithV2Retention(retention time.Duration) testcontainers.CustomizeRequestOpt
208189
return errors.New("retention is required")
209190
}
210191

211-
req.Env["DOCKER_INFLUXDB_INIT_RETENTION"] = retention.String()
212-
213-
return nil
192+
return testcontainers.WithEnv(map[string]string{
193+
"DOCKER_INFLUXDB_INIT_RETENTION": retention.String(),
194+
})(req)
214195
}
215196
}
216197

@@ -225,9 +206,9 @@ func WithV2AdminToken(token string) testcontainers.CustomizeRequestOption {
225206
return errors.New("admin token file already set, use either WithV2AdminToken or WithV2SecretsAdminToken")
226207
}
227208

228-
req.Env["DOCKER_INFLUXDB_INIT_ADMIN_TOKEN"] = token
229-
230-
return nil
209+
return testcontainers.WithEnv(map[string]string{
210+
"DOCKER_INFLUXDB_INIT_ADMIN_TOKEN": token,
211+
})(req)
231212
}
232213
}
233214

@@ -242,9 +223,9 @@ func WithV2SecretsAdminToken(tokenFile string) testcontainers.CustomizeRequestOp
242223
return errors.New("admin token already set, use either WithV2AdminToken or WithV2SecretsAdminToken")
243224
}
244225

245-
req.Env["DOCKER_INFLUXDB_INIT_ADMIN_TOKEN_FILE"] = secretsPath(tokenFile)
246-
247-
return nil
226+
return testcontainers.WithEnv(map[string]string{
227+
"DOCKER_INFLUXDB_INIT_ADMIN_TOKEN_FILE": secretsPath(tokenFile),
228+
})(req)
248229
}
249230
}
250231

@@ -254,18 +235,18 @@ func WithV2SecretsAdminToken(tokenFile string) testcontainers.CustomizeRequestOp
254235
//nolint:staticcheck //FIXME
255236
func WithInitDb(srcPath string) testcontainers.CustomizeRequestOption {
256237
return func(req *testcontainers.GenericContainerRequest) error {
257-
cf := testcontainers.ContainerFile{
238+
if err := testcontainers.WithFiles(testcontainers.ContainerFile{
258239
HostFilePath: path.Join(srcPath, "docker-entrypoint-initdb.d"),
259240
ContainerFilePath: "/",
260241
FileMode: 0o755,
242+
})(req); err != nil {
243+
return err
261244
}
262-
req.Files = append(req.Files, cf)
263245

264-
req.WaitingFor = wait.ForAll(
246+
return testcontainers.WithAdditionalWaitStrategy(
265247
wait.ForLog("Server shutdown completed"),
266248
waitForHTTPHealth(),
267-
)
268-
return nil
249+
)(req)
269250
}
270251
}
271252

modules/k3s/k3s.go

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -61,51 +61,38 @@ func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustom
6161
return nil, err
6262
}
6363

64-
req := testcontainers.ContainerRequest{
65-
Image: img,
66-
ExposedPorts: []string{
67-
defaultKubeSecurePort,
68-
defaultRancherWebhookPort,
69-
},
70-
HostConfigModifier: func(hc *container.HostConfig) {
64+
moduleOpts := []testcontainers.ContainerCustomizer{
65+
testcontainers.WithExposedPorts(defaultKubeSecurePort, defaultRancherWebhookPort),
66+
testcontainers.WithHostConfigModifier(func(hc *container.HostConfig) {
7167
hc.Privileged = true
7268
hc.CgroupnsMode = "host"
7369
hc.Tmpfs = map[string]string{
7470
"/run": "",
7571
"/var/run": "",
7672
}
7773
hc.Mounts = []mount.Mount{}
78-
},
79-
Cmd: []string{
74+
}),
75+
testcontainers.WithCmd(
8076
"server",
8177
"--disable=traefik",
82-
"--tls-san=" + host, // Host which will be used to access the Kubernetes server from tests.
83-
},
84-
Env: map[string]string{
78+
"--tls-san="+host, // Host which will be used to access the Kubernetes server from tests.
79+
),
80+
testcontainers.WithEnv(map[string]string{
8581
"K3S_KUBECONFIG_MODE": "644",
86-
},
87-
WaitingFor: wait.ForLog(".*Node controller sync successful.*").AsRegexp(),
82+
}),
83+
testcontainers.WithWaitStrategy(wait.ForLog(".*Node controller sync successful.*").AsRegexp()),
8884
}
8985

90-
genericContainerReq := testcontainers.GenericContainerRequest{
91-
ContainerRequest: req,
92-
Started: true,
93-
}
94-
95-
for _, opt := range opts {
96-
if err := opt.Customize(&genericContainerReq); err != nil {
97-
return nil, err
98-
}
99-
}
86+
moduleOpts = append(moduleOpts, opts...)
10087

101-
container, err := testcontainers.GenericContainer(ctx, genericContainerReq)
88+
ctr, err := testcontainers.Run(ctx, img, moduleOpts...)
10289
var c *K3sContainer
103-
if container != nil {
104-
c = &K3sContainer{Container: container}
90+
if ctr != nil {
91+
c = &K3sContainer{Container: ctr}
10592
}
10693

10794
if err != nil {
108-
return c, fmt.Errorf("generic container: %w", err)
95+
return c, fmt.Errorf("run k3s: %w", err)
10996
}
11097

11198
return c, nil

0 commit comments

Comments
 (0)