integration-cli/docker_api_volumes_test.go
b3b7eb27
 package main
 
 import (
a46f757c
 	"fmt"
2af5034c
 	"path/filepath"
a46f757c
 	"strings"
 	"time"
b3b7eb27
 
0fd5a654
 	"github.com/docker/docker/api/types/filters"
29df3bdb
 	volumetypes "github.com/docker/docker/api/types/volume"
0fd5a654
 	"github.com/docker/docker/client"
33968e6c
 	"github.com/docker/docker/integration-cli/checker"
b3b7eb27
 	"github.com/go-check/check"
0fd5a654
 	"golang.org/x/net/context"
b3b7eb27
 )
 
7fb7a477
 func (s *DockerSuite) TestVolumesAPIList(c *check.C) {
382c96ee
 	prefix, _ := getPrefixAndSlashFromDaemonPlatform()
b7d8d2c4
 	cid, _ := dockerCmd(c, "run", "-d", "-v", prefix+"/foo", "busybox")
b3b7eb27
 
0fd5a654
 	cli, err := client.NewEnvClient()
710817a7
 	c.Assert(err, checker.IsNil)
0fd5a654
 	defer cli.Close()
b3b7eb27
 
b7d8d2c4
 	container, err := cli.ContainerInspect(context.Background(), strings.TrimSpace(cid))
 	c.Assert(err, checker.IsNil)
 	vname := container.Mounts[0].Name
 
0fd5a654
 	volumes, err := cli.VolumeList(context.Background(), filters.Args{})
 	c.Assert(err, checker.IsNil)
b3b7eb27
 
b7d8d2c4
 	found := false
 	for _, vol := range volumes.Volumes {
 		if vol.Name == vname {
 			found = true
 			break
 		}
 	}
 	c.Assert(found, checker.Equals, true)
b3b7eb27
 }
 
7fb7a477
 func (s *DockerSuite) TestVolumesAPICreate(c *check.C) {
5c2498fd
 	config := volumetypes.VolumesCreateBody{
b3b7eb27
 		Name: "test",
 	}
 
0fd5a654
 	cli, err := client.NewEnvClient()
710817a7
 	c.Assert(err, checker.IsNil)
0fd5a654
 	defer cli.Close()
 
 	vol, err := cli.VolumeCreate(context.Background(), config)
 	c.Assert(err, check.IsNil)
b3b7eb27
 
2af5034c
 	c.Assert(filepath.Base(filepath.Dir(vol.Mountpoint)), checker.Equals, config.Name)
b3b7eb27
 }
 
7fb7a477
 func (s *DockerSuite) TestVolumesAPIRemove(c *check.C) {
382c96ee
 	prefix, _ := getPrefixAndSlashFromDaemonPlatform()
b7d8d2c4
 	cid, _ := dockerCmd(c, "run", "-d", "-v", prefix+"/foo", "--name=test", "busybox")
b3b7eb27
 
0fd5a654
 	cli, err := client.NewEnvClient()
710817a7
 	c.Assert(err, checker.IsNil)
0fd5a654
 	defer cli.Close()
b3b7eb27
 
b7d8d2c4
 	container, err := cli.ContainerInspect(context.Background(), strings.TrimSpace(cid))
0fd5a654
 	c.Assert(err, checker.IsNil)
b7d8d2c4
 	vname := container.Mounts[0].Name
b3b7eb27
 
b7d8d2c4
 	err = cli.VolumeRemove(context.Background(), vname, false)
0fd5a654
 	c.Assert(err.Error(), checker.Contains, "volume is in use")
b3b7eb27
 
 	dockerCmd(c, "rm", "-f", "test")
b7d8d2c4
 	err = cli.VolumeRemove(context.Background(), vname, false)
710817a7
 	c.Assert(err, checker.IsNil)
b3b7eb27
 }
 
7fb7a477
 func (s *DockerSuite) TestVolumesAPIInspect(c *check.C) {
5c2498fd
 	config := volumetypes.VolumesCreateBody{
b3b7eb27
 		Name: "test",
 	}
0fd5a654
 
a46f757c
 	// sampling current time minus a minute so to now have false positive in case of delays
 	now := time.Now().Truncate(time.Minute)
b3b7eb27
 
0fd5a654
 	cli, err := client.NewEnvClient()
710817a7
 	c.Assert(err, checker.IsNil)
0fd5a654
 	defer cli.Close()
b3b7eb27
 
0fd5a654
 	_, err = cli.VolumeCreate(context.Background(), config)
 	c.Assert(err, check.IsNil)
 
 	vol, err := cli.VolumeInspect(context.Background(), config.Name)
710817a7
 	c.Assert(err, checker.IsNil)
 	c.Assert(vol.Name, checker.Equals, config.Name)
a46f757c
 
 	// comparing CreatedAt field time for the new volume to now. Removing a minute from both to avoid false positive
 	testCreatedAt, err := time.Parse(time.RFC3339, strings.TrimSpace(vol.CreatedAt))
 	c.Assert(err, check.IsNil)
 	testCreatedAt = testCreatedAt.Truncate(time.Minute)
 	if !testCreatedAt.Equal(now) {
 		c.Assert(fmt.Errorf("Time Volume is CreatedAt not equal to current time"), check.NotNil)
 	}
b3b7eb27
 }