rename_test.go 8.0 KB

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