Skip to content

Commit c40d7a9

Browse files
hao022claude
andcommitted
fix: skip TestHostnameByPid when setns is not permitted
setns(2) requires CAP_SYS_ADMIN. Probe with self PID before running the test cases and skip the whole test when EPERM is returned, so the suite passes in unprivileged environments such as OrbStack on Apple Silicon. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent c822327 commit c40d7a9

1 file changed

Lines changed: 7 additions & 0 deletions

File tree

internal/utils/executil/exec_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ package executil
1616

1717
import (
1818
"bytes"
19+
"errors"
1920
"fmt"
2021
"os"
2122
"path/filepath"
2223
"runtime"
24+
"syscall"
2325
"testing"
2426

2527
"huatuo-bamai/internal/procfs"
@@ -132,6 +134,11 @@ func TestHostnameByPid(t *testing.T) {
132134

133135
selfPid := uint32(os.Getpid())
134136

137+
// setns(2) requires CAP_SYS_ADMIN; skip the whole test in unprivileged environments.
138+
if _, err := HostnameByPid(selfPid); errors.Is(err, syscall.EPERM) {
139+
t.Skip("setns requires CAP_SYS_ADMIN; skipping in unprivileged environment")
140+
}
141+
135142
tests := []struct {
136143
name string
137144
pid uint32

0 commit comments

Comments
 (0)