Skip to content

Commit 3736da6

Browse files
committed
Add tests rootless run
The options here were either to override the EUID in the code, or require the E2E tests to be run one as sudo, and one as not sudo. The latter was chosen as the changes to the code for tests were somewhat heavy. Add sudo test run to the actions. Signed-off-by: luke <luke.parkin@arm.com>
1 parent d57170c commit 3736da6

4 files changed

Lines changed: 57 additions & 1 deletion

File tree

.github/workflows/ci.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,6 @@ 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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ 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
6669
```
6770

6871
### Manual testing with Remoteproc Simulator

e2e/runtime_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,10 @@ func TestRuntimeWriteProcessPid(t *testing.T) {
129129
}
130130

131131
func TestRuntimeProxyInheritsCorrectNamespace(t *testing.T) {
132+
if os.Geteuid() != 0 {
133+
t.Skip("requires root privileges to create new namespaces")
134+
}
135+
132136
rootDir := t.TempDir()
133137
remoteprocName := "a-lovely-blue-device"
134138
sim := remoteproc.NewSimulator(rootDir).WithName(remoteprocName)
@@ -167,6 +171,48 @@ func TestRuntimeProxyInheritsCorrectNamespace(t *testing.T) {
167171
require.NoError(t, err)
168172
}
169173

174+
func TestRuntimeProxyKeepsHostNamespaceWhenNotRoot(t *testing.T) {
175+
if os.Geteuid() == 0 {
176+
t.Skip("this test must be run as non-root")
177+
}
178+
rootDir := t.TempDir()
179+
remoteprocName := "a-lovely-blue-device"
180+
sim := remoteproc.NewSimulator(rootDir).WithName(remoteprocName)
181+
if err := sim.Start(); err != nil {
182+
t.Fatalf("failed to run simulator: %s", err)
183+
}
184+
defer func() { _ = sim.Stop() }()
185+
186+
bin, err := repo.BuildRuntimeBin(t.TempDir(), rootDir, nil)
187+
require.NoError(t, err)
188+
189+
const containerName = "good-looking-container"
190+
bundlePath := t.TempDir()
191+
192+
require.NoError(t, generateBundle(
193+
bundlePath,
194+
remoteprocName,
195+
specs.LinuxNamespace{Type: specs.MountNamespace},
196+
))
197+
198+
_, err = invokeRuntime(bin, "create", "--bundle", bundlePath, containerName)
199+
require.NoError(t, err)
200+
defer func() {
201+
_, _ = invokeRuntime(bin, "delete", containerName)
202+
}()
203+
204+
state, err := getContainerState(bin, containerName)
205+
require.NoError(t, err)
206+
require.Greater(t, state.Pid, 0)
207+
208+
hostMountNS, err := os.Readlink("/proc/self/ns/mnt")
209+
require.NoError(t, err)
210+
proxyMountNS, err := os.Readlink(fmt.Sprintf("/proc/%d/ns/mnt", state.Pid))
211+
require.NoError(t, err)
212+
213+
assert.Equal(t, hostMountNS, proxyMountNS)
214+
}
215+
170216
func assertContainerStatus(t testing.TB, bin repo.RuntimeBin, containerName string, wantStatus specs.ContainerState) {
171217
t.Helper()
172218
state, err := getContainerState(bin, containerName)
@@ -183,6 +229,10 @@ func getContainerPid(bin repo.RuntimeBin, containerName string) (int, error) {
183229
}
184230

185231
func getContainerState(bin repo.RuntimeBin, containerName string) (specs.State, error) {
232+
return getContainerStateWithEnv(bin, containerName, nil)
233+
}
234+
235+
func getContainerStateWithEnv(bin repo.RuntimeBin, containerName string, extraEnv []string) (specs.State, error) {
186236
var state specs.State
187237
out, err := invokeRuntime(bin, "state", containerName)
188238
if err != nil {

internal/proxy/proxy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func effectiveNamespaceFlags(spec *specs.Spec) (uintptr, error) {
5050

5151
if getEUID() != 0 {
5252
if flags != 0 {
53-
fmt.Fprintln(os.Stderr, "[WARN] running without root: namespaces disabled")
53+
fmt.Fprintln(os.Stderr, "[WARN] running without root; namespace isolation disabled")
5454
}
5555
return 0, nil
5656
}

0 commit comments

Comments
 (0)