From 943c8daf6db1ec9995f0e7a8f1392c2d83dc4376 Mon Sep 17 00:00:00 2001 From: Qi Wang Date: Tue, 9 Jul 2019 14:25:36 -0400 Subject: [PATCH] fix bug convert volume host path to absolute fix ##3504 If --volume host:dest host is not a named volume, convert the host to a absolute directory path. Signed-off-by: Qi Wang --- pkg/spec/storage.go | 7 ++++++- pkg/spec/storage_test.go | 29 ++++++++++++++++++++++------- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/pkg/spec/storage.go b/pkg/spec/storage.go index ed767f5ba4d2..192ca3968609 100644 --- a/pkg/spec/storage.go +++ b/pkg/spec/storage.go @@ -663,10 +663,15 @@ func (config *CreateConfig) getVolumeMounts() (map[string]spec.Mount, map[string if strings.HasPrefix(src, "/") || strings.HasPrefix(src, ".") { // This is not a named volume + // convert src to an absolute path + source, err := filepath.Abs(src) + if err != nil { + return nil, nil, errors.Wrapf(err, "error getting absolute path of %s", source) + } newMount := spec.Mount{ Destination: dest, Type: string(TypeBind), - Source: src, + Source: source, Options: options, } if _, ok := mounts[newMount.Destination]; ok { diff --git a/pkg/spec/storage_test.go b/pkg/spec/storage_test.go index 04a9d6976dd6..2c4a2795e0c6 100644 --- a/pkg/spec/storage_test.go +++ b/pkg/spec/storage_test.go @@ -1,6 +1,7 @@ package createconfig import ( + "os" "testing" spec "github.com/opencontainers/runtime-spec/specs-go" @@ -8,18 +9,32 @@ import ( ) func TestGetVolumeMountsOneVolume(t *testing.T) { - data := spec.Mount{ - Destination: "/foobar", - Type: "bind", - Source: "/tmp", - Options: []string{"ro"}, + + dir, err := os.Getwd() + if err != nil { + t.Fatal(err) + } + data := map[string]spec.Mount{ + "foobar": { + Destination: "/foobar", + Type: "bind", + Source: "/tmp", + Options: []string{"ro"}}, + "redhat": { + Destination: "/redhat", + Type: "bind", + Source: dir, + Options: []string{"ro"}, + }, } config := CreateConfig{ - Volumes: []string{"/tmp:/foobar:ro"}, + Volumes: []string{"/tmp:/foobar:ro", ".:/redhat:ro"}, } specMount, _, err := config.getVolumeMounts() assert.NoError(t, err) - assert.EqualValues(t, data, specMount[data.Destination]) + assert.EqualValues(t, data["foobar"], specMount["/foobar"]) + assert.EqualValues(t, data["redhat"], specMount["/redhat"]) + } func TestGetTmpfsMounts(t *testing.T) {