-
Notifications
You must be signed in to change notification settings - Fork 451
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: bobz965 <[email protected]>
- Loading branch information
Showing
3 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |