diff --git a/daemon/container.go b/daemon/container.go index 23a63a52db..468dc23044 100644 --- a/daemon/container.go +++ b/daemon/container.go @@ -1039,7 +1039,7 @@ func (container *Container) networkMounts() []execdriver.Mount { return mounts } -func (container *Container) AddLocalMountPoint(name, destination string, rw bool) { +func (container *Container) addLocalMountPoint(name, destination string, rw bool) { container.MountPoints[destination] = &mountPoint{ Name: name, Driver: volume.DefaultDriverName, @@ -1048,7 +1048,7 @@ func (container *Container) AddLocalMountPoint(name, destination string, rw bool } } -func (container *Container) AddMountPointWithVolume(destination string, vol volume.Volume, rw bool) { +func (container *Container) addMountPointWithVolume(destination string, vol volume.Volume, rw bool) { container.MountPoints[destination] = &mountPoint{ Name: vol.Name(), Driver: vol.DriverName(), @@ -1058,11 +1058,11 @@ func (container *Container) AddMountPointWithVolume(destination string, vol volu } } -func (container *Container) IsDestinationMounted(destination string) bool { +func (container *Container) isDestinationMounted(destination string) bool { return container.MountPoints[destination] != nil } -func (container *Container) PrepareMountPoints() error { +func (container *Container) prepareMountPoints() error { for _, config := range container.MountPoints { if len(config.Driver) > 0 { v, err := createVolume(config.Name, config.Driver) @@ -1075,7 +1075,7 @@ func (container *Container) PrepareMountPoints() error { return nil } -func (container *Container) RemoveMountPoints() error { +func (container *Container) removeMountPoints() error { for _, m := range container.MountPoints { if m.Volume != nil { if err := removeVolume(m.Volume); err != nil { @@ -1086,7 +1086,7 @@ func (container *Container) RemoveMountPoints() error { return nil } -func (container *Container) ShouldRestart() bool { +func (container *Container) shouldRestart() bool { return container.hostConfig.RestartPolicy.Name == "always" || (container.hostConfig.RestartPolicy.Name == "on-failure" && container.ExitCode != 0) } diff --git a/daemon/create.go b/daemon/create.go index bf55febe02..0020226dd3 100644 --- a/daemon/create.go +++ b/daemon/create.go @@ -113,7 +113,7 @@ func (daemon *Daemon) Create(config *runconfig.Config, hostConfig *runconfig.Hos } // Skip volumes for which we already have something mounted on that // destination because of a --volume-from. - if container.IsDestinationMounted(destination) { + if container.isDestinationMounted(destination) { continue } path, err := container.GetResourcePath(destination) @@ -136,7 +136,7 @@ func (daemon *Daemon) Create(config *runconfig.Config, hostConfig *runconfig.Hos } copyExistingContents(rootfs, path) - container.AddMountPointWithVolume(destination, v, true) + container.addMountPointWithVolume(destination, v, true) } if err := container.ToDisk(); err != nil { return nil, nil, err diff --git a/daemon/daemon.go b/daemon/daemon.go index 269e689b99..22bbf8d7cd 100644 --- a/daemon/daemon.go +++ b/daemon/daemon.go @@ -216,7 +216,7 @@ func (daemon *Daemon) register(container *Container, updateSuffixarray bool) err return err } - if err := container.PrepareMountPoints(); err != nil { + if err := container.prepareMountPoints(); err != nil { return err } @@ -333,7 +333,7 @@ func (daemon *Daemon) restore() error { // check the restart policy on the containers and restart any container with // the restart policy of "always" - if daemon.config.AutoRestart && container.ShouldRestart() { + if daemon.config.AutoRestart && container.shouldRestart() { logrus.Debugf("Starting container %s", container.ID) if err := container.Start(); err != nil { diff --git a/daemon/delete.go b/daemon/delete.go index 20451bd9de..02e6c9814e 100644 --- a/daemon/delete.go +++ b/daemon/delete.go @@ -71,7 +71,7 @@ func (daemon *Daemon) ContainerRm(name string, config *ContainerRmConfig) error } container.LogEvent("destroy") if config.RemoveVolume { - container.RemoveMountPoints() + container.removeMountPoints() } } return nil @@ -154,5 +154,5 @@ func (daemon *Daemon) commonRm(container *Container, forceRemove bool) (err erro } func (daemon *Daemon) DeleteVolumes(c *Container) error { - return c.RemoveMountPoints() + return c.removeMountPoints() } diff --git a/daemon/volumes.go b/daemon/volumes.go index c6220249b5..bc96b345ff 100644 --- a/daemon/volumes.go +++ b/daemon/volumes.go @@ -12,7 +12,6 @@ import ( "github.com/docker/docker/pkg/chrootarchive" "github.com/docker/docker/runconfig" "github.com/docker/docker/volume" - volumedrivers "github.com/docker/docker/volume/drivers" ) type mountPoint struct { @@ -246,7 +245,7 @@ func (daemon *Daemon) verifyOldVolumesInfo(container *Container) error { if strings.HasPrefix(hostPath, vfsPath) { id := filepath.Base(hostPath) - container.AddLocalMountPoint(id, destination, vols.VolumesRW[destination]) + container.addLocalMountPoint(id, destination, vols.VolumesRW[destination]) } } @@ -268,14 +267,3 @@ func removeVolume(v volume.Volume) error { } return vd.Remove(v) } - -func getVolumeDriver(name string) (volume.Driver, error) { - if name == "" { - name = volume.DefaultDriverName - } - vd := volumedrivers.Lookup(name) - if vd == nil { - return nil, fmt.Errorf("Volumes Driver %s isn't registered", name) - } - return vd, nil -} diff --git a/daemon/volumes_experimental.go b/daemon/volumes_experimental.go new file mode 100644 index 0000000000..a4f5a9cbe8 --- /dev/null +++ b/daemon/volumes_experimental.go @@ -0,0 +1,15 @@ +// +build experimental + +package daemon + +import ( + "github.com/docker/docker/volume" + "github.com/docker/docker/volume/drivers" +) + +func getVolumeDriver(name string) (volume.Driver, error) { + if name == "" { + name = volume.DefaultDriverName + } + return volumedrivers.Lookup(name) +} diff --git a/daemon/volumes_experimental_unit_test.go b/daemon/volumes_experimental_unit_test.go new file mode 100644 index 0000000000..89ef5e872d --- /dev/null +++ b/daemon/volumes_experimental_unit_test.go @@ -0,0 +1,32 @@ +// +build experimental + +package daemon + +import ( + "testing" + + "github.com/docker/docker/volume" + "github.com/docker/docker/volume/drivers" +) + +type fakeDriver struct{} + +func (fakeDriver) Name() string { return "fake" } +func (fakeDriver) Create(name string) (volume.Volume, error) { return nil, nil } +func (fakeDriver) Remove(v volume.Volume) error { return nil } + +func TestGetVolumeDriver(t *testing.T) { + _, err := getVolumeDriver("missing") + if err == nil { + t.Fatal("Expected error, was nil") + } + + volumedrivers.Register(fakeDriver{}, "fake") + d, err := getVolumeDriver("fake") + if err != nil { + t.Fatal(err) + } + if d.Name() != "fake" { + t.Fatalf("Expected fake driver, got %s\n", d.Name()) + } +} diff --git a/daemon/volumes_stubs.go b/daemon/volumes_stubs.go new file mode 100644 index 0000000000..41f8b67343 --- /dev/null +++ b/daemon/volumes_stubs.go @@ -0,0 +1,12 @@ +// +build !experimental + +package daemon + +import ( + "github.com/docker/docker/volume" + "github.com/docker/docker/volume/drivers" +) + +func getVolumeDriver(_ string) (volume.Driver, error) { + return volumedrivers.Lookup(volume.DefaultDriverName) +} diff --git a/daemon/volumes_stubs_unit_test.go b/daemon/volumes_stubs_unit_test.go new file mode 100644 index 0000000000..655f5f4b79 --- /dev/null +++ b/daemon/volumes_stubs_unit_test.go @@ -0,0 +1,35 @@ +// +build !experimental + +package daemon + +import ( + "io/ioutil" + "os" + "testing" + + "github.com/docker/docker/volume" + "github.com/docker/docker/volume/drivers" + "github.com/docker/docker/volume/local" +) + +func TestGetVolumeDefaultDriver(t *testing.T) { + tmp, err := ioutil.TempDir("", "volume-test-") + if err != nil { + t.Fatal(err) + } + defer os.RemoveAll(tmp) + + l, err := local.New(tmp) + if err != nil { + t.Fatal(err) + } + volumedrivers.Register(l, volume.DefaultDriverName) + d, err := getVolumeDriver("missing") + if err != nil { + t.Fatal(err) + } + + if d.Name() != volume.DefaultDriverName { + t.Fatalf("Expected local driver, was %s\n", d.Name) + } +} diff --git a/daemon/volumes_unit_test.go b/daemon/volumes_unit_test.go index dba8b9d844..6578aa2043 100644 --- a/daemon/volumes_unit_test.go +++ b/daemon/volumes_unit_test.go @@ -4,8 +4,6 @@ import ( "testing" "github.com/docker/docker/runconfig" - "github.com/docker/docker/volume" - volumedrivers "github.com/docker/docker/volume/drivers" ) func TestParseBindMount(t *testing.T) { @@ -95,25 +93,3 @@ func TestParseVolumeFrom(t *testing.T) { } } } - -type fakeDriver struct{} - -func (fakeDriver) Name() string { return "fake" } -func (fakeDriver) Create(name string) (volume.Volume, error) { return nil, nil } -func (fakeDriver) Remove(v volume.Volume) error { return nil } - -func TestGetVolumeDriver(t *testing.T) { - _, err := getVolumeDriver("missing") - if err == nil { - t.Fatal("Expected error, was nil") - } - - volumedrivers.Register(fakeDriver{}, "fake") - d, err := getVolumeDriver("fake") - if err != nil { - t.Fatal(err) - } - if d.Name() != "fake" { - t.Fatalf("Expected fake driver, got %s\n", d.Name()) - } -} diff --git a/volume/drivers/extpoint.go b/volume/drivers/extpoint.go index 228b337be3..0e90843517 100644 --- a/volume/drivers/extpoint.go +++ b/volume/drivers/extpoint.go @@ -43,19 +43,19 @@ func Unregister(name string) bool { return true } -func Lookup(name string) volume.Driver { +func Lookup(name string) (volume.Driver, error) { drivers.Lock() defer drivers.Unlock() ext, ok := drivers.extensions[name] if ok { - return ext + return ext, nil } pl, err := plugins.Get(name, "VolumeDriver") if err != nil { logrus.Errorf("Error: %v", err) - return nil + return nil, err } d := NewVolumeDriver(name, pl.Client) drivers.extensions[name] = d - return d + return d, nil }