Skip to content

Commit

Permalink
replace netlink.Socket
Browse files Browse the repository at this point in the history
Signed-off-by: Florian Lehner <dev@der-flo.net>
  • Loading branch information
florianl committed Nov 11, 2022
1 parent 7715151 commit 66cb435
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 12 deletions.
15 changes: 14 additions & 1 deletion tc.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,22 @@ import (
"github.com/mdlayher/netlink"
)

// tcConn defines a subset of netlink.Conn.
type tcConn interface {
Close() error
JoinGroup(group uint32) error
LeaveGroup(group uint32) error
Receive() ([]netlink.Message, error)
Send(m netlink.Message) (netlink.Message, error)
SetOption(option netlink.ConnOption, enable bool) error
SetReadDeadline(t time.Time) error
}

var _ tcConn = &netlink.Conn{}

// Tc represents a RTNETLINK wrapper
type Tc struct {
con *netlink.Conn
con tcConn

logger *log.Logger
}
Expand Down
29 changes: 18 additions & 11 deletions tc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,16 @@ func testConn(t *testing.T) (*Tc, func()) {
}
}

var _ netlink.Socket = &socket{}
var _ tcConn = &fakeConn{}

func (c *socket) Close() error { return nil }
func (c *socket) SendMessages(m []netlink.Message) error { c.msgs = append(c.msgs, m...); return nil }
func (c *socket) Send(m netlink.Message) error { c.msgs = append(c.msgs, m); return nil }
func (c *socket) Receive() ([]netlink.Message, error) {
func (c *fakeConn) Close() error { return nil }
func (c *fakeConn) SendMessages(m []netlink.Message) error { c.msgs = append(c.msgs, m...); return nil }
func (c *fakeConn) Send(m netlink.Message) (netlink.Message, error) {
c.msgs = append(c.msgs, m)
return m, nil
}

func (c *fakeConn) Receive() ([]netlink.Message, error) {
if len(c.msgs) > 0 {
var resp []netlink.Message
for _, msg := range c.msgs {
Expand Down Expand Up @@ -113,19 +117,22 @@ func (c *socket) Receive() ([]netlink.Message, error) {
}
return []netlink.Message{}, nil
}
func (c *socket) JoinGroup(g uint32) error { return nil }
func (c *socket) LeaveGroup(g uint32) error { return nil }

// A socket is a netlink.Socket used for testing.
type socket struct {
func (c *fakeConn) JoinGroup(uint32) error { return nil }
func (c *fakeConn) LeaveGroup(uint32) error { return nil }
func (c *fakeConn) SetOption(netlink.ConnOption, bool) error { return nil }
func (c *fakeConn) SetReadDeadline(time.Time) error { return nil }

// fakeConn is a netlink.Conn used for testing.
type fakeConn struct {
msgs []netlink.Message
}

func testHookConn(t *testing.T) (*Tc, func()) {
t.Helper()

hookSocket := &socket{}
c := &Tc{con: netlink.NewConn(hookSocket, 1)}
hookedConn := &fakeConn{}
c := &Tc{con: hookedConn}

return c, func() {
if err := c.Close(); err != nil {
Expand Down

0 comments on commit 66cb435

Please sign in to comment.