Skip to content

Commit

Permalink
Merge pull request #130 from hazaelsan/fix_netlink
Browse files Browse the repository at this point in the history
netlink: Make tests work with newer `libnl`
  • Loading branch information
hazaelsan authored Apr 26, 2023
2 parents 8264e02 + 648c544 commit 2ec90dc
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
12 changes: 10 additions & 2 deletions ipvs/ipvs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ import (
"github.com/google/seesaw/netlink"
)

// Old versions of libnl didn't set NLA_F_TESTED to indicate nested attributes.
// For now let's accept both formats in tests by forcing NLA_F_TESTED on.
func fixNestedAttrs(b []byte) {
b[23] |= 0x80
}

var testStats = Stats{
Connections: 1234,
PacketsIn: 100000,
Expand Down Expand Up @@ -396,7 +402,7 @@ var (
nlmIPVSAddDestination = []byte{
0x6c, 0x00, 0x00, 0x00, 0x19, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x01, 0x01, 0x00, 0x00, 0x58, 0x00, 0x02, 0x00,
0x01, 0x01, 0x00, 0x00, 0x58, 0x00, 0x02, 0x80,
0x14, 0x00, 0x01, 0x00, 0x20, 0x02, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0xca, 0xfe, 0x06, 0x00, 0x02, 0x00,
Expand Down Expand Up @@ -441,7 +447,7 @@ var (
nlmIPVSAddService = []byte{
0x68, 0x00, 0x00, 0x00, 0x19, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x01, 0x01, 0x00, 0x00, 0x54, 0x00, 0x01, 0x00,
0x01, 0x01, 0x00, 0x00, 0x54, 0x00, 0x01, 0x80,
0x06, 0x00, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00,
0x06, 0x00, 0x02, 0x00, 0x06, 0x00, 0x00, 0x00,
0x14, 0x00, 0x03, 0x00, 0x01, 0x01, 0x01, 0x01,
Expand Down Expand Up @@ -539,6 +545,7 @@ func TestDestinationNetlinkMarshal(t *testing.T) {
if err != nil {
t.Fatalf("Failed to get message bytes: %v", err)
}
fixNestedAttrs(got)
if want := nlmIPVSAddDestination; !bytes.Equal(got, want) {
t.Errorf("Got netlink bytes %#v, want %#v", got, want)
}
Expand Down Expand Up @@ -579,6 +586,7 @@ func TestServiceNetlinkMarshal(t *testing.T) {
if err != nil {
t.Fatalf("Failed to get message bytes: %v", err)
}
fixNestedAttrs(got)
if want := nlmIPVSAddService; !bytes.Equal(got, want) {
t.Errorf("Got netlink bytes %#v, want %#v", got, want)
}
Expand Down
9 changes: 8 additions & 1 deletion netlink/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ type ipvsIPAddr struct {
IP net.IP `netlink:"attr:1"`
}

// Old versions of libnl didn't set NLA_F_TESTED to indicate nested attributes.
// For now let's accept both formats in tests by forcing NLA_F_TESTED on.
func fixNestedAttrs(b []byte) {
b[23] |= 0x80
}

var (
nlmIPVSInfo = []byte{
0x24, 0x00, 0x00, 0x00, 0x19, 0x00, 0x00, 0x00,
Expand All @@ -79,7 +85,7 @@ var (
nlmIPVSAddService = []byte{
0x68, 0x00, 0x00, 0x00, 0x19, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x01, 0x01, 0x00, 0x00, 0x54, 0x00, 0x01, 0x00,
0x01, 0x01, 0x00, 0x00, 0x54, 0x00, 0x01, 0x80,
0x06, 0x00, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00,
0x06, 0x00, 0x02, 0x00, 0x06, 0x00, 0x00, 0x00,
0x14, 0x00, 0x03, 0x00, 0x01, 0x01, 0x01, 0x01,
Expand Down Expand Up @@ -193,6 +199,7 @@ func TestMessageMarshal(t *testing.T) {
if err != nil {
t.Fatalf("Failed to get message bytes: %v", err)
}
fixNestedAttrs(got)
if want := nlmIPVSAddService; !bytes.Equal(got, want) {
t.Fatalf("Got netlink bytes %#v, want %#v", got, want)
}
Expand Down

0 comments on commit 2ec90dc

Please sign in to comment.