Skip to content

Commit

Permalink
fix bug convert volume host path to absolute
Browse files Browse the repository at this point in the history
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 <qiwan@redhat.com>
  • Loading branch information
QiWang19 committed Jul 9, 2019
1 parent 76aa8f6 commit 943c8da
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
7 changes: 6 additions & 1 deletion pkg/spec/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
29 changes: 22 additions & 7 deletions pkg/spec/storage_test.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,40 @@
package createconfig

import (
"os"
"testing"

spec "github.com/opencontainers/runtime-spec/specs-go"
"github.com/stretchr/testify/assert"
)

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) {
Expand Down

0 comments on commit 943c8da

Please sign in to comment.