add tests

This commit is contained in:
Pascal Fischer 2023-06-09 16:18:48 +02:00
parent 3724323f76
commit bc8ee8fc3c
2 changed files with 15 additions and 7 deletions

View File

@ -107,16 +107,11 @@ func existsInRouteTable(prefix netip.Prefix) (bool, error) {
} }
dst, err := toIPAddr(m.Addrs[0]) dst, err := toIPAddr(m.Addrs[0])
log.Debugf("checking route: %s", dst)
if err != nil { if err != nil {
return true, fmt.Errorf("unexpected RIB destination: %v", err) return true, fmt.Errorf("unexpected RIB destination: %v", err)
} }
mask, err := toIPAddr(m.Addrs[2]) mask, _ := toIPAddr(m.Addrs[2])
log.Debugf("checking route mask: %s", mask)
if err != nil {
return true, fmt.Errorf("unexpected RIB destination: %v", err)
}
cidr, _ := net.IPMask(mask.To4()).Size() cidr, _ := net.IPMask(mask.To4()).Size()
if dst.String() == prefix.Addr().String() && cidr == prefix.Bits() { if dst.String() == prefix.Addr().String() && cidr == prefix.Bits() {
return true, nil return true, nil

View File

@ -1,12 +1,16 @@
package routemanager package routemanager
import ( import (
"bytes"
"fmt" "fmt"
"net" "net"
"net/netip" "net/netip"
"os"
"strings"
"testing" "testing"
"github.com/pion/transport/v2/stdnet" "github.com/pion/transport/v2/stdnet"
log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"github.com/netbirdio/netbird/iface" "github.com/netbirdio/netbird/iface"
@ -120,6 +124,11 @@ func TestAddExistAndRemoveRoute(t *testing.T) {
MOCK_ADDR := "127.0.0.1" MOCK_ADDR := "127.0.0.1"
for _, testCase := range testCases { for _, testCase := range testCases {
var buf bytes.Buffer
log.SetOutput(&buf)
defer func() {
log.SetOutput(os.Stderr)
}()
t.Run(testCase.name, func(t *testing.T) { t.Run(testCase.name, func(t *testing.T) {
// Prepare the environment // Prepare the environment
if testCase.preExistingPrefix.IsValid() { if testCase.preExistingPrefix.IsValid() {
@ -143,9 +152,13 @@ func TestAddExistAndRemoveRoute(t *testing.T) {
} }
// route should either not have been added or should have been removed // route should either not have been added or should have been removed
// In case of already existing route, it should not have been added (but still exist)
ok, err := existsInRouteTable(testCase.prefix) ok, err := existsInRouteTable(testCase.prefix)
fmt.Println("Buffer string: ", buf.String())
require.NoError(t, err, "should not return err") require.NoError(t, err, "should not return err")
require.False(t, ok, "route should not exist") if !strings.Contains(buf.String(), "because it already exists") {
require.False(t, ok, "route should not exist")
}
}) })
} }
} }