docker_cli_volume_test.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package main
  2. import (
  3. "os/exec"
  4. "strings"
  5. derr "github.com/docker/docker/errors"
  6. "github.com/docker/docker/pkg/integration/checker"
  7. "github.com/docker/docker/volume"
  8. "github.com/go-check/check"
  9. )
  10. func (s *DockerSuite) TestVolumeCliCreate(c *check.C) {
  11. dockerCmd(c, "volume", "create")
  12. _, err := runCommand(exec.Command(dockerBinary, "volume", "create", "-d", "nosuchdriver"))
  13. c.Assert(err, check.Not(check.IsNil))
  14. out, _ := dockerCmd(c, "volume", "create", "--name=test")
  15. name := strings.TrimSpace(out)
  16. c.Assert(name, check.Equals, "test")
  17. }
  18. func (s *DockerSuite) TestVolumeCliCreateOptionConflict(c *check.C) {
  19. dockerCmd(c, "volume", "create", "--name=test")
  20. out, _, err := dockerCmdWithError("volume", "create", "--name", "test", "--driver", "nosuchdriver")
  21. c.Assert(err, check.NotNil, check.Commentf("volume create exception name already in use with another driver"))
  22. stderr := derr.ErrorVolumeNameTaken.WithArgs("test", volume.DefaultDriverName).Error()
  23. c.Assert(strings.Contains(out, strings.TrimPrefix(stderr, "volume name taken: ")), check.Equals, true)
  24. out, _ = dockerCmd(c, "volume", "inspect", "--format='{{ .Driver }}'", "test")
  25. _, _, err = dockerCmdWithError("volume", "create", "--name", "test", "--driver", strings.TrimSpace(out))
  26. c.Assert(err, check.IsNil)
  27. }
  28. func (s *DockerSuite) TestVolumeCliInspect(c *check.C) {
  29. c.Assert(
  30. exec.Command(dockerBinary, "volume", "inspect", "doesntexist").Run(),
  31. check.Not(check.IsNil),
  32. check.Commentf("volume inspect should error on non-existent volume"),
  33. )
  34. out, _ := dockerCmd(c, "volume", "create")
  35. name := strings.TrimSpace(out)
  36. out, _ = dockerCmd(c, "volume", "inspect", "--format='{{ .Name }}'", name)
  37. c.Assert(strings.TrimSpace(out), check.Equals, name)
  38. dockerCmd(c, "volume", "create", "--name", "test")
  39. out, _ = dockerCmd(c, "volume", "inspect", "--format='{{ .Name }}'", "test")
  40. c.Assert(strings.TrimSpace(out), check.Equals, "test")
  41. }
  42. func (s *DockerSuite) TestVolumeCliLs(c *check.C) {
  43. prefix := ""
  44. if daemonPlatform == "windows" {
  45. prefix = "c:"
  46. }
  47. out, _ := dockerCmd(c, "volume", "create")
  48. id := strings.TrimSpace(out)
  49. dockerCmd(c, "volume", "create", "--name", "test")
  50. dockerCmd(c, "run", "-v", prefix+"/foo", "busybox", "ls", "/")
  51. out, _ = dockerCmd(c, "volume", "ls")
  52. outArr := strings.Split(strings.TrimSpace(out), "\n")
  53. c.Assert(len(outArr), check.Equals, 4, check.Commentf("\n%s", out))
  54. // Since there is no guarantee of ordering of volumes, we just make sure the names are in the output
  55. c.Assert(strings.Contains(out, id+"\n"), check.Equals, true)
  56. c.Assert(strings.Contains(out, "test\n"), check.Equals, true)
  57. }
  58. func (s *DockerSuite) TestVolumeCliLsFilterDangling(c *check.C) {
  59. prefix := ""
  60. if daemonPlatform == "windows" {
  61. prefix = "c:"
  62. }
  63. dockerCmd(c, "volume", "create", "--name", "testnotinuse1")
  64. dockerCmd(c, "volume", "create", "--name", "testisinuse1")
  65. dockerCmd(c, "volume", "create", "--name", "testisinuse2")
  66. // Make sure both "created" (but not started), and started
  67. // containers are included in reference counting
  68. dockerCmd(c, "run", "--name", "volume-test1", "-v", "testisinuse1:"+prefix+"/foo", "busybox", "true")
  69. dockerCmd(c, "create", "--name", "volume-test2", "-v", "testisinuse2:"+prefix+"/foo", "busybox", "true")
  70. out, _ := dockerCmd(c, "volume", "ls")
  71. // No filter, all volumes should show
  72. c.Assert(out, checker.Contains, "testnotinuse1\n", check.Commentf("expected volume 'testnotinuse1' in output"))
  73. c.Assert(out, checker.Contains, "testisinuse1\n", check.Commentf("expected volume 'testisinuse1' in output"))
  74. c.Assert(out, checker.Contains, "testisinuse2\n", check.Commentf("expected volume 'testisinuse2' in output"))
  75. out, _ = dockerCmd(c, "volume", "ls", "--filter", "dangling=false")
  76. // Same as above, but expicitly disabling dangling
  77. c.Assert(out, checker.Contains, "testnotinuse1\n", check.Commentf("expected volume 'testnotinuse1' in output"))
  78. c.Assert(out, checker.Contains, "testisinuse1\n", check.Commentf("expected volume 'testisinuse1' in output"))
  79. c.Assert(out, checker.Contains, "testisinuse2\n", check.Commentf("expected volume 'testisinuse2' in output"))
  80. out, _ = dockerCmd(c, "volume", "ls", "--filter", "dangling=true")
  81. // Filter "dangling" volumes; ony "dangling" (unused) volumes should be in the output
  82. c.Assert(out, checker.Contains, "testnotinuse1\n", check.Commentf("expected volume 'testnotinuse1' in output"))
  83. c.Assert(out, check.Not(checker.Contains), "testisinuse1\n", check.Commentf("volume 'testisinuse1' in output, but not expected"))
  84. c.Assert(out, check.Not(checker.Contains), "testisinuse2\n", check.Commentf("volume 'testisinuse2' in output, but not expected"))
  85. }
  86. func (s *DockerSuite) TestVolumeCliRm(c *check.C) {
  87. prefix := ""
  88. if daemonPlatform == "windows" {
  89. prefix = "c:"
  90. }
  91. out, _ := dockerCmd(c, "volume", "create")
  92. id := strings.TrimSpace(out)
  93. dockerCmd(c, "volume", "create", "--name", "test")
  94. dockerCmd(c, "volume", "rm", id)
  95. dockerCmd(c, "volume", "rm", "test")
  96. out, _ = dockerCmd(c, "volume", "ls")
  97. outArr := strings.Split(strings.TrimSpace(out), "\n")
  98. c.Assert(len(outArr), check.Equals, 1, check.Commentf("%s\n", out))
  99. volumeID := "testing"
  100. dockerCmd(c, "run", "-v", volumeID+":"+prefix+"/foo", "--name=test", "busybox", "sh", "-c", "echo hello > /foo/bar")
  101. out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "volume", "rm", "testing"))
  102. c.Assert(
  103. err,
  104. check.Not(check.IsNil),
  105. check.Commentf("Should not be able to remove volume that is in use by a container\n%s", out))
  106. out, _ = dockerCmd(c, "run", "--volumes-from=test", "--name=test2", "busybox", "sh", "-c", "cat /foo/bar")
  107. c.Assert(strings.TrimSpace(out), check.Equals, "hello")
  108. dockerCmd(c, "rm", "-fv", "test2")
  109. dockerCmd(c, "volume", "inspect", volumeID)
  110. dockerCmd(c, "rm", "-f", "test")
  111. out, _ = dockerCmd(c, "run", "--name=test2", "-v", volumeID+":"+prefix+"/foo", "busybox", "sh", "-c", "cat /foo/bar")
  112. c.Assert(strings.TrimSpace(out), check.Equals, "hello", check.Commentf("volume data was removed"))
  113. dockerCmd(c, "rm", "test2")
  114. dockerCmd(c, "volume", "rm", volumeID)
  115. c.Assert(
  116. exec.Command("volume", "rm", "doesntexist").Run(),
  117. check.Not(check.IsNil),
  118. check.Commentf("volume rm should fail with non-existent volume"),
  119. )
  120. }
  121. func (s *DockerSuite) TestVolumeCliNoArgs(c *check.C) {
  122. out, _ := dockerCmd(c, "volume")
  123. // no args should produce the cmd usage output
  124. usage := "Usage: docker volume [OPTIONS] [COMMAND]"
  125. c.Assert(out, checker.Contains, usage)
  126. // invalid arg should error and show the command usage on stderr
  127. _, stderr, _, err := runCommandWithStdoutStderr(exec.Command(dockerBinary, "volume", "somearg"))
  128. c.Assert(err, check.NotNil, check.Commentf(stderr))
  129. c.Assert(stderr, checker.Contains, usage)
  130. // invalid flag should error and show the flag error and cmd usage
  131. _, stderr, _, err = runCommandWithStdoutStderr(exec.Command(dockerBinary, "volume", "--no-such-flag"))
  132. c.Assert(err, check.NotNil, check.Commentf(stderr))
  133. c.Assert(stderr, checker.Contains, usage)
  134. c.Assert(stderr, checker.Contains, "flag provided but not defined: --no-such-flag")
  135. }