util.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. _, err := spawnWithExpectLines(args, xs...)
  40. return err
  41. }
  42. func spawnWithExpectLines(args []string, xs ...string) ([]string, error) {
  43. proc, err := spawnCmd(args)
  44. if err != nil {
  45. return nil, err
  46. }
  47. // process until either stdout or stderr contains
  48. // the expected string
  49. var (
  50. lines []string
  51. lineFunc = func(txt string) bool { return true }
  52. )
  53. for _, txt := range xs {
  54. for {
  55. l, lerr := proc.ExpectFunc(lineFunc)
  56. if lerr != nil {
  57. proc.Close()
  58. return nil, fmt.Errorf("%v (expected %q, got %q)", lerr, txt, lines)
  59. }
  60. lines = append(lines, l)
  61. if strings.Contains(l, txt) {
  62. break
  63. }
  64. }
  65. }
  66. perr := proc.Close()
  67. if len(xs) == 0 && proc.LineCount() != noOutputLineCount { // expect no output
  68. return nil, fmt.Errorf("unexpected output (got lines %q, line count %d)", lines, proc.LineCount())
  69. }
  70. return lines, perr
  71. }
  72. func closeWithTimeout(p *expect.ExpectProcess, d time.Duration) error {
  73. errc := make(chan error, 1)
  74. go func() { errc <- p.Close() }()
  75. select {
  76. case err := <-errc:
  77. return err
  78. case <-time.After(d):
  79. p.Stop()
  80. // retry close after stopping to collect SIGQUIT data, if any
  81. closeWithTimeout(p, time.Second)
  82. }
  83. return fmt.Errorf("took longer than %v to Close process %+v", d, p)
  84. }
  85. func toTLS(s string) string {
  86. return strings.Replace(s, "http://", "https://", 1)
  87. }