Table driven test for Addr

This commit is contained in:
Hubert Krauze 2015-12-09 11:53:28 +01:00
parent f351153998
commit 124b1f4449
1 changed files with 62 additions and 113 deletions

View File

@ -6,93 +6,34 @@ import (
"testing" "testing"
) )
func TestAddrAddDel(t *testing.T) { func TestAddr(t *testing.T) {
tearDown := setUpNetlinkTest(t) var address = &net.IPNet{net.IPv4(127, 0, 0, 2), net.CIDRMask(24, 32)}
defer tearDown() var addrTests = []struct {
addr *Addr
link, err := LinkByName("lo") expected *Addr
if err != nil { }{
t.Fatal(err) {
} &Addr{IPNet: address},
&Addr{IPNet: address, Label: "lo", Scope: syscall.RT_SCOPE_UNIVERSE, Flags: syscall.IFA_F_PERMANENT},
addr, err := ParseAddr("127.1.1.1/24 local") },
if err != nil { {
t.Fatal(err) &Addr{IPNet: address, Label: "local"},
} &Addr{IPNet: address, Label: "local", Scope: syscall.RT_SCOPE_UNIVERSE, Flags: syscall.IFA_F_PERMANENT},
},
if err = AddrAdd(link, addr); err != nil { {
t.Fatal(err) &Addr{IPNet: address, Flags: syscall.IFA_F_OPTIMISTIC},
} &Addr{IPNet: address, Label: "lo", Flags: syscall.IFA_F_OPTIMISTIC | syscall.IFA_F_PERMANENT, Scope: syscall.RT_SCOPE_UNIVERSE},
},
addrs, err := AddrList(link, FAMILY_ALL) {
if err != nil { &Addr{IPNet: address, Flags: syscall.IFA_F_OPTIMISTIC | syscall.IFA_F_DADFAILED},
t.Fatal(err) &Addr{IPNet: address, Label: "lo", Flags: syscall.IFA_F_OPTIMISTIC | syscall.IFA_F_DADFAILED | syscall.IFA_F_PERMANENT, Scope: syscall.RT_SCOPE_UNIVERSE},
} },
{
if len(addrs) != 1 || !addr.Equal(addrs[0]) || addrs[0].Label != addr.Label { &Addr{IPNet: address, Scope: syscall.RT_SCOPE_NOWHERE},
t.Fatal("Address not added properly") &Addr{IPNet: address, Label: "lo", Flags: syscall.IFA_F_PERMANENT, Scope: syscall.RT_SCOPE_NOWHERE},
}
if err = AddrDel(link, addr); err != nil {
t.Fatal(err)
}
addrs, err = AddrList(link, FAMILY_ALL)
if err != nil {
t.Fatal(err)
}
if len(addrs) != 0 {
t.Fatal("Address not removed properly")
}
}
func TestAddrAddDelScope(t *testing.T) {
tearDown := setUpNetlinkTest(t)
defer tearDown()
link, err := LinkByName("lo")
if err != nil {
t.Fatal(err)
}
addr := &Addr{
IPNet: &net.IPNet{
IP: net.IPv4(127, 1, 1, 1),
Mask: net.CIDRMask(24, 32),
}, },
Scope: syscall.RT_SCOPE_LINK,
}
if err = AddrAdd(link, addr); err != nil {
t.Fatal(err)
} }
addrs, err := AddrList(link, FAMILY_ALL)
if err != nil {
t.Fatal(err)
}
if len(addrs) != 1 || !addr.Equal(addrs[0]) {
t.Fatal("Address not added properly")
}
if addrs[0].Scope != addr.Scope {
t.Fatal("Address scope not added properly")
}
if err = AddrDel(link, addr); err != nil {
t.Fatal(err)
}
addrs, err = AddrList(link, FAMILY_ALL)
if err != nil {
t.Fatal(err)
}
if len(addrs) != 0 {
t.Fatal("Address not removed properly")
}
}
func TestAddrAddDelFlags(t *testing.T) {
tearDown := setUpNetlinkTest(t) tearDown := setUpNetlinkTest(t)
defer tearDown() defer tearDown()
@ -101,39 +42,47 @@ func TestAddrAddDelFlags(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
addr := &Addr{ for _, tt := range addrTests {
IPNet: &net.IPNet{ if err = AddrAdd(link, tt.addr); err != nil {
IP: net.IPv4(127, 1, 1, 1), t.Fatal(err)
Mask: net.CIDRMask(24, 32), }
},
Flags: syscall.IFA_F_PERMANENT,
}
if err = AddrAdd(link, addr); err != nil {
t.Fatal(err)
}
addrs, err := AddrList(link, FAMILY_ALL) addrs, err := AddrList(link, FAMILY_ALL)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if len(addrs) != 1 || !addr.Equal(addrs[0]) { if len(addrs) != 1 {
t.Fatal("Address not added properly") t.Fatal("Address not added properly")
} }
if addrs[0].Flags != addr.Flags { if !addrs[0].Equal(*tt.expected) {
t.Fatal("Address flags not set properly") t.Fatalf("Address ip no set properly, got=%s, expected=%s", addrs[0], tt.expected)
} }
if err = AddrDel(link, addr); err != nil { if addrs[0].Label != tt.expected.Label {
t.Fatal(err) t.Fatalf("Address label not set properly, got=%s, expected=%s", addrs[0].Label, tt.expected.Label)
} }
addrs, err = AddrList(link, FAMILY_ALL)
if err != nil {
t.Fatal(err)
}
if len(addrs) != 0 { if addrs[0].Flags != tt.expected.Flags {
t.Fatal("Address not removed properly") 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 err = AddrDel(link, tt.addr); err != nil {
t.Fatal(err)
}
addrs, err = AddrList(link, FAMILY_ALL)
if err != nil {
t.Fatal(err)
}
if len(addrs) != 0 {
t.Fatal("Address not removed properly")
}
} }
} }