-
Notifications
You must be signed in to change notification settings - Fork 250
/
fsconfig_test.go
86 lines (74 loc) · 2.32 KB
/
fsconfig_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package wazero
import (
"testing"
"github.com/tetratelabs/wazero/experimental/sys"
"github.com/tetratelabs/wazero/internal/sysfs"
testfs "github.com/tetratelabs/wazero/internal/testing/fs"
"github.com/tetratelabs/wazero/internal/testing/require"
)
// TestFSConfig only tests the cases that change the inputs to sysfs.ValidatePreopens.
func TestFSConfig(t *testing.T) {
base := NewFSConfig()
testFS := testfs.FS{}
testFS2 := testfs.FS{"/": &testfs.File{}}
tests := []struct {
name string
input FSConfig
expectedFS []sys.FS
expectedGuestPaths []string
}{
{
name: "empty",
input: base,
},
{
name: "WithFSMount",
input: base.WithFSMount(testFS, "/"),
expectedFS: []sys.FS{&sysfs.AdaptFS{FS: testFS}},
expectedGuestPaths: []string{"/"},
},
{
name: "WithFSMount overwrites",
input: base.WithFSMount(testFS, "/").WithFSMount(testFS2, "/"),
expectedFS: []sys.FS{&sysfs.AdaptFS{FS: testFS2}},
expectedGuestPaths: []string{"/"},
},
{
name: "WithFsMount nil",
input: base.WithFSMount(nil, "/"),
},
{
name: "WithDirMount overwrites",
input: base.WithFSMount(testFS, "/").WithDirMount(".", "/"),
expectedFS: []sys.FS{sysfs.DirFS(".")},
expectedGuestPaths: []string{"/"},
},
{
name: "multiple",
input: base.WithReadOnlyDirMount(".", "/").WithDirMount("/tmp", "/tmp"),
expectedFS: []sys.FS{&sysfs.ReadFS{FS: sysfs.DirFS(".")}, sysfs.DirFS("/tmp")},
expectedGuestPaths: []string{"/", "/tmp"},
},
}
for _, tt := range tests {
tc := tt
t.Run(tc.name, func(t *testing.T) {
fs, guestPaths := tc.input.(*fsConfig).preopens()
require.Equal(t, tc.expectedFS, fs)
require.Equal(t, tc.expectedGuestPaths, guestPaths)
})
}
}
func TestFSConfig_clone(t *testing.T) {
fc := NewFSConfig().(*fsConfig)
fc.guestPathToFS["/"] = 0
cloned := fc.clone()
// Make post-clone changes
fc.guestPaths = []string{"/"}
fc.guestPathToFS["/"] = 1
// Ensure the guestPathToFS map is not shared
require.Equal(t, map[string]int{"/": 1}, fc.guestPathToFS)
require.Equal(t, map[string]int{"/": 0}, cloned.guestPathToFS)
// Ensure the guestPaths slice is not shared
require.Zero(t, len(cloned.guestPaths))
}