github.com/lazyboychen7/engine@v17.12.1-ce-rc2+incompatible/integration-cli/docker_api_volumes_test.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"path/filepath"
     6  	"strings"
     7  	"time"
     8  
     9  	"github.com/docker/docker/api/types/filters"
    10  	volumetypes "github.com/docker/docker/api/types/volume"
    11  	"github.com/docker/docker/client"
    12  	"github.com/docker/docker/integration-cli/checker"
    13  	"github.com/go-check/check"
    14  	"golang.org/x/net/context"
    15  )
    16  
    17  func (s *DockerSuite) TestVolumesAPIList(c *check.C) {
    18  	prefix, _ := getPrefixAndSlashFromDaemonPlatform()
    19  	cid, _ := dockerCmd(c, "run", "-d", "-v", prefix+"/foo", "busybox")
    20  
    21  	cli, err := client.NewEnvClient()
    22  	c.Assert(err, checker.IsNil)
    23  	defer cli.Close()
    24  
    25  	container, err := cli.ContainerInspect(context.Background(), strings.TrimSpace(cid))
    26  	c.Assert(err, checker.IsNil)
    27  	vname := container.Mounts[0].Name
    28  
    29  	volumes, err := cli.VolumeList(context.Background(), filters.Args{})
    30  	c.Assert(err, checker.IsNil)
    31  
    32  	found := false
    33  	for _, vol := range volumes.Volumes {
    34  		if vol.Name == vname {
    35  			found = true
    36  			break
    37  		}
    38  	}
    39  	c.Assert(found, checker.Equals, true)
    40  }
    41  
    42  func (s *DockerSuite) TestVolumesAPICreate(c *check.C) {
    43  	config := volumetypes.VolumesCreateBody{
    44  		Name: "test",
    45  	}
    46  
    47  	cli, err := client.NewEnvClient()
    48  	c.Assert(err, checker.IsNil)
    49  	defer cli.Close()
    50  
    51  	vol, err := cli.VolumeCreate(context.Background(), config)
    52  	c.Assert(err, check.IsNil)
    53  
    54  	c.Assert(filepath.Base(filepath.Dir(vol.Mountpoint)), checker.Equals, config.Name)
    55  }
    56  
    57  func (s *DockerSuite) TestVolumesAPIRemove(c *check.C) {
    58  	prefix, _ := getPrefixAndSlashFromDaemonPlatform()
    59  	cid, _ := dockerCmd(c, "run", "-d", "-v", prefix+"/foo", "--name=test", "busybox")
    60  
    61  	cli, err := client.NewEnvClient()
    62  	c.Assert(err, checker.IsNil)
    63  	defer cli.Close()
    64  
    65  	container, err := cli.ContainerInspect(context.Background(), strings.TrimSpace(cid))
    66  	c.Assert(err, checker.IsNil)
    67  	vname := container.Mounts[0].Name
    68  
    69  	err = cli.VolumeRemove(context.Background(), vname, false)
    70  	c.Assert(err.Error(), checker.Contains, "volume is in use")
    71  
    72  	dockerCmd(c, "rm", "-f", "test")
    73  	err = cli.VolumeRemove(context.Background(), vname, false)
    74  	c.Assert(err, checker.IsNil)
    75  }
    76  
    77  func (s *DockerSuite) TestVolumesAPIInspect(c *check.C) {
    78  	config := volumetypes.VolumesCreateBody{
    79  		Name: "test",
    80  	}
    81  
    82  	// sampling current time minus a minute so to now have false positive in case of delays
    83  	now := time.Now().Truncate(time.Minute)
    84  
    85  	cli, err := client.NewEnvClient()
    86  	c.Assert(err, checker.IsNil)
    87  	defer cli.Close()
    88  
    89  	_, err = cli.VolumeCreate(context.Background(), config)
    90  	c.Assert(err, check.IsNil)
    91  
    92  	vol, err := cli.VolumeInspect(context.Background(), config.Name)
    93  	c.Assert(err, checker.IsNil)
    94  	c.Assert(vol.Name, checker.Equals, config.Name)
    95  
    96  	// comparing CreatedAt field time for the new volume to now. Removing a minute from both to avoid false positive
    97  	testCreatedAt, err := time.Parse(time.RFC3339, strings.TrimSpace(vol.CreatedAt))
    98  	c.Assert(err, check.IsNil)
    99  	testCreatedAt = testCreatedAt.Truncate(time.Minute)
   100  	if !testCreatedAt.Equal(now) {
   101  		c.Assert(fmt.Errorf("Time Volume is CreatedAt not equal to current time"), check.NotNil)
   102  	}
   103  }