Skip to content

Commit

Permalink
add ut
Browse files Browse the repository at this point in the history
Signed-off-by: bobz965 <[email protected]>
  • Loading branch information
bobz965 committed Sep 14, 2024
1 parent 544f2ad commit 0881cc8
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pkg/util/arp_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package util

import (
"errors"
"net"
"strings"
"testing"
Expand Down Expand Up @@ -56,6 +57,9 @@ func TestMacEqual(t *testing.T) {
func TestArpResolve(t *testing.T) {
// get the default route gw and nic
routes, err := netlink.RouteList(nil, unix.AF_UNSPEC)
if errors.Is(err, netlink.ErrNotImplemented) {
return // skip if not implemented
}
if err != nil {
t.Fatalf("failed to get routes: %v", err)
}
Expand Down
38 changes: 38 additions & 0 deletions pkg/util/klog_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package util

import (
"bytes"
"errors"
"os"
"os/exec"
"strings"
"testing"
)

func TestLogFatalAndExit(t *testing.T) {
expectedMessage := "An error occurred: test error"

cmd := exec.Command(os.Args[0], "-test.run=TestHelperProcess")
cmd.Env = append(os.Environ(), "GO_WANT_HELPER_PROCESS=1")
cmd.Stderr = &bytes.Buffer{}
err := cmd.Run()

if exitError, ok := err.(*exec.ExitError); ok {
if exitError.ExitCode() != 1 {
t.Fatalf("expected exit code 1, got %d", exitError.ExitCode())
}
if !strings.Contains(cmd.Stderr.(*bytes.Buffer).String(), expectedMessage) {
t.Fatalf("expected error message %q, got %q", expectedMessage, cmd.Stderr.(*bytes.Buffer).String())
}
} else {
t.Fatalf("expected an exit error, got %v", err)
}
}

func TestHelperProcess(*testing.T) {
if os.Getenv("GO_WANT_HELPER_PROCESS") != "1" {
return
}
err := errors.New("test error")
LogFatalAndExit(err, "An error occurred: %s", err.Error())
}
50 changes: 50 additions & 0 deletions pkg/util/link_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package util

import (
"errors"
"strings"
"testing"

"github.com/stretchr/testify/require"
"github.com/vishvananda/netlink"
"golang.org/x/sys/unix"
)

func TestSetLinkUp(t *testing.T) {
// 1. should failed
linkName := "abc"
err := SetLinkUp(linkName)
require.Error(t, err)

// 2. should ok
// get the default route gw and nic
routes, err := netlink.RouteList(nil, unix.AF_UNSPEC)
if errors.Is(err, netlink.ErrNotImplemented) {
return // skip if not implemented
}
if err != nil {
t.Fatalf("failed to get routes: %v", err)
}
var nicIndex int
for _, r := range routes {
if r.Dst != nil && r.Dst.IP.String() == "0.0.0.0" {
nicIndex = r.LinkIndex
}
}
if nicIndex == 0 {
t.Fatalf("failed to get nic")
}

link, err := netlink.LinkByIndex(nicIndex)
if err != nil {
t.Fatalf("failed to get link: %v", err)
}
linkName = link.Attrs().Name
if !strings.HasPrefix(linkName, "e") {
// default gw nic should be ethernet
t.Fatalf("invalid default gw nic link name: %s", linkName)
}

err = SetLinkUp(linkName)
require.NoError(t, err)
}

0 comments on commit 0881cc8

Please sign in to comment.