From 1d1230ea32544bcc646a5b79ddf9cc66b70c66cd Mon Sep 17 00:00:00 2001 From: Antonio Murdaca Date: Tue, 24 Mar 2015 22:11:45 +0100 Subject: [PATCH] Fix volume initialize error check, Fixes #11725 Signed-off-by: Antonio Murdaca --- volumes/volume.go | 5 ++++- volumes/volume_test.go | 38 +++++++++++++++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/volumes/volume.go b/volumes/volume.go index 8041160cee..c5191c48c5 100644 --- a/volumes/volume.go +++ b/volumes/volume.go @@ -90,7 +90,10 @@ func (v *Volume) initialize() error { v.lock.Lock() defer v.lock.Unlock() - if _, err := os.Stat(v.Path); err != nil && os.IsNotExist(err) { + if _, err := os.Stat(v.Path); err != nil { + if !os.IsNotExist(err) { + return err + } if err := os.MkdirAll(v.Path, 0755); err != nil { return err } diff --git a/volumes/volume_test.go b/volumes/volume_test.go index 5f3fdcfe6b..caf38c8bb4 100644 --- a/volumes/volume_test.go +++ b/volumes/volume_test.go @@ -1,6 +1,11 @@ package volumes -import "testing" +import ( + "strings" + "testing" + + "github.com/docker/docker/pkg/stringutils" +) func TestContainers(t *testing.T) { v := &Volume{containers: make(map[string]struct{})} @@ -17,3 +22,34 @@ func TestContainers(t *testing.T) { t.Fatalf("removing container failed") } } + +// os.Stat(v.Path) is returning ErrNotExist, initialize catch it and try to +// mkdir v.Path but it dies and correctly returns the error +func TestInitializeCannotMkdirOnNonExistentPath(t *testing.T) { + v := &Volume{Path: "nonexistentpath"} + + err := v.initialize() + if err == nil { + t.Fatal("Expected not to initialize volume with a non existent path") + } + + if !strings.Contains(err.Error(), "mkdir : no such file or directory") { + t.Fatalf("Expected to get mkdir no such file or directory, got %s", err) + } +} + +// os.Stat(v.Path) is NOT returning ErrNotExist so skip and return error from +// initialize +func TestInitializeCannotStatPathFileNameTooLong(t *testing.T) { + // ENAMETOOLONG + v := &Volume{Path: stringutils.GenerateRandomAlphaOnlyString(300)} + + err := v.initialize() + if err == nil { + t.Fatal("Expected not to initialize volume with a non existent path") + } + + if !strings.Contains(err.Error(), "file name too long") { + t.Fatalf("Expected to get ENAMETOOLONG error, got %s", err) + } +}