Skip to content

Commit 75f0a14

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

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed

pkg/mcp/toolset/toolset_test.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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+
"github.com/lima-vm/lima/v2/pkg/limatype"
10+
"gotest.tools/v3/assert"
11+
)
12+
13+
func TestIsMounted(t *testing.T) {
14+
tests := []struct {
15+
name string
16+
hostPath string
17+
toolSet ToolSet
18+
want bool
19+
}{
20+
{
21+
name: "file in mounted directory",
22+
hostPath: "/home/user/documents/file.txt",
23+
toolSet: ToolSet{
24+
inst: &limatype.Instance{
25+
Config: &limatype.LimaYAML{
26+
Mounts: []limatype.Mount{
27+
{Location: "/home/user"},
28+
},
29+
},
30+
},
31+
},
32+
want: true,
33+
},
34+
{
35+
name: "path outside mount",
36+
hostPath: "/other/path/file.txt",
37+
toolSet: ToolSet{
38+
inst: &limatype.Instance{
39+
Config: &limatype.LimaYAML{
40+
Mounts: []limatype.Mount{
41+
{Location: "/home/user"},
42+
},
43+
},
44+
},
45+
},
46+
want: false,
47+
},
48+
{
49+
name: "similar prefix but not under mount",
50+
hostPath: "/home/user2/file.txt",
51+
toolSet: ToolSet{
52+
inst: &limatype.Instance{
53+
Config: &limatype.LimaYAML{
54+
Mounts: []limatype.Mount{
55+
{Location: "/home/user"},
56+
},
57+
},
58+
},
59+
},
60+
want: false,
61+
},
62+
{
63+
name: "multiple mounts",
64+
hostPath: "/tmp/myfile",
65+
toolSet: ToolSet{
66+
inst: &limatype.Instance{
67+
Config: &limatype.LimaYAML{
68+
Mounts: []limatype.Mount{
69+
{Location: "/home/user"},
70+
{Location: "/tmp"},
71+
},
72+
},
73+
},
74+
},
75+
want: true,
76+
},
77+
}
78+
79+
for _, test := range tests {
80+
t.Run(test.name, func(t *testing.T) {
81+
got := test.toolSet.isMounted(test.hostPath)
82+
assert.Equal(t, test.want, got)
83+
})
84+
}
85+
}

0 commit comments

Comments
 (0)