|
| 1 | +package testcontainers_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | + |
| 10 | + "github.com/testcontainers/testcontainers-go" |
| 11 | + "github.com/testcontainers/testcontainers-go/internal/core" |
| 12 | +) |
| 13 | + |
| 14 | +func TestGenericContainer_stop_start_withReuse(t *testing.T) { |
| 15 | + containerName := "my-nginx" |
| 16 | + |
| 17 | + req := testcontainers.GenericContainerRequest{ |
| 18 | + ContainerRequest: testcontainers.ContainerRequest{ |
| 19 | + Image: nginxAlpineImage, |
| 20 | + ExposedPorts: []string{"8080/tcp"}, |
| 21 | + Name: containerName, |
| 22 | + }, |
| 23 | + Reuse: true, |
| 24 | + Started: true, |
| 25 | + } |
| 26 | + |
| 27 | + ctr, err := testcontainers.GenericContainer(context.Background(), req) |
| 28 | + testcontainers.CleanupContainer(t, ctr) |
| 29 | + require.NoError(t, err) |
| 30 | + require.NotNil(t, ctr) |
| 31 | + |
| 32 | + err = ctr.Stop(context.Background(), nil) |
| 33 | + require.NoError(t, err) |
| 34 | + |
| 35 | + // Run another container with same container name: |
| 36 | + // The checks for the exposed ports must not fail when restarting the container. |
| 37 | + ctr1, err := testcontainers.GenericContainer(context.Background(), req) |
| 38 | + testcontainers.CleanupContainer(t, ctr1) |
| 39 | + require.NoError(t, err) |
| 40 | + require.NotNil(t, ctr1) |
| 41 | +} |
| 42 | + |
| 43 | +func TestGenericContainer_pause_start_withReuse(t *testing.T) { |
| 44 | + containerName := "my-nginx" |
| 45 | + |
| 46 | + req := testcontainers.GenericContainerRequest{ |
| 47 | + ContainerRequest: testcontainers.ContainerRequest{ |
| 48 | + Image: nginxAlpineImage, |
| 49 | + ExposedPorts: []string{"8080/tcp"}, |
| 50 | + Name: containerName, |
| 51 | + }, |
| 52 | + Reuse: true, |
| 53 | + Started: true, |
| 54 | + } |
| 55 | + |
| 56 | + ctr, err := testcontainers.GenericContainer(context.Background(), req) |
| 57 | + testcontainers.CleanupContainer(t, ctr) |
| 58 | + require.NoError(t, err) |
| 59 | + require.NotNil(t, ctr) |
| 60 | + |
| 61 | + // Pause the container is not supported by our API, but we can do it manually |
| 62 | + // by using the Docker client. |
| 63 | + cli, err := core.NewClient(context.Background()) |
| 64 | + require.NoError(t, err) |
| 65 | + |
| 66 | + err = cli.ContainerPause(context.Background(), ctr.GetContainerID()) |
| 67 | + require.NoError(t, err) |
| 68 | + |
| 69 | + // Because the container is paused, it should not be possible to start it again. |
| 70 | + ctr1, err := testcontainers.GenericContainer(context.Background(), req) |
| 71 | + testcontainers.CleanupContainer(t, ctr1) |
| 72 | + require.ErrorIs(t, err, errors.ErrUnsupported) |
| 73 | +} |
0 commit comments