rename_test.go 8.1 KB

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