forked from arm/remoteproc-runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy_test.go
More file actions
110 lines (90 loc) · 2.74 KB
/
Copy pathproxy_test.go
File metadata and controls
110 lines (90 loc) · 2.74 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package proxy
import (
"errors"
"io"
"os"
"testing"
"github.com/opencontainers/runtime-spec/specs-go"
"github.com/stretchr/testify/assert"
"golang.org/x/sys/unix"
)
func stubNamespaceCloneFlags(t *testing.T, fn func(*specs.Spec) (uintptr, error)) {
t.Helper()
original := namespaceCloneFlagsFn
namespaceCloneFlagsFn = fn
t.Cleanup(func() {
namespaceCloneFlagsFn = original
})
}
func TestNamespaceCloneFlags(t *testing.T) {
t.Run("returns zero when spec is nil", func(t *testing.T) {
got, err := namespaceCloneFlags(nil)
assert.NoError(t, err)
assert.Equal(t, uintptr(0), got)
})
t.Run("marks for creation namespaces where paths are not provided", func(t *testing.T) {
spec := &specs.Spec{
Linux: &specs.Linux{
Namespaces: []specs.LinuxNamespace{
{Type: specs.NetworkNamespace},
{Type: specs.PIDNamespace, Path: "/proc/self/ns/pid"},
{Type: specs.UTSNamespace},
},
},
}
got, err := namespaceCloneFlags(spec)
assert.NoError(t, err)
want := uintptr(unix.CLONE_NEWNET | unix.CLONE_NEWUTS)
assert.Equal(t, want, got)
})
t.Run("errors with unknown namespace types", func(t *testing.T) {
spec := &specs.Spec{
Linux: &specs.Linux{
Namespaces: []specs.LinuxNamespace{
{Type: specs.LinuxNamespaceType("unknown")},
},
},
}
_, err := namespaceCloneFlags(spec)
assert.Error(t, err)
assert.Equal(t, "unknown namespace type \"unknown\"", err.Error())
})
}
func TestEffectiveNamespaceFlags(t *testing.T) {
t.Run("returns clone flags when running as root", func(t *testing.T) {
stubNamespaceCloneFlags(t, func(spec *specs.Spec) (uintptr, error) {
return 0x1507, nil
})
got, err := effectiveNamespaceFlags(true, &specs.Spec{})
assert.NoError(t, err)
assert.Equal(t, uintptr(0x1507), got)
})
t.Run("warns and does not set namespace if root is not set but flags are given", func(t *testing.T) {
stubNamespaceCloneFlags(t, func(spec *specs.Spec) (uintptr, error) {
return 0x2, nil
})
stderr := os.Stderr
reader, writer, err := os.Pipe()
assert.NoError(t, err)
os.Stderr = writer
t.Cleanup(func() {
os.Stderr = stderr
})
got, err := effectiveNamespaceFlags(false, &specs.Spec{})
assert.NoError(t, err)
assert.Equal(t, uintptr(0), got)
assert.NoError(t, writer.Close())
out, readErr := io.ReadAll(reader)
assert.NoError(t, readErr)
assert.Contains(t, string(out), "[WARN] running non-root; namespace isolation disabled")
assert.NoError(t, reader.Close())
})
t.Run("propagates namespaceCloneFlags errors", func(t *testing.T) {
expected := errors.New("error!")
stubNamespaceCloneFlags(t, func(spec *specs.Spec) (uintptr, error) {
return 0, expected
})
_, err := effectiveNamespaceFlags(true, &specs.Spec{})
assert.ErrorIs(t, err, expected)
})
}