github.phpd.cn/cilium/cilium@v1.6.12/test/helpers/wrappers.go (about) 1 // Copyright 2017 Authors of Cilium 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 helpers 16 17 import "fmt" 18 19 // PerfTest represents a type of test to run when running `netperf`. 20 type PerfTest string 21 22 const ( 23 // TCP_RR represents a netperf test for TCP Request/Response performance. 24 // For more information, consult : http://www.cs.kent.edu/~farrell/dist/ref/Netperf.html 25 TCP_RR = PerfTest("TCP_RR") 26 27 // UDP_RR represents a netperf test for UDP Request/Response performance. 28 // For more information, consult : http://www.cs.kent.edu/~farrell/dist/ref/Netperf.html 29 UDP_RR = PerfTest("UDP_RR") 30 ) 31 32 // Ping returns the string representing the ping command to ping the specified 33 // endpoint. 34 func Ping(endpoint string) string { 35 return fmt.Sprintf("ping -W 2 -c %d %s", PingCount, endpoint) 36 } 37 38 // Ping6 returns the string representing the ping6 command to ping6 the 39 // specified endpoint. 40 func Ping6(endpoint string) string { 41 return fmt.Sprintf("ping6 -c %d %s", PingCount, endpoint) 42 } 43 44 // CurlFail returns the string representing the curl command with `-s` and 45 // `--fail` options enabled to curl the specified endpoint. It takes a 46 // variadic optinalValues argument. This is passed on to fmt.Sprintf() and uses 47 // into the curl message 48 func CurlFail(endpoint string, optionalValues ...interface{}) string { 49 statsInfo := `time-> DNS: '%{time_namelookup}(%{remote_ip})', Connect: '%{time_connect}',` + 50 `Transfer '%{time_starttransfer}', total '%{time_total}'` 51 52 if len(optionalValues) > 0 { 53 endpoint = fmt.Sprintf(endpoint, optionalValues...) 54 } 55 return fmt.Sprintf( 56 `curl --path-as-is -s -D /dev/stderr --fail --connect-timeout %[1]d --max-time %[2]d %[3]s -w "%[4]s"`, 57 CurlConnectTimeout, CurlMaxTimeout, endpoint, statsInfo) 58 } 59 60 // CurlWithHTTPCode retunrs the string representation of the curl command which 61 // only outputs the HTTP code returned by its execution against the specified 62 // endpoint. It takes a variadic optinalValues argument. This is passed on to 63 // fmt.Sprintf() and uses into the curl message 64 func CurlWithHTTPCode(endpoint string, optionalValues ...interface{}) string { 65 if len(optionalValues) > 0 { 66 endpoint = fmt.Sprintf(endpoint, optionalValues...) 67 } 68 69 return fmt.Sprintf( 70 `curl --path-as-is -s -D /dev/stderr --output /dev/stderr -w '%%{http_code}' --connect-timeout %d %s`, 71 CurlConnectTimeout, endpoint) 72 } 73 74 // Netperf returns the string representing the netperf command to use when testing 75 // connectivity between endpoints. 76 func Netperf(endpoint string, perfTest PerfTest) string { 77 return fmt.Sprintf("netperf -l 3 -t %s -H %s", perfTest, endpoint) 78 } 79 80 // Netcat returns the string representing the netcat command to the specified 81 // endpoint. It takes a variadic optionalValues arguments, This is passed to 82 // fmt.Sprintf uses in the netcat message 83 func Netcat(endpoint string, optionalValues ...interface{}) string { 84 if len(optionalValues) > 0 { 85 endpoint = fmt.Sprintf(endpoint, optionalValues...) 86 } 87 return fmt.Sprintf("nc -w 4 %s", endpoint) 88 }