1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Merge pull request #19983 from srust/volume_get_compat_hack

[1.10] Fix volume driver API compatibility mode (a little)
This commit is contained in:
David Calavera 2016-02-09 10:47:30 -08:00
commit 10226ac845
3 changed files with 46 additions and 17 deletions

View file

@ -24,11 +24,18 @@ func init() {
})
}
type vol struct {
Name string
Mountpoint string
Opts map[string]string
}
type DockerExternalVolumeSuiteCompatV1_1 struct {
server *httptest.Server
ds *DockerSuite
d *Daemon
ec *eventCounter
server *httptest.Server
ds *DockerSuite
d *Daemon
ec *eventCounter
volList []vol
}
func (s *DockerExternalVolumeSuiteCompatV1_1) SetUpTest(c *check.C) {
@ -47,6 +54,7 @@ func (s *DockerExternalVolumeSuiteCompatV1_1) SetUpSuite(c *check.C) {
type pluginRequest struct {
Name string
Opts map[string]string
}
type pluginResp struct {
@ -54,12 +62,6 @@ func (s *DockerExternalVolumeSuiteCompatV1_1) SetUpSuite(c *check.C) {
Err string `json:",omitempty"`
}
type vol struct {
Name string
Mountpoint string
}
var volList []vol
read := func(b io.ReadCloser) (pluginRequest, error) {
defer b.Close()
var pr pluginRequest
@ -94,7 +96,7 @@ func (s *DockerExternalVolumeSuiteCompatV1_1) SetUpSuite(c *check.C) {
send(w, err)
return
}
volList = append(volList, vol{Name: pr.Name})
s.volList = append(s.volList, vol{Name: pr.Name, Opts: pr.Opts})
send(w, nil)
})
@ -111,13 +113,13 @@ func (s *DockerExternalVolumeSuiteCompatV1_1) SetUpSuite(c *check.C) {
return
}
for i, v := range volList {
for i, v := range s.volList {
if v.Name == pr.Name {
if err := os.RemoveAll(hostVolumePath(v.Name)); err != nil {
send(w, fmt.Sprintf(`{"Err": "%v"}`, err))
return
}
volList = append(volList[:i], volList[i+1:]...)
s.volList = append(s.volList[:i], s.volList[i+1:]...)
break
}
}
@ -213,3 +215,20 @@ func (s *DockerExternalVolumeSuiteCompatV1_1) TestExternalVolumeDriverCompatV1_1
out, err = s.d.Cmd("volume", "rm", "foo")
c.Assert(err, checker.IsNil, check.Commentf(out))
}
func (s *DockerExternalVolumeSuiteCompatV1_1) TestExternalVolumeDriverCompatOptionsV1_1(c *check.C) {
err := s.d.StartWithBusybox()
c.Assert(err, checker.IsNil)
out, err := s.d.Cmd("volume", "create", "--name", "optvol", "--driver", "test-external-volume-driver", "--opt", "opt1=opt1val", "--opt", "opt2=opt2val")
c.Assert(err, checker.IsNil, check.Commentf(out))
out, err = s.d.Cmd("volume", "inspect", "optvol")
c.Assert(err, checker.IsNil, check.Commentf(out))
c.Assert(s.volList[0].Opts["opt1"], checker.Equals, "opt1val")
c.Assert(s.volList[0].Opts["opt2"], checker.Equals, "opt2val")
out, err = s.d.Cmd("volume", "rm", "optvol")
c.Assert(err, checker.IsNil, check.Commentf(out))
}

View file

@ -15,7 +15,20 @@ func (a *volumeDriverAdapter) Name() string {
}
func (a *volumeDriverAdapter) Create(name string, opts map[string]string) (volume.Volume, error) {
err := a.proxy.Create(name, opts)
// First try a Get. For drivers that support Get this will return any
// existing volume.
v, err := a.proxy.Get(name)
if v != nil {
return &volumeAdapter{
proxy: a.proxy,
name: v.Name,
driverName: a.Name(),
eMount: v.Mountpoint,
}, nil
}
// Driver didn't support Get or volume didn't exist. Perform Create.
err = a.proxy.Create(name, opts)
if err != nil {
return nil, err
}

View file

@ -192,9 +192,6 @@ func (s *VolumeStore) create(name, driverName string, opts map[string]string) (v
return nil, &OpErr{Op: "create", Name: name, Err: err}
}
if v, err := vd.Get(name); err == nil {
return v, nil
}
return vd.Create(name, opts)
}