...

Source file src/go.etcd.io/etcd/pkg/v3/expect/expect_test.go

Documentation: go.etcd.io/etcd/pkg/v3/expect

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

View as plain text