Skip to content

Commit

Permalink
netlink: Make tests work with newer libnl
Browse files Browse the repository at this point in the history
`libnl` 3.5.0 started setting `NLA_F_NESTED` to mark nested attributes
in netlink messages.

As seen in #128, this causes tests to fail in some distros with newer
`libnl` installations.

Since we don't actually need newer `libnl` versions, we can simply
modify these tests to accept both behaviors.
  • Loading branch information
hazaelsan committed Apr 26, 2023
1 parent 8264e02 commit 648c544
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
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
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 648c544

Please sign in to comment.