rename_test.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package container
  2. import (
  3. "context"
  4. "testing"
  5. "github.com/docker/docker/api/types"
  6. "github.com/docker/docker/api/types/container"
  7. "github.com/docker/docker/api/types/network"
  8. "github.com/docker/docker/api/types/strslice"
  9. "github.com/docker/docker/client"
  10. "github.com/docker/docker/integration/util/request"
  11. "github.com/stretchr/testify/assert"
  12. "github.com/stretchr/testify/require"
  13. )
  14. func runContainer(ctx context.Context, t *testing.T, client client.APIClient, cntCfg *container.Config, hstCfg *container.HostConfig, nwkCfg *network.NetworkingConfig, cntName string) string {
  15. cnt, err := client.ContainerCreate(ctx, cntCfg, hstCfg, nwkCfg, cntName)
  16. require.NoError(t, err)
  17. err = client.ContainerStart(ctx, cnt.ID, types.ContainerStartOptions{})
  18. require.NoError(t, err)
  19. return cnt.ID
  20. }
  21. // This test simulates the scenario mentioned in #31392:
  22. // Having two linked container, renaming the target and bringing a replacement
  23. // and then deleting and recreating the source container linked to the new target.
  24. // This checks that "rename" updates source container correctly and doesn't set it to null.
  25. func TestRenameLinkedContainer(t *testing.T) {
  26. defer setupTest(t)()
  27. ctx := context.Background()
  28. client := request.NewAPIClient(t)
  29. cntConfig := &container.Config{
  30. Image: "busybox",
  31. Tty: true,
  32. Cmd: strslice.StrSlice([]string{"top"}),
  33. }
  34. var (
  35. aID, bID string
  36. cntJSON types.ContainerJSON
  37. err error
  38. )
  39. aID = runContainer(ctx, t, client,
  40. cntConfig,
  41. &container.HostConfig{},
  42. &network.NetworkingConfig{},
  43. "a0",
  44. )
  45. bID = runContainer(ctx, t, client,
  46. cntConfig,
  47. &container.HostConfig{
  48. Links: []string{"a0"},
  49. },
  50. &network.NetworkingConfig{},
  51. "b0",
  52. )
  53. err = client.ContainerRename(ctx, aID, "a1")
  54. require.NoError(t, err)
  55. runContainer(ctx, t, client,
  56. cntConfig,
  57. &container.HostConfig{},
  58. &network.NetworkingConfig{},
  59. "a0",
  60. )
  61. err = client.ContainerRemove(ctx, bID, types.ContainerRemoveOptions{Force: true})
  62. require.NoError(t, err)
  63. bID = runContainer(ctx, t, client,
  64. cntConfig,
  65. &container.HostConfig{
  66. Links: []string{"a0"},
  67. },
  68. &network.NetworkingConfig{},
  69. "b0",
  70. )
  71. cntJSON, err = client.ContainerInspect(ctx, bID)
  72. require.NoError(t, err)
  73. assert.Equal(t, []string{"/a0:/b0/a0"}, cntJSON.HostConfig.Links)
  74. }