Skip to content

Commit

Permalink
Fix IPv6 tests running on unsupported environments
Browse files Browse the repository at this point in the history
  • Loading branch information
pulsastrix committed Mar 8, 2024
1 parent 03aa74b commit 23204c7
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 11 deletions.
6 changes: 6 additions & 0 deletions client/firewall/iptables/router_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ func TestIptablesManager_InsertRoutingRules(t *testing.T) {

for _, testCase := range test.InsertRuleTestCases {
t.Run(testCase.Name, func(t *testing.T) {
if testCase.IsV6 {
t.Skip("Environment does not support IPv6, skipping IPv6 test...")
}
iptablesClient, err := iptables.NewWithProtocol(iptables.ProtocolIPv4)
require.NoError(t, err, "failed to init iptables client")

Expand Down Expand Up @@ -156,6 +159,9 @@ func TestIptablesManager_RemoveRoutingRules(t *testing.T) {

for _, testCase := range test.RemoveRuleTestCases {
t.Run(testCase.Name, func(t *testing.T) {
if testCase.IsV6 {
t.Skip("Environment does not support IPv6, skipping IPv6 test...")
}
iptablesClient, _ := iptables.NewWithProtocol(iptables.ProtocolIPv4)

manager, err := newRouterManager(context.TODO(), iptablesClient)
Expand Down
5 changes: 5 additions & 0 deletions client/firewall/nftables/manager_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package nftables
import (
"context"
"fmt"
"github.com/netbirdio/netbird/client/firewall"
"net"
"net/netip"
"testing"
Expand Down Expand Up @@ -160,6 +161,10 @@ func TestNftablesManager(t *testing.T) {
}

func TestNftablesManager6(t *testing.T) {

if !iface.SupportsIPv6() || !firewall.SupportsIPv6() {
t.Skip("Environment does not support IPv6, skipping IPv6 test...")
}
mock := &iFaceMock{
NameFunc: func() string {
return "lo"
Expand Down
13 changes: 12 additions & 1 deletion client/firewall/nftables/router_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ package nftables

import (
"context"
"github.com/netbirdio/netbird/iface"
"testing"

"github.com/coreos/go-iptables/iptables"
"github.com/google/nftables"
"github.com/google/nftables/expr"
"github.com/stretchr/testify/require"

fw "github.com/netbirdio/netbird/client/firewall"
firewall "github.com/netbirdio/netbird/client/firewall/manager"
"github.com/netbirdio/netbird/client/firewall/test"
)
Expand Down Expand Up @@ -38,6 +40,9 @@ func TestNftablesManager_InsertRoutingRules(t *testing.T) {

for _, testCase := range test.InsertRuleTestCases {
t.Run(testCase.Name, func(t *testing.T) {
if testCase.IsV6 && table6 == nil {
t.Skip("Environment does not support IPv6, skipping IPv6 test...")
}
manager, err := newRouter(context.TODO(), table, table6)
require.NoError(t, err, "failed to create router")

Expand Down Expand Up @@ -145,6 +150,9 @@ func TestNftablesManager_RemoveRoutingRules(t *testing.T) {

for _, testCase := range test.RemoveRuleTestCases {
t.Run(testCase.Name, func(t *testing.T) {
if testCase.IsV6 && table6 == nil {
t.Skip("Environment does not support IPv6, skipping IPv6 test...")
}
manager, err := newRouter(context.TODO(), table, table6)
require.NoError(t, err, "failed to create router")

Expand Down Expand Up @@ -273,7 +281,10 @@ func createWorkTables() (*nftables.Table, *nftables.Table, error) {
}

table := sConn.AddTable(&nftables.Table{Name: tableName, Family: nftables.TableFamilyIPv4})
table6 := sConn.AddTable(&nftables.Table{Name: tableName, Family: nftables.TableFamilyIPv6})
var table6 *nftables.Table
if iface.SupportsIPv6() && fw.SupportsIPv6() {
table6 = sConn.AddTable(&nftables.Table{Name: tableName, Family: nftables.TableFamilyIPv6})
}
err = sConn.Flush()

return table, table6, err
Expand Down
2 changes: 1 addition & 1 deletion client/internal/routemanager/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -823,7 +823,7 @@ func TestManagerUpdateRoutes(t *testing.T) {

v6Addr := ""
//goland:noinspection GoBoolExpressions
if runtime.GOOS != "linux" && testCase.isV6 {
if !iface.SupportsIPv6() && testCase.isV6 {
t.Skip("Platform does not support IPv6, skipping IPv6 test...")
} else if testCase.isV6 {
v6Addr = "2001:db8::4242:4711/128"
Expand Down
7 changes: 4 additions & 3 deletions client/internal/routemanager/systemops_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,12 @@ loop:
}

var is6 bool
if rt.Family == syscall.AF_INET {
switch rt.Family {
case syscall.AF_INET:
is6 = false
} else if rt.Family == syscall.AF_INET6 {
case syscall.AF_INET6:
is6 = true
} else {
default:
continue loop
}

Expand Down
8 changes: 4 additions & 4 deletions client/internal/routemanager/systemops_nonandroid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ package routemanager
import (
"bytes"
"fmt"
"github.com/netbirdio/netbird/client/firewall"
"net"
"net/netip"
"os"
"runtime"
"strings"
"testing"

Expand Down Expand Up @@ -58,7 +58,7 @@ func TestAddRemoveRoutes(t *testing.T) {

v6Addr := ""
//goland:noinspection GoBoolExpressions
if runtime.GOOS != "linux" && testCase.prefix.Addr().Is6() {
if (!iface.SupportsIPv6() || !firewall.SupportsIPv6()) && testCase.prefix.Addr().Is6() {
t.Skip("Platform does not support IPv6, skipping IPv6 test...")
} else if testCase.prefix.Addr().Is6() {
v6Addr = "2001:db8::4242:4711/128"
Expand Down Expand Up @@ -160,7 +160,7 @@ func TestGetExistingRIBRouteGateway(t *testing.T) {

func TestGetExistingRIBRouteGateway6(t *testing.T) {
//goland:noinspection GoBoolExpressions
if runtime.GOOS != "linux" {
if !iface.SupportsIPv6() {
t.Skip("Platform does not support IPv6, skipping IPv6 test...")
}

Expand Down Expand Up @@ -213,7 +213,7 @@ func TestAddExistAndRemoveRouteNonAndroid(t *testing.T) {
}
var defaultGateway6 net.IP
//goland:noinspection GoBoolExpressions
if runtime.GOOS == "linux" {
if iface.SupportsIPv6() && firewall.SupportsIPv6() {
defaultGateway6, err = getExistingRIBRouteGateway(netip.MustParsePrefix("::/0"))
t.Log("defaultGateway6: ", defaultGateway6)
if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions client/system/info_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"context"
"github.com/netbirdio/netbird/client/firewall"
"github.com/netbirdio/netbird/iface"
"github.com/netbirdio/netbird/iface/netstack"
"os"
"os/exec"
"runtime"
Expand Down Expand Up @@ -129,5 +128,5 @@ func sysInfo() (serialNumber string, productName string, manufacturer string) {

func _checkIPv6Support() bool {
return firewall.SupportsIPv6() &&
iface.WireGuardModuleIsLoaded() && !netstack.IsEnabled()
iface.SupportsIPv6()
}
4 changes: 4 additions & 0 deletions iface/iface_android.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,7 @@ func (w *WGIface) CreateOnAndroid(routes []string, dns string, searchDomains []s
func (w *WGIface) Create() error {
return fmt.Errorf("this function has not implemented on this platform")
}

func SupportsIPv6() bool {
return false
}
4 changes: 4 additions & 0 deletions iface/iface_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,7 @@ func NewWGIFace(iFaceName string, address string, address6 string, wgPort int, w
func (w *WGIface) CreateOnAndroid([]string, string, []string) error {
return fmt.Errorf("this function has not implemented on this platform")
}

func SupportsIPv6() bool {
return false
}
4 changes: 4 additions & 0 deletions iface/iface_ios.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,7 @@ func NewWGIFace(iFaceName string, address string, address6 string, wgPort int, w
func (w *WGIface) CreateOnAndroid([]string, string, []string) error {
return fmt.Errorf("this function has not implemented on this platform")
}

func SupportsIPv6() bool {
return false
}
5 changes: 5 additions & 0 deletions iface/iface_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package iface
import (
"fmt"
log "github.com/sirupsen/logrus"
"golang.org/x/net/nettest"

"github.com/pion/transport/v3"

Expand Down Expand Up @@ -59,3 +60,7 @@ func NewWGIFace(iFaceName string, address string, address6 string, wgPort int, w
func (w *WGIface) CreateOnAndroid([]string, string, []string) error {
return fmt.Errorf("this function has not implemented on this platform")
}

func SupportsIPv6() bool {
return nettest.SupportsIPv6() && WireGuardModuleIsLoaded() && !netstack.IsEnabled()
}

0 comments on commit 23204c7

Please sign in to comment.