Add AddrReplace

Add an AddrReplace call (and tests).

Equivalent to:
 $ ip addr replace $addr dev $link
This commit is contained in:
Stuart McLaren 2017-03-31 15:52:43 +01:00 committed by Vish Ishaya
parent c682914b0b
commit aec6f885c2
2 changed files with 83 additions and 2 deletions

View File

@ -27,6 +27,19 @@ func (h *Handle) AddrAdd(link Link, addr *Addr) error {
return h.addrHandle(link, addr, req) return h.addrHandle(link, addr, req)
} }
// AddrReplace will replace (or, if not present, add) an IP address on a link device.
// Equivalent to: `ip addr replace $addr dev $link`
func AddrReplace(link Link, addr *Addr) error {
return pkgHandle.AddrReplace(link, addr)
}
// AddrReplace will replace (or, if not present, add) an IP address on a link device.
// Equivalent to: `ip addr replace $addr dev $link`
func (h *Handle) AddrReplace(link Link, addr *Addr) error {
req := h.newNetlinkRequest(syscall.RTM_NEWADDR, syscall.NLM_F_CREATE|syscall.NLM_F_REPLACE|syscall.NLM_F_ACK)
return h.addrHandle(link, addr, req)
}
// AddrDel will delete an IP address from a link device. // AddrDel will delete an IP address from a link device.
// Equivalent to: `ip addr del $addr dev $link` // Equivalent to: `ip addr del $addr dev $link`
func AddrDel(link Link, addr *Addr) error { func AddrDel(link Link, addr *Addr) error {

View File

@ -9,7 +9,15 @@ import (
"testing" "testing"
) )
func TestAddr(t *testing.T) { func TestAddrAdd(t *testing.T) {
DoTestAddr(t, AddrAdd)
}
func TestAddrReplace(t *testing.T) {
DoTestAddr(t, AddrReplace)
}
func DoTestAddr(t *testing.T, FunctionUndertest func(Link, *Addr) error) {
if os.Getenv("TRAVIS_BUILD_DIR") != "" { if os.Getenv("TRAVIS_BUILD_DIR") != "" {
t.Skipf("Fails in travis with: addr_test.go:68: Address flags not set properly, got=0, expected=128") t.Skipf("Fails in travis with: addr_test.go:68: Address flags not set properly, got=0, expected=128")
} }
@ -55,7 +63,7 @@ func TestAddr(t *testing.T) {
} }
for _, tt := range addrTests { for _, tt := range addrTests {
if err = AddrAdd(link, tt.addr); err != nil { if err = FunctionUndertest(link, tt.addr); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -122,4 +130,64 @@ func TestAddr(t *testing.T) {
t.Fatal("Address not removed properly") t.Fatal("Address not removed properly")
} }
} }
}
func TestAddrAddReplace(t *testing.T) {
tearDown := setUpNetlinkTest(t)
defer tearDown()
var address = &net.IPNet{IP: net.IPv4(127, 0, 0, 2), Mask: net.CIDRMask(24, 32)}
var addr = &Addr{IPNet: address}
link, err := LinkByName("lo")
if err != nil {
t.Fatal(err)
}
err = AddrAdd(link, addr)
if err != nil {
t.Fatal(err)
}
addrs, err := AddrList(link, FAMILY_ALL)
if err != nil {
t.Fatal(err)
}
if len(addrs) != 1 {
t.Fatal("Address not added properly")
}
err = AddrAdd(link, addr)
if err == nil {
t.Fatal("Re-adding address should fail (but succeeded unexpectedly).")
}
err = AddrReplace(link, addr)
if err != nil {
t.Fatal("Replacing address failed.")
}
addrs, err = AddrList(link, FAMILY_ALL)
if err != nil {
t.Fatal(err)
}
if len(addrs) != 1 {
t.Fatal("Address 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")
}
} }