Debian Codegraph @

pkg/testutils/ping.go

raw symbols SCIP
1 // Copyright 2017 CNI authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package testutilsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/testutils`/
16
17 import (
18 "bytesscip-go gomod github.com/golang/go/src go1.24.2 bytes/"
19 "fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/"
20 "netscip-go gomod github.com/golang/go/src go1.24.2 net/"
21 "os/execscip-go gomod github.com/golang/go/src go1.24.2 `os/exec`/"
22 "strconvscip-go gomod github.com/golang/go/src go1.24.2 strconv/"
23 "syscallscip-go gomod github.com/golang/go/src go1.24.2 syscall/"
24 )
25
26 // Ping shells out to the `ping` command. Returns nil if successful.
27 func Pingscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/testutils`/Ping().(saddr, daddr string, timeoutSec int) error {
28 ip := netscip-go gomod github.com/golang/go/src go1.24.2 net/.ParseIPscip-go gomod github.com/golang/go/src go1.24.2 net/ParseIP().(saddr)
29 if ip == nil {
30 return fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("failed to parse IP %q", saddr)
31 }
32
33 bin := "ping6"
34 if ip.To4scip-go gomod github.com/golang/go/src go1.24.2 net/IP#To4().() != nil {
35 bin = "ping"
36 }
37
38 args := []string{
39 "-c", "1",
40 "-W", strconvscip-go gomod github.com/golang/go/src go1.24.2 strconv/.Itoascip-go gomod github.com/golang/go/src go1.24.2 strconv/Itoa().(timeoutSec),
41 "-I", saddr,
42 daddr,
43 }
44
45 cmd := execscip-go gomod github.com/golang/go/src go1.24.2 `os/exec`/.Commandscip-go gomod github.com/golang/go/src go1.24.2 `os/exec`/Command().(bin, args...)
46 var stderr bytesscip-go gomod github.com/golang/go/src go1.24.2 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.24.2 bytes/Buffer#
47 cmd.Stderrscip-go gomod github.com/golang/go/src go1.24.2 `os/exec`/Cmd#Stderr. = &stderr
48
49 if err := cmd.Runscip-go gomod github.com/golang/go/src go1.24.2 `os/exec`/Cmd#Run().(); err != nil {
50 switch e := err.(type) {
51 case *execscip-go gomod github.com/golang/go/src go1.24.2 `os/exec`/.ExitErrorscip-go gomod github.com/golang/go/src go1.24.2 `os/exec`/ExitError#:
52 return fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("%v exit status %d: %s",
53 args, e.Sysscip-go gomod github.com/golang/go/src go1.24.2 os/ProcessState#Sys().().(syscallscip-go gomod github.com/golang/go/src go1.24.2 syscall/.WaitStatusscip-go gomod github.com/golang/go/src go1.24.2 syscall/WaitStatus#).ExitStatusscip-go gomod github.com/golang/go/src go1.24.2 syscall/WaitStatus#ExitStatus().(),
54 stderr.Stringscip-go gomod github.com/golang/go/src go1.24.2 bytes/Buffer#String().())
55 default:
56 return err
57 }
58 }
59
60 return nil
61 }