-
-
Notifications
You must be signed in to change notification settings - Fork 541
/
Copy pathreuse_test.go
73 lines (59 loc) · 2.09 KB
/
reuse_test.go
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
65
66
67
68
69
70
71
72
73
package testcontainers_test
import (
"context"
"errors"
"testing"
"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"
req := testcontainers.GenericContainerRequest{
ContainerRequest: testcontainers.ContainerRequest{
Image: nginxAlpineImage,
ExposedPorts: []string{"8080/tcp"},
Name: containerName,
},
Reuse: true,
Started: true,
}
ctr, err := testcontainers.GenericContainer(context.Background(), req)
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.GenericContainer(context.Background(), req)
testcontainers.CleanupContainer(t, ctr1)
require.NoError(t, err)
require.NotNil(t, ctr1)
}
func TestGenericContainer_pause_start_withReuse(t *testing.T) {
containerName := "my-nginx"
req := testcontainers.GenericContainerRequest{
ContainerRequest: testcontainers.ContainerRequest{
Image: nginxAlpineImage,
ExposedPorts: []string{"8080/tcp"},
Name: containerName,
},
Reuse: true,
Started: true,
}
ctr, err := testcontainers.GenericContainer(context.Background(), req)
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())
require.NoError(t, err)
// Because the container is paused, it should not be possible to start it again.
ctr1, err := testcontainers.GenericContainer(context.Background(), req)
testcontainers.CleanupContainer(t, ctr1)
require.ErrorIs(t, err, errors.ErrUnsupported)
}