Skip to content

Commit 84ff74e

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

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed

pkg/mcp/toolset/toolset_test.go

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

0 commit comments

Comments
 (0)