mirror of
https://github.com/vishvananda/netlink.git
synced 2025-10-06 16:27:05 +08:00
tests: Improve address unit test infrastructure
Signed-off-by: kayos@tcp.direct <kayos@tcp.direct>
This commit is contained in:

committed by
Adrian Chiris

parent
cb48698f25
commit
38b12299c1
199
addr_test.go
199
addr_test.go
@@ -20,123 +20,152 @@ func TestAddrReplace(t *testing.T) {
|
|||||||
DoTestAddr(t, AddrReplace)
|
DoTestAddr(t, AddrReplace)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type addrTest struct {
|
||||||
|
name string
|
||||||
|
addr *Addr
|
||||||
|
expected *Addr
|
||||||
|
canFail bool
|
||||||
|
t *testing.T
|
||||||
|
}
|
||||||
|
|
||||||
|
func (at *addrTest) Fatal(a interface{}) {
|
||||||
|
at.t.Helper()
|
||||||
|
if !at.canFail {
|
||||||
|
at.t.Fatal(a)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
at.t.Skipf("Non-fatal: %v", a)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (at *addrTest) Fatalf(fmt string, a ...interface{}) {
|
||||||
|
at.t.Helper()
|
||||||
|
if !at.canFail {
|
||||||
|
at.t.Fatalf(fmt, a...)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
at.t.Skipf("Non-fatal: "+fmt, a...)
|
||||||
|
}
|
||||||
|
|
||||||
func DoTestAddr(t *testing.T, FunctionUndertest func(Link, *Addr) error) {
|
func DoTestAddr(t *testing.T, FunctionUndertest func(Link, *Addr) error) {
|
||||||
if os.Getenv("CI") == "true" {
|
if os.Getenv("CI") == "true" {
|
||||||
t.Skipf("Fails in CI with: addr_test.go:*: Address flags not set properly, got=128, expected=132")
|
t.Skipf("Fails in CI with: addr_test.go:*: Address flags not set properly, got=128, expected=132")
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: IFA_F_PERMANENT does not seem to be set by default on older kernels?
|
// TODO: IFA_F_PERMANENT does not seem to be set by default on older kernels?
|
||||||
// TODO: IFA_F_OPTIMISTIC failing in CI. should we just skip that one check?
|
// TODO: IFA_F_OPTIMISTIC failing in CI. should we just skip that one check?
|
||||||
var address = &net.IPNet{IP: net.IPv4(127, 0, 0, 2), Mask: net.CIDRMask(32, 32)}
|
var address = &net.IPNet{IP: net.IPv4(127, 0, 0, 2), Mask: net.CIDRMask(32, 32)}
|
||||||
var peer = &net.IPNet{IP: net.IPv4(127, 0, 0, 3), Mask: net.CIDRMask(24, 32)}
|
var peer = &net.IPNet{IP: net.IPv4(127, 0, 0, 3), Mask: net.CIDRMask(24, 32)}
|
||||||
var addrTests = []struct {
|
var addrTests = []addrTest{
|
||||||
addr *Addr
|
|
||||||
expected *Addr
|
|
||||||
}{
|
|
||||||
{
|
{
|
||||||
&Addr{IPNet: address},
|
name: "lo_uni_perm", addr: &Addr{IPNet: address},
|
||||||
&Addr{IPNet: address, Label: "lo", Scope: unix.RT_SCOPE_UNIVERSE, Flags: unix.IFA_F_PERMANENT},
|
expected: &Addr{IPNet: address, Label: "lo", Scope: unix.RT_SCOPE_UNIVERSE, Flags: unix.IFA_F_PERMANENT},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
&Addr{IPNet: address, Label: "local"},
|
name: "local_uni_perm", addr: &Addr{IPNet: address, Label: "local"},
|
||||||
&Addr{IPNet: address, Label: "local", Scope: unix.RT_SCOPE_UNIVERSE, Flags: unix.IFA_F_PERMANENT},
|
expected: &Addr{IPNet: address, Label: "local", Scope: unix.RT_SCOPE_UNIVERSE, Flags: unix.IFA_F_PERMANENT},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
&Addr{IPNet: address, Flags: unix.IFA_F_OPTIMISTIC},
|
name: "lo_uni_optimistic_perm", addr: &Addr{IPNet: address, Flags: unix.IFA_F_OPTIMISTIC}, canFail: true,
|
||||||
&Addr{IPNet: address, Label: "lo", Flags: unix.IFA_F_OPTIMISTIC | unix.IFA_F_PERMANENT, Scope: unix.RT_SCOPE_UNIVERSE},
|
expected: &Addr{IPNet: address, Label: "lo", Flags: unix.IFA_F_OPTIMISTIC | unix.IFA_F_PERMANENT, Scope: unix.RT_SCOPE_UNIVERSE},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
&Addr{IPNet: address, Flags: unix.IFA_F_OPTIMISTIC | unix.IFA_F_DADFAILED},
|
// Is this a valid scenario for IPv4?
|
||||||
&Addr{IPNet: address, Label: "lo", Flags: unix.IFA_F_OPTIMISTIC | unix.IFA_F_DADFAILED | unix.IFA_F_PERMANENT, Scope: unix.RT_SCOPE_UNIVERSE},
|
name: "lo_uni_optimistic_perm_dupe", addr: &Addr{IPNet: address, Flags: unix.IFA_F_OPTIMISTIC | unix.IFA_F_DADFAILED}, canFail: true,
|
||||||
|
expected: &Addr{IPNet: address, Label: "lo", Flags: unix.IFA_F_OPTIMISTIC | unix.IFA_F_DADFAILED | unix.IFA_F_PERMANENT, Scope: unix.RT_SCOPE_UNIVERSE},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
&Addr{IPNet: address, Scope: unix.RT_SCOPE_NOWHERE},
|
name: "lo_nullroute_perm", addr: &Addr{IPNet: address, Scope: unix.RT_SCOPE_NOWHERE},
|
||||||
&Addr{IPNet: address, Label: "lo", Flags: unix.IFA_F_PERMANENT, Scope: unix.RT_SCOPE_NOWHERE},
|
expected: &Addr{IPNet: address, Label: "lo", Flags: unix.IFA_F_PERMANENT, Scope: unix.RT_SCOPE_NOWHERE},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
&Addr{IPNet: address, Peer: peer},
|
name: "lo_uni_perm_with_peer", addr: &Addr{IPNet: address, Peer: peer},
|
||||||
&Addr{IPNet: address, Peer: peer, Label: "lo", Scope: unix.RT_SCOPE_UNIVERSE, Flags: unix.IFA_F_PERMANENT},
|
expected: &Addr{IPNet: address, Peer: peer, Label: "lo", Scope: unix.RT_SCOPE_UNIVERSE, Flags: unix.IFA_F_PERMANENT},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
tearDown := setUpNetlinkTest(t)
|
|
||||||
defer tearDown()
|
|
||||||
|
|
||||||
link, err := LinkByName("lo")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, tt := range addrTests {
|
for _, tt := range addrTests {
|
||||||
if err = FunctionUndertest(link, tt.addr); err != nil {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
t.Fatal(err)
|
tt.t = t
|
||||||
}
|
|
||||||
|
|
||||||
addrs, err := AddrList(link, FAMILY_ALL)
|
tearDown := setUpNetlinkTest(t)
|
||||||
if err != nil {
|
defer tearDown()
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(addrs) != 1 {
|
link, err := LinkByName("lo")
|
||||||
t.Fatal("Address not added properly")
|
if err != nil {
|
||||||
}
|
tt.Fatal(err)
|
||||||
|
|
||||||
if !addrs[0].Equal(*tt.expected) {
|
|
||||||
t.Fatalf("Address ip no set properly, got=%s, expected=%s", addrs[0], tt.expected)
|
|
||||||
}
|
|
||||||
|
|
||||||
if addrs[0].Label != tt.expected.Label {
|
|
||||||
t.Fatalf("Address label not set properly, got=%s, expected=%s", addrs[0].Label, tt.expected.Label)
|
|
||||||
}
|
|
||||||
|
|
||||||
if addrs[0].Flags != tt.expected.Flags {
|
|
||||||
t.Fatalf("Address flags not set properly, got=%d, expected=%d", addrs[0].Flags, tt.expected.Flags)
|
|
||||||
}
|
|
||||||
|
|
||||||
if addrs[0].Scope != tt.expected.Scope {
|
|
||||||
t.Fatalf("Address scope not set properly, got=%d, expected=%d", addrs[0].Scope, tt.expected.Scope)
|
|
||||||
}
|
|
||||||
|
|
||||||
if ifindex := link.Attrs().Index; ifindex != addrs[0].LinkIndex {
|
|
||||||
t.Fatalf("Address ifindex not set properly, got=%d, expected=%d", addrs[0].LinkIndex, ifindex)
|
|
||||||
}
|
|
||||||
|
|
||||||
if tt.expected.Peer != nil {
|
|
||||||
if !addrs[0].PeerEqual(*tt.expected) {
|
|
||||||
t.Fatalf("Peer Address ip no set properly, got=%s, expected=%s", addrs[0].Peer, tt.expected.Peer)
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Pass FAMILY_V4, we should get the same results as FAMILY_ALL
|
if err = FunctionUndertest(link, tt.addr); err != nil {
|
||||||
addrs, err = AddrList(link, FAMILY_V4)
|
tt.Fatal(err)
|
||||||
if err != nil {
|
}
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if len(addrs) != 1 {
|
|
||||||
t.Fatal("Address not added properly")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Pass a wrong family number, we should get nil list
|
addrs, err := AddrList(link, FAMILY_ALL)
|
||||||
addrs, err = AddrList(link, 0x8)
|
if err != nil {
|
||||||
if err != nil {
|
tt.Fatal(err)
|
||||||
t.Fatal(err)
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if len(addrs) != 0 {
|
if len(addrs) != 1 {
|
||||||
t.Fatal("Address not expected")
|
tt.Fatal("Address not added properly")
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = AddrDel(link, tt.addr); err != nil {
|
if !addrs[0].Equal(*tt.expected) {
|
||||||
t.Fatal(err)
|
tt.Fatalf("Address ip not set properly, got=%s, expected=%s", addrs[0], tt.expected)
|
||||||
}
|
}
|
||||||
|
|
||||||
addrs, err = AddrList(link, FAMILY_ALL)
|
if addrs[0].Label != tt.expected.Label {
|
||||||
if err != nil {
|
tt.Fatalf("Address label not set properly, got=%s, expected=%s", addrs[0].Label, tt.expected.Label)
|
||||||
t.Fatal(err)
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if len(addrs) != 0 {
|
if addrs[0].Flags != tt.expected.Flags {
|
||||||
t.Fatal("Address not removed properly")
|
tt.Fatalf("Address flags not set properly, got=%d, expected=%d", addrs[0].Flags, tt.expected.Flags)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if addrs[0].Scope != tt.expected.Scope {
|
||||||
|
tt.Fatalf("Address scope not set properly, got=%d, expected=%d", addrs[0].Scope, tt.expected.Scope)
|
||||||
|
}
|
||||||
|
|
||||||
|
if ifindex := link.Attrs().Index; ifindex != addrs[0].LinkIndex {
|
||||||
|
tt.Fatalf("Address ifindex not set properly, got=%d, expected=%d", addrs[0].LinkIndex, ifindex)
|
||||||
|
}
|
||||||
|
|
||||||
|
if tt.expected.Peer != nil {
|
||||||
|
if !addrs[0].PeerEqual(*tt.expected) {
|
||||||
|
tt.Fatalf("Peer Address ip not set properly, got=%s, expected=%s", addrs[0].Peer, tt.expected.Peer)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Pass FAMILY_V4, we should get the same results as FAMILY_ALL
|
||||||
|
addrs, err = AddrList(link, FAMILY_V4)
|
||||||
|
if err != nil {
|
||||||
|
tt.Fatal(err)
|
||||||
|
}
|
||||||
|
if len(addrs) != 1 {
|
||||||
|
tt.Fatal("Address not added properly")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Pass a wrong family number, we should get nil list
|
||||||
|
addrs, err = AddrList(link, 0x8)
|
||||||
|
if err != nil {
|
||||||
|
tt.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(addrs) != 0 {
|
||||||
|
tt.Fatal("Address not expected")
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = AddrDel(link, tt.addr); err != nil {
|
||||||
|
tt.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
addrs, err = AddrList(link, FAMILY_ALL)
|
||||||
|
if err != nil {
|
||||||
|
tt.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(addrs) != 0 {
|
||||||
|
tt.Fatal("Address not removed properly")
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user