mirror of
https://github.com/qdm12/gluetun.git
synced 2026-06-15 16:04:08 +02:00
chore(all): replace netlink library for more flexibility (#3107)
This commit is contained in:
@@ -6,7 +6,6 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/golang/mock/gomock"
|
||||
"github.com/qdm12/gluetun/internal/netlink"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
@@ -20,21 +19,21 @@ func Test_Wireguard_addAddresses(t *testing.T) {
|
||||
errDummy := errors.New("dummy")
|
||||
|
||||
testCases := map[string]struct {
|
||||
link netlink.Link
|
||||
linkIndex uint32
|
||||
addrs []netip.Prefix
|
||||
wgBuilder func(ctrl *gomock.Controller, link netlink.Link) *Wireguard
|
||||
wgBuilder func(ctrl *gomock.Controller, linkIndex uint32) *Wireguard
|
||||
err error
|
||||
}{
|
||||
"success": {
|
||||
link: netlink.Link{Type: "wireguard"},
|
||||
addrs: []netip.Prefix{ipNetOne, ipNetTwo},
|
||||
wgBuilder: func(ctrl *gomock.Controller, link netlink.Link) *Wireguard {
|
||||
linkIndex: 1,
|
||||
addrs: []netip.Prefix{ipNetOne, ipNetTwo},
|
||||
wgBuilder: func(ctrl *gomock.Controller, linkIndex uint32) *Wireguard {
|
||||
netLinker := NewMockNetLinker(ctrl)
|
||||
firstCall := netLinker.EXPECT().
|
||||
AddrReplace(link, netlink.Addr{Network: ipNetOne}).
|
||||
AddrReplace(linkIndex, ipNetOne).
|
||||
Return(nil)
|
||||
netLinker.EXPECT().
|
||||
AddrReplace(link, netlink.Addr{Network: ipNetTwo}).
|
||||
AddrReplace(linkIndex, ipNetTwo).
|
||||
Return(nil).After(firstCall)
|
||||
return &Wireguard{
|
||||
netlink: netLinker,
|
||||
@@ -45,12 +44,12 @@ func Test_Wireguard_addAddresses(t *testing.T) {
|
||||
},
|
||||
},
|
||||
"first add error": {
|
||||
link: netlink.Link{Type: "wireguard", Name: "a_bridge"},
|
||||
addrs: []netip.Prefix{ipNetOne, ipNetTwo},
|
||||
wgBuilder: func(ctrl *gomock.Controller, link netlink.Link) *Wireguard {
|
||||
linkIndex: 1,
|
||||
addrs: []netip.Prefix{ipNetOne, ipNetTwo},
|
||||
wgBuilder: func(ctrl *gomock.Controller, linkIndex uint32) *Wireguard {
|
||||
netLinker := NewMockNetLinker(ctrl)
|
||||
netLinker.EXPECT().
|
||||
AddrReplace(link, netlink.Addr{Network: ipNetOne}).
|
||||
AddrReplace(linkIndex, ipNetOne).
|
||||
Return(errDummy)
|
||||
return &Wireguard{
|
||||
netlink: netLinker,
|
||||
@@ -59,18 +58,18 @@ func Test_Wireguard_addAddresses(t *testing.T) {
|
||||
},
|
||||
}
|
||||
},
|
||||
err: errors.New("dummy: when adding address 1.2.3.4/32 to link a_bridge"),
|
||||
err: errors.New("dummy: when adding address 1.2.3.4/32 to link with index 1"),
|
||||
},
|
||||
"second add error": {
|
||||
link: netlink.Link{Type: "wireguard", Name: "a_bridge"},
|
||||
addrs: []netip.Prefix{ipNetOne, ipNetTwo},
|
||||
wgBuilder: func(ctrl *gomock.Controller, link netlink.Link) *Wireguard {
|
||||
linkIndex: 1,
|
||||
addrs: []netip.Prefix{ipNetOne, ipNetTwo},
|
||||
wgBuilder: func(ctrl *gomock.Controller, linkIndex uint32) *Wireguard {
|
||||
netLinker := NewMockNetLinker(ctrl)
|
||||
firstCall := netLinker.EXPECT().
|
||||
AddrReplace(link, netlink.Addr{Network: ipNetOne}).
|
||||
AddrReplace(linkIndex, ipNetOne).
|
||||
Return(nil)
|
||||
netLinker.EXPECT().
|
||||
AddrReplace(link, netlink.Addr{Network: ipNetTwo}).
|
||||
AddrReplace(linkIndex, ipNetTwo).
|
||||
Return(errDummy).After(firstCall)
|
||||
return &Wireguard{
|
||||
netlink: netLinker,
|
||||
@@ -79,11 +78,11 @@ func Test_Wireguard_addAddresses(t *testing.T) {
|
||||
},
|
||||
}
|
||||
},
|
||||
err: errors.New("dummy: when adding address ::1234/64 to link a_bridge"),
|
||||
err: errors.New("dummy: when adding address ::1234/64 to link with index 1"),
|
||||
},
|
||||
"ignore IPv6": {
|
||||
addrs: []netip.Prefix{ipNetTwo},
|
||||
wgBuilder: func(_ *gomock.Controller, _ netlink.Link) *Wireguard {
|
||||
wgBuilder: func(_ *gomock.Controller, _ uint32) *Wireguard {
|
||||
return &Wireguard{
|
||||
settings: Settings{
|
||||
IPv6: ptrTo(false),
|
||||
@@ -98,9 +97,9 @@ func Test_Wireguard_addAddresses(t *testing.T) {
|
||||
t.Parallel()
|
||||
ctrl := gomock.NewController(t)
|
||||
|
||||
wg := testCase.wgBuilder(ctrl, testCase.link)
|
||||
wg := testCase.wgBuilder(ctrl, testCase.linkIndex)
|
||||
|
||||
err := wg.addAddresses(testCase.link, testCase.addrs)
|
||||
err := wg.addAddresses(testCase.linkIndex, testCase.addrs)
|
||||
|
||||
if testCase.err != nil {
|
||||
require.Error(t, err)
|
||||
|
||||
Reference in New Issue
Block a user