chore(netlink): debug log ip rule commands in netlink instead of routing package

This commit is contained in:
Quentin McGaw
2024-10-19 12:43:26 +00:00
parent 2388e0550b
commit 3dfb43e117
8 changed files with 100 additions and 181 deletions

View File

@@ -1,7 +1,5 @@
package routing
//go:generate mockgen -destination=logger_mock_test.go -package routing . Logger
type Logger interface {
Debug(s string)
Info(s string)

View File

@@ -1,82 +0,0 @@
// Code generated by MockGen. DO NOT EDIT.
// Source: github.com/qdm12/gluetun/internal/routing (interfaces: Logger)
// Package routing is a generated GoMock package.
package routing
import (
reflect "reflect"
gomock "github.com/golang/mock/gomock"
)
// MockLogger is a mock of Logger interface.
type MockLogger struct {
ctrl *gomock.Controller
recorder *MockLoggerMockRecorder
}
// MockLoggerMockRecorder is the mock recorder for MockLogger.
type MockLoggerMockRecorder struct {
mock *MockLogger
}
// NewMockLogger creates a new mock instance.
func NewMockLogger(ctrl *gomock.Controller) *MockLogger {
mock := &MockLogger{ctrl: ctrl}
mock.recorder = &MockLoggerMockRecorder{mock}
return mock
}
// EXPECT returns an object that allows the caller to indicate expected use.
func (m *MockLogger) EXPECT() *MockLoggerMockRecorder {
return m.recorder
}
// Debug mocks base method.
func (m *MockLogger) Debug(arg0 string) {
m.ctrl.T.Helper()
m.ctrl.Call(m, "Debug", arg0)
}
// Debug indicates an expected call of Debug.
func (mr *MockLoggerMockRecorder) Debug(arg0 interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Debug", reflect.TypeOf((*MockLogger)(nil).Debug), arg0)
}
// Error mocks base method.
func (m *MockLogger) Error(arg0 string) {
m.ctrl.T.Helper()
m.ctrl.Call(m, "Error", arg0)
}
// Error indicates an expected call of Error.
func (mr *MockLoggerMockRecorder) Error(arg0 interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Error", reflect.TypeOf((*MockLogger)(nil).Error), arg0)
}
// Info mocks base method.
func (m *MockLogger) Info(arg0 string) {
m.ctrl.T.Helper()
m.ctrl.Call(m, "Info", arg0)
}
// Info indicates an expected call of Info.
func (mr *MockLoggerMockRecorder) Info(arg0 interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Info", reflect.TypeOf((*MockLogger)(nil).Info), arg0)
}
// Warn mocks base method.
func (m *MockLogger) Warn(arg0 string) {
m.ctrl.T.Helper()
m.ctrl.Call(m, "Warn", arg0)
}
// Warn indicates an expected call of Warn.
func (mr *MockLoggerMockRecorder) Warn(arg0 interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Warn", reflect.TypeOf((*MockLogger)(nil).Warn), arg0)
}

View File

@@ -8,9 +8,6 @@ import (
)
func (r *Routing) addIPRule(src, dst netip.Prefix, table, priority int) error {
const add = true
r.logger.Debug(ruleDbgMsg(add, src, dst, table, priority))
rule := netlink.NewRule()
rule.Src = src
rule.Dst = dst
@@ -35,9 +32,6 @@ func (r *Routing) addIPRule(src, dst netip.Prefix, table, priority int) error {
}
func (r *Routing) deleteIPRule(src, dst netip.Prefix, table, priority int) error {
const add = false
r.logger.Debug(ruleDbgMsg(add, src, dst, table, priority))
rule := netlink.NewRule()
rule.Src = src
rule.Dst = dst
@@ -59,36 +53,6 @@ func (r *Routing) deleteIPRule(src, dst netip.Prefix, table, priority int) error
return nil
}
func ruleDbgMsg(add bool, src, dst netip.Prefix,
table, priority int,
) (debugMessage string) {
debugMessage = "ip rule"
if add {
debugMessage += " add"
} else {
debugMessage += " del"
}
if src.IsValid() {
debugMessage += " from " + src.String()
}
if dst.IsValid() {
debugMessage += " to " + dst.String()
}
if table != 0 {
debugMessage += " lookup " + fmt.Sprint(table)
}
if priority != -1 {
debugMessage += " pref " + fmt.Sprint(priority)
}
return debugMessage
}
func rulesAreEqual(a, b netlink.Rule) bool {
return ipPrefixesAreEqual(a.Src, b.Src) &&
ipPrefixesAreEqual(a.Dst, b.Dst) &&

View File

@@ -48,13 +48,11 @@ func Test_Routing_addIPRule(t *testing.T) {
dst netip.Prefix
table int
priority int
dbgMsg string
ruleList ruleListCall
ruleAdd ruleAddCall
err error
}{
"list error": {
dbgMsg: "ip rule add pref 0",
ruleList: ruleListCall{
err: errDummy,
},
@@ -65,7 +63,6 @@ func Test_Routing_addIPRule(t *testing.T) {
dst: makeNetipPrefix(2),
table: 99,
priority: 99,
dbgMsg: "ip rule add from 1.1.1.0/24 to 2.2.2.0/24 lookup 99 pref 99",
ruleList: ruleListCall{
rules: []netlink.Rule{
makeIPRule(makeNetipPrefix(2), makeNetipPrefix(2), 99, 99),
@@ -78,7 +75,6 @@ func Test_Routing_addIPRule(t *testing.T) {
dst: makeNetipPrefix(2),
table: 99,
priority: 99,
dbgMsg: "ip rule add from 1.1.1.0/24 to 2.2.2.0/24 lookup 99 pref 99",
ruleAdd: ruleAddCall{
expected: true,
ruleToAdd: makeIPRule(makeNetipPrefix(1), makeNetipPrefix(2), 99, 99),
@@ -91,7 +87,6 @@ func Test_Routing_addIPRule(t *testing.T) {
dst: makeNetipPrefix(2),
table: 99,
priority: 99,
dbgMsg: "ip rule add from 1.1.1.0/24 to 2.2.2.0/24 lookup 99 pref 99",
ruleList: ruleListCall{
rules: []netlink.Rule{
makeIPRule(makeNetipPrefix(2), makeNetipPrefix(2), 99, 99),
@@ -110,9 +105,6 @@ func Test_Routing_addIPRule(t *testing.T) {
t.Parallel()
ctrl := gomock.NewController(t)
logger := NewMockLogger(ctrl)
logger.EXPECT().Debug(testCase.dbgMsg)
netLinker := NewMockNetLinker(ctrl)
netLinker.EXPECT().RuleList(netlink.FamilyAll).
Return(testCase.ruleList.rules, testCase.ruleList.err)
@@ -122,7 +114,6 @@ func Test_Routing_addIPRule(t *testing.T) {
}
r := Routing{
logger: logger,
netLinker: netLinker,
}
@@ -160,13 +151,11 @@ func Test_Routing_deleteIPRule(t *testing.T) {
dst netip.Prefix
table int
priority int
dbgMsg string
ruleList ruleListCall
ruleDel ruleDelCall
err error
}{
"list error": {
dbgMsg: "ip rule del pref 0",
ruleList: ruleListCall{
err: errDummy,
},
@@ -177,7 +166,6 @@ func Test_Routing_deleteIPRule(t *testing.T) {
dst: makeNetipPrefix(2),
table: 99,
priority: 99,
dbgMsg: "ip rule del from 1.1.1.0/24 to 2.2.2.0/24 lookup 99 pref 99",
ruleList: ruleListCall{
rules: []netlink.Rule{
makeIPRule(makeNetipPrefix(1), makeNetipPrefix(2), 99, 99),
@@ -195,7 +183,6 @@ func Test_Routing_deleteIPRule(t *testing.T) {
dst: makeNetipPrefix(2),
table: 99,
priority: 99,
dbgMsg: "ip rule del from 1.1.1.0/24 to 2.2.2.0/24 lookup 99 pref 99",
ruleList: ruleListCall{
rules: []netlink.Rule{
makeIPRule(makeNetipPrefix(2), makeNetipPrefix(2), 99, 99),
@@ -212,7 +199,6 @@ func Test_Routing_deleteIPRule(t *testing.T) {
dst: makeNetipPrefix(2),
table: 99,
priority: 99,
dbgMsg: "ip rule del from 1.1.1.0/24 to 2.2.2.0/24 lookup 99 pref 99",
ruleList: ruleListCall{
rules: []netlink.Rule{
makeIPRule(makeNetipPrefix(2), makeNetipPrefix(2), 99, 99),
@@ -227,9 +213,6 @@ func Test_Routing_deleteIPRule(t *testing.T) {
t.Parallel()
ctrl := gomock.NewController(t)
logger := NewMockLogger(ctrl)
logger.EXPECT().Debug(testCase.dbgMsg)
netLinker := NewMockNetLinker(ctrl)
netLinker.EXPECT().RuleList(netlink.FamilyAll).
Return(testCase.ruleList.rules, testCase.ruleList.err)
@@ -239,7 +222,6 @@ func Test_Routing_deleteIPRule(t *testing.T) {
}
r := Routing{
logger: logger,
netLinker: netLinker,
}
@@ -256,49 +238,6 @@ func Test_Routing_deleteIPRule(t *testing.T) {
}
}
func Test_ruleDbgMsg(t *testing.T) {
t.Parallel()
testCases := map[string]struct {
add bool
src netip.Prefix
dst netip.Prefix
table int
priority int
dbgMsg string
}{
"default values": {
dbgMsg: "ip rule del pref 0",
},
"add rule": {
add: true,
src: makeNetipPrefix(1),
dst: makeNetipPrefix(2),
table: 100,
priority: 101,
dbgMsg: "ip rule add from 1.1.1.0/24 to 2.2.2.0/24 lookup 100 pref 101",
},
"del rule": {
src: makeNetipPrefix(1),
dst: makeNetipPrefix(2),
table: 100,
priority: 101,
dbgMsg: "ip rule del from 1.1.1.0/24 to 2.2.2.0/24 lookup 100 pref 101",
},
}
for name, testCase := range testCases {
t.Run(name, func(t *testing.T) {
t.Parallel()
dbgMsg := ruleDbgMsg(testCase.add, testCase.src,
testCase.dst, testCase.table, testCase.priority)
assert.Equal(t, testCase.dbgMsg, dbgMsg)
})
}
}
func Test_rulesAreEqual(t *testing.T) {
t.Parallel()