github.com/rumpl/bof@v23.0.0-rc.2+incompatible/integration/container/rename_test.go (about)

     1  package container // import "github.com/docker/docker/integration/container"
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/docker/docker/api/types"
     9  	containertypes "github.com/docker/docker/api/types/container"
    10  	"github.com/docker/docker/api/types/network"
    11  	"github.com/docker/docker/api/types/versions"
    12  	"github.com/docker/docker/integration/internal/container"
    13  	"github.com/docker/docker/pkg/stringid"
    14  	"gotest.tools/v3/assert"
    15  	is "gotest.tools/v3/assert/cmp"
    16  	"gotest.tools/v3/poll"
    17  	"gotest.tools/v3/skip"
    18  )
    19  
    20  // This test simulates the scenario mentioned in #31392:
    21  // Having two linked container, renaming the target and bringing a replacement
    22  // and then deleting and recreating the source container linked to the new target.
    23  // This checks that "rename" updates source container correctly and doesn't set it to null.
    24  func TestRenameLinkedContainer(t *testing.T) {
    25  	skip.If(t, versions.LessThan(testEnv.DaemonAPIVersion(), "1.32"), "broken in earlier versions")
    26  	skip.If(t, testEnv.OSType == "windows", "FIXME")
    27  	defer setupTest(t)()
    28  	ctx := context.Background()
    29  	client := testEnv.APIClient()
    30  
    31  	aName := "a0" + t.Name()
    32  	bName := "b0" + t.Name()
    33  	aID := container.Run(ctx, t, client, container.WithName(aName))
    34  	bID := container.Run(ctx, t, client, container.WithName(bName), container.WithLinks(aName))
    35  
    36  	err := client.ContainerRename(ctx, aID, "a1"+t.Name())
    37  	assert.NilError(t, err)
    38  
    39  	container.Run(ctx, t, client, container.WithName(aName))
    40  
    41  	err = client.ContainerRemove(ctx, bID, types.ContainerRemoveOptions{Force: true})
    42  	assert.NilError(t, err)
    43  
    44  	bID = container.Run(ctx, t, client, container.WithName(bName), container.WithLinks(aName))
    45  
    46  	inspect, err := client.ContainerInspect(ctx, bID)
    47  	assert.NilError(t, err)
    48  	assert.Check(t, is.DeepEqual([]string{"/" + aName + ":/" + bName + "/" + aName}, inspect.HostConfig.Links))
    49  }
    50  
    51  func TestRenameStoppedContainer(t *testing.T) {
    52  	defer setupTest(t)()
    53  	ctx := context.Background()
    54  	client := testEnv.APIClient()
    55  
    56  	oldName := "first_name" + t.Name()
    57  	cID := container.Run(ctx, t, client, container.WithName(oldName), container.WithCmd("sh"))
    58  	poll.WaitOn(t, container.IsInState(ctx, client, cID, "exited"), poll.WithDelay(100*time.Millisecond))
    59  
    60  	inspect, err := client.ContainerInspect(ctx, cID)
    61  	assert.NilError(t, err)
    62  	assert.Check(t, is.Equal("/"+oldName, inspect.Name))
    63  
    64  	newName := "new_name" + stringid.GenerateRandomID()
    65  	err = client.ContainerRename(ctx, oldName, newName)
    66  	assert.NilError(t, err)
    67  
    68  	inspect, err = client.ContainerInspect(ctx, cID)
    69  	assert.NilError(t, err)
    70  	assert.Check(t, is.Equal("/"+newName, inspect.Name))
    71  }
    72  
    73  func TestRenameRunningContainerAndReuse(t *testing.T) {
    74  	defer setupTest(t)()
    75  	ctx := context.Background()
    76  	client := testEnv.APIClient()
    77  
    78  	oldName := "first_name" + t.Name()
    79  	cID := container.Run(ctx, t, client, container.WithName(oldName))
    80  	poll.WaitOn(t, container.IsInState(ctx, client, cID, "running"), poll.WithDelay(100*time.Millisecond))
    81  
    82  	newName := "new_name" + stringid.GenerateRandomID()
    83  	err := client.ContainerRename(ctx, oldName, newName)
    84  	assert.NilError(t, err)
    85  
    86  	inspect, err := client.ContainerInspect(ctx, cID)
    87  	assert.NilError(t, err)
    88  	assert.Check(t, is.Equal("/"+newName, inspect.Name))
    89  
    90  	_, err = client.ContainerInspect(ctx, oldName)
    91  	assert.Check(t, is.ErrorContains(err, "No such container: "+oldName))
    92  
    93  	cID = container.Run(ctx, t, client, container.WithName(oldName))
    94  	poll.WaitOn(t, container.IsInState(ctx, client, cID, "running"), poll.WithDelay(100*time.Millisecond))
    95  
    96  	inspect, err = client.ContainerInspect(ctx, cID)
    97  	assert.NilError(t, err)
    98  	assert.Check(t, is.Equal("/"+oldName, inspect.Name))
    99  }
   100  
   101  func TestRenameInvalidName(t *testing.T) {
   102  	defer setupTest(t)()
   103  	ctx := context.Background()
   104  	client := testEnv.APIClient()
   105  
   106  	oldName := "first_name" + t.Name()
   107  	cID := container.Run(ctx, t, client, container.WithName(oldName))
   108  	poll.WaitOn(t, container.IsInState(ctx, client, cID, "running"), poll.WithDelay(100*time.Millisecond))
   109  
   110  	err := client.ContainerRename(ctx, oldName, "new:invalid")
   111  	assert.Check(t, is.ErrorContains(err, "Invalid container name"))
   112  
   113  	inspect, err := client.ContainerInspect(ctx, oldName)
   114  	assert.NilError(t, err)
   115  	assert.Check(t, is.Equal(cID, inspect.ID))
   116  }
   117  
   118  // Test case for GitHub issue 22466
   119  // Docker's service discovery works for named containers so
   120  // ping to a named container should work, and an anonymous
   121  // container without a name does not work with service discovery.
   122  // However, an anonymous could be renamed to a named container.
   123  // This test is to make sure once the container has been renamed,
   124  // the service discovery for the (re)named container works.
   125  func TestRenameAnonymousContainer(t *testing.T) {
   126  	defer setupTest(t)()
   127  	ctx := context.Background()
   128  	client := testEnv.APIClient()
   129  
   130  	networkName := "network1" + t.Name()
   131  	_, err := client.NetworkCreate(ctx, networkName, types.NetworkCreate{})
   132  
   133  	assert.NilError(t, err)
   134  	cID := container.Run(ctx, t, client, func(c *container.TestContainerConfig) {
   135  		c.NetworkingConfig.EndpointsConfig = map[string]*network.EndpointSettings{
   136  			networkName: {},
   137  		}
   138  		c.HostConfig.NetworkMode = containertypes.NetworkMode(networkName)
   139  	})
   140  
   141  	container1Name := "container1" + t.Name()
   142  	err = client.ContainerRename(ctx, cID, container1Name)
   143  	assert.NilError(t, err)
   144  	// Stop/Start the container to get registered
   145  	// FIXME(vdemeester) this is a really weird behavior as it fails otherwise
   146  	err = client.ContainerStop(ctx, container1Name, containertypes.StopOptions{})
   147  	assert.NilError(t, err)
   148  	err = client.ContainerStart(ctx, container1Name, types.ContainerStartOptions{})
   149  	assert.NilError(t, err)
   150  
   151  	poll.WaitOn(t, container.IsInState(ctx, client, cID, "running"), poll.WithDelay(100*time.Millisecond))
   152  
   153  	count := "-c"
   154  	if testEnv.OSType == "windows" {
   155  		count = "-n"
   156  	}
   157  	cID = container.Run(ctx, t, client, func(c *container.TestContainerConfig) {
   158  		c.NetworkingConfig.EndpointsConfig = map[string]*network.EndpointSettings{
   159  			networkName: {},
   160  		}
   161  		c.HostConfig.NetworkMode = containertypes.NetworkMode(networkName)
   162  	}, container.WithCmd("ping", count, "1", container1Name))
   163  	poll.WaitOn(t, container.IsInState(ctx, client, cID, "exited"), poll.WithDelay(100*time.Millisecond))
   164  
   165  	inspect, err := client.ContainerInspect(ctx, cID)
   166  	assert.NilError(t, err)
   167  	assert.Check(t, is.Equal(0, inspect.State.ExitCode), "container %s exited with the wrong exitcode: %s", cID, inspect.State.Error)
   168  }
   169  
   170  // TODO: should be a unit test
   171  func TestRenameContainerWithSameName(t *testing.T) {
   172  	defer setupTest(t)()
   173  	ctx := context.Background()
   174  	client := testEnv.APIClient()
   175  
   176  	oldName := "old" + t.Name()
   177  	cID := container.Run(ctx, t, client, container.WithName(oldName))
   178  
   179  	poll.WaitOn(t, container.IsInState(ctx, client, cID, "running"), poll.WithDelay(100*time.Millisecond))
   180  	err := client.ContainerRename(ctx, oldName, oldName)
   181  	assert.Check(t, is.ErrorContains(err, "Renaming a container with the same name"))
   182  	err = client.ContainerRename(ctx, cID, oldName)
   183  	assert.Check(t, is.ErrorContains(err, "Renaming a container with the same name"))
   184  }
   185  
   186  // Test case for GitHub issue 23973
   187  // When a container is being renamed, the container might
   188  // be linked to another container. In that case, the meta data
   189  // of the linked container should be updated so that the other
   190  // container could still reference to the container that is renamed.
   191  func TestRenameContainerWithLinkedContainer(t *testing.T) {
   192  	skip.If(t, testEnv.IsRemoteDaemon)
   193  	skip.If(t, testEnv.OSType == "windows", "FIXME")
   194  
   195  	defer setupTest(t)()
   196  	ctx := context.Background()
   197  	client := testEnv.APIClient()
   198  
   199  	db1Name := "db1" + t.Name()
   200  	db1ID := container.Run(ctx, t, client, container.WithName(db1Name))
   201  	poll.WaitOn(t, container.IsInState(ctx, client, db1ID, "running"), poll.WithDelay(100*time.Millisecond))
   202  
   203  	app1Name := "app1" + t.Name()
   204  	app2Name := "app2" + t.Name()
   205  	app1ID := container.Run(ctx, t, client, container.WithName(app1Name), container.WithLinks(db1Name+":/mysql"))
   206  	poll.WaitOn(t, container.IsInState(ctx, client, app1ID, "running"), poll.WithDelay(100*time.Millisecond))
   207  
   208  	err := client.ContainerRename(ctx, app1Name, app2Name)
   209  	assert.NilError(t, err)
   210  
   211  	inspect, err := client.ContainerInspect(ctx, app2Name+"/mysql")
   212  	assert.NilError(t, err)
   213  	assert.Check(t, is.Equal(db1ID, inspect.ID))
   214  }