github.com/demonoid81/moby@v0.0.0-20200517203328-62dd8e17c460/integration/container/rename_test.go (about) 1 package container // import "github.com/demonoid81/moby/integration/container" 2 3 import ( 4 "context" 5 "testing" 6 "time" 7 8 "github.com/demonoid81/moby/api/types" 9 containertypes "github.com/demonoid81/moby/api/types/container" 10 "github.com/demonoid81/moby/api/types/network" 11 "github.com/demonoid81/moby/api/types/versions" 12 "github.com/demonoid81/moby/integration/internal/container" 13 "github.com/demonoid81/moby/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 skip.If(t, testEnv.OSType == "windows", "FIXME") 127 defer setupTest(t)() 128 ctx := context.Background() 129 client := testEnv.APIClient() 130 131 networkName := "network1" + t.Name() 132 _, err := client.NetworkCreate(ctx, networkName, types.NetworkCreate{}) 133 134 assert.NilError(t, err) 135 cID := container.Run(ctx, t, client, func(c *container.TestContainerConfig) { 136 c.NetworkingConfig.EndpointsConfig = map[string]*network.EndpointSettings{ 137 networkName: {}, 138 } 139 c.HostConfig.NetworkMode = containertypes.NetworkMode(networkName) 140 }) 141 142 container1Name := "container1" + t.Name() 143 err = client.ContainerRename(ctx, cID, container1Name) 144 assert.NilError(t, err) 145 // Stop/Start the container to get registered 146 // FIXME(vdemeester) this is a really weird behavior as it fails otherwise 147 err = client.ContainerStop(ctx, container1Name, nil) 148 assert.NilError(t, err) 149 err = client.ContainerStart(ctx, container1Name, types.ContainerStartOptions{}) 150 assert.NilError(t, err) 151 152 poll.WaitOn(t, container.IsInState(ctx, client, cID, "running"), poll.WithDelay(100*time.Millisecond)) 153 154 count := "-c" 155 if testEnv.OSType == "windows" { 156 count = "-n" 157 } 158 cID = container.Run(ctx, t, client, func(c *container.TestContainerConfig) { 159 c.NetworkingConfig.EndpointsConfig = map[string]*network.EndpointSettings{ 160 networkName: {}, 161 } 162 c.HostConfig.NetworkMode = containertypes.NetworkMode(networkName) 163 }, container.WithCmd("ping", count, "1", container1Name)) 164 poll.WaitOn(t, container.IsInState(ctx, client, cID, "exited"), poll.WithDelay(100*time.Millisecond)) 165 166 inspect, err := client.ContainerInspect(ctx, cID) 167 assert.NilError(t, err) 168 assert.Check(t, is.Equal(0, inspect.State.ExitCode), "container %s exited with the wrong exitcode: %s", cID, inspect.State.Error) 169 } 170 171 // TODO: should be a unit test 172 func TestRenameContainerWithSameName(t *testing.T) { 173 defer setupTest(t)() 174 ctx := context.Background() 175 client := testEnv.APIClient() 176 177 oldName := "old" + t.Name() 178 cID := container.Run(ctx, t, client, container.WithName(oldName)) 179 180 poll.WaitOn(t, container.IsInState(ctx, client, cID, "running"), poll.WithDelay(100*time.Millisecond)) 181 err := client.ContainerRename(ctx, oldName, oldName) 182 assert.Check(t, is.ErrorContains(err, "Renaming a container with the same name")) 183 err = client.ContainerRename(ctx, cID, oldName) 184 assert.Check(t, is.ErrorContains(err, "Renaming a container with the same name")) 185 } 186 187 // Test case for GitHub issue 23973 188 // When a container is being renamed, the container might 189 // be linked to another container. In that case, the meta data 190 // of the linked container should be updated so that the other 191 // container could still reference to the container that is renamed. 192 func TestRenameContainerWithLinkedContainer(t *testing.T) { 193 skip.If(t, testEnv.IsRemoteDaemon) 194 skip.If(t, testEnv.OSType == "windows", "FIXME") 195 196 defer setupTest(t)() 197 ctx := context.Background() 198 client := testEnv.APIClient() 199 200 db1Name := "db1" + t.Name() 201 db1ID := container.Run(ctx, t, client, container.WithName(db1Name)) 202 poll.WaitOn(t, container.IsInState(ctx, client, db1ID, "running"), poll.WithDelay(100*time.Millisecond)) 203 204 app1Name := "app1" + t.Name() 205 app2Name := "app2" + t.Name() 206 app1ID := container.Run(ctx, t, client, container.WithName(app1Name), container.WithLinks(db1Name+":/mysql")) 207 poll.WaitOn(t, container.IsInState(ctx, client, app1ID, "running"), poll.WithDelay(100*time.Millisecond)) 208 209 err := client.ContainerRename(ctx, app1Name, app2Name) 210 assert.NilError(t, err) 211 212 inspect, err := client.ContainerInspect(ctx, app2Name+"/mysql") 213 assert.NilError(t, err) 214 assert.Check(t, is.Equal(db1ID, inspect.ID)) 215 }