Skip to content

Commit 13b180a

Browse files
committed
TestSELinux: use LockOSThread to avoid flakes
The SetFSCreateLabel documentation says the caller should use runtime.LockOSThread. Indeed, if not used, there is an occasional flake: > $ go test -run TestSELinux -count 10000 . > selinux_linux_test.go:168: SetFSCreateLabel failed write /proc/thread-self/attr/fscreate: permission denied > selinux_linux_test.go:169: write /proc/thread-self/attr/fscreate: permission denied Add LockOSThread to fix the flake. While at it, simplify code flow to avoid using "else", and fix logging to avoid printing the same error twice. Signed-off-by: Kir Kolyshkin <[email protected]>
1 parent 03cde75 commit 13b180a

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

go-selinux/selinux_linux_test.go

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"fmt"
88
"os"
99
"path/filepath"
10+
"runtime"
1011
"strconv"
1112
"strings"
1213
"testing"
@@ -235,6 +236,12 @@ func TestSELinux(t *testing.T) {
235236
t.Skip("SELinux not enabled, skipping.")
236237
}
237238

239+
// Ensure the thread stays the same for duration of the test.
240+
// Otherwise Go runtime can switch this to a different thread,
241+
// which results in EACCES in call to SetFSCreateLabel.
242+
runtime.LockOSThread()
243+
defer runtime.UnlockOSThread()
244+
238245
var (
239246
err error
240247
plabel, flabel string
@@ -259,21 +266,17 @@ func TestSELinux(t *testing.T) {
259266
ReleaseLabel(plabel)
260267

261268
pid := os.Getpid()
262-
t.Logf("PID:%d MCS:%s\n", pid, intToMcs(pid, 1023))
269+
t.Logf("PID:%d MCS:%s", pid, intToMcs(pid, 1023))
263270
err = SetFSCreateLabel("unconfined_u:unconfined_r:unconfined_t:s0")
264-
if err == nil {
265-
t.Log(FSCreateLabel())
266-
} else {
267-
t.Log("SetFSCreateLabel failed", err)
268-
t.Fatal(err)
271+
if err != nil {
272+
t.Fatal("SetFSCreateLabel failed:", err)
269273
}
274+
t.Log(FSCreateLabel())
270275
err = SetFSCreateLabel("")
271-
if err == nil {
272-
t.Log(FSCreateLabel())
273-
} else {
274-
t.Log("SetFSCreateLabel failed", err)
275-
t.Fatal(err)
276+
if err != nil {
277+
t.Fatal("SetFSCreateLabel failed:", err)
276278
}
279+
t.Log(FSCreateLabel())
277280
t.Log(PidLabel(1))
278281
}
279282

0 commit comments

Comments
 (0)