Skip to content

Commit 4555df1

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, fix logging to avoid printing the same error twice. Signed-off-by: Kir Kolyshkin <[email protected]>
1 parent 6de9828 commit 4555df1

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

go-selinux/selinux_linux_test.go

Lines changed: 9 additions & 4 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
"testing"
1213
)
@@ -136,6 +137,12 @@ func TestSELinux(t *testing.T) {
136137
t.Skip("SELinux not enabled, skipping.")
137138
}
138139

140+
// Ensure the thread stays the same for duration of the test.
141+
// Otherwise Go runtime can switch this to a different thread,
142+
// which results in EACCES in call to SetFSCreateLabel.
143+
runtime.LockOSThread()
144+
defer runtime.UnlockOSThread()
145+
139146
var (
140147
err error
141148
plabel, flabel string
@@ -165,15 +172,13 @@ func TestSELinux(t *testing.T) {
165172
if err == nil {
166173
t.Log(FSCreateLabel())
167174
} else {
168-
t.Log("SetFSCreateLabel failed", err)
169-
t.Fatal(err)
175+
t.Fatal("SetFSCreateLabel failed:", err)
170176
}
171177
err = SetFSCreateLabel("")
172178
if err == nil {
173179
t.Log(FSCreateLabel())
174180
} else {
175-
t.Log("SetFSCreateLabel failed", err)
176-
t.Fatal(err)
181+
t.Fatal("SetFSCreateLabel failed:", err)
177182
}
178183
t.Log(PidLabel(1))
179184
}

0 commit comments

Comments
 (0)