expect.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // Copyright 2016 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 expect implements a small expect-style interface
  15. package expect
  16. import (
  17. "bufio"
  18. "io"
  19. "os"
  20. "os/exec"
  21. "strings"
  22. "sync"
  23. "github.com/kr/pty"
  24. )
  25. type ExpectProcess struct {
  26. cmd *exec.Cmd
  27. fpty *os.File
  28. wg sync.WaitGroup
  29. ptyMu sync.Mutex // protects accessing fpty
  30. cond *sync.Cond // for broadcasting updates are available
  31. mu sync.Mutex // protects lines and err
  32. lines []string
  33. count int // increment whenever new line gets added
  34. err error
  35. }
  36. // NewExpect creates a new process for expect testing.
  37. func NewExpect(name string, arg ...string) (ep *ExpectProcess, err error) {
  38. ep = &ExpectProcess{cmd: exec.Command(name, arg...)}
  39. ep.cond = sync.NewCond(&ep.mu)
  40. ep.cmd.Stderr = ep.cmd.Stdout
  41. ep.cmd.Stdin = nil
  42. if ep.fpty, err = pty.Start(ep.cmd); err != nil {
  43. return nil, err
  44. }
  45. ep.wg.Add(1)
  46. go ep.read()
  47. return ep, nil
  48. }
  49. func (ep *ExpectProcess) read() {
  50. defer ep.wg.Done()
  51. r := bufio.NewReader(ep.fpty)
  52. for ep.err == nil {
  53. ep.ptyMu.Lock()
  54. l, rerr := r.ReadString('\n')
  55. ep.ptyMu.Unlock()
  56. ep.mu.Lock()
  57. ep.err = rerr
  58. if l != "" {
  59. ep.lines = append(ep.lines, l)
  60. ep.count++
  61. if len(ep.lines) == 1 {
  62. ep.cond.Signal()
  63. }
  64. }
  65. ep.mu.Unlock()
  66. }
  67. ep.cond.Signal()
  68. }
  69. // ExpectFunc returns the first line satisfying the function f.
  70. func (ep *ExpectProcess) ExpectFunc(f func(string) bool) (string, error) {
  71. ep.mu.Lock()
  72. for {
  73. for len(ep.lines) == 0 && ep.err == nil {
  74. ep.cond.Wait()
  75. }
  76. if len(ep.lines) == 0 {
  77. break
  78. }
  79. l := ep.lines[0]
  80. ep.lines = ep.lines[1:]
  81. if f(l) {
  82. ep.mu.Unlock()
  83. return l, nil
  84. }
  85. }
  86. ep.mu.Unlock()
  87. return "", ep.err
  88. }
  89. // Expect returns the first line containing the given string.
  90. func (ep *ExpectProcess) Expect(s string) (string, error) {
  91. return ep.ExpectFunc(func(txt string) bool { return strings.Contains(txt, s) })
  92. }
  93. // LineCount returns the number of recorded lines since
  94. // the beginning of the process.
  95. func (ep *ExpectProcess) LineCount() int {
  96. ep.mu.Lock()
  97. defer ep.mu.Unlock()
  98. return ep.count
  99. }
  100. // Stop kills the expect process and waits for it to exit.
  101. func (ep *ExpectProcess) Stop() error { return ep.close(true) }
  102. // Signal sends a signal to the expect process
  103. func (ep *ExpectProcess) Signal(sig os.Signal) error {
  104. return ep.cmd.Process.Signal(sig)
  105. }
  106. // Close waits for the expect process to exit.
  107. func (ep *ExpectProcess) Close() error { return ep.close(false) }
  108. func (ep *ExpectProcess) close(kill bool) error {
  109. if ep.cmd == nil {
  110. return ep.err
  111. }
  112. if kill {
  113. ep.cmd.Process.Kill()
  114. }
  115. err := ep.cmd.Wait()
  116. ep.ptyMu.Lock()
  117. ep.fpty.Close()
  118. ep.ptyMu.Unlock()
  119. ep.wg.Wait()
  120. if err != nil {
  121. ep.err = err
  122. if !kill && strings.Contains(err.Error(), "exit status") {
  123. // non-zero exit code
  124. err = nil
  125. } else if kill && strings.Contains(err.Error(), "signal:") {
  126. err = nil
  127. }
  128. }
  129. ep.cmd = nil
  130. return err
  131. }
  132. func (ep *ExpectProcess) Send(command string) error {
  133. _, err := io.WriteString(ep.fpty, command)
  134. return err
  135. }