mirror of
https://github.com/vishvananda/netlink.git
synced 2025-09-27 04:05:59 +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.
116 lines
2.1 KiB
Go
116 lines
2.1 KiB
Go
//go:build linux
|
|
// +build linux
|
|
|
|
package netlink
|
|
|
|
import (
|
|
"net"
|
|
"testing"
|
|
)
|
|
|
|
func TestPDPv0AddDel(t *testing.T) {
|
|
t.Cleanup(setUpNetlinkTestWithKModule(t, "gtp"))
|
|
|
|
if err := LinkAdd(testGTPLink(t)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
link, err := LinkByName("gtp0")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
err = GTPPDPAdd(link, &PDP{
|
|
PeerAddress: net.ParseIP("1.1.1.1"),
|
|
MSAddress: net.ParseIP("2.2.2.2"),
|
|
TID: 10,
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
list, err := GTPPDPList()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(list) != 1 {
|
|
t.Fatal("Failed to add v0 PDP")
|
|
}
|
|
pdp, err := GTPPDPByMSAddress(link, net.ParseIP("2.2.2.2"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if pdp == nil {
|
|
t.Fatal("failed to get v0 PDP by MS address")
|
|
}
|
|
pdp, err = GTPPDPByTID(link, 10)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if pdp == nil {
|
|
t.Fatal("failed to get v0 PDP by TID")
|
|
}
|
|
err = GTPPDPDel(link, &PDP{TID: 10})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
list, err = GTPPDPList()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(list) != 0 {
|
|
t.Fatal("Failed to delete v0 PDP")
|
|
}
|
|
}
|
|
|
|
func TestPDPv1AddDel(t *testing.T) {
|
|
t.Cleanup(setUpNetlinkTestWithKModule(t, "gtp"))
|
|
|
|
if err := LinkAdd(testGTPLink(t)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
link, err := LinkByName("gtp0")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
err = GTPPDPAdd(link, &PDP{
|
|
PeerAddress: net.ParseIP("1.1.1.1"),
|
|
MSAddress: net.ParseIP("2.2.2.2"),
|
|
Version: 1,
|
|
ITEI: 10,
|
|
OTEI: 10,
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
list, err := GTPPDPList()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(list) != 1 {
|
|
t.Fatal("Failed to add v1 PDP")
|
|
}
|
|
pdp, err := GTPPDPByMSAddress(link, net.ParseIP("2.2.2.2"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if pdp == nil {
|
|
t.Fatal("failed to get v1 PDP by MS address")
|
|
}
|
|
pdp, err = GTPPDPByITEI(link, 10)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if pdp == nil {
|
|
t.Fatal("failed to get v1 PDP by ITEI")
|
|
}
|
|
err = GTPPDPDel(link, &PDP{Version: 1, ITEI: 10})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
list, err = GTPPDPList()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(list) != 0 {
|
|
t.Fatal("Failed to delete v1 PDP")
|
|
}
|
|
}
|