Skip to content

Commit f46b9d3

Browse files
committed
Don't use root-requiring tests, add a unit test.
Signed-off-by: luke <luke.parkin@arm.com>
1 parent 9649e44 commit f46b9d3

5 files changed

Lines changed: 52 additions & 56 deletions

File tree

.github/workflows/ci.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,3 @@ jobs:
7878

7979
- name: Run e2e tests
8080
run: go test -v ./e2e/...
81-
82-
- name: Run root namespace E2E test
83-
run: sudo -E env "PATH=$PATH" go test -v -run '^TestRuntimeProxyInheritsCorrectNamespace$' ./e2e

docs/DEVELOPMENT.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,6 @@ brew install lima # macOS
6363

6464
# Run tests
6565
go test -v ./e2e/...
66-
67-
# Run root namespace-specific tests (requires sudo)
68-
sudo -E env "PATH=$PATH" go test -v -run '^TestRuntimeProxyInheritsCorrectNamespace$' ./e2e
6966
```
7067

7168
### Manual testing with Remoteproc Simulator

e2e/runtime_test.go

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -128,49 +128,6 @@ func TestRuntimeWriteProcessPid(t *testing.T) {
128128
assertFileContent(t, pidFilePath, fmt.Sprintf("%d", pid))
129129
}
130130

131-
func TestRuntimeProxyInheritsCorrectNamespace(t *testing.T) {
132-
if os.Geteuid() != 0 {
133-
t.Skip("requires root privileges to create new namespaces")
134-
}
135-
136-
rootDir := t.TempDir()
137-
remoteprocName := "a-lovely-blue-device"
138-
sim := remoteproc.NewSimulator(rootDir).WithName(remoteprocName)
139-
if err := sim.Start(); err != nil {
140-
t.Fatalf("failed to run simulator: %s", err)
141-
}
142-
defer func() { _ = sim.Stop() }()
143-
144-
bin, err := repo.BuildRuntimeBin(t.TempDir(), rootDir, nil)
145-
require.NoError(t, err)
146-
147-
const containerName = "blue-container"
148-
bundlePath := t.TempDir()
149-
150-
require.NoError(t, generateBundle(
151-
bundlePath,
152-
remoteprocName,
153-
specs.LinuxNamespace{Type: specs.MountNamespace},
154-
))
155-
156-
_, err = invokeRuntime(bin, "create", "--bundle", bundlePath, containerName)
157-
require.NoError(t, err)
158-
159-
pid, err := getContainerPid(bin, containerName)
160-
require.NoError(t, err)
161-
require.Greater(t, pid, 0)
162-
163-
hostMountNS, err := os.Readlink("/proc/self/ns/mnt")
164-
require.NoError(t, err)
165-
proxyMountNS, err := os.Readlink(fmt.Sprintf("/proc/%d/ns/mnt", pid))
166-
require.NoError(t, err)
167-
168-
assert.NotEqual(t, hostMountNS, proxyMountNS)
169-
170-
_, err = invokeRuntime(bin, "delete", containerName)
171-
require.NoError(t, err)
172-
}
173-
174131
func TestRuntimeProxyKeepsHostNamespaceWhenNotRoot(t *testing.T) {
175132
if os.Geteuid() == 0 {
176133
t.Skip("this test must be run as non-root")
@@ -229,10 +186,6 @@ func getContainerPid(bin repo.RuntimeBin, containerName string) (int, error) {
229186
}
230187

231188
func getContainerState(bin repo.RuntimeBin, containerName string) (specs.State, error) {
232-
return getContainerStateWithEnv(bin, containerName)
233-
}
234-
235-
func getContainerStateWithEnv(bin repo.RuntimeBin, containerName string) (specs.State, error) {
236189
var state specs.State
237190
out, err := invokeRuntime(bin, "state", containerName)
238191
if err != nil {

internal/proxy/proxy.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ var namespaceFlags = map[specs.LinuxNamespaceType]uintptr{
2121
specs.UTSNamespace: unix.CLONE_NEWUTS,
2222
}
2323

24-
var getEUID = os.Geteuid
25-
2624
func namespaceCloneFlags(spec *specs.Spec) (uintptr, error) {
2725
if spec == nil {
2826
return 0, nil
@@ -48,7 +46,7 @@ func effectiveNamespaceFlags(spec *specs.Spec) (uintptr, error) {
4846
return 0, err
4947
}
5048

51-
if getEUID() != 0 {
49+
if os.Geteuid() != 0 {
5250
if flags != 0 {
5351
fmt.Fprintln(os.Stderr, "[WARN] running non-root; namespace isolation disabled")
5452
}

internal/proxy/proxy_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package proxy
2+
3+
import (
4+
"testing"
5+
6+
"github.com/opencontainers/runtime-spec/specs-go"
7+
"github.com/stretchr/testify/assert"
8+
"golang.org/x/sys/unix"
9+
)
10+
11+
func TestNamespaceCloneFlags(t *testing.T) {
12+
t.Run("returns zero when spec is nil", func(t *testing.T) {
13+
got, err := namespaceCloneFlags(nil)
14+
15+
assert.NoError(t, err)
16+
assert.Equal(t, uintptr(0), got)
17+
})
18+
19+
t.Run("marks for creation namespaces where paths are not provided", func(t *testing.T) {
20+
spec := &specs.Spec{
21+
Linux: &specs.Linux{
22+
Namespaces: []specs.LinuxNamespace{
23+
{Type: specs.NetworkNamespace},
24+
{Type: specs.PIDNamespace, Path: "/proc/self/ns/pid"},
25+
{Type: specs.UTSNamespace},
26+
},
27+
},
28+
}
29+
30+
got, err := namespaceCloneFlags(spec)
31+
32+
assert.NoError(t, err)
33+
want := uintptr(unix.CLONE_NEWNET | unix.CLONE_NEWUTS)
34+
assert.Equal(t, want, got)
35+
})
36+
37+
t.Run("errors with unknown namespace types", func(t *testing.T) {
38+
spec := &specs.Spec{
39+
Linux: &specs.Linux{
40+
Namespaces: []specs.LinuxNamespace{
41+
{Type: specs.LinuxNamespaceType("unknown")},
42+
},
43+
},
44+
}
45+
46+
_, err := namespaceCloneFlags(spec)
47+
48+
assert.Error(t, err)
49+
assert.Equal(t, "unknown namespace type \"unknown\"", err.Error())
50+
})
51+
}

0 commit comments

Comments
 (0)