mirror of
https://github.com/vishvananda/netlink.git
synced 2025-09-26 20:01:13 +08:00

Refactors test setup and teardown logic to use `t.Cleanup` instead of `defer`. This ensures that cleanup functions are correctly scoped to each subtest's lifecycle, improving test isolation and reliability. The `setUpNetlinkTest` helper function is also improved to correctly save and restore the original network namespace, ensuring that tests do not leak state. To support this, a `Close()` method that returns an error is added to the `Handle` struct, allowing for proper cleanup of underlying netlink sockets. The test helpers are updated to use this new method, preventing resource leaks between tests. Additionally, a bug in the `netns` tests is fixed where a large namespace ID could overflow a 32-bit integer, causing spurious failures on some systems.
82 lines
1.4 KiB
Go
82 lines
1.4 KiB
Go
//go:build linux
|
|
// +build linux
|
|
|
|
package netlink
|
|
|
|
import (
|
|
"github.com/vishvananda/netns"
|
|
"os"
|
|
"os/exec"
|
|
"runtime"
|
|
"testing"
|
|
)
|
|
|
|
func TestSubscribeProcEvent(t *testing.T) {
|
|
skipUnlessRoot(t)
|
|
runtime.LockOSThread()
|
|
defer runtime.UnlockOSThread()
|
|
|
|
pid1ns, err := netns.GetFromPid(1)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
err = netns.Set(pid1ns)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
ch := make(chan ProcEvent)
|
|
done := make(chan struct{})
|
|
defer close(done)
|
|
|
|
errChan := make(chan error)
|
|
|
|
if err := ProcEventMonitor(ch, done, errChan); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
cmd := exec.Command("false")
|
|
if err := cmd.Start(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
// first we wait for proc - i.e. childTgid is cmd.Process.Pid
|
|
for {
|
|
e := <-ch
|
|
t.Logf("pid: %+v e: %+v", os.Getpid(), e)
|
|
if e.Msg.Tgid() == uint32(os.Getpid()) {
|
|
if forkEvent, ok := e.Msg.(*ForkProcEvent); ok {
|
|
if forkEvent.ChildTgid == uint32(cmd.Process.Pid) {
|
|
break
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// wait for exec event
|
|
for {
|
|
e := <-ch
|
|
if e.Msg.Tgid() == uint32(cmd.Process.Pid) {
|
|
if _, ok := e.Msg.(*ExecProcEvent); ok {
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
cmd.Wait()
|
|
for {
|
|
e := <-ch
|
|
if e.Msg.Tgid() == uint32(cmd.Process.Pid) {
|
|
if exitEvent, ok := e.Msg.(*ExitProcEvent); ok {
|
|
if exitEvent.ExitCode != 256 {
|
|
t.Errorf("Expected error code 256 (-1), but got %+v", exitEvent)
|
|
}
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
done <- struct{}{}
|
|
}
|