Fix volume initialize error check, Fixes #11725

Signed-off-by: Antonio Murdaca <me@runcom.ninja>
This commit is contained in:
Antonio Murdaca 2015-03-24 22:11:45 +01:00
parent 322095ed63
commit 1d1230ea32
2 changed files with 41 additions and 2 deletions

View File

@ -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
}

View File

@ -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)
}
}