Files
netlink/xfrm_monitor_linux_test.go
Vishvananda Abrams 1e35ba25dc test: Improve test reliability with proper cleanup and isolation
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.
2025-08-27 09:24:17 -07:00

53 lines
1.2 KiB
Go

package netlink
import (
"os"
"testing"
"github.com/vishvananda/netlink/nl"
)
func TestXfrmMonitorExpire(t *testing.T) {
if os.Getenv("CI") == "true" {
t.Skipf("Flaky in CI: Intermittently causes 10 minute timeout")
}
t.Cleanup(setUpNetlinkTest(t))
ch := make(chan XfrmMsg)
done := make(chan struct{})
defer close(done)
errChan := make(chan error)
if err := XfrmMonitor(ch, nil, errChan, nl.XFRM_MSG_EXPIRE); err != nil {
t.Fatal(err)
}
// Program state with limits
state := getBaseState()
state.Limits.TimeHard = 2
state.Limits.TimeSoft = 1
if err := XfrmStateAdd(state); err != nil {
t.Fatal(err)
}
hardFound := false
softFound := false
msg := (<-ch).(*XfrmMsgExpire)
if msg.XfrmState.Spi != state.Spi {
t.Fatal("Received unexpected msg, spi does not match")
}
hardFound = msg.Hard || hardFound
softFound = !msg.Hard || softFound
msg = (<-ch).(*XfrmMsgExpire)
if msg.XfrmState.Spi != state.Spi {
t.Fatal("Received unexpected msg, spi does not match")
}
hardFound = msg.Hard || hardFound
softFound = !msg.Hard || softFound
if !hardFound || !softFound {
t.Fatal("Missing expire msg: hard found:", hardFound, "soft found:", softFound)
}
}