github.com/lfch/etcd-io/tests/v3@v3.0.0-20221004140520-eac99acd3e9d/functional/tester/utils.go (about) 1 // Copyright 2018 The etcd 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 tester 16 17 import ( 18 "fmt" 19 "math/rand" 20 "net" 21 "net/url" 22 "strings" 23 ) 24 25 func isValidURL(u string) bool { 26 _, err := url.Parse(u) 27 return err == nil 28 } 29 30 func getPort(addr string) (port string, err error) { 31 urlAddr, err := url.Parse(addr) 32 if err != nil { 33 return "", err 34 } 35 _, port, err = net.SplitHostPort(urlAddr.Host) 36 if err != nil { 37 return "", err 38 } 39 return port, nil 40 } 41 42 func getSameValue(vals map[string]int64) bool { 43 var rv int64 44 for _, v := range vals { 45 if rv == 0 { 46 rv = v 47 } 48 if rv != v { 49 return false 50 } 51 } 52 return true 53 } 54 55 func max(n1, n2 int64) int64 { 56 if n1 > n2 { 57 return n1 58 } 59 return n2 60 } 61 62 func errsToError(errs []error) error { 63 if len(errs) == 0 { 64 return nil 65 } 66 stringArr := make([]string, len(errs)) 67 for i, err := range errs { 68 stringArr[i] = err.Error() 69 } 70 return fmt.Errorf(strings.Join(stringArr, ", ")) 71 } 72 73 func randBytes(size int) []byte { 74 data := make([]byte, size) 75 for i := 0; i < size; i++ { 76 data[i] = byte(int('a') + rand.Intn(26)) 77 } 78 return data 79 }