Skip to content

Commit 21b248e

Browse files
authored
chore: use the Run funcion in tests and docs (part 1) (#3304)
* chore: migrate tests to Run * docs: update quickstart with the Run function * chore: update more tests * chore: use filepath * docs: wording * chore: reduce test time
1 parent d9a7436 commit 21b248e

12 files changed

+334
-516
lines changed

container_test.go

Lines changed: 22 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ func Test_BuildImageWithContexts(t *testing.T) {
278278
ContextArchive: func() (io.ReadSeeker, error) {
279279
return nil, nil
280280
},
281-
ExpectedError: "create container: you must specify either a build context or an image",
281+
ExpectedError: "generic container: create container: you must specify either a build context or an image",
282282
},
283283
}
284284

@@ -290,19 +290,15 @@ func Test_BuildImageWithContexts(t *testing.T) {
290290
a, err := testCase.ContextArchive()
291291
require.NoError(t, err)
292292

293-
req := testcontainers.ContainerRequest{
294-
FromDockerfile: testcontainers.FromDockerfile{
293+
c, err := testcontainers.Run(
294+
ctx, "",
295+
testcontainers.WithDockerfile(testcontainers.FromDockerfile{
295296
ContextArchive: a,
296297
Context: testCase.ContextPath,
297298
Dockerfile: testCase.Dockerfile,
298-
},
299-
WaitingFor: wait.ForLog(testCase.ExpectedEchoOutput).WithStartupTimeout(1 * time.Minute),
300-
}
301-
302-
c, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
303-
ContainerRequest: req,
304-
Started: true,
305-
})
299+
}),
300+
testcontainers.WithWaitStrategy(wait.ForLog(testCase.ExpectedEchoOutput).WithStartupTimeout(1*time.Minute)),
301+
)
306302
testcontainers.CleanupContainer(t, c)
307303

308304
if testCase.ExpectedError != "" {
@@ -322,17 +318,10 @@ func TestCustomLabelsImage(t *testing.T) {
322318
)
323319

324320
ctx := context.Background()
325-
req := testcontainers.GenericContainerRequest{
326-
ContainerRequest: testcontainers.ContainerRequest{
327-
Image: "alpine:latest",
328-
Labels: map[string]string{myLabelName: myLabelValue},
329-
},
330-
}
331-
332-
ctr, err := testcontainers.GenericContainer(ctx, req)
333321

322+
ctr, err := testcontainers.Run(ctx, "alpine:latest", testcontainers.WithLabels(map[string]string{myLabelName: myLabelValue}))
323+
testcontainers.CleanupContainer(t, ctr)
334324
require.NoError(t, err)
335-
t.Cleanup(func() { require.NoError(t, ctr.Terminate(ctx)) })
336325

337326
ctrJSON, err := ctr.Inspect(ctx)
338327
require.NoError(t, err)
@@ -348,22 +337,20 @@ func TestCustomLabelsBuildOptionsModifier(t *testing.T) {
348337
)
349338

350339
ctx := context.Background()
351-
req := testcontainers.GenericContainerRequest{
352-
ContainerRequest: testcontainers.ContainerRequest{
353-
FromDockerfile: testcontainers.FromDockerfile{
354-
Context: "./testdata",
355-
Dockerfile: "Dockerfile",
356-
BuildOptionsModifier: func(opts *build.ImageBuildOptions) {
357-
opts.Labels = map[string]string{
358-
myBuildOptionLabel: myBuildOptionValue,
359-
}
360-
},
361-
},
362-
Labels: map[string]string{myLabelName: myLabelValue},
363-
},
364-
}
365340

366-
ctr, err := testcontainers.GenericContainer(ctx, req)
341+
ctr, err := testcontainers.Run(
342+
ctx, "",
343+
testcontainers.WithDockerfile(testcontainers.FromDockerfile{
344+
Context: "./testdata",
345+
Dockerfile: "Dockerfile",
346+
BuildOptionsModifier: func(opts *build.ImageBuildOptions) {
347+
opts.Labels = map[string]string{
348+
myBuildOptionLabel: myBuildOptionValue,
349+
}
350+
},
351+
}),
352+
testcontainers.WithLabels(map[string]string{myLabelName: myLabelValue}),
353+
)
367354
testcontainers.CleanupContainer(t, ctr)
368355
require.NoError(t, err)
369356

docker_auth_test.go

Lines changed: 36 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -159,16 +159,15 @@ func TestDockerImageAuth(t *testing.T) {
159159

160160
func TestBuildContainerFromDockerfile(t *testing.T) {
161161
ctx := context.Background()
162-
req := ContainerRequest{
163-
FromDockerfile: FromDockerfile{
164-
Context: "./testdata",
165-
},
166-
AlwaysPullImage: true, // make sure the authentication takes place
167-
ExposedPorts: []string{"6379/tcp"},
168-
WaitingFor: wait.ForLog("Ready to accept connections"),
169-
}
170162

171-
redisC, err := prepareRedisImage(ctx, req)
163+
redisC, err := Run(ctx, "",
164+
WithDockerfile(FromDockerfile{
165+
Context: "./testdata",
166+
}),
167+
WithAlwaysPull(),
168+
WithExposedPorts("6379/tcp"),
169+
WithWaitStrategy(wait.ForLog("Ready to accept connections")),
170+
)
172171
CleanupContainer(t, redisC)
173172
require.NoError(t, err)
174173
}
@@ -201,21 +200,19 @@ func TestBuildContainerFromDockerfileWithDockerAuthConfig(t *testing.T) {
201200

202201
ctx := context.Background()
203202

204-
req := ContainerRequest{
205-
FromDockerfile: FromDockerfile{
203+
redisC, err := Run(ctx, "",
204+
WithDockerfile(FromDockerfile{
206205
Context: "./testdata",
207206
Dockerfile: "auth.Dockerfile",
208207
BuildArgs: map[string]*string{
209208
"REGISTRY_HOST": &registryHost,
210209
},
211210
Repo: "localhost",
212-
},
213-
AlwaysPullImage: true, // make sure the authentication takes place
214-
ExposedPorts: []string{"6379/tcp"},
215-
WaitingFor: wait.ForLog("Ready to accept connections"),
216-
}
217-
218-
redisC, err := prepareRedisImage(ctx, req)
211+
}),
212+
WithAlwaysPull(),
213+
WithExposedPorts("6379/tcp"),
214+
WithWaitStrategy(wait.ForLog("Ready to accept connections")),
215+
)
219216
CleanupContainer(t, redisC)
220217
require.NoError(t, err)
221218
}
@@ -228,20 +225,18 @@ func TestBuildContainerFromDockerfileShouldFailWithWrongDockerAuthConfig(t *test
228225

229226
ctx := context.Background()
230227

231-
req := ContainerRequest{
232-
FromDockerfile: FromDockerfile{
228+
redisC, err := Run(ctx, "",
229+
WithDockerfile(FromDockerfile{
233230
Context: "./testdata",
234231
Dockerfile: "auth.Dockerfile",
235232
BuildArgs: map[string]*string{
236233
"REGISTRY_HOST": &registryHost,
237234
},
238-
},
239-
AlwaysPullImage: true, // make sure the authentication takes place
240-
ExposedPorts: []string{"6379/tcp"},
241-
WaitingFor: wait.ForLog("Ready to accept connections"),
242-
}
243-
244-
redisC, err := prepareRedisImage(ctx, req)
235+
}),
236+
WithAlwaysPull(),
237+
WithExposedPorts("6379/tcp"),
238+
WithWaitStrategy(wait.ForLog("Ready to accept connections")),
239+
)
245240
CleanupContainer(t, redisC)
246241
require.Error(t, err)
247242
}
@@ -253,17 +248,8 @@ func TestCreateContainerFromPrivateRegistry(t *testing.T) {
253248
setAuthConfig(t, registryHost, "testuser", "testpassword")
254249

255250
ctx := context.Background()
256-
req := ContainerRequest{
257-
Image: registryHost + "/redis:5.0-alpine",
258-
AlwaysPullImage: true, // make sure the authentication takes place
259-
ExposedPorts: []string{"6379/tcp"},
260-
WaitingFor: wait.ForLog("Ready to accept connections"),
261-
}
262251

263-
redisContainer, err := GenericContainer(ctx, GenericContainerRequest{
264-
ContainerRequest: req,
265-
Started: true,
266-
})
252+
redisContainer, err := Run(ctx, registryHost+"/redis:5.0-alpine", WithAlwaysPull(), WithExposedPorts("6379/tcp"), WithWaitStrategy(wait.ForLog("Ready to accept connections")))
267253
CleanupContainer(t, redisContainer)
268254
require.NoError(t, err)
269255
}
@@ -274,36 +260,28 @@ func prepareLocalRegistryWithAuth(t *testing.T) string {
274260
wd, err := os.Getwd()
275261
require.NoError(t, err)
276262
// copyDirectoryToContainer {
277-
req := ContainerRequest{
278-
Image: "registry:2",
279-
ExposedPorts: []string{"5000/tcp"},
280-
Env: map[string]string{
263+
registryC, err := Run(ctx, "registry:2",
264+
WithAlwaysPull(),
265+
WithEnv(map[string]string{
281266
"REGISTRY_AUTH": "htpasswd",
282267
"REGISTRY_AUTH_HTPASSWD_REALM": "Registry",
283268
"REGISTRY_AUTH_HTPASSWD_PATH": "/auth/htpasswd",
284269
"REGISTRY_STORAGE_FILESYSTEM_ROOTDIRECTORY": "/data",
285-
},
286-
Files: []ContainerFile{
287-
{
288-
HostFilePath: wd + "/testdata/auth",
270+
}),
271+
WithFiles(
272+
ContainerFile{
273+
HostFilePath: filepath.Join(wd, "testdata", "auth"),
289274
ContainerFilePath: "/auth",
290275
},
291-
{
292-
HostFilePath: wd + "/testdata/data",
276+
ContainerFile{
277+
HostFilePath: filepath.Join(wd, "testdata", "data"),
293278
ContainerFilePath: "/data",
294279
},
295-
},
296-
WaitingFor: wait.ForHTTP("/").WithPort("5000/tcp"),
297-
}
280+
),
281+
WithExposedPorts("5000/tcp"),
282+
WithWaitStrategy(wait.ForHTTP("/").WithPort("5000/tcp")),
283+
)
298284
// }
299-
300-
genContainerReq := GenericContainerRequest{
301-
ProviderType: providerType,
302-
ContainerRequest: req,
303-
Started: true,
304-
}
305-
306-
registryC, err := GenericContainer(ctx, genContainerReq)
307285
CleanupContainer(t, registryC)
308286
require.NoError(t, err)
309287

@@ -321,16 +299,6 @@ func prepareLocalRegistryWithAuth(t *testing.T) string {
321299
return addr
322300
}
323301

324-
func prepareRedisImage(ctx context.Context, req ContainerRequest) (Container, error) {
325-
genContainerReq := GenericContainerRequest{
326-
ProviderType: providerType,
327-
ContainerRequest: req,
328-
Started: true,
329-
}
330-
331-
return GenericContainer(ctx, genContainerReq)
332-
}
333-
334302
// setAuthConfig sets the DOCKER_AUTH_CONFIG environment variable with
335303
// authentication for with the given host, username and password.
336304
// It returns the base64 encoded credentials.

docker_exec_test.go

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,8 @@ func TestExecWithOptions(t *testing.T) {
4747
for _, tt := range tests {
4848
t.Run(tt.name, func(t *testing.T) {
4949
ctx := context.Background()
50-
req := ContainerRequest{
51-
Image: nginxAlpineImage,
52-
}
53-
54-
ctr, err := GenericContainer(ctx, GenericContainerRequest{
55-
ContainerRequest: req,
56-
Started: true,
57-
})
50+
51+
ctr, err := Run(ctx, nginxAlpineImage)
5852
CleanupContainer(t, ctr)
5953
require.NoError(t, err)
6054

@@ -79,14 +73,8 @@ func TestExecWithOptions(t *testing.T) {
7973

8074
func TestExecWithMultiplexedResponse(t *testing.T) {
8175
ctx := context.Background()
82-
req := ContainerRequest{
83-
Image: nginxAlpineImage,
84-
}
8576

86-
ctr, err := GenericContainer(ctx, GenericContainerRequest{
87-
ContainerRequest: req,
88-
Started: true,
89-
})
77+
ctr, err := Run(ctx, nginxAlpineImage)
9078
CleanupContainer(t, ctr)
9179
require.NoError(t, err)
9280

@@ -106,14 +94,8 @@ func TestExecWithMultiplexedResponse(t *testing.T) {
10694

10795
func TestExecWithNonMultiplexedResponse(t *testing.T) {
10896
ctx := context.Background()
109-
req := ContainerRequest{
110-
Image: nginxAlpineImage,
111-
}
11297

113-
ctr, err := GenericContainer(ctx, GenericContainerRequest{
114-
ContainerRequest: req,
115-
Started: true,
116-
})
98+
ctr, err := Run(ctx, nginxAlpineImage)
11799
CleanupContainer(t, ctr)
118100
require.NoError(t, err)
119101

0 commit comments

Comments
 (0)