ing
This commit is contained in:
parent
4b9311adb7
commit
9870f6e51f
|
@ -1 +1,23 @@
|
|||
package ping
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os/exec"
|
||||
)
|
||||
|
||||
func Ping(destination string, options *PingOptions) (*PingResult, error) {
|
||||
options.Validate()
|
||||
|
||||
params := make([]string, 0)
|
||||
params = append(params, destination)
|
||||
params = append(params, fmt.Sprintf("-c %d", options.Retry))
|
||||
params = append(params, fmt.Sprintf("-i %d", options.Interval))
|
||||
|
||||
pCmd := exec.Command("ping", params...)
|
||||
output, err := pCmd.CombinedOutput()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return parseDarwinPing(output)
|
||||
}
|
||||
|
|
41
net/ping/ping_darwin_test.go
Normal file
41
net/ping/ping_darwin_test.go
Normal file
|
@ -0,0 +1,41 @@
|
|||
package ping
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestPing(t *testing.T) {
|
||||
type args struct {
|
||||
destination string
|
||||
options *PingOptions
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want *PingResult
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "192.168.1.1",
|
||||
args: args{
|
||||
destination: "192.168.1.1",
|
||||
options: &PingOptions{
|
||||
Retry: 4,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, err := Ping(tt.args.destination, tt.args.options)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("Ping() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
if !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("Ping() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user