mirror of
https://github.com/docker/cli.git
synced 2026-01-15 07:40:57 +03:00
Merge pull request #11730 from runcom/11725-fix-volume-initialize-error-check
Fix volume initialize error check Upstream-commit: bb65c808df825181250935c642270df220e554f7 Component: engine
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user