util.go 2.3 KB

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