github.com/ssdev-go/moby@v17.12.1-ce-rc2+incompatible/integration/container/rename_test.go (about)

     1  package container
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/docker/docker/api/types"
     8  	"github.com/docker/docker/api/types/container"
     9  	"github.com/docker/docker/api/types/network"
    10  	"github.com/docker/docker/api/types/strslice"
    11  	"github.com/docker/docker/client"
    12  	"github.com/docker/docker/integration/util/request"
    13  	"github.com/stretchr/testify/assert"
    14  	"github.com/stretchr/testify/require"
    15  )
    16  
    17  func runContainer(ctx context.Context, t *testing.T, client client.APIClient, cntCfg *container.Config, hstCfg *container.HostConfig, nwkCfg *network.NetworkingConfig, cntName string) string {
    18  	cnt, err := client.ContainerCreate(ctx, cntCfg, hstCfg, nwkCfg, cntName)
    19  	require.NoError(t, err)
    20  
    21  	err = client.ContainerStart(ctx, cnt.ID, types.ContainerStartOptions{})
    22  	require.NoError(t, err)
    23  	return cnt.ID
    24  }
    25  
    26  // This test simulates the scenario mentioned in #31392:
    27  // Having two linked container, renaming the target and bringing a replacement
    28  // and then deleting and recreating the source container linked to the new target.
    29  // This checks that "rename" updates source container correctly and doesn't set it to null.
    30  func TestRenameLinkedContainer(t *testing.T) {
    31  	defer setupTest(t)()
    32  	ctx := context.Background()
    33  	client := request.NewAPIClient(t)
    34  
    35  	cntConfig := &container.Config{
    36  		Image: "busybox",
    37  		Tty:   true,
    38  		Cmd:   strslice.StrSlice([]string{"top"}),
    39  	}
    40  
    41  	var (
    42  		aID, bID string
    43  		cntJSON  types.ContainerJSON
    44  		err      error
    45  	)
    46  
    47  	aID = runContainer(ctx, t, client,
    48  		cntConfig,
    49  		&container.HostConfig{},
    50  		&network.NetworkingConfig{},
    51  		"a0",
    52  	)
    53  
    54  	bID = runContainer(ctx, t, client,
    55  		cntConfig,
    56  		&container.HostConfig{
    57  			Links: []string{"a0"},
    58  		},
    59  		&network.NetworkingConfig{},
    60  		"b0",
    61  	)
    62  
    63  	err = client.ContainerRename(ctx, aID, "a1")
    64  	require.NoError(t, err)
    65  
    66  	runContainer(ctx, t, client,
    67  		cntConfig,
    68  		&container.HostConfig{},
    69  		&network.NetworkingConfig{},
    70  		"a0",
    71  	)
    72  
    73  	err = client.ContainerRemove(ctx, bID, types.ContainerRemoveOptions{Force: true})
    74  	require.NoError(t, err)
    75  
    76  	bID = runContainer(ctx, t, client,
    77  		cntConfig,
    78  		&container.HostConfig{
    79  			Links: []string{"a0"},
    80  		},
    81  		&network.NetworkingConfig{},
    82  		"b0",
    83  	)
    84  
    85  	cntJSON, err = client.ContainerInspect(ctx, bID)
    86  	require.NoError(t, err)
    87  	assert.Equal(t, []string{"/a0:/b0/a0"}, cntJSON.HostConfig.Links)
    88  }