Skip to content

Commit 360b2a3

Browse files
committed
TestSocketLabel: use LockOSThread to avoid flakes
The SetSocketLabel documentation says the caller should use runtime.LockOSThread. Indeed, if not used, there is an occasional flake: > $ go test -count 10000 -run SocketLabel . > --- FAIL: TestSocketLabel (0.00s) > label_linux_test.go:189: write /proc/thread-self/attr/sockcreate: permission denied Add LockOSThread to fix the flake. Signed-off-by: Kir Kolyshkin <[email protected]>
1 parent 4aa96bb commit 360b2a3

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

go-selinux/label/label_linux_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package label
33
import (
44
"errors"
55
"os"
6+
"runtime"
67
"strings"
78
"testing"
89

@@ -181,6 +182,12 @@ func TestSELinuxNoLevel(t *testing.T) {
181182
func TestSocketLabel(t *testing.T) {
182183
needSELinux(t)
183184

185+
// Ensure the thread stays the same for duration of the test.
186+
// Otherwise Go runtime can switch this to a different thread,
187+
// which results in EACCES in call to SetSocketLabel.
188+
runtime.LockOSThread()
189+
defer runtime.UnlockOSThread()
190+
184191
label := "system_u:object_r:container_t:s0:c1,c2"
185192
if err := selinux.SetSocketLabel(label); err != nil {
186193
t.Fatal(err)

0 commit comments

Comments
 (0)