Skip to content

Commit daabf50

Browse files
authored
chore(clickhouse|k6|localstack|redpanda|registry|socat): use Run in tests (#3432)
* chore(clickhouse): use Run in tests * chore(k6): use Run in tests * chore(localstack): use Run in tests * chore(redpanda): use Run in tests * chore(registry): use Run in tests * chore(socat): use Run in tests * chore: address comments from code review * fix: use correct API
1 parent 791acab commit daabf50

File tree

8 files changed

+115
-205
lines changed

8 files changed

+115
-205
lines changed

modules/clickhouse/clickhouse_test.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -207,14 +207,7 @@ func TestClickHouseWithZookeeper(t *testing.T) {
207207
// withZookeeper {
208208
zkPort := nat.Port("2181/tcp")
209209

210-
zkcontainer, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
211-
ContainerRequest: testcontainers.ContainerRequest{
212-
ExposedPorts: []string{zkPort.Port()},
213-
Image: "zookeeper:3.7",
214-
WaitingFor: wait.ForListeningPort(zkPort),
215-
},
216-
Started: true,
217-
})
210+
zkcontainer, err := testcontainers.Run(ctx, "zookeeper:3.7", testcontainers.WithExposedPorts(zkPort.Port()), testcontainers.WithWaitStrategy(wait.ForListeningPort(zkPort)))
218211
testcontainers.CleanupContainer(t, zkcontainer)
219212
require.NoError(t, err)
220213

modules/k6/examples_test.go

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,7 @@ func ExampleRun() {
1717

1818
// create a container with the httpbin application that will be the target
1919
// for the test script that runs in the k6 container
20-
gcr := testcontainers.GenericContainerRequest{
21-
ProviderType: testcontainers.ProviderDocker,
22-
ContainerRequest: testcontainers.ContainerRequest{
23-
Image: "kennethreitz/httpbin",
24-
ExposedPorts: []string{
25-
"80",
26-
},
27-
WaitingFor: wait.ForExposedPort(),
28-
},
29-
Started: true,
30-
}
31-
httpbin, err := testcontainers.GenericContainer(ctx, gcr)
20+
httpbin, err := testcontainers.Run(ctx, "kennethreitz/httpbin", testcontainers.WithExposedPorts("80/tcp"), testcontainers.WithWaitStrategy(wait.ForExposedPort()))
3221
defer func() {
3322
if err := testcontainers.TerminateContainer(httpbin); err != nil {
3423
log.Printf("failed to terminate container: %s", err)

modules/localstack/localstack_test.go

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -224,35 +224,31 @@ func TestStartV2WithNetwork(t *testing.T) {
224224
require.NoError(t, err)
225225
require.NotNil(t, localstack)
226226

227-
networkName := nw.Name
228-
229-
cli, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
230-
ContainerRequest: testcontainers.ContainerRequest{
231-
Image: "amazon/aws-cli:2.7.27",
232-
Networks: []string{networkName},
233-
Entrypoint: []string{"tail"},
234-
Cmd: []string{"-f", "/dev/null"},
235-
Env: map[string]string{
236-
"AWS_ACCESS_KEY_ID": "accesskey",
237-
"AWS_SECRET_ACCESS_KEY": "secretkey",
238-
"AWS_REGION": "eu-west-1",
239-
},
240-
WaitingFor: wait.ForExec([]string{
241-
"/usr/local/bin/aws", "sqs", "create-queue", "--queue-name", "baz", "--region", "eu-west-1",
242-
"--endpoint-url", "http://localstack:4566", "--no-verify-ssl",
227+
cli, err := testcontainers.Run(
228+
ctx, "amazon/aws-cli:2.7.27",
229+
network.WithNetwork([]string{"cli"}, nw),
230+
testcontainers.WithEntrypoint("tail"),
231+
testcontainers.WithCmd("-f", "/dev/null"),
232+
testcontainers.WithEnv(map[string]string{
233+
"AWS_ACCESS_KEY_ID": "accesskey",
234+
"AWS_SECRET_ACCESS_KEY": "secretkey",
235+
"AWS_REGION": "eu-west-1",
236+
}),
237+
testcontainers.WithWaitStrategy(wait.ForExec([]string{
238+
"/usr/local/bin/aws", "sqs", "create-queue", "--queue-name", "baz", "--region", "eu-west-1",
239+
"--endpoint-url", "http://localstack:4566", "--no-verify-ssl",
240+
}).
241+
WithStartupTimeout(time.Second*10).
242+
WithExitCodeMatcher(func(exitCode int) bool {
243+
return exitCode == 0
243244
}).
244-
WithStartupTimeout(time.Second * 10).
245-
WithExitCodeMatcher(func(exitCode int) bool {
246-
return exitCode == 0
247-
}).
248-
WithResponseMatcher(func(r io.Reader) bool {
249-
respBytes, _ := io.ReadAll(r)
250-
resp := string(respBytes)
251-
return strings.Contains(resp, "http://localstack:4566")
252-
}),
253-
},
254-
Started: true,
255-
})
245+
WithResponseMatcher(func(r io.Reader) bool {
246+
respBytes, _ := io.ReadAll(r)
247+
resp := string(respBytes)
248+
return strings.Contains(resp, "http://localstack:4566")
249+
}),
250+
),
251+
)
256252
testcontainers.CleanupContainer(t, cli)
257253
require.NoError(t, err)
258254
require.NotNil(t, cli)

modules/redpanda/redpanda_test.go

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -587,22 +587,12 @@ func TestRedpandaListener_Simple(t *testing.T) {
587587

588588
// 3. Start KCat container
589589
// withListenerKcat {
590-
kcat, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
591-
ContainerRequest: testcontainers.ContainerRequest{
592-
Image: "confluentinc/cp-kcat:7.4.1",
593-
Networks: []string{
594-
rpNetwork.Name,
595-
},
596-
Entrypoint: []string{
597-
"sh",
598-
},
599-
Cmd: []string{
600-
"-c",
601-
"tail -f /dev/null",
602-
},
603-
},
604-
Started: true,
605-
})
590+
kcat, err := testcontainers.Run(
591+
ctx, "confluentinc/cp-kcat:7.4.1",
592+
network.WithNetwork([]string{"kcat"}, rpNetwork),
593+
testcontainers.WithEntrypoint("sh"),
594+
testcontainers.WithCmd("-c", "tail -f /dev/null"),
595+
)
606596
// }
607597
testcontainers.CleanupContainer(t, kcat)
608598
require.NoError(t, err)

modules/registry/examples_test.go

Lines changed: 27 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -73,20 +73,18 @@ func ExampleRun_withAuthentication() {
7373
// build a custom redis image from the private registry,
7474
// using RegistryName of the container as the registry.
7575

76-
redisC, err := testcontainers.GenericContainer(context.Background(), testcontainers.GenericContainerRequest{
77-
ContainerRequest: testcontainers.ContainerRequest{
78-
FromDockerfile: testcontainers.FromDockerfile{
79-
Context: filepath.Join("testdata", "redis"),
80-
BuildArgs: map[string]*string{
81-
"REGISTRY_HOST": &registryHost,
82-
},
76+
redisC, err := testcontainers.Run(
77+
context.Background(), "",
78+
testcontainers.WithDockerfile(testcontainers.FromDockerfile{
79+
Context: filepath.Join("testdata", "redis"),
80+
BuildArgs: map[string]*string{
81+
"REGISTRY_HOST": &registryHost,
8382
},
84-
AlwaysPullImage: true, // make sure the authentication takes place
85-
ExposedPorts: []string{"6379/tcp"},
86-
WaitingFor: wait.ForLog("Ready to accept connections"),
87-
},
88-
Started: true,
89-
})
83+
}),
84+
testcontainers.WithAlwaysPull(), // make sure the authentication takes place
85+
testcontainers.WithExposedPorts("6379/tcp"),
86+
testcontainers.WithWaitStrategy(wait.ForLog("Ready to accept connections")),
87+
)
9088
defer func() {
9189
if err := testcontainers.TerminateContainer(redisC); err != nil {
9290
log.Printf("failed to terminate container: %s", err)
@@ -155,22 +153,19 @@ func ExampleRun_pushImage() {
155153
repo := registryContainer.RegistryName + "/customredis"
156154
tag := "v1.2.3"
157155

158-
redisC, err := testcontainers.GenericContainer(context.Background(), testcontainers.GenericContainerRequest{
159-
ContainerRequest: testcontainers.ContainerRequest{
160-
FromDockerfile: testcontainers.FromDockerfile{
161-
Context: filepath.Join("testdata", "redis"),
162-
BuildArgs: map[string]*string{
163-
"REGISTRY_HOST": &registryHost,
164-
},
165-
Repo: repo,
166-
Tag: tag,
156+
redisC, err := testcontainers.Run(context.Background(), "",
157+
testcontainers.WithDockerfile(testcontainers.FromDockerfile{
158+
Context: filepath.Join("testdata", "redis"),
159+
BuildArgs: map[string]*string{
160+
"REGISTRY_HOST": &registryHost,
167161
},
168-
AlwaysPullImage: true, // make sure the authentication takes place
169-
ExposedPorts: []string{"6379/tcp"},
170-
WaitingFor: wait.ForLog("Ready to accept connections"),
171-
},
172-
Started: true,
173-
})
162+
Repo: repo,
163+
Tag: tag,
164+
}),
165+
testcontainers.WithAlwaysPull(), // make sure the authentication takes place
166+
testcontainers.WithExposedPorts("6379/tcp"),
167+
testcontainers.WithWaitStrategy(wait.ForLog("Ready to accept connections")),
168+
)
174169
defer func() {
175170
if err := testcontainers.TerminateContainer(redisC); err != nil {
176171
log.Printf("failed to terminate container: %s", err)
@@ -247,14 +242,10 @@ func ExampleRun_pushImage() {
247242
return
248243
}
249244

250-
newRedisC, err := testcontainers.GenericContainer(context.Background(), testcontainers.GenericContainerRequest{
251-
ContainerRequest: testcontainers.ContainerRequest{
252-
Image: taggedImage,
253-
ExposedPorts: []string{"6379/tcp"},
254-
WaitingFor: wait.ForLog("Ready to accept connections"),
255-
},
256-
Started: true,
257-
})
245+
newRedisC, err := testcontainers.Run(context.Background(), taggedImage,
246+
testcontainers.WithExposedPorts("6379/tcp"),
247+
testcontainers.WithWaitStrategy(wait.ForLog("Ready to accept connections")),
248+
)
258249
defer func() {
259250
if err := testcontainers.TerminateContainer(newRedisC); err != nil {
260251
log.Printf("failed to terminate container: %s", err)

modules/registry/registry_test.go

Lines changed: 32 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -96,20 +96,18 @@ func TestRunContainer_authenticated(t *testing.T) {
9696
t.Run("build images with wrong credentials fails", func(tt *testing.T) {
9797
setAuthConfig(tt, registryHost, "foo", "bar")
9898

99-
redisC, err := testcontainers.GenericContainer(context.Background(), testcontainers.GenericContainerRequest{
100-
ContainerRequest: testcontainers.ContainerRequest{
101-
FromDockerfile: testcontainers.FromDockerfile{
102-
Context: filepath.Join("testdata", "redis"),
103-
BuildArgs: map[string]*string{
104-
"REGISTRY_HOST": &registryHost,
105-
},
99+
redisC, err := testcontainers.Run(
100+
context.Background(), "",
101+
testcontainers.WithDockerfile(testcontainers.FromDockerfile{
102+
Context: filepath.Join("testdata", "redis"),
103+
BuildArgs: map[string]*string{
104+
"REGISTRY_HOST": &registryHost,
106105
},
107-
AlwaysPullImage: true, // make sure the authentication takes place
108-
ExposedPorts: []string{"6379/tcp"},
109-
WaitingFor: wait.ForLog("Ready to accept connections"),
110-
},
111-
Started: true,
112-
})
106+
}),
107+
testcontainers.WithAlwaysPull(), // make sure the authentication takes place
108+
testcontainers.WithExposedPorts("6379/tcp"),
109+
testcontainers.WithWaitStrategy(wait.ForLog("Ready to accept connections")),
110+
)
113111
testcontainers.CleanupContainer(tt, redisC)
114112
require.Error(tt, err)
115113
require.Contains(tt, err.Error(), "unauthorized: authentication required")
@@ -123,20 +121,18 @@ func TestRunContainer_authenticated(t *testing.T) {
123121
// The container should start because the authentication
124122
// is correct.
125123

126-
redisC, err := testcontainers.GenericContainer(context.Background(), testcontainers.GenericContainerRequest{
127-
ContainerRequest: testcontainers.ContainerRequest{
128-
FromDockerfile: testcontainers.FromDockerfile{
129-
Context: filepath.Join("testdata", "redis"),
130-
BuildArgs: map[string]*string{
131-
"REGISTRY_HOST": &registryHost,
132-
},
124+
redisC, err := testcontainers.Run(
125+
context.Background(), "",
126+
testcontainers.WithDockerfile(testcontainers.FromDockerfile{
127+
Context: filepath.Join("testdata", "redis"),
128+
BuildArgs: map[string]*string{
129+
"REGISTRY_HOST": &registryHost,
133130
},
134-
AlwaysPullImage: true, // make sure the authentication takes place
135-
ExposedPorts: []string{"6379/tcp"},
136-
WaitingFor: wait.ForLog("Ready to accept connections"),
137-
},
138-
Started: true,
139-
})
131+
}),
132+
testcontainers.WithAlwaysPull(), // make sure the authentication takes place
133+
testcontainers.WithExposedPorts("6379/tcp"),
134+
testcontainers.WithWaitStrategy(wait.ForLog("Ready to accept connections")),
135+
)
140136
testcontainers.CleanupContainer(tt, redisC)
141137
require.NoError(tt, err)
142138

@@ -256,20 +252,17 @@ func TestRunContainer_wrongData(t *testing.T) {
256252
// The container won't be able to start because the data
257253
// directory is wrong.
258254

259-
redisC, err := testcontainers.GenericContainer(context.Background(), testcontainers.GenericContainerRequest{
260-
ContainerRequest: testcontainers.ContainerRequest{
261-
FromDockerfile: testcontainers.FromDockerfile{
262-
Context: filepath.Join("testdata", "redis"),
263-
BuildArgs: map[string]*string{
264-
"REGISTRY_HOST": &registryHost,
265-
},
255+
redisC, err := testcontainers.Run(context.Background(), "",
256+
testcontainers.WithDockerfile(testcontainers.FromDockerfile{
257+
Context: filepath.Join("testdata", "redis"),
258+
BuildArgs: map[string]*string{
259+
"REGISTRY_HOST": &registryHost,
266260
},
267-
AlwaysPullImage: true, // make sure the authentication takes place
268-
ExposedPorts: []string{"6379/tcp"},
269-
WaitingFor: wait.ForLog("Ready to accept connections"),
270-
},
271-
Started: true,
272-
})
261+
}),
262+
testcontainers.WithAlwaysPull(), // make sure the authentication takes place
263+
testcontainers.WithExposedPorts("6379/tcp"),
264+
testcontainers.WithWaitStrategy(wait.ForLog("Ready to accept connections")),
265+
)
273266
testcontainers.CleanupContainer(t, redisC)
274267
require.ErrorContains(t, err, "manifest unknown")
275268
}

modules/socat/examples_test.go

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,10 @@ func ExampleRun() {
2626
}
2727
}()
2828

29-
ctr, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
30-
ContainerRequest: testcontainers.ContainerRequest{
31-
Image: "testcontainers/helloworld:1.2.0",
32-
ExposedPorts: []string{"8080/tcp"},
33-
Networks: []string{nw.Name},
34-
NetworkAliases: map[string][]string{
35-
nw.Name: {"helloworld"},
36-
},
37-
},
38-
Started: true,
39-
})
29+
ctr, err := testcontainers.Run(ctx, "testcontainers/helloworld:1.2.0",
30+
testcontainers.WithExposedPorts("8080/tcp"),
31+
network.WithNetwork([]string{"helloworld"}, nw),
32+
)
4033
if err != nil {
4134
log.Printf("failed to create container: %v", err)
4235
return
@@ -106,17 +99,10 @@ func ExampleRun_multipleTargets() {
10699
// }
107100

108101
// createHelloWorldContainer {
109-
ctr, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
110-
ContainerRequest: testcontainers.ContainerRequest{
111-
Image: "testcontainers/helloworld:1.2.0",
112-
ExposedPorts: []string{"8080/tcp"},
113-
Networks: []string{nw.Name},
114-
NetworkAliases: map[string][]string{
115-
nw.Name: {"helloworld"},
116-
},
117-
},
118-
Started: true,
119-
})
102+
ctr, err := testcontainers.Run(ctx, "testcontainers/helloworld:1.2.0",
103+
network.WithNetwork([]string{"helloworld"}, nw),
104+
testcontainers.WithExposedPorts("8080/tcp"),
105+
)
120106
if err != nil {
121107
log.Printf("failed to create container: %v", err)
122108
return

0 commit comments

Comments
 (0)