-
-
Notifications
You must be signed in to change notification settings - Fork 603
Expand file tree
/
Copy pathreuse_test.go
More file actions
64 lines (50 loc) · 1.98 KB
/
reuse_test.go
File metadata and controls
64 lines (50 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package testcontainers_test
import (
"context"
"errors"
"testing"
"github.com/moby/moby/client"
"github.com/stretchr/testify/require"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/internal/core"
)
func TestGenericContainer_stop_start_withReuse(t *testing.T) {
containerName := "my-nginx"
opts := []testcontainers.ContainerCustomizer{
testcontainers.WithExposedPorts("8080/tcp"),
testcontainers.WithReuseByName(containerName),
}
ctr, err := testcontainers.Run(context.Background(), nginxAlpineImage, opts...)
testcontainers.CleanupContainer(t, ctr)
require.NoError(t, err)
require.NotNil(t, ctr)
err = ctr.Stop(context.Background(), nil)
require.NoError(t, err)
// Run another container with same container name:
// The checks for the exposed ports must not fail when restarting the container.
ctr1, err := testcontainers.Run(context.Background(), nginxAlpineImage, opts...)
testcontainers.CleanupContainer(t, ctr1)
require.NoError(t, err)
require.NotNil(t, ctr1)
}
func TestGenericContainer_pause_start_withReuse(t *testing.T) {
containerName := "my-nginx"
opts := []testcontainers.ContainerCustomizer{
testcontainers.WithExposedPorts("8080/tcp"),
testcontainers.WithReuseByName(containerName),
}
ctr, err := testcontainers.Run(context.Background(), nginxAlpineImage, opts...)
testcontainers.CleanupContainer(t, ctr)
require.NoError(t, err)
require.NotNil(t, ctr)
// Pause the container is not supported by our API, but we can do it manually
// by using the Docker client.
cli, err := core.NewClient(context.Background())
require.NoError(t, err)
_, err = cli.ContainerPause(context.Background(), ctr.GetContainerID(), client.ContainerPauseOptions{})
require.NoError(t, err)
// Because the container is paused, it should not be possible to start it again.
ctr1, err := testcontainers.Run(context.Background(), nginxAlpineImage, opts...)
testcontainers.CleanupContainer(t, ctr1)
require.ErrorIs(t, err, errors.ErrUnsupported)
}