rename_test.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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/v3/assert"
  13. is "gotest.tools/v3/assert/cmp"
  14. "gotest.tools/v3/poll"
  15. "gotest.tools/v3/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(ctx, t, client, container.WithName(aName))
  30. bID := container.Run(ctx, t, client, container.WithName(bName), container.WithLinks(aName))
  31. err := client.ContainerRename(ctx, aID, "a1"+t.Name())
  32. assert.NilError(t, err)
  33. container.Run(ctx, t, client, container.WithName(aName))
  34. err = client.ContainerRemove(ctx, bID, types.ContainerRemoveOptions{Force: true})
  35. assert.NilError(t, err)
  36. bID = container.Run(ctx, t, 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(ctx, t, 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.GenerateRandomID()
  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(ctx, t, client, container.WithName(oldName))
  64. poll.WaitOn(t, container.IsInState(ctx, client, cID, "running"), poll.WithDelay(100*time.Millisecond))
  65. newName := "new_name" + stringid.GenerateRandomID()
  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(ctx, t, 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(ctx, t, 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. defer setupTest(t)()
  101. ctx := context.Background()
  102. client := testEnv.APIClient()
  103. networkName := "network1" + t.Name()
  104. _, err := client.NetworkCreate(ctx, networkName, types.NetworkCreate{})
  105. assert.NilError(t, err)
  106. cID := container.Run(ctx, t, client, func(c *container.TestContainerConfig) {
  107. c.NetworkingConfig.EndpointsConfig = map[string]*network.EndpointSettings{
  108. networkName: {},
  109. }
  110. c.HostConfig.NetworkMode = containertypes.NetworkMode(networkName)
  111. })
  112. container1Name := "container1" + t.Name()
  113. err = client.ContainerRename(ctx, cID, container1Name)
  114. assert.NilError(t, err)
  115. // Stop/Start the container to get registered
  116. // FIXME(vdemeester) this is a really weird behavior as it fails otherwise
  117. err = client.ContainerStop(ctx, container1Name, nil)
  118. assert.NilError(t, err)
  119. err = client.ContainerStart(ctx, container1Name, types.ContainerStartOptions{})
  120. assert.NilError(t, err)
  121. poll.WaitOn(t, container.IsInState(ctx, client, cID, "running"), poll.WithDelay(100*time.Millisecond))
  122. count := "-c"
  123. if testEnv.OSType == "windows" {
  124. count = "-n"
  125. }
  126. cID = container.Run(ctx, t, client, func(c *container.TestContainerConfig) {
  127. c.NetworkingConfig.EndpointsConfig = map[string]*network.EndpointSettings{
  128. networkName: {},
  129. }
  130. c.HostConfig.NetworkMode = containertypes.NetworkMode(networkName)
  131. }, container.WithCmd("ping", count, "1", container1Name))
  132. poll.WaitOn(t, container.IsInState(ctx, client, cID, "exited"), poll.WithDelay(100*time.Millisecond))
  133. inspect, err := client.ContainerInspect(ctx, cID)
  134. assert.NilError(t, err)
  135. assert.Check(t, is.Equal(0, inspect.State.ExitCode), "container %s exited with the wrong exitcode: %s", cID, inspect.State.Error)
  136. }
  137. // TODO: should be a unit test
  138. func TestRenameContainerWithSameName(t *testing.T) {
  139. defer setupTest(t)()
  140. ctx := context.Background()
  141. client := testEnv.APIClient()
  142. oldName := "old" + t.Name()
  143. cID := container.Run(ctx, t, client, container.WithName(oldName))
  144. poll.WaitOn(t, container.IsInState(ctx, client, cID, "running"), poll.WithDelay(100*time.Millisecond))
  145. err := client.ContainerRename(ctx, oldName, oldName)
  146. assert.Check(t, is.ErrorContains(err, "Renaming a container with the same name"))
  147. err = client.ContainerRename(ctx, cID, oldName)
  148. assert.Check(t, is.ErrorContains(err, "Renaming a container with the same name"))
  149. }
  150. // Test case for GitHub issue 23973
  151. // When a container is being renamed, the container might
  152. // be linked to another container. In that case, the meta data
  153. // of the linked container should be updated so that the other
  154. // container could still reference to the container that is renamed.
  155. func TestRenameContainerWithLinkedContainer(t *testing.T) {
  156. skip.If(t, testEnv.IsRemoteDaemon)
  157. skip.If(t, testEnv.OSType == "windows", "FIXME")
  158. defer setupTest(t)()
  159. ctx := context.Background()
  160. client := testEnv.APIClient()
  161. db1Name := "db1" + t.Name()
  162. db1ID := container.Run(ctx, t, client, container.WithName(db1Name))
  163. poll.WaitOn(t, container.IsInState(ctx, client, db1ID, "running"), poll.WithDelay(100*time.Millisecond))
  164. app1Name := "app1" + t.Name()
  165. app2Name := "app2" + t.Name()
  166. app1ID := container.Run(ctx, t, client, container.WithName(app1Name), container.WithLinks(db1Name+":/mysql"))
  167. poll.WaitOn(t, container.IsInState(ctx, client, app1ID, "running"), poll.WithDelay(100*time.Millisecond))
  168. err := client.ContainerRename(ctx, app1Name, app2Name)
  169. assert.NilError(t, err)
  170. inspect, err := client.ContainerInspect(ctx, app2Name+"/mysql")
  171. assert.NilError(t, err)
  172. assert.Check(t, is.Equal(db1ID, inspect.ID))
  173. }