2015-06-12 09:25:32 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-04-19 18:30:59 -04:00
|
|
|
"context"
|
2016-09-17 15:32:31 -04:00
|
|
|
"fmt"
|
2016-08-04 08:59:55 -04:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2015-06-12 09:25:32 -04:00
|
|
|
"os/exec"
|
2016-08-04 08:59:55 -04:00
|
|
|
"path/filepath"
|
2015-06-12 09:25:32 -04:00
|
|
|
"strings"
|
2019-09-09 17:06:12 -04:00
|
|
|
"testing"
|
2015-06-12 09:25:32 -04:00
|
|
|
|
2017-05-23 23:56:26 -04:00
|
|
|
"github.com/docker/docker/api/types/container"
|
|
|
|
"github.com/docker/docker/api/types/mount"
|
|
|
|
"github.com/docker/docker/api/types/network"
|
|
|
|
"github.com/docker/docker/client"
|
2017-03-23 13:35:22 -04:00
|
|
|
"github.com/docker/docker/integration-cli/cli/build"
|
2019-04-04 09:23:19 -04:00
|
|
|
"gotest.tools/assert"
|
2018-06-11 09:32:11 -04:00
|
|
|
"gotest.tools/icmd"
|
2015-06-12 09:25:32 -04:00
|
|
|
)
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestVolumeCLICreate(c *testing.T) {
|
2015-06-12 09:25:32 -04:00
|
|
|
dockerCmd(c, "volume", "create")
|
|
|
|
|
2016-12-13 15:21:51 -05:00
|
|
|
_, _, err := dockerCmdWithError("volume", "create", "-d", "nosuchdriver")
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.ErrorContains(c, err, "")
|
2015-06-12 09:25:32 -04:00
|
|
|
|
2016-06-14 18:42:30 -04:00
|
|
|
// test using hidden --name option
|
2015-06-12 09:25:32 -04:00
|
|
|
out, _ := dockerCmd(c, "volume", "create", "--name=test")
|
|
|
|
name := strings.TrimSpace(out)
|
2019-09-09 17:05:56 -04:00
|
|
|
assert.Equal(c, name, "test")
|
2016-06-14 18:42:30 -04:00
|
|
|
|
|
|
|
out, _ = dockerCmd(c, "volume", "create", "test2")
|
|
|
|
name = strings.TrimSpace(out)
|
2019-09-09 17:05:56 -04:00
|
|
|
assert.Equal(c, name, "test2")
|
2015-06-12 09:25:32 -04:00
|
|
|
}
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestVolumeCLIInspect(c *testing.T) {
|
2019-09-09 17:08:22 -04:00
|
|
|
assert.Assert(c, exec.Command(dockerBinary, "volume", "inspect", "doesnotexist").Run() != nil, "volume inspect should error on non-existent volume")
|
2015-06-12 09:25:32 -04:00
|
|
|
out, _ := dockerCmd(c, "volume", "create")
|
|
|
|
name := strings.TrimSpace(out)
|
2016-05-16 17:20:29 -04:00
|
|
|
out, _ = dockerCmd(c, "volume", "inspect", "--format={{ .Name }}", name)
|
2019-09-09 17:05:56 -04:00
|
|
|
assert.Equal(c, strings.TrimSpace(out), name)
|
2015-06-12 09:25:32 -04:00
|
|
|
|
2016-06-14 18:42:30 -04:00
|
|
|
dockerCmd(c, "volume", "create", "test")
|
2016-05-16 17:20:29 -04:00
|
|
|
out, _ = dockerCmd(c, "volume", "inspect", "--format={{ .Name }}", "test")
|
2019-09-09 17:05:56 -04:00
|
|
|
assert.Equal(c, strings.TrimSpace(out), "test")
|
2015-06-12 09:25:32 -04:00
|
|
|
}
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestVolumeCLIInspectMulti(c *testing.T) {
|
2016-06-14 18:42:30 -04:00
|
|
|
dockerCmd(c, "volume", "create", "test1")
|
|
|
|
dockerCmd(c, "volume", "create", "test2")
|
2017-02-01 15:03:58 -05:00
|
|
|
dockerCmd(c, "volume", "create", "test3")
|
2015-11-07 12:11:37 -05:00
|
|
|
|
2017-05-21 19:24:07 -04:00
|
|
|
result := dockerCmdWithResult("volume", "inspect", "--format={{ .Name }}", "test1", "test2", "doesnotexist", "test3")
|
2017-08-23 17:01:29 -04:00
|
|
|
result.Assert(c, icmd.Expected{
|
2016-08-04 12:57:34 -04:00
|
|
|
ExitCode: 1,
|
2017-05-21 19:24:07 -04:00
|
|
|
Err: "No such volume: doesnotexist",
|
2016-08-04 12:57:34 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
out := result.Stdout()
|
2019-09-09 17:08:22 -04:00
|
|
|
assert.Assert(c, strings.Contains(out, "test1"))
|
|
|
|
assert.Assert(c, strings.Contains(out, "test2"))
|
|
|
|
assert.Assert(c, strings.Contains(out, "test3"))
|
2015-11-07 12:11:37 -05:00
|
|
|
}
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestVolumeCLILs(c *testing.T) {
|
2016-02-03 09:16:00 -05:00
|
|
|
prefix, _ := getPrefixAndSlashFromDaemonPlatform()
|
2016-06-14 18:42:30 -04:00
|
|
|
dockerCmd(c, "volume", "create", "aaa")
|
2015-06-12 09:25:32 -04:00
|
|
|
|
2016-06-14 18:42:30 -04:00
|
|
|
dockerCmd(c, "volume", "create", "test")
|
2016-02-17 03:59:53 -05:00
|
|
|
|
2016-06-14 18:42:30 -04:00
|
|
|
dockerCmd(c, "volume", "create", "soo")
|
2016-02-17 03:59:53 -05:00
|
|
|
dockerCmd(c, "run", "-v", "soo:"+prefix+"/foo", "busybox", "ls", "/")
|
2015-06-12 09:25:32 -04:00
|
|
|
|
2017-09-15 09:16:38 -04:00
|
|
|
out, _ := dockerCmd(c, "volume", "ls", "-q")
|
|
|
|
assertVolumesInList(c, out, []string{"aaa", "soo", "test"})
|
2016-02-17 03:59:53 -05:00
|
|
|
}
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestVolumeLsFormat(c *testing.T) {
|
2016-06-14 18:42:30 -04:00
|
|
|
dockerCmd(c, "volume", "create", "aaa")
|
|
|
|
dockerCmd(c, "volume", "create", "test")
|
|
|
|
dockerCmd(c, "volume", "create", "soo")
|
2016-08-04 08:59:55 -04:00
|
|
|
|
|
|
|
out, _ := dockerCmd(c, "volume", "ls", "--format", "{{.Name}}")
|
2017-09-15 09:16:38 -04:00
|
|
|
assertVolumesInList(c, out, []string{"aaa", "soo", "test"})
|
2016-08-04 08:59:55 -04:00
|
|
|
}
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestVolumeLsFormatDefaultFormat(c *testing.T) {
|
2016-06-14 18:42:30 -04:00
|
|
|
dockerCmd(c, "volume", "create", "aaa")
|
|
|
|
dockerCmd(c, "volume", "create", "test")
|
|
|
|
dockerCmd(c, "volume", "create", "soo")
|
2016-08-04 08:59:55 -04:00
|
|
|
|
|
|
|
config := `{
|
|
|
|
"volumesFormat": "{{ .Name }} default"
|
|
|
|
}`
|
|
|
|
d, err := ioutil.TempDir("", "integration-cli-")
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2016-08-04 08:59:55 -04:00
|
|
|
defer os.RemoveAll(d)
|
|
|
|
|
|
|
|
err = ioutil.WriteFile(filepath.Join(d, "config.json"), []byte(config), 0644)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2016-08-04 08:59:55 -04:00
|
|
|
|
|
|
|
out, _ := dockerCmd(c, "--config", d, "volume", "ls")
|
2017-09-15 09:16:38 -04:00
|
|
|
assertVolumesInList(c, out, []string{"aaa default", "soo default", "test default"})
|
2016-08-04 08:59:55 -04:00
|
|
|
}
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func assertVolumesInList(c *testing.T, out string, expected []string) {
|
2017-09-15 09:16:38 -04:00
|
|
|
lines := strings.Split(strings.TrimSpace(string(out)), "\n")
|
|
|
|
for _, expect := range expected {
|
|
|
|
found := false
|
|
|
|
for _, v := range lines {
|
|
|
|
found = v == expect
|
|
|
|
if found {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Assert(c, found, "Expected volume not found: %v, got: %v", expect, lines)
|
2017-09-15 09:16:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestVolumeCLILsFilterDangling(c *testing.T) {
|
2016-02-03 09:16:00 -05:00
|
|
|
prefix, _ := getPrefixAndSlashFromDaemonPlatform()
|
2016-06-14 18:42:30 -04:00
|
|
|
dockerCmd(c, "volume", "create", "testnotinuse1")
|
|
|
|
dockerCmd(c, "volume", "create", "testisinuse1")
|
|
|
|
dockerCmd(c, "volume", "create", "testisinuse2")
|
2015-09-05 18:44:29 -04:00
|
|
|
|
|
|
|
// Make sure both "created" (but not started), and started
|
|
|
|
// containers are included in reference counting
|
2015-09-23 19:04:51 -04:00
|
|
|
dockerCmd(c, "run", "--name", "volume-test1", "-v", "testisinuse1:"+prefix+"/foo", "busybox", "true")
|
|
|
|
dockerCmd(c, "create", "--name", "volume-test2", "-v", "testisinuse2:"+prefix+"/foo", "busybox", "true")
|
2015-09-05 18:44:29 -04:00
|
|
|
|
|
|
|
out, _ := dockerCmd(c, "volume", "ls")
|
|
|
|
|
|
|
|
// No filter, all volumes should show
|
2019-09-09 17:08:22 -04:00
|
|
|
assert.Assert(c, strings.Contains(out, "testnotinuse1\n"), "expected volume 'testnotinuse1' in output")
|
|
|
|
assert.Assert(c, strings.Contains(out, "testisinuse1\n"), "expected volume 'testisinuse1' in output")
|
|
|
|
assert.Assert(c, strings.Contains(out, "testisinuse2\n"), "expected volume 'testisinuse2' in output")
|
2015-09-05 18:44:29 -04:00
|
|
|
out, _ = dockerCmd(c, "volume", "ls", "--filter", "dangling=false")
|
|
|
|
|
2016-01-25 14:39:41 -05:00
|
|
|
// Explicitly disabling dangling
|
2019-09-09 17:08:22 -04:00
|
|
|
assert.Assert(c, !strings.Contains(out, "testnotinuse1\n"), "expected volume 'testnotinuse1' in output")
|
|
|
|
assert.Assert(c, strings.Contains(out, "testisinuse1\n"), "expected volume 'testisinuse1' in output")
|
|
|
|
assert.Assert(c, strings.Contains(out, "testisinuse2\n"), "expected volume 'testisinuse2' in output")
|
2015-09-05 18:44:29 -04:00
|
|
|
out, _ = dockerCmd(c, "volume", "ls", "--filter", "dangling=true")
|
|
|
|
|
2015-12-13 11:00:39 -05:00
|
|
|
// Filter "dangling" volumes; only "dangling" (unused) volumes should be in the output
|
2019-09-09 17:08:22 -04:00
|
|
|
assert.Assert(c, strings.Contains(out, "testnotinuse1\n"), "expected volume 'testnotinuse1' in output")
|
2019-09-11 06:57:29 -04:00
|
|
|
assert.Assert(c, !strings.Contains(out, "testisinuse1\n"), "volume 'testisinuse1' in output, but not expected")
|
|
|
|
assert.Assert(c, !strings.Contains(out, "testisinuse2\n"), "volume 'testisinuse2' in output, but not expected")
|
2016-01-04 03:37:01 -05:00
|
|
|
out, _ = dockerCmd(c, "volume", "ls", "--filter", "dangling=1")
|
|
|
|
// Filter "dangling" volumes; only "dangling" (unused) volumes should be in the output, dangling also accept 1
|
2019-09-09 17:08:22 -04:00
|
|
|
assert.Assert(c, strings.Contains(out, "testnotinuse1\n"), "expected volume 'testnotinuse1' in output")
|
2019-09-11 06:57:29 -04:00
|
|
|
assert.Assert(c, !strings.Contains(out, "testisinuse1\n"), "volume 'testisinuse1' in output, but not expected")
|
|
|
|
assert.Assert(c, !strings.Contains(out, "testisinuse2\n"), "volume 'testisinuse2' in output, but not expected")
|
2016-01-04 03:37:01 -05:00
|
|
|
out, _ = dockerCmd(c, "volume", "ls", "--filter", "dangling=0")
|
|
|
|
// dangling=0 is same as dangling=false case
|
2019-09-09 17:08:22 -04:00
|
|
|
assert.Assert(c, !strings.Contains(out, "testnotinuse1\n"), "expected volume 'testnotinuse1' in output")
|
|
|
|
assert.Assert(c, strings.Contains(out, "testisinuse1\n"), "expected volume 'testisinuse1' in output")
|
|
|
|
assert.Assert(c, strings.Contains(out, "testisinuse2\n"), "expected volume 'testisinuse2' in output")
|
2016-03-21 03:39:48 -04:00
|
|
|
out, _ = dockerCmd(c, "volume", "ls", "--filter", "name=testisin")
|
2019-09-09 17:08:22 -04:00
|
|
|
assert.Assert(c, !strings.Contains(out, "testnotinuse1\n"), "expected volume 'testnotinuse1' in output")
|
|
|
|
assert.Assert(c, strings.Contains(out, "testisinuse1\n"), "expected volume 'testisinuse1' in output")
|
|
|
|
assert.Assert(c, strings.Contains(out, "testisinuse2\n"), "expected volume 'testisinuse2' in output")
|
2016-01-04 03:37:01 -05:00
|
|
|
}
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestVolumeCLILsErrorWithInvalidFilterName(c *testing.T) {
|
2016-01-04 03:37:01 -05:00
|
|
|
out, _, err := dockerCmdWithError("volume", "ls", "-f", "FOO=123")
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.ErrorContains(c, err, "")
|
2019-09-09 17:08:22 -04:00
|
|
|
assert.Assert(c, strings.Contains(out, "Invalid filter"))
|
2016-01-04 03:37:01 -05:00
|
|
|
}
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestVolumeCLILsWithIncorrectFilterValue(c *testing.T) {
|
2016-01-04 03:37:01 -05:00
|
|
|
out, _, err := dockerCmdWithError("volume", "ls", "-f", "dangling=invalid")
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.ErrorContains(c, err, "")
|
2019-09-09 17:08:22 -04:00
|
|
|
assert.Assert(c, strings.Contains(out, "Invalid filter"))
|
2015-09-05 18:44:29 -04:00
|
|
|
}
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestVolumeCLIRm(c *testing.T) {
|
2016-02-03 09:16:00 -05:00
|
|
|
prefix, _ := getPrefixAndSlashFromDaemonPlatform()
|
2015-06-12 09:25:32 -04:00
|
|
|
out, _ := dockerCmd(c, "volume", "create")
|
|
|
|
id := strings.TrimSpace(out)
|
|
|
|
|
2016-06-14 18:42:30 -04:00
|
|
|
dockerCmd(c, "volume", "create", "test")
|
2015-06-12 09:25:32 -04:00
|
|
|
dockerCmd(c, "volume", "rm", id)
|
|
|
|
dockerCmd(c, "volume", "rm", "test")
|
|
|
|
|
|
|
|
volumeID := "testing"
|
2015-09-23 19:04:51 -04:00
|
|
|
dockerCmd(c, "run", "-v", volumeID+":"+prefix+"/foo", "--name=test", "busybox", "sh", "-c", "echo hello > /foo/bar")
|
2017-01-05 06:38:34 -05:00
|
|
|
|
|
|
|
icmd.RunCommand(dockerBinary, "volume", "rm", "testing").Assert(c, icmd.Expected{
|
|
|
|
ExitCode: 1,
|
2017-01-05 13:08:24 -05:00
|
|
|
Error: "exit status 1",
|
2017-01-05 06:38:34 -05:00
|
|
|
})
|
2015-06-12 09:25:32 -04:00
|
|
|
|
|
|
|
out, _ = dockerCmd(c, "run", "--volumes-from=test", "--name=test2", "busybox", "sh", "-c", "cat /foo/bar")
|
2019-09-09 17:05:56 -04:00
|
|
|
assert.Equal(c, strings.TrimSpace(out), "hello")
|
2015-06-12 09:25:32 -04:00
|
|
|
dockerCmd(c, "rm", "-fv", "test2")
|
|
|
|
dockerCmd(c, "volume", "inspect", volumeID)
|
|
|
|
dockerCmd(c, "rm", "-f", "test")
|
|
|
|
|
2015-09-23 19:04:51 -04:00
|
|
|
out, _ = dockerCmd(c, "run", "--name=test2", "-v", volumeID+":"+prefix+"/foo", "busybox", "sh", "-c", "cat /foo/bar")
|
2019-09-09 17:08:22 -04:00
|
|
|
assert.Equal(c, strings.TrimSpace(out), "hello", "volume data was removed")
|
2015-06-12 09:25:32 -04:00
|
|
|
dockerCmd(c, "rm", "test2")
|
|
|
|
|
|
|
|
dockerCmd(c, "volume", "rm", volumeID)
|
2019-09-09 17:08:22 -04:00
|
|
|
assert.Assert(c, exec.Command("volume", "rm", "doesnotexist").Run() != nil, "volume rm should fail with non-existent volume")
|
2015-06-12 09:25:32 -04:00
|
|
|
}
|
2015-08-26 20:31:31 -04:00
|
|
|
|
2016-12-13 15:21:51 -05:00
|
|
|
// FIXME(vdemeester) should be a unit test in cli/command/volume package
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestVolumeCLINoArgs(c *testing.T) {
|
2015-08-26 20:31:31 -04:00
|
|
|
out, _ := dockerCmd(c, "volume")
|
2015-10-07 12:22:08 -04:00
|
|
|
// no args should produce the cmd usage output
|
2016-05-16 17:20:29 -04:00
|
|
|
usage := "Usage: docker volume COMMAND"
|
2019-09-09 17:08:22 -04:00
|
|
|
assert.Assert(c, strings.Contains(out, usage))
|
2015-08-26 20:31:31 -04:00
|
|
|
// invalid arg should error and show the command usage on stderr
|
2016-12-13 15:21:51 -05:00
|
|
|
icmd.RunCommand(dockerBinary, "volume", "somearg").Assert(c, icmd.Expected{
|
|
|
|
ExitCode: 1,
|
|
|
|
Error: "exit status 1",
|
|
|
|
Err: usage,
|
|
|
|
})
|
2015-10-07 12:22:08 -04:00
|
|
|
|
|
|
|
// invalid flag should error and show the flag error and cmd usage
|
2016-12-13 15:21:51 -05:00
|
|
|
result := icmd.RunCommand(dockerBinary, "volume", "--no-such-flag")
|
|
|
|
result.Assert(c, icmd.Expected{
|
|
|
|
ExitCode: 125,
|
|
|
|
Error: "exit status 125",
|
|
|
|
Err: usage,
|
|
|
|
})
|
2019-09-09 17:08:22 -04:00
|
|
|
assert.Assert(c, strings.Contains(result.Stderr(), "unknown flag: --no-such-flag"))
|
2015-08-26 20:31:31 -04:00
|
|
|
}
|
2015-12-02 15:24:30 -05:00
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestVolumeCLIInspectTmplError(c *testing.T) {
|
2015-12-02 15:24:30 -05:00
|
|
|
out, _ := dockerCmd(c, "volume", "create")
|
|
|
|
name := strings.TrimSpace(out)
|
|
|
|
|
|
|
|
out, exitCode, err := dockerCmdWithError("volume", "inspect", "--format='{{ .FooBar }}'", name)
|
2019-09-11 06:57:29 -04:00
|
|
|
assert.Assert(c, err != nil, "Output: %s", out)
|
2019-09-09 17:08:22 -04:00
|
|
|
assert.Equal(c, exitCode, 1, fmt.Sprintf("Output: %s", out))
|
2019-09-09 17:08:22 -04:00
|
|
|
assert.Assert(c, strings.Contains(out, "Template parsing error"))
|
2015-12-02 15:24:30 -05:00
|
|
|
}
|
2016-02-11 21:48:16 -05:00
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestVolumeCLICreateWithOpts(c *testing.T) {
|
2016-02-11 21:48:16 -05:00
|
|
|
testRequires(c, DaemonIsLinux)
|
|
|
|
|
2016-06-14 18:42:30 -04:00
|
|
|
dockerCmd(c, "volume", "create", "-d", "local", "test", "--opt=type=tmpfs", "--opt=device=tmpfs", "--opt=o=size=1m,uid=1000")
|
2016-02-11 21:48:16 -05:00
|
|
|
out, _ := dockerCmd(c, "run", "-v", "test:/foo", "busybox", "mount")
|
|
|
|
|
|
|
|
mounts := strings.Split(out, "\n")
|
|
|
|
var found bool
|
|
|
|
for _, m := range mounts {
|
|
|
|
if strings.Contains(m, "/foo") {
|
|
|
|
found = true
|
|
|
|
info := strings.Fields(m)
|
|
|
|
// tmpfs on <path> type tmpfs (rw,relatime,size=1024k,uid=1000)
|
2019-09-09 17:05:56 -04:00
|
|
|
assert.Equal(c, info[0], "tmpfs")
|
|
|
|
assert.Equal(c, info[2], "/foo")
|
|
|
|
assert.Equal(c, info[4], "tmpfs")
|
2019-09-09 17:08:22 -04:00
|
|
|
assert.Assert(c, strings.Contains(info[5], "uid=1000"))
|
|
|
|
assert.Assert(c, strings.Contains(info[5], "size=1024k"))
|
2016-12-08 02:59:52 -05:00
|
|
|
break
|
2016-02-11 21:48:16 -05:00
|
|
|
}
|
|
|
|
}
|
2019-09-09 17:05:56 -04:00
|
|
|
assert.Equal(c, found, true)
|
2016-02-11 21:48:16 -05:00
|
|
|
}
|
2016-03-16 17:52:34 -04:00
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestVolumeCLICreateLabel(c *testing.T) {
|
2016-03-16 17:52:34 -04:00
|
|
|
testVol := "testvolcreatelabel"
|
|
|
|
testLabel := "foo"
|
|
|
|
testValue := "bar"
|
|
|
|
|
2018-07-09 13:40:34 -04:00
|
|
|
_, _, err := dockerCmdWithError("volume", "create", "--label", testLabel+"="+testValue, testVol)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2016-03-16 17:52:34 -04:00
|
|
|
|
2018-07-09 13:40:34 -04:00
|
|
|
out, _ := dockerCmd(c, "volume", "inspect", "--format={{ .Labels."+testLabel+" }}", testVol)
|
2019-09-09 17:05:56 -04:00
|
|
|
assert.Equal(c, strings.TrimSpace(out), testValue)
|
2016-03-16 17:52:34 -04:00
|
|
|
}
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestVolumeCLICreateLabelMultiple(c *testing.T) {
|
2016-03-16 17:52:34 -04:00
|
|
|
testVol := "testvolcreatelabel"
|
|
|
|
|
|
|
|
testLabels := map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
"baz": "foo",
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"volume",
|
|
|
|
"create",
|
|
|
|
testVol,
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range testLabels {
|
|
|
|
args = append(args, "--label", k+"="+v)
|
|
|
|
}
|
|
|
|
|
2018-07-09 13:40:34 -04:00
|
|
|
_, _, err := dockerCmdWithError(args...)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2016-03-16 17:52:34 -04:00
|
|
|
|
|
|
|
for k, v := range testLabels {
|
2018-07-09 13:40:34 -04:00
|
|
|
out, _ := dockerCmd(c, "volume", "inspect", "--format={{ .Labels."+k+" }}", testVol)
|
2019-09-09 17:05:56 -04:00
|
|
|
assert.Equal(c, strings.TrimSpace(out), v)
|
2016-03-16 17:52:34 -04:00
|
|
|
}
|
|
|
|
}
|
2016-03-28 04:43:41 -04:00
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestVolumeCLILsFilterLabels(c *testing.T) {
|
2016-03-28 04:43:41 -04:00
|
|
|
testVol1 := "testvolcreatelabel-1"
|
2018-07-09 13:40:34 -04:00
|
|
|
_, _, err := dockerCmdWithError("volume", "create", "--label", "foo=bar1", testVol1)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2016-03-28 04:43:41 -04:00
|
|
|
|
|
|
|
testVol2 := "testvolcreatelabel-2"
|
2018-07-09 13:40:34 -04:00
|
|
|
_, _, err = dockerCmdWithError("volume", "create", "--label", "foo=bar2", testVol2)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2016-03-28 04:43:41 -04:00
|
|
|
|
2018-07-09 13:40:34 -04:00
|
|
|
out, _ := dockerCmd(c, "volume", "ls", "--filter", "label=foo")
|
2016-03-28 04:43:41 -04:00
|
|
|
|
|
|
|
// filter with label=key
|
2019-09-09 17:08:22 -04:00
|
|
|
assert.Assert(c, strings.Contains(out, "testvolcreatelabel-1\n"), "expected volume 'testvolcreatelabel-1' in output")
|
|
|
|
assert.Assert(c, strings.Contains(out, "testvolcreatelabel-2\n"), "expected volume 'testvolcreatelabel-2' in output")
|
2016-03-28 04:43:41 -04:00
|
|
|
out, _ = dockerCmd(c, "volume", "ls", "--filter", "label=foo=bar1")
|
|
|
|
|
|
|
|
// filter with label=key=value
|
2019-09-09 17:08:22 -04:00
|
|
|
assert.Assert(c, strings.Contains(out, "testvolcreatelabel-1\n"), "expected volume 'testvolcreatelabel-1' in output")
|
|
|
|
assert.Assert(c, !strings.Contains(out, "testvolcreatelabel-2\n"), "expected volume 'testvolcreatelabel-2 in output")
|
2016-03-28 04:43:41 -04:00
|
|
|
out, _ = dockerCmd(c, "volume", "ls", "--filter", "label=non-exist")
|
|
|
|
outArr := strings.Split(strings.TrimSpace(out), "\n")
|
2019-09-09 17:08:22 -04:00
|
|
|
assert.Equal(c, len(outArr), 1, fmt.Sprintf("\n%s", out))
|
2016-03-28 04:43:41 -04:00
|
|
|
|
|
|
|
out, _ = dockerCmd(c, "volume", "ls", "--filter", "label=foo=non-exist")
|
|
|
|
outArr = strings.Split(strings.TrimSpace(out), "\n")
|
2019-09-09 17:08:22 -04:00
|
|
|
assert.Equal(c, len(outArr), 1, fmt.Sprintf("\n%s", out))
|
2016-03-28 04:43:41 -04:00
|
|
|
}
|
2016-06-10 10:40:09 -04:00
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestVolumeCLILsFilterDrivers(c *testing.T) {
|
2017-01-04 01:43:29 -05:00
|
|
|
// using default volume driver local to create volumes
|
|
|
|
testVol1 := "testvol-1"
|
2018-07-09 13:40:34 -04:00
|
|
|
_, _, err := dockerCmdWithError("volume", "create", testVol1)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2017-01-04 01:43:29 -05:00
|
|
|
|
|
|
|
testVol2 := "testvol-2"
|
2018-07-09 13:40:34 -04:00
|
|
|
_, _, err = dockerCmdWithError("volume", "create", testVol2)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2017-01-04 01:43:29 -05:00
|
|
|
|
|
|
|
// filter with driver=local
|
2018-07-09 13:40:34 -04:00
|
|
|
out, _ := dockerCmd(c, "volume", "ls", "--filter", "driver=local")
|
2019-09-09 17:08:22 -04:00
|
|
|
assert.Assert(c, strings.Contains(out, "testvol-1\n"), "expected volume 'testvol-1' in output")
|
|
|
|
assert.Assert(c, strings.Contains(out, "testvol-2\n"), "expected volume 'testvol-2' in output")
|
2017-01-04 01:43:29 -05:00
|
|
|
// filter with driver=invaliddriver
|
|
|
|
out, _ = dockerCmd(c, "volume", "ls", "--filter", "driver=invaliddriver")
|
|
|
|
outArr := strings.Split(strings.TrimSpace(out), "\n")
|
2019-09-09 17:08:22 -04:00
|
|
|
assert.Equal(c, len(outArr), 1, fmt.Sprintf("\n%s", out))
|
2017-01-04 01:43:29 -05:00
|
|
|
|
|
|
|
// filter with driver=loca
|
|
|
|
out, _ = dockerCmd(c, "volume", "ls", "--filter", "driver=loca")
|
|
|
|
outArr = strings.Split(strings.TrimSpace(out), "\n")
|
2019-09-09 17:08:22 -04:00
|
|
|
assert.Equal(c, len(outArr), 1, fmt.Sprintf("\n%s", out))
|
2017-01-04 01:43:29 -05:00
|
|
|
|
|
|
|
// filter with driver=
|
|
|
|
out, _ = dockerCmd(c, "volume", "ls", "--filter", "driver=")
|
|
|
|
outArr = strings.Split(strings.TrimSpace(out), "\n")
|
2019-09-09 17:08:22 -04:00
|
|
|
assert.Equal(c, len(outArr), 1, fmt.Sprintf("\n%s", out))
|
2017-01-04 01:43:29 -05:00
|
|
|
}
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestVolumeCLIRmForceUsage(c *testing.T) {
|
2016-06-10 10:40:09 -04:00
|
|
|
out, _ := dockerCmd(c, "volume", "create")
|
|
|
|
id := strings.TrimSpace(out)
|
|
|
|
|
|
|
|
dockerCmd(c, "volume", "rm", "-f", id)
|
|
|
|
dockerCmd(c, "volume", "rm", "--force", "nonexist")
|
|
|
|
}
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestVolumeCLIRmForce(c *testing.T) {
|
2018-12-24 07:25:53 -05:00
|
|
|
testRequires(c, testEnv.IsLocalDaemon, DaemonIsLinux)
|
2016-06-10 10:40:09 -04:00
|
|
|
|
|
|
|
name := "test"
|
2016-06-14 18:42:30 -04:00
|
|
|
out, _ := dockerCmd(c, "volume", "create", name)
|
2016-06-10 10:40:09 -04:00
|
|
|
id := strings.TrimSpace(out)
|
2019-09-09 17:05:56 -04:00
|
|
|
assert.Equal(c, id, name)
|
2016-06-10 10:40:09 -04:00
|
|
|
|
|
|
|
out, _ = dockerCmd(c, "volume", "inspect", "--format", "{{.Mountpoint}}", name)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Assert(c, strings.TrimSpace(out) != "")
|
2016-06-10 10:40:09 -04:00
|
|
|
// Mountpoint is in the form of "/var/lib/docker/volumes/.../_data", removing `/_data`
|
|
|
|
path := strings.TrimSuffix(strings.TrimSpace(out), "/_data")
|
2017-01-05 06:38:34 -05:00
|
|
|
icmd.RunCommand("rm", "-rf", path).Assert(c, icmd.Success)
|
2016-06-10 10:40:09 -04:00
|
|
|
|
2017-03-01 08:36:09 -05:00
|
|
|
dockerCmd(c, "volume", "rm", "-f", name)
|
2016-06-10 10:40:09 -04:00
|
|
|
out, _ = dockerCmd(c, "volume", "ls")
|
2019-09-09 17:07:46 -04:00
|
|
|
assert.Assert(c, !strings.Contains(out, name))
|
2017-03-01 08:36:09 -05:00
|
|
|
dockerCmd(c, "volume", "create", name)
|
2016-06-10 10:40:09 -04:00
|
|
|
out, _ = dockerCmd(c, "volume", "ls")
|
2019-09-09 17:08:22 -04:00
|
|
|
assert.Assert(c, strings.Contains(out, name))
|
2016-06-10 10:40:09 -04:00
|
|
|
}
|
2016-09-17 15:32:31 -04:00
|
|
|
|
2017-03-01 08:36:09 -05:00
|
|
|
// TestVolumeCLIRmForceInUse verifies that repeated `docker volume rm -f` calls does not remove a volume
|
|
|
|
// if it is in use. Test case for https://github.com/docker/docker/issues/31446
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestVolumeCLIRmForceInUse(c *testing.T) {
|
2017-03-01 08:36:09 -05:00
|
|
|
name := "testvolume"
|
|
|
|
out, _ := dockerCmd(c, "volume", "create", name)
|
|
|
|
id := strings.TrimSpace(out)
|
2019-09-09 17:05:56 -04:00
|
|
|
assert.Equal(c, id, name)
|
2017-03-01 08:36:09 -05:00
|
|
|
|
|
|
|
prefix, slash := getPrefixAndSlashFromDaemonPlatform()
|
2018-07-09 13:40:34 -04:00
|
|
|
out, _ = dockerCmd(c, "create", "-v", "testvolume:"+prefix+slash+"foo", "busybox")
|
2017-03-01 08:36:09 -05:00
|
|
|
cid := strings.TrimSpace(out)
|
|
|
|
|
|
|
|
_, _, err := dockerCmdWithError("volume", "rm", "-f", name)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.ErrorContains(c, err, "")
|
|
|
|
assert.ErrorContains(c, err, "volume is in use")
|
2017-03-01 08:36:09 -05:00
|
|
|
out, _ = dockerCmd(c, "volume", "ls")
|
2019-09-09 17:08:22 -04:00
|
|
|
assert.Assert(c, strings.Contains(out, name))
|
2017-03-01 08:36:09 -05:00
|
|
|
// The original issue did not _remove_ the volume from the list
|
|
|
|
// the first time. But a second call to `volume rm` removed it.
|
|
|
|
// Calling `volume rm` a second time to confirm it's not removed
|
|
|
|
// when calling twice.
|
|
|
|
_, _, err = dockerCmdWithError("volume", "rm", "-f", name)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.ErrorContains(c, err, "")
|
|
|
|
assert.ErrorContains(c, err, "volume is in use")
|
2017-03-01 08:36:09 -05:00
|
|
|
out, _ = dockerCmd(c, "volume", "ls")
|
2019-09-09 17:08:22 -04:00
|
|
|
assert.Assert(c, strings.Contains(out, name))
|
2017-03-01 08:36:09 -05:00
|
|
|
// Verify removing the volume after the container is removed works
|
2018-07-09 13:40:34 -04:00
|
|
|
_, e := dockerCmd(c, "rm", cid)
|
2019-09-09 17:05:56 -04:00
|
|
|
assert.Equal(c, e, 0)
|
2017-03-01 08:36:09 -05:00
|
|
|
|
|
|
|
_, e = dockerCmd(c, "volume", "rm", "-f", name)
|
2019-09-09 17:05:56 -04:00
|
|
|
assert.Equal(c, e, 0)
|
2017-03-01 08:36:09 -05:00
|
|
|
|
|
|
|
out, e = dockerCmd(c, "volume", "ls")
|
2019-09-09 17:05:56 -04:00
|
|
|
assert.Equal(c, e, 0)
|
2019-09-09 17:07:46 -04:00
|
|
|
assert.Assert(c, !strings.Contains(out, name))
|
2017-03-01 08:36:09 -05:00
|
|
|
}
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestVolumeCliInspectWithVolumeOpts(c *testing.T) {
|
2016-09-17 15:32:31 -04:00
|
|
|
testRequires(c, DaemonIsLinux)
|
|
|
|
|
|
|
|
// Without options
|
|
|
|
name := "test1"
|
|
|
|
dockerCmd(c, "volume", "create", "-d", "local", name)
|
|
|
|
out, _ := dockerCmd(c, "volume", "inspect", "--format={{ .Options }}", name)
|
2019-09-09 17:08:22 -04:00
|
|
|
assert.Assert(c, strings.Contains(strings.TrimSpace(out), "map[]"))
|
2016-09-17 15:32:31 -04:00
|
|
|
// With options
|
|
|
|
name = "test2"
|
|
|
|
k1, v1 := "type", "tmpfs"
|
|
|
|
k2, v2 := "device", "tmpfs"
|
|
|
|
k3, v3 := "o", "size=1m,uid=1000"
|
|
|
|
dockerCmd(c, "volume", "create", "-d", "local", name, "--opt", fmt.Sprintf("%s=%s", k1, v1), "--opt", fmt.Sprintf("%s=%s", k2, v2), "--opt", fmt.Sprintf("%s=%s", k3, v3))
|
|
|
|
out, _ = dockerCmd(c, "volume", "inspect", "--format={{ .Options }}", name)
|
2019-09-09 17:08:22 -04:00
|
|
|
assert.Assert(c, strings.Contains(strings.TrimSpace(out), fmt.Sprintf("%s:%s", k1, v1)))
|
|
|
|
assert.Assert(c, strings.Contains(strings.TrimSpace(out), fmt.Sprintf("%s:%s", k2, v2)))
|
|
|
|
assert.Assert(c, strings.Contains(strings.TrimSpace(out), fmt.Sprintf("%s:%s", k3, v3)))
|
2016-09-17 15:32:31 -04:00
|
|
|
}
|
2017-01-27 17:12:45 -05:00
|
|
|
|
|
|
|
// Test case (1) for 21845: duplicate targets for --volumes-from
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestDuplicateMountpointsForVolumesFrom(c *testing.T) {
|
2017-01-27 17:12:45 -05:00
|
|
|
testRequires(c, DaemonIsLinux)
|
|
|
|
|
|
|
|
image := "vimage"
|
2017-03-23 13:35:22 -04:00
|
|
|
buildImageSuccessfully(c, image, build.WithDockerfile(`
|
2017-01-27 17:12:45 -05:00
|
|
|
FROM busybox
|
|
|
|
VOLUME ["/tmp/data"]`))
|
|
|
|
|
|
|
|
dockerCmd(c, "run", "--name=data1", image, "true")
|
|
|
|
dockerCmd(c, "run", "--name=data2", image, "true")
|
|
|
|
|
|
|
|
out, _ := dockerCmd(c, "inspect", "--format", "{{(index .Mounts 0).Name}}", "data1")
|
|
|
|
data1 := strings.TrimSpace(out)
|
2019-09-09 17:05:56 -04:00
|
|
|
assert.Assert(c, data1 != "")
|
2017-01-27 17:12:45 -05:00
|
|
|
|
|
|
|
out, _ = dockerCmd(c, "inspect", "--format", "{{(index .Mounts 0).Name}}", "data2")
|
|
|
|
data2 := strings.TrimSpace(out)
|
2019-09-09 17:05:56 -04:00
|
|
|
assert.Assert(c, data2 != "")
|
2017-01-27 17:12:45 -05:00
|
|
|
|
|
|
|
// Both volume should exist
|
|
|
|
out, _ = dockerCmd(c, "volume", "ls", "-q")
|
2019-09-09 17:08:22 -04:00
|
|
|
assert.Assert(c, strings.Contains(strings.TrimSpace(out), data1))
|
|
|
|
assert.Assert(c, strings.Contains(strings.TrimSpace(out), data2))
|
2017-01-27 17:12:45 -05:00
|
|
|
out, _, err := dockerCmdWithError("run", "--name=app", "--volumes-from=data1", "--volumes-from=data2", "-d", "busybox", "top")
|
2019-09-11 06:57:29 -04:00
|
|
|
assert.Assert(c, err == nil, "Out: %s", out)
|
2017-01-27 17:12:45 -05:00
|
|
|
|
|
|
|
// Only the second volume will be referenced, this is backward compatible
|
|
|
|
out, _ = dockerCmd(c, "inspect", "--format", "{{(index .Mounts 0).Name}}", "app")
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Equal(c, strings.TrimSpace(out), data2)
|
2017-01-27 17:12:45 -05:00
|
|
|
|
|
|
|
dockerCmd(c, "rm", "-f", "-v", "app")
|
|
|
|
dockerCmd(c, "rm", "-f", "-v", "data1")
|
|
|
|
dockerCmd(c, "rm", "-f", "-v", "data2")
|
|
|
|
|
|
|
|
// Both volume should not exist
|
|
|
|
out, _ = dockerCmd(c, "volume", "ls", "-q")
|
2019-09-09 17:07:46 -04:00
|
|
|
assert.Assert(c, !strings.Contains(strings.TrimSpace(out), data1))
|
|
|
|
assert.Assert(c, !strings.Contains(strings.TrimSpace(out), data2))
|
2017-01-27 17:12:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test case (2) for 21845: duplicate targets for --volumes-from and -v (bind)
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestDuplicateMountpointsForVolumesFromAndBind(c *testing.T) {
|
2017-01-27 17:12:45 -05:00
|
|
|
testRequires(c, DaemonIsLinux)
|
|
|
|
|
|
|
|
image := "vimage"
|
2017-03-23 13:35:22 -04:00
|
|
|
buildImageSuccessfully(c, image, build.WithDockerfile(`
|
2017-01-27 17:12:45 -05:00
|
|
|
FROM busybox
|
|
|
|
VOLUME ["/tmp/data"]`))
|
|
|
|
|
|
|
|
dockerCmd(c, "run", "--name=data1", image, "true")
|
|
|
|
dockerCmd(c, "run", "--name=data2", image, "true")
|
|
|
|
|
|
|
|
out, _ := dockerCmd(c, "inspect", "--format", "{{(index .Mounts 0).Name}}", "data1")
|
|
|
|
data1 := strings.TrimSpace(out)
|
2019-09-09 17:05:56 -04:00
|
|
|
assert.Assert(c, data1 != "")
|
2017-01-27 17:12:45 -05:00
|
|
|
|
|
|
|
out, _ = dockerCmd(c, "inspect", "--format", "{{(index .Mounts 0).Name}}", "data2")
|
|
|
|
data2 := strings.TrimSpace(out)
|
2019-09-09 17:05:56 -04:00
|
|
|
assert.Assert(c, data2 != "")
|
2017-01-27 17:12:45 -05:00
|
|
|
|
|
|
|
// Both volume should exist
|
|
|
|
out, _ = dockerCmd(c, "volume", "ls", "-q")
|
2019-09-09 17:08:22 -04:00
|
|
|
assert.Assert(c, strings.Contains(strings.TrimSpace(out), data1))
|
|
|
|
assert.Assert(c, strings.Contains(strings.TrimSpace(out), data2))
|
2017-03-09 04:32:04 -05:00
|
|
|
// /tmp/data is automatically created, because we are not using the modern mount API here
|
2017-01-27 17:12:45 -05:00
|
|
|
out, _, err := dockerCmdWithError("run", "--name=app", "--volumes-from=data1", "--volumes-from=data2", "-v", "/tmp/data:/tmp/data", "-d", "busybox", "top")
|
2019-09-11 06:57:29 -04:00
|
|
|
assert.Assert(c, err == nil, "Out: %s", out)
|
2017-01-27 17:12:45 -05:00
|
|
|
|
|
|
|
// No volume will be referenced (mount is /tmp/data), this is backward compatible
|
|
|
|
out, _ = dockerCmd(c, "inspect", "--format", "{{(index .Mounts 0).Name}}", "app")
|
2019-09-09 17:07:46 -04:00
|
|
|
assert.Assert(c, !strings.Contains(strings.TrimSpace(out), data1))
|
|
|
|
assert.Assert(c, !strings.Contains(strings.TrimSpace(out), data2))
|
2017-01-27 17:12:45 -05:00
|
|
|
dockerCmd(c, "rm", "-f", "-v", "app")
|
|
|
|
dockerCmd(c, "rm", "-f", "-v", "data1")
|
|
|
|
dockerCmd(c, "rm", "-f", "-v", "data2")
|
|
|
|
|
|
|
|
// Both volume should not exist
|
|
|
|
out, _ = dockerCmd(c, "volume", "ls", "-q")
|
2019-09-09 17:07:46 -04:00
|
|
|
assert.Assert(c, !strings.Contains(strings.TrimSpace(out), data1))
|
|
|
|
assert.Assert(c, !strings.Contains(strings.TrimSpace(out), data2))
|
2017-01-27 17:12:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test case (3) for 21845: duplicate targets for --volumes-from and `Mounts` (API only)
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestDuplicateMountpointsForVolumesFromAndMounts(c *testing.T) {
|
2018-12-24 07:25:53 -05:00
|
|
|
testRequires(c, testEnv.IsLocalDaemon, DaemonIsLinux)
|
2017-01-27 17:12:45 -05:00
|
|
|
|
|
|
|
image := "vimage"
|
2017-03-23 13:35:22 -04:00
|
|
|
buildImageSuccessfully(c, image, build.WithDockerfile(`
|
2017-01-27 17:12:45 -05:00
|
|
|
FROM busybox
|
|
|
|
VOLUME ["/tmp/data"]`))
|
|
|
|
|
|
|
|
dockerCmd(c, "run", "--name=data1", image, "true")
|
|
|
|
dockerCmd(c, "run", "--name=data2", image, "true")
|
|
|
|
|
|
|
|
out, _ := dockerCmd(c, "inspect", "--format", "{{(index .Mounts 0).Name}}", "data1")
|
|
|
|
data1 := strings.TrimSpace(out)
|
2019-09-09 17:05:56 -04:00
|
|
|
assert.Assert(c, data1 != "")
|
2017-01-27 17:12:45 -05:00
|
|
|
|
|
|
|
out, _ = dockerCmd(c, "inspect", "--format", "{{(index .Mounts 0).Name}}", "data2")
|
|
|
|
data2 := strings.TrimSpace(out)
|
2019-09-09 17:05:56 -04:00
|
|
|
assert.Assert(c, data2 != "")
|
2017-01-27 17:12:45 -05:00
|
|
|
|
|
|
|
// Both volume should exist
|
|
|
|
out, _ = dockerCmd(c, "volume", "ls", "-q")
|
2019-09-09 17:08:22 -04:00
|
|
|
assert.Assert(c, strings.Contains(strings.TrimSpace(out), data1))
|
|
|
|
assert.Assert(c, strings.Contains(strings.TrimSpace(out), data2))
|
2017-03-09 04:32:04 -05:00
|
|
|
err := os.MkdirAll("/tmp/data", 0755)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2017-01-27 17:12:45 -05:00
|
|
|
// Mounts is available in API
|
2019-01-03 16:49:00 -05:00
|
|
|
cli, err := client.NewClientWithOpts(client.FromEnv)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2017-05-23 23:56:26 -04:00
|
|
|
defer cli.Close()
|
|
|
|
|
|
|
|
config := container.Config{
|
|
|
|
Cmd: []string{"top"},
|
|
|
|
Image: "busybox",
|
|
|
|
}
|
|
|
|
|
|
|
|
hostConfig := container.HostConfig{
|
|
|
|
VolumesFrom: []string{"data1", "data2"},
|
|
|
|
Mounts: []mount.Mount{
|
|
|
|
{
|
|
|
|
Type: "bind",
|
|
|
|
Source: "/tmp/data",
|
|
|
|
Target: "/tmp/data",
|
2017-01-27 17:12:45 -05:00
|
|
|
},
|
2017-05-23 23:56:26 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
_, err = cli.ContainerCreate(context.Background(), &config, &hostConfig, &network.NetworkingConfig{}, "app")
|
|
|
|
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2017-01-27 17:12:45 -05:00
|
|
|
|
|
|
|
// No volume will be referenced (mount is /tmp/data), this is backward compatible
|
|
|
|
out, _ = dockerCmd(c, "inspect", "--format", "{{(index .Mounts 0).Name}}", "app")
|
2019-09-09 17:07:46 -04:00
|
|
|
assert.Assert(c, !strings.Contains(strings.TrimSpace(out), data1))
|
|
|
|
assert.Assert(c, !strings.Contains(strings.TrimSpace(out), data2))
|
2017-01-27 17:12:45 -05:00
|
|
|
dockerCmd(c, "rm", "-f", "-v", "app")
|
|
|
|
dockerCmd(c, "rm", "-f", "-v", "data1")
|
|
|
|
dockerCmd(c, "rm", "-f", "-v", "data2")
|
|
|
|
|
|
|
|
// Both volume should not exist
|
|
|
|
out, _ = dockerCmd(c, "volume", "ls", "-q")
|
2019-09-09 17:07:46 -04:00
|
|
|
assert.Assert(c, !strings.Contains(strings.TrimSpace(out), data1))
|
|
|
|
assert.Assert(c, !strings.Contains(strings.TrimSpace(out), data2))
|
2017-01-27 17:12:45 -05:00
|
|
|
}
|