-
Notifications
You must be signed in to change notification settings - Fork 19
/
mount_test.go
83 lines (70 loc) · 2.95 KB
/
mount_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
package lxcri
import (
"os"
"path/filepath"
"strings"
"testing"
"github.com/stretchr/testify/require"
)
func TestResolveMountDestination_absolute(t *testing.T) {
tmpdir, err := os.MkdirTemp("", "golang.test")
require.NoError(t, err)
defer os.RemoveAll(tmpdir)
err = os.MkdirAll(filepath.Join(tmpdir, "folder1"), 0750)
require.NoError(t, err)
err = os.MkdirAll(filepath.Join(tmpdir, "folder2"), 0750)
require.NoError(t, err)
err = os.MkdirAll(filepath.Join(tmpdir, "folder3"), 0750)
require.NoError(t, err)
err = os.Symlink("/folder2", filepath.Join(tmpdir, "folder1", "f2"))
require.NoError(t, err)
err = os.Symlink("/folder3", filepath.Join(tmpdir, "folder2", "f3"))
require.NoError(t, err)
err = os.WriteFile(filepath.Join(tmpdir, "folder3", "test.txt"), []byte("hello"), 0640)
require.NoError(t, err)
p, err := resolveMountDestination(tmpdir, "/folder1/f2/f3/test.txt")
require.Equal(t, filepath.Join(tmpdir, "/folder3/test.txt"), p)
require.NoError(t, err)
p, err = resolveMountDestination(tmpdir, "/folder1/f2/xxxxx/fooo")
require.Equal(t, filepath.Join(tmpdir, "/folder2/xxxxx/fooo"), p)
require.Error(t, err, os.ErrExist)
p, err = resolveMountDestination(tmpdir, "/folder1/f2/f3/hello.txt")
require.Equal(t, filepath.Join(tmpdir, "/folder3/hello.txt"), p)
require.Error(t, err, os.ErrExist)
}
func TestResolveMountDestination_relative(t *testing.T) {
tmpdir, err := os.MkdirTemp("", "golang.test")
require.NoError(t, err)
defer os.RemoveAll(tmpdir)
err = os.MkdirAll(filepath.Join(tmpdir, "folder1"), 0750)
require.NoError(t, err)
err = os.MkdirAll(filepath.Join(tmpdir, "folder2"), 0750)
require.NoError(t, err)
err = os.MkdirAll(filepath.Join(tmpdir, "folder3"), 0750)
require.NoError(t, err)
err = os.Symlink("../folder2", filepath.Join(tmpdir, "folder1", "f2"))
require.NoError(t, err)
err = os.Symlink("../folder3", filepath.Join(tmpdir, "folder2", "f3"))
require.NoError(t, err)
err = os.WriteFile(filepath.Join(tmpdir, "folder3", "test.txt"), []byte("hello"), 0640)
require.NoError(t, err)
//err = os.Symlink("../../folder2", filepath.Join(tmpdir, "folder1", "f2"))
//require.NoError(t, err)
p, err := resolveMountDestination(tmpdir, "/folder1/f2/f3/test.txt")
require.Equal(t, filepath.Join(tmpdir, "/folder3/test.txt"), p)
require.NoError(t, err)
p, err = resolveMountDestination(tmpdir, "/folder1/f2/xxxxx/fooo")
require.Equal(t, filepath.Join(tmpdir, "/folder2/xxxxx/fooo"), p)
require.Error(t, err, os.ErrExist)
p, err = resolveMountDestination(tmpdir, "/folder1/f2/f3/hello.txt")
require.Equal(t, filepath.Join(tmpdir, "/folder3/hello.txt"), p)
require.Error(t, err, os.ErrExist)
}
func TestFilterMountOptions(t *testing.T) {
opts := strings.Split("rw,rprivate,noexec,nosuid,nodev,tmpcopyup,create=dir", ",")
rt := Runtime{}
out := filterMountOptions(&rt, "tmpfs", opts)
require.Equal(t, []string{"rw", "noexec", "nosuid", "nodev", "create=dir"}, out)
out = filterMountOptions(&rt, "nosuchfs", opts)
require.Equal(t, opts, out)
}