Skip to content

Commit c44939c

Browse files
committed
added tests for hostPath validation
Signed-off-by: Praful Khanduri <holiodin@gmail.com>
1 parent 8873405 commit c44939c

File tree

2 files changed

+110
-7
lines changed

2 files changed

+110
-7
lines changed

pkg/mcp/toolset/toolset.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -110,25 +110,29 @@ func (ts *ToolSet) TranslateHostPath(hostPath string) (string, error) {
110110
if !filepath.IsAbs(hostPath) {
111111
return "", fmt.Errorf("expected an absolute path, got a relative path: %q", hostPath)
112112
}
113-
if !ts.isMounted(hostPath) {
114-
return "", fmt.Errorf("path %q is not mounted", hostPath)
113+
114+
guestPath, isMounted := ts.translateToGuestPath(hostPath)
115+
if !isMounted {
116+
logrus.Warnf("path %q is not under any mounted directory, using as guest path", hostPath)
115117
}
116-
return hostPath, nil
118+
return guestPath, nil
117119
}
118120

119-
func (ts *ToolSet) isMounted(hostPath string) bool {
121+
func (ts *ToolSet) translateToGuestPath(hostPath string) (string, bool) {
120122
for _, mount := range ts.inst.Config.Mounts {
121123
location := filepath.Clean(mount.Location)
122124
cleanPath := filepath.Clean(hostPath)
123125

124126
if cleanPath == location {
125-
return true
127+
return *mount.MountPoint, true
126128
}
127129

128130
rel, err := filepath.Rel(location, cleanPath)
129131
if err == nil && !strings.HasPrefix(rel, "..") && rel != ".." {
130-
return true
132+
guestPath := filepath.Join(*mount.MountPoint, rel)
133+
return guestPath, true
131134
}
132135
}
133-
return false
136+
137+
return hostPath, false
134138
}

pkg/mcp/toolset/toolset_test.go

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
// SPDX-FileCopyrightText: Copyright The Lima Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package toolset
5+
6+
import (
7+
"testing"
8+
9+
"gotest.tools/v3/assert"
10+
11+
"github.com/lima-vm/lima/v2/pkg/limatype"
12+
)
13+
14+
func TestTranslateHostPath(t *testing.T) {
15+
mountPoint1 := "/mnt/home-user"
16+
mountPoint2 := "/mnt/tmp"
17+
18+
tests := []struct {
19+
name string
20+
hostPath string
21+
toolSet ToolSet
22+
wantGuestPath string
23+
wantErr bool
24+
}{
25+
{
26+
name: "file in mounted directory",
27+
hostPath: "/home/user/documents/file.txt",
28+
toolSet: ToolSet{
29+
inst: &limatype.Instance{
30+
Config: &limatype.LimaYAML{
31+
Mounts: []limatype.Mount{
32+
{Location: "/home/user", MountPoint: &mountPoint1},
33+
},
34+
},
35+
},
36+
},
37+
wantGuestPath: "/mnt/home-user/documents/file.txt",
38+
wantErr: false,
39+
},
40+
{
41+
name: "path outside mount - fallback to guest path",
42+
hostPath: "/other/path/file.txt",
43+
toolSet: ToolSet{
44+
inst: &limatype.Instance{
45+
Config: &limatype.LimaYAML{
46+
Mounts: []limatype.Mount{
47+
{Location: "/home/user", MountPoint: &mountPoint1},
48+
},
49+
},
50+
},
51+
},
52+
wantGuestPath: "/other/path/file.txt",
53+
wantErr: false,
54+
},
55+
{
56+
name: "similar prefix but not under mount",
57+
hostPath: "/home/user2/file.txt",
58+
toolSet: ToolSet{
59+
inst: &limatype.Instance{
60+
Config: &limatype.LimaYAML{
61+
Mounts: []limatype.Mount{
62+
{Location: "/home/user", MountPoint: &mountPoint1},
63+
},
64+
},
65+
},
66+
},
67+
wantGuestPath: "/home/user2/file.txt",
68+
wantErr: false,
69+
},
70+
{
71+
name: "multiple mounts",
72+
hostPath: "/tmp/myfile",
73+
toolSet: ToolSet{
74+
inst: &limatype.Instance{
75+
Config: &limatype.LimaYAML{
76+
Mounts: []limatype.Mount{
77+
{Location: "/home/user", MountPoint: &mountPoint1},
78+
{Location: "/tmp", MountPoint: &mountPoint2},
79+
},
80+
},
81+
},
82+
},
83+
wantGuestPath: "/mnt/tmp/myfile",
84+
wantErr: false,
85+
},
86+
}
87+
88+
for _, test := range tests {
89+
t.Run(test.name, func(t *testing.T) {
90+
got, err := test.toolSet.TranslateHostPath(test.hostPath)
91+
if test.wantErr {
92+
assert.Assert(t, err != nil)
93+
} else {
94+
assert.NilError(t, err)
95+
assert.Equal(t, test.wantGuestPath, got)
96+
}
97+
})
98+
}
99+
}

0 commit comments

Comments
 (0)