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])
log.Debugf("checking route: %s", dst)
if err != nil {
return true, fmt.Errorf("unexpected RIB destination: %v", err)
}
mask, err := toIPAddr(m.Addrs[2])
log.Debugf("checking route mask: %s", mask)
if err != nil {
return true, fmt.Errorf("unexpected RIB destination: %v", err)
}
mask, _ := toIPAddr(m.Addrs[2])
cidr, _ := net.IPMask(mask.To4()).Size()
if dst.String() == prefix.Addr().String() && cidr == prefix.Bits() {
return true, nil

View File

@ -1,12 +1,16 @@
package routemanager
import (
"bytes"
"fmt"
"net"
"net/netip"
"os"
"strings"
"testing"
"github.com/pion/transport/v2/stdnet"
log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/require"
"github.com/netbirdio/netbird/iface"
@ -120,6 +124,11 @@ func TestAddExistAndRemoveRoute(t *testing.T) {
MOCK_ADDR := "127.0.0.1"
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) {
// Prepare the environment
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
// In case of already existing route, it should not have been added (but still exist)
ok, err := existsInRouteTable(testCase.prefix)
fmt.Println("Buffer string: ", buf.String())
require.NoError(t, err, "should not return err")
if !strings.Contains(buf.String(), "because it already exists") {
require.False(t, ok, "route should not exist")
}
})
}
}