expect_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. // build !windows
  15. package expect
  16. import (
  17. "os"
  18. "testing"
  19. "time"
  20. )
  21. func TestExpectFunc(t *testing.T) {
  22. ep, err := NewExpect("/bin/echo", "hello world")
  23. if err != nil {
  24. t.Fatal(err)
  25. }
  26. wstr := "hello world\r\n"
  27. l, eerr := ep.ExpectFunc(func(a string) bool { return len(a) > 10 })
  28. if eerr != nil {
  29. t.Fatal(eerr)
  30. }
  31. if l != wstr {
  32. t.Fatalf(`got "%v", expected "%v"`, l, wstr)
  33. }
  34. if cerr := ep.Close(); cerr != nil {
  35. t.Fatal(cerr)
  36. }
  37. }
  38. func TestEcho(t *testing.T) {
  39. ep, err := NewExpect("/bin/echo", "hello world")
  40. if err != nil {
  41. t.Fatal(err)
  42. }
  43. l, eerr := ep.Expect("world")
  44. if eerr != nil {
  45. t.Fatal(eerr)
  46. }
  47. wstr := "hello world"
  48. if l[:len(wstr)] != wstr {
  49. t.Fatalf(`got "%v", expected "%v"`, l, wstr)
  50. }
  51. if cerr := ep.Close(); cerr != nil {
  52. t.Fatal(cerr)
  53. }
  54. if _, eerr = ep.Expect("..."); eerr == nil {
  55. t.Fatalf("expected error on closed expect process")
  56. }
  57. }
  58. func TestLineCount(t *testing.T) {
  59. ep, err := NewExpect("/usr/bin/printf", "1\n2\n3")
  60. if err != nil {
  61. t.Fatal(err)
  62. }
  63. wstr := "3"
  64. l, eerr := ep.Expect(wstr)
  65. if eerr != nil {
  66. t.Fatal(eerr)
  67. }
  68. if l != wstr {
  69. t.Fatalf(`got "%v", expected "%v"`, l, wstr)
  70. }
  71. if ep.LineCount() != 3 {
  72. t.Fatalf("got %d, expected 3", ep.LineCount())
  73. }
  74. if cerr := ep.Close(); cerr != nil {
  75. t.Fatal(cerr)
  76. }
  77. }
  78. func TestSend(t *testing.T) {
  79. ep, err := NewExpect("/usr/bin/tr", "a", "b")
  80. if err != nil {
  81. t.Fatal(err)
  82. }
  83. if err := ep.Send("a\r"); err != nil {
  84. t.Fatal(err)
  85. }
  86. if _, err := ep.Expect("b"); err != nil {
  87. t.Fatal(err)
  88. }
  89. if err := ep.Stop(); err != nil {
  90. t.Fatal(err)
  91. }
  92. }
  93. func TestSignal(t *testing.T) {
  94. ep, err := NewExpect("/bin/sleep", "100")
  95. if err != nil {
  96. t.Fatal(err)
  97. }
  98. ep.Signal(os.Interrupt)
  99. donec := make(chan struct{})
  100. go func() {
  101. defer close(donec)
  102. werr := "signal: interrupt"
  103. if cerr := ep.Close(); cerr == nil || cerr.Error() != werr {
  104. t.Fatalf("got error %v, wanted error %s", cerr, werr)
  105. }
  106. }()
  107. select {
  108. case <-time.After(5 * time.Second):
  109. t.Fatalf("signal test timed out")
  110. case <-donec:
  111. }
  112. }