util.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // Copyright 2017 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 e2e
  15. import (
  16. "encoding/json"
  17. "fmt"
  18. "math/rand"
  19. "strings"
  20. "time"
  21. "go.etcd.io/etcd/pkg/expect"
  22. )
  23. func waitReadyExpectProc(exproc *expect.ExpectProcess, readyStrs []string) error {
  24. c := 0
  25. matchSet := func(l string) bool {
  26. for _, s := range readyStrs {
  27. if strings.Contains(l, s) {
  28. c++
  29. break
  30. }
  31. }
  32. return c == len(readyStrs)
  33. }
  34. _, err := exproc.ExpectFunc(matchSet)
  35. return err
  36. }
  37. func spawnWithExpect(args []string, expected string) error {
  38. return spawnWithExpects(args, []string{expected}...)
  39. }
  40. func spawnWithExpects(args []string, xs ...string) error {
  41. _, err := spawnWithExpectLines(args, xs...)
  42. return err
  43. }
  44. func spawnWithExpectLines(args []string, xs ...string) ([]string, error) {
  45. proc, err := spawnCmd(args)
  46. if err != nil {
  47. return nil, err
  48. }
  49. // process until either stdout or stderr contains
  50. // the expected string
  51. var (
  52. lines []string
  53. lineFunc = func(txt string) bool { return true }
  54. )
  55. for _, txt := range xs {
  56. for {
  57. l, lerr := proc.ExpectFunc(lineFunc)
  58. if lerr != nil {
  59. proc.Close()
  60. return nil, fmt.Errorf("%v (expected %q, got %q)", lerr, txt, lines)
  61. }
  62. lines = append(lines, l)
  63. if strings.Contains(l, txt) {
  64. break
  65. }
  66. }
  67. }
  68. perr := proc.Close()
  69. if len(xs) == 0 && proc.LineCount() != noOutputLineCount { // expect no output
  70. return nil, fmt.Errorf("unexpected output (got lines %q, line count %d)", lines, proc.LineCount())
  71. }
  72. return lines, perr
  73. }
  74. func randomLeaseID() int64 {
  75. return rand.New(rand.NewSource(time.Now().UnixNano())).Int63()
  76. }
  77. func dataMarshal(data interface{}) (d string, e error) {
  78. m, err := json.Marshal(data)
  79. if err != nil {
  80. return "", err
  81. }
  82. return string(m), nil
  83. }
  84. func closeWithTimeout(p *expect.ExpectProcess, d time.Duration) error {
  85. errc := make(chan error, 1)
  86. go func() { errc <- p.Close() }()
  87. select {
  88. case err := <-errc:
  89. return err
  90. case <-time.After(d):
  91. p.Stop()
  92. // retry close after stopping to collect SIGQUIT data, if any
  93. closeWithTimeout(p, time.Second)
  94. }
  95. return fmt.Errorf("took longer than %v to Close process %+v", d, p)
  96. }
  97. func toTLS(s string) string {
  98. return strings.Replace(s, "http://", "https://", 1)
  99. }