Skip to content

Commit

Permalink
fix(ping): fix fallback for ping
Browse files Browse the repository at this point in the history
Signed-off-by: Yagiz Degirmenci <yagizcanilbey1903@gmail.com>
  • Loading branch information
ycd committed Nov 22, 2021
1 parent f9b2120 commit 1ce9bf2
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
16 changes: 8 additions & 8 deletions pkg/ping/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ func runPing(ctx context.Context, wg *sync.WaitGroup, addr common.Address, count
}
err = pinger.Run()
if err != nil {
if out, err := runPingFallback(ctx, addr, count, timeout); err == nil {
if out, err := runPingFallback(ctx, addr, count); err == nil {
output = out.String()
} else {
return fmt.Errorf("failed to run ping: %v", err.Error())
}
} else {
stats := pinger.Statistics()
if stats.PacketsRecv == 0 {
if out, err := runPingFallback(ctx, addr, count, timeout); err == nil {
if out, err := runPingFallback(ctx, addr, count); err == nil {
output = out.String()
} else {
output = "no response"
Expand All @@ -63,11 +63,11 @@ func runPing(ctx context.Context, wg *sync.WaitGroup, addr common.Address, count

// runPingFallback executes the ping command from cli
// Currently fallback is not implemented for windows.
func runPingFallback(ctx context.Context, addr common.Address, count int, timeout int) (common.Output, error) {
args := fmt.Sprintf("-c %v -t %v", count, timeout)
func runPingFallback(ctx context.Context, addr common.Address, count int) (common.Output, error) {
args := fmt.Sprintf("-c %v", count)
command := fmt.Sprintf("ping %s %s", args, addr.String())

out, err := executeCommand("bash", command)
out, err := executeCommand(command)
if err != nil {
return common.Output(""), err
}
Expand All @@ -80,15 +80,15 @@ func runPingFallback(ctx context.Context, addr common.Address, count int, timeou
return common.Output(po.AvgRTT + "ms"), nil
}

func executeCommand(shell, command string) (string, error) {
func executeCommand(command string) (string, error) {
var errb bytes.Buffer
var out string

var cmd *exec.Cmd
if runtime.GOOS == "windows" {
cmd = exec.Command("cmd", "/C", command)
} else {
cmd = exec.Command(shell, "-c", command)
cmd = exec.Command("/bin/bash", "-c", command)
}
cmd.Stderr = &errb
stdout, err := cmd.StdoutPipe()
Expand Down Expand Up @@ -148,7 +148,7 @@ func parsePingOutput(out string) (pingOutput, error) {
case strings.Contains(line, "packets transmitted"):
arr := strings.Split(line, ",")
fmt.Println(arr)
if len(arr) != 3 {
if len(arr) < 3 {
continue
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/ping/ping_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build integration
//go:build integration || darwin

package ping

Expand All @@ -10,7 +10,7 @@ import (
)

func TestPingFallback(t *testing.T) {
out, err := runPingFallback(context.Background(), common.Address("8.8.8.8"), 3, 10)
out, err := runPingFallback(context.Background(), common.Address("8.8.8.8"), 3)
if err != nil {
t.Fatal(err.Error())
}
Expand Down

0 comments on commit 1ce9bf2

Please sign in to comment.