From c3985bdf797a4526eac5fb0bd92047ac0f8513d9 Mon Sep 17 00:00:00 2001 From: Stephen Rust Date: Tue, 2 Feb 2016 11:35:05 -0500 Subject: [PATCH] Fix volume driver API compatibility mode (a little) Signed-off-by: Stephen Rust --- ...cker_cli_volume_driver_compat_unix_test.go | 45 +++++++++++++------ volume/drivers/adapter.go | 15 ++++++- volume/store/store.go | 3 -- 3 files changed, 46 insertions(+), 17 deletions(-) diff --git a/integration-cli/docker_cli_volume_driver_compat_unix_test.go b/integration-cli/docker_cli_volume_driver_compat_unix_test.go index b15baccb03..2207822f76 100644 --- a/integration-cli/docker_cli_volume_driver_compat_unix_test.go +++ b/integration-cli/docker_cli_volume_driver_compat_unix_test.go @@ -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)) +} diff --git a/volume/drivers/adapter.go b/volume/drivers/adapter.go index 98e4c11ddf..064dbffe36 100644 --- a/volume/drivers/adapter.go +++ b/volume/drivers/adapter.go @@ -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 } diff --git a/volume/store/store.go b/volume/store/store.go index b871902fd8..0d227ae01a 100644 --- a/volume/store/store.go +++ b/volume/store/store.go @@ -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) }