Skip to content

Commit

Permalink
ebpf: run TestProgramTestRunInterrupt only on Linux
Browse files Browse the repository at this point in the history
TestProgramTestRunInterrupt tests Linux specific behaviour around signal
delivery. It doesn't make sense to run it on a different platform.

The test was copied without making any changes.

Signed-off-by: Lorenz Bauer <[email protected]>
  • Loading branch information
lmb committed Mar 3, 2025
1 parent 21f046d commit eb39cd8
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 74 deletions.
84 changes: 84 additions & 0 deletions prog_linux_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package ebpf

import (
"math"
"runtime"
"testing"
"time"

"github.com/cilium/ebpf/internal"
"github.com/cilium/ebpf/internal/testutils"
"github.com/cilium/ebpf/internal/unix"
)

func TestProgramTestRunInterrupt(t *testing.T) {
testutils.SkipOnOldKernel(t, "5.0", "EINTR from BPF_PROG_TEST_RUN")

prog := createBasicProgram(t)

var (
tgid = unix.Getpid()
tidChan = make(chan int, 1)
exit = make(chan struct{})
errs = make(chan error, 1)
timeout = time.After(5 * time.Second)
)

defer close(exit)

go func() {
runtime.LockOSThread()
defer func() {
// Wait for the test to allow us to unlock the OS thread, to
// ensure that we don't send SIGUSR1 to the wrong thread.
<-exit
runtime.UnlockOSThread()
}()

tidChan <- unix.Gettid()

// Block this thread in the BPF syscall, so that we can
// trigger EINTR by sending a signal.
opts := RunOptions{
Data: internal.EmptyBPFContext,
Repeat: math.MaxInt32,
Reset: func() {
// We don't know how long finishing the
// test run would take, so flag that we've seen
// an interruption and abort the goroutine.
close(errs)
runtime.Goexit()
},
}
_, _, err := prog.run(&opts)

errs <- err
}()

tid := <-tidChan
for {
err := unix.Tgkill(tgid, tid, unix.SIGUSR1)
if err != nil {
t.Fatal("Can't send signal to goroutine thread:", err)
}

select {
case err, ok := <-errs:
if !ok {
return
}

testutils.SkipIfNotSupported(t, err)
if err == nil {
t.Fatal("testRun wasn't interrupted")
}

t.Fatal("testRun returned an error:", err)

case <-timeout:
t.Fatal("Timed out trying to interrupt the goroutine")

default:
}
}
}
74 changes: 0 additions & 74 deletions prog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@ import (
"math"
"os"
"path/filepath"
"runtime"
"slices"
"strings"
"syscall"
"testing"
"time"

"github.com/go-quicktest/qt"

Expand Down Expand Up @@ -182,78 +180,6 @@ func TestProgramBenchmark(t *testing.T) {
}
}

func TestProgramTestRunInterrupt(t *testing.T) {
testutils.SkipOnOldKernel(t, "5.0", "EINTR from BPF_PROG_TEST_RUN")

prog := createBasicProgram(t)

var (
tgid = unix.Getpid()
tidChan = make(chan int, 1)
exit = make(chan struct{})
errs = make(chan error, 1)
timeout = time.After(5 * time.Second)
)

defer close(exit)

go func() {
runtime.LockOSThread()
defer func() {
// Wait for the test to allow us to unlock the OS thread, to
// ensure that we don't send SIGUSR1 to the wrong thread.
<-exit
runtime.UnlockOSThread()
}()

tidChan <- unix.Gettid()

// Block this thread in the BPF syscall, so that we can
// trigger EINTR by sending a signal.
opts := RunOptions{
Data: internal.EmptyBPFContext,
Repeat: math.MaxInt32,
Reset: func() {
// We don't know how long finishing the
// test run would take, so flag that we've seen
// an interruption and abort the goroutine.
close(errs)
runtime.Goexit()
},
}
_, _, err := prog.run(&opts)

errs <- err
}()

tid := <-tidChan
for {
err := unix.Tgkill(tgid, tid, unix.SIGUSR1)
if err != nil {
t.Fatal("Can't send signal to goroutine thread:", err)
}

select {
case err, ok := <-errs:
if !ok {
return
}

testutils.SkipIfNotSupported(t, err)
if err == nil {
t.Fatal("testRun wasn't interrupted")
}

t.Fatal("testRun returned an error:", err)

case <-timeout:
t.Fatal("Timed out trying to interrupt the goroutine")

default:
}
}
}

func TestProgramClose(t *testing.T) {
prog := createBasicProgram(t)

Expand Down

0 comments on commit eb39cd8

Please sign in to comment.