utils.go 1.6 KB

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