Merge pull request #4909 from heyitsanthony/pkg-expect

e2e: replace gexpect with simpler expect
release-3.0
Anthony Romano 2016-03-30 15:36:41 -07:00
commit 93c3f920ca
16 changed files with 203 additions and 576 deletions

4
Godeps/Godeps.json generated
View File

@ -37,10 +37,6 @@
"Comment": "1.2.0-183-gb5232bb",
"Rev": "b5232bb2934f606f9f27a1305f1eea224e8e8b88"
},
{
"ImportPath": "github.com/coreos/gexpect",
"Rev": "5173270e159f5aa8fbc999dc7e3dcb50f4098a69"
},
{
"ImportPath": "github.com/coreos/go-semver/semver",
"Rev": "568e959cd89871e61434c1143528d9162da89ef2"

View File

@ -23,9 +23,9 @@ import (
"strings"
"testing"
"github.com/coreos/etcd/pkg/expect"
"github.com/coreos/etcd/pkg/fileutil"
"github.com/coreos/etcd/pkg/testutil"
"github.com/coreos/gexpect"
)
const (
@ -190,15 +190,15 @@ func cURLPrefixArgsUseTLS(clus *etcdProcessCluster, key string) []string {
func cURLPut(clus *etcdProcessCluster, key, val, expected string) error {
args := append(cURLPrefixArgs(clus, key), "-XPUT", "-d", "value="+val)
return spawnWithExpectedString(args, expected)
return spawnWithExpect(args, expected)
}
func cURLGet(clus *etcdProcessCluster, key, expected string) error {
return spawnWithExpectedString(cURLPrefixArgs(clus, key), expected)
return spawnWithExpect(cURLPrefixArgs(clus, key), expected)
}
func cURLGetUseTLS(clus *etcdProcessCluster, key, expected string) error {
return spawnWithExpectedString(cURLPrefixArgsUseTLS(clus, key), expected)
return spawnWithExpect(cURLPrefixArgsUseTLS(clus, key), expected)
}
type etcdProcessCluster struct {
@ -208,7 +208,7 @@ type etcdProcessCluster struct {
type etcdProcess struct {
cfg *etcdProcessConfig
proc *gexpect.ExpectSubprocess
proc *expect.ExpectProcess
donec chan struct{} // closed when Interact() terminates
}
@ -260,17 +260,8 @@ func newEtcdProcessCluster(cfg *etcdProcessClusterConfig) (*etcdProcessCluster,
// rs = "proxy: listening for client requests on"
rs = "proxy: endpoints found"
}
ok, err := etcdp.proc.ExpectRegex(rs)
if err != nil {
readyC <- err
} else if !ok {
readyC <- fmt.Errorf("couldn't get expected output: '%s'", rs)
} else {
readyC <- nil
}
etcdp.proc.ReadLine()
etcdp.proc.Interact() // this blocks(leaks) if another goroutine is reading
etcdp.proc.ReadLine() // wait for leaky goroutine to accept an EOF
_, err := etcdp.proc.Expect(rs)
readyC <- err
close(etcdp.donec)
}(epc.procs[i])
}
@ -424,10 +415,10 @@ func (epc *etcdProcessCluster) Close() (err error) {
return err
}
func spawnCmd(args []string) (*gexpect.ExpectSubprocess, error) {
// redirect stderr to stdout since gexpect only uses stdout
cmd := `/bin/sh -c "` + strings.Join(args, " ") + ` 2>&1 "`
return gexpect.Spawn(cmd)
func spawnCmd(args []string) (*expect.ExpectProcess, error) {
// redirect stderr to stdout since expect only uses stdout
cmdargs := append([]string{"-c"}, strings.Join(append(args, "2>&1"), " "))
return expect.NewExpect("/bin/sh", cmdargs...)
}
func spawnWithExpect(args []string, expected string) error {
@ -435,33 +426,11 @@ func spawnWithExpect(args []string, expected string) error {
if err != nil {
return err
}
ok, err := proc.ExpectRegex(expected)
_, err = proc.Expect(expected)
perr := proc.Close()
if err != nil {
return err
}
if !ok {
return fmt.Errorf("couldn't get expected output: '%s'", expected)
}
return perr
}
// spawnWithExpectedString compares outputs in string format.
// This is useful when gexpect does not match regex correctly with
// some UTF-8 format characters.
func spawnWithExpectedString(args []string, expected string) error {
proc, err := spawnCmd(args)
if err != nil {
return err
}
s, err := proc.ReadLine()
perr := proc.Close()
if err != nil {
return err
}
if !strings.Contains(s, expected) {
return fmt.Errorf("expected %q, got %q", expected, s)
}
return perr
}

View File

@ -243,15 +243,15 @@ func etcdctlPrefixArgs(clus *etcdProcessCluster) []string {
func etcdctlSet(clus *etcdProcessCluster, key, value string) error {
cmdArgs := append(etcdctlPrefixArgs(clus), "set", key, value)
return spawnWithExpectedString(cmdArgs, value)
return spawnWithExpect(cmdArgs, value)
}
func etcdctlMk(clus *etcdProcessCluster, key, value string, first bool) error {
cmdArgs := append(etcdctlPrefixArgs(clus), "mk", key, value)
if first {
return spawnWithExpectedString(cmdArgs, value)
return spawnWithExpect(cmdArgs, value)
}
return spawnWithExpectedString(cmdArgs, "Error: 105: Key already exists")
return spawnWithExpect(cmdArgs, "Error: 105: Key already exists")
}
func etcdctlGet(clus *etcdProcessCluster, key, value string, quorum bool) error {
@ -259,13 +259,13 @@ func etcdctlGet(clus *etcdProcessCluster, key, value string, quorum bool) error
if quorum {
cmdArgs = append(cmdArgs, "--quorum")
}
return spawnWithExpectedString(cmdArgs, value)
return spawnWithExpect(cmdArgs, value)
}
func etcdctlRm(clus *etcdProcessCluster, key, value string, first bool) error {
cmdArgs := append(etcdctlPrefixArgs(clus), "rm", key)
if first {
return spawnWithExpectedString(cmdArgs, "PrevNode.Value: "+value)
return spawnWithExpect(cmdArgs, "PrevNode.Value: "+value)
}
return spawnWithExpect(cmdArgs, "Error: 100: Key not found")
}
@ -292,32 +292,32 @@ func etcdctlWatch(clus *etcdProcessCluster, key, value string, noSync bool) <-ch
func etcdctlRoleAdd(clus *etcdProcessCluster, role string) error {
cmdArgs := append(etcdctlPrefixArgs(clus), "role", "add", role)
return spawnWithExpectedString(cmdArgs, role)
return spawnWithExpect(cmdArgs, role)
}
func etcdctlRoleList(clus *etcdProcessCluster, expectedRole string) error {
cmdArgs := append(etcdctlPrefixArgs(clus), "role", "list")
return spawnWithExpectedString(cmdArgs, expectedRole)
return spawnWithExpect(cmdArgs, expectedRole)
}
func etcdctlUserAdd(clus *etcdProcessCluster, user, pass string) error {
cmdArgs := append(etcdctlPrefixArgs(clus), "user", "add", user+":"+pass)
return spawnWithExpectedString(cmdArgs, "User "+user+" created")
return spawnWithExpect(cmdArgs, "User "+user+" created")
}
func etcdctlUserGrant(clus *etcdProcessCluster, user, role string) error {
cmdArgs := append(etcdctlPrefixArgs(clus), "user", "grant", "--roles", role, user)
return spawnWithExpectedString(cmdArgs, "User "+user+" updated")
return spawnWithExpect(cmdArgs, "User "+user+" updated")
}
func etcdctlUserGet(clus *etcdProcessCluster, user string) error {
cmdArgs := append(etcdctlPrefixArgs(clus), "user", "get", user)
return spawnWithExpectedString(cmdArgs, "User: "+user)
return spawnWithExpect(cmdArgs, "User: "+user)
}
func etcdctlUserList(clus *etcdProcessCluster, expectedUser string) error {
cmdArgs := append(etcdctlPrefixArgs(clus), "user", "list")
return spawnWithExpectedString(cmdArgs, expectedUser)
return spawnWithExpect(cmdArgs, expectedUser)
}
func mustEtcdctl(t *testing.T) {

View File

@ -15,6 +15,7 @@
package e2e
import (
"fmt"
"os"
"strings"
"testing"
@ -69,23 +70,22 @@ func testCtlV3Set(t *testing.T, cfg *etcdProcessClusterConfig, dialTimeout time.
key, value := "foo", "bar"
donec := make(chan struct{})
errc := make(chan error, 1)
expectTimeout := dialTimeout > 0 && dialTimeout <= time.Nanosecond
go func() {
defer close(errc)
if err := ctlV3Put(epc, key, value, dialTimeout); err != nil {
if dialTimeout > 0 && dialTimeout <= time.Nanosecond && isGRPCTimedout(err) { // timeout expected
donec <- struct{}{}
if expectTimeout && isGRPCTimedout(err) {
errc <- fmt.Errorf("put error (%v)", err)
return
}
t.Fatalf("put error (%v)", err)
}
if err := ctlV3Get(epc, key, value, dialTimeout, quorum); err != nil {
if dialTimeout > 0 && dialTimeout <= time.Nanosecond && isGRPCTimedout(err) { // timeout expected
donec <- struct{}{}
if expectTimeout && isGRPCTimedout(err) {
errc <- fmt.Errorf("get error (%v)", err)
return
}
t.Fatalf("get error (%v)", err)
}
donec <- struct{}{}
}()
select {
@ -93,7 +93,10 @@ func testCtlV3Set(t *testing.T, cfg *etcdProcessClusterConfig, dialTimeout time.
if dialTimeout > 0 {
t.Fatalf("test timed out for %v", dialTimeout)
}
case <-donec:
case err := <-errc:
if err != nil {
t.Fatal(err)
}
}
}
@ -119,7 +122,7 @@ func ctlV3PrefixArgs(clus *etcdProcessCluster, dialTimeout time.Duration) []stri
func ctlV3Put(clus *etcdProcessCluster, key, value string, dialTimeout time.Duration) error {
cmdArgs := append(ctlV3PrefixArgs(clus, dialTimeout), "put", key, value)
return spawnWithExpectedString(cmdArgs, "OK")
return spawnWithExpect(cmdArgs, "OK")
}
func ctlV3Get(clus *etcdProcessCluster, key, value string, dialTimeout time.Duration, quorum bool) error {
@ -128,7 +131,7 @@ func ctlV3Get(clus *etcdProcessCluster, key, value string, dialTimeout time.Dura
cmdArgs = append(cmdArgs, "--consistency", "s")
}
// TODO: match by value. Currently it prints out both key and value in multi-lines.
return spawnWithExpectedString(cmdArgs, key)
return spawnWithExpect(cmdArgs, key)
}
func setupCtlV3Test(t *testing.T, cfg *etcdProcessClusterConfig, quorum bool) *etcdProcessCluster {

View File

@ -400,7 +400,7 @@ func (ivt *IntervalTree) MaxHeight() int {
return int((2 * math.Log2(float64(ivt.Len()+1))) + 0.5)
}
// InternalVisitor is used on tree searchs; return false to stop searching.
// IntervalVisitor is used on tree searchs; return false to stop searching.
type IntervalVisitor func(n *IntervalValue) bool
// Visit calls a visitor function on every tree node intersecting the given interval.

View File

@ -19,12 +19,13 @@ import (
"fmt"
"net/http"
"net/url"
"sort"
"strings"
)
type CORSInfo map[string]bool
// CORSInfo implements the flag.Value interface to allow users to define a list of CORS origins
// Set implements the flag.Value interface to allow users to define a list of CORS origins
func (ci *CORSInfo) Set(s string) error {
m := make(map[string]bool)
for _, v := range strings.Split(s, ",") {
@ -49,6 +50,7 @@ func (ci *CORSInfo) String() string {
for k := range *ci {
o = append(o, k)
}
sort.StringSlice(o).Sort()
return strings.Join(o, ",")
}

View File

@ -91,7 +91,7 @@ func TestCORSHandler(t *testing.T) {
return http.Header{
"Access-Control-Allow-Methods": []string{"POST, GET, OPTIONS, PUT, DELETE"},
"Access-Control-Allow-Origin": []string{origin},
"Access-Control-Allow-Headers": []string{"accept, content-type"},
"Access-Control-Allow-Headers": []string{"accept, content-type, authorization"},
}
}
tests := []struct {

114
pkg/expect/expect.go Normal file
View File

@ -0,0 +1,114 @@
// Copyright 2016 CoreOS, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package expect implements a small expect-style interface
package expect
import (
"bufio"
"os"
"os/exec"
"strings"
"sync"
"github.com/kr/pty"
)
type ExpectProcess struct {
cmd *exec.Cmd
fpty *os.File
wg sync.WaitGroup
ptyMu sync.Mutex // protects accessing fpty
cond *sync.Cond // for broadcasting updates are avaiable
mu sync.Mutex // protects lines and err
lines []string
err error
}
// NewExpect creates a new process for expect testing.
func NewExpect(name string, arg ...string) (ep *ExpectProcess, err error) {
ep = &ExpectProcess{cmd: exec.Command(name, arg...)}
ep.cond = sync.NewCond(&ep.mu)
ep.cmd.Stderr = ep.cmd.Stdout
ep.cmd.Stdin = nil
if ep.fpty, err = pty.Start(ep.cmd); err != nil {
return nil, err
}
ep.wg.Add(1)
go ep.read()
return ep, nil
}
func (ep *ExpectProcess) read() {
defer ep.wg.Done()
r := bufio.NewReader(ep.fpty)
for ep.err == nil {
ep.ptyMu.Lock()
l, rerr := r.ReadString('\n')
ep.ptyMu.Unlock()
ep.mu.Lock()
ep.err = rerr
if l != "" {
ep.lines = append(ep.lines, l)
if len(ep.lines) == 1 {
ep.cond.Signal()
}
}
ep.mu.Unlock()
}
ep.cond.Signal()
}
// Expect returns the first line containing the given string.
func (ep *ExpectProcess) Expect(s string) (string, error) {
ep.mu.Lock()
for {
for len(ep.lines) == 0 && ep.err == nil {
ep.cond.Wait()
}
if len(ep.lines) == 0 {
break
}
l := ep.lines[0]
ep.lines = ep.lines[1:]
if strings.Contains(l, s) {
ep.mu.Unlock()
return l, nil
}
}
ep.mu.Unlock()
return "", ep.err
}
// Close waits for the expect process to close
func (ep *ExpectProcess) Close() error {
if ep.cmd == nil {
return nil
}
ep.cmd.Process.Kill()
ep.ptyMu.Lock()
ep.fpty.Close()
ep.ptyMu.Unlock()
err := ep.cmd.Wait()
ep.wg.Wait()
if err != nil && strings.Contains(err.Error(), "signal:") {
// ignore signal errors; expected from pty
err = nil
}
ep.cmd = nil
return err
}

42
pkg/expect/expect_test.go Normal file
View File

@ -0,0 +1,42 @@
// Copyright 2016 CoreOS, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// build !windows
package expect
import (
"testing"
)
func TestEcho(t *testing.T) {
ep, err := NewExpect("/bin/echo", "hello world")
if err != nil {
t.Fatal(err)
}
l, eerr := ep.Expect("world")
if eerr != nil {
t.Fatal(eerr)
}
wstr := "hello world"
if l[:len(wstr)] != wstr {
t.Fatalf(`got "%v", expected "%v"`, l, wstr)
}
if cerr := ep.Close(); cerr != nil {
t.Fatal(cerr)
}
if _, eerr = ep.Expect("..."); eerr == nil {
t.Fatalf("expected error on closed expect process")
}
}

View File

@ -28,7 +28,7 @@ func TestFIFOSchedule(t *testing.T) {
jobCreator := func(i int) Job {
return func(ctx context.Context) {
if next != i {
t.Fatalf("job#%d: got %d, want %d", next, i)
t.Fatalf("job#%d: got %d, want %d", i, next, i)
}
next = i + 1
}

View File

@ -22,6 +22,7 @@ import (
"time"
)
// WaitSchedule briefly sleeps in order to invoke the go scheduler.
// TODO: improve this when we are able to know the schedule or status of target go-routine.
func WaitSchedule() {
time.Sleep(10 * time.Millisecond)

View File

@ -98,7 +98,7 @@ func TestParse(t *testing.T) {
}
}
// This is only tested in Go1.5+ because Go1.4 doesn't support literal IPv6 address with zone in
// TestNewURLsMapIPV6 is only tested in Go1.5+ because Go1.4 doesn't support literal IPv6 address with zone in
// URI (https://github.com/golang/go/issues/6530).
func TestNewURLsMapIPV6(t *testing.T) {
c, err := NewURLsMap("mem1=http://[2001:db8::1]:2380,mem1=http://[fe80::6e40:8ff:feb1:58e4%25en0]:2380,mem2=http://[fe80::92e2:baff:fe7c:3224%25ext0]:2380")

3
test
View File

@ -17,7 +17,8 @@ GO_BUILD_FLAGS=-a
source ./build
# Hack: gofmt ./ will recursively check the .git directory. So use *.go for gofmt.
TESTABLE_AND_FORMATTABLE="client clientv3 discovery error etcdctl/ctlv2 etcdctl/ctlv3 etcdmain etcdserver etcdserver/auth etcdserver/etcdhttp etcdserver/etcdhttp/httptypes pkg/fileutil pkg/flags pkg/idutil pkg/ioutil pkg/netutil pkg/osutil pkg/pbutil pkg/types pkg/transport pkg/wait proxy raft snap storage storage/backend store version wal"
PKGS=`ls pkg/*/*go | cut -f1,2 -d/ | sort | uniq`
TESTABLE_AND_FORMATTABLE="client clientv3 discovery error etcdctl/ctlv2 etcdctl/ctlv3 etcdmain etcdserver etcdserver/auth etcdserver/etcdhttp etcdserver/etcdhttp/httptypes $PKGS proxy raft snap storage storage/backend store version wal"
# TODO: add it to race testing when the issue is resolved
# https://github.com/golang/go/issues/9946
NO_RACE_TESTABLE="rafthttp"

View File

@ -1,7 +0,0 @@
Copyright (C) 2014 Thomas Rooney
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View File

@ -1,64 +0,0 @@
# Gexpect
Gexpect is a pure golang expect-like module.
It makes it simpler to create and control other terminal applications.
child, err := gexpect.Spawn("python")
if err != nil {
panic(err)
}
child.Expect(">>>")
child.SendLine("print 'Hello World'")
child.Interact()
child.Close()
## Examples
`Spawn` handles the argument parsing from a string
child.Spawn("/bin/sh -c 'echo \"my complicated command\" | tee log | cat > log2'")
child.ReadLine() // ReadLine() (string, error)
child.ReadUntil(' ') // ReadUntil(delim byte) ([]byte, error)
`ReadLine`, `ReadUntil` and `SendLine` send strings from/to `stdout/stdin` respectively
child := gexpect.Spawn("cat")
child.SendLine("echoing process_stdin") // SendLine(command string) (error)
msg, _ := child.ReadLine() // msg = echoing process_stdin
`Wait` and `Close` allow for graceful and ungraceful termination.
child.Wait() // Waits until the child terminates naturally.
child.Close() // Sends a kill command
`AsyncInteractChannels` spawns two go routines to pipe into and from `stdout`/`stdin`, allowing for some usecases to be a little simpler.
child := gexpect.spawn("sh")
sender, reciever := child.AsyncInteractChannels()
sender <- "echo Hello World\n" // Send to stdin
line, open := <- reciever // Recieve a line from stdout/stderr
// When the subprocess stops (e.g. with child.Close()) , receiver is closed
if open {
fmt.Printf("Received %s", line)]
}
`ExpectRegex` uses golang's internal regex engine to wait until a match from the process with the given regular expression (or an error on process termination with no match).
child := gexpect.Spawn("echo accb")
match, _ := child.ExpectRegex("a..b")
// (match=true)
`ExpectRegexFind` allows for groups to be extracted from process stdout. The first element is an array of containing the total matched text, followed by each subexpression group match.
child := gexpect.Spawn("echo 123 456 789")
result, _ := child.ExpectRegexFind("\d+ (\d+) (\d+)")
// result = []string{"123 456 789", "456", "789"}
See `gexpect_test.go` and the `examples` folder for full syntax
## Credits
github.com/kballard/go-shellquote
github.com/kr/pty
KMP Algorithm: "http://blog.databigbang.com/searching-for-substrings-in-streams-a-slight-modification-of-the-knuth-morris-pratt-algorithm-in-haxe/"

View File

@ -1,430 +0,0 @@
package gexpect
import (
"bytes"
"errors"
"fmt"
"io"
"os"
"os/exec"
"regexp"
"time"
"unicode/utf8"
shell "github.com/kballard/go-shellquote"
"github.com/kr/pty"
)
type ExpectSubprocess struct {
Cmd *exec.Cmd
buf *buffer
outputBuffer []byte
}
type buffer struct {
f *os.File
b bytes.Buffer
collect bool
collection bytes.Buffer
}
func (buf *buffer) StartCollecting() {
buf.collect = true
}
func (buf *buffer) StopCollecting() (result string) {
result = string(buf.collection.Bytes())
buf.collect = false
buf.collection.Reset()
return result
}
func (buf *buffer) Read(chunk []byte) (int, error) {
nread := 0
if buf.b.Len() > 0 {
n, err := buf.b.Read(chunk)
if err != nil {
return n, err
}
if n == len(chunk) {
return n, nil
}
nread = n
}
fn, err := buf.f.Read(chunk[nread:])
return fn + nread, err
}
func (buf *buffer) ReadRune() (r rune, size int, err error) {
l := buf.b.Len()
chunk := make([]byte, utf8.UTFMax)
if l > 0 {
n, err := buf.b.Read(chunk)
if err != nil {
return 0, 0, err
}
if utf8.FullRune(chunk) {
r, rL := utf8.DecodeRune(chunk)
if n > rL {
buf.PutBack(chunk[rL:n])
}
if buf.collect {
buf.collection.WriteRune(r)
}
return r, rL, nil
}
}
// else add bytes from the file, then try that
for l < utf8.UTFMax {
fn, err := buf.f.Read(chunk[l : l+1])
if err != nil {
return 0, 0, err
}
l = l + fn
if utf8.FullRune(chunk) {
r, rL := utf8.DecodeRune(chunk)
if buf.collect {
buf.collection.WriteRune(r)
}
return r, rL, nil
}
}
return 0, 0, errors.New("File is not a valid UTF=8 encoding")
}
func (buf *buffer) PutBack(chunk []byte) {
if len(chunk) == 0 {
return
}
if buf.b.Len() == 0 {
buf.b.Write(chunk)
return
}
d := make([]byte, 0, len(chunk)+buf.b.Len())
d = append(d, chunk...)
d = append(d, buf.b.Bytes()...)
buf.b.Reset()
buf.b.Write(d)
}
func SpawnAtDirectory(command string, directory string) (*ExpectSubprocess, error) {
expect, err := _spawn(command)
if err != nil {
return nil, err
}
expect.Cmd.Dir = directory
return _start(expect)
}
func Command(command string) (*ExpectSubprocess, error) {
expect, err := _spawn(command)
if err != nil {
return nil, err
}
return expect, nil
}
func (expect *ExpectSubprocess) Start() error {
_, err := _start(expect)
return err
}
func Spawn(command string) (*ExpectSubprocess, error) {
expect, err := _spawn(command)
if err != nil {
return nil, err
}
return _start(expect)
}
func (expect *ExpectSubprocess) Close() error {
return expect.Cmd.Process.Kill()
}
func (expect *ExpectSubprocess) AsyncInteractChannels() (send chan string, receive chan string) {
receive = make(chan string)
send = make(chan string)
go func() {
for {
str, err := expect.ReadLine()
if err != nil {
close(receive)
return
}
receive <- str
}
}()
go func() {
for {
select {
case sendCommand, exists := <-send:
{
if !exists {
return
}
err := expect.Send(sendCommand)
if err != nil {
receive <- "gexpect Error: " + err.Error()
return
}
}
}
}
}()
return
}
func (expect *ExpectSubprocess) ExpectRegex(regex string) (bool, error) {
return regexp.MatchReader(regex, expect.buf)
}
func (expect *ExpectSubprocess) expectRegexFind(regex string, output bool) ([]string, string, error) {
re, err := regexp.Compile(regex)
if err != nil {
return nil, "", err
}
expect.buf.StartCollecting()
pairs := re.FindReaderSubmatchIndex(expect.buf)
stringIndexedInto := expect.buf.StopCollecting()
l := len(pairs)
numPairs := l / 2
result := make([]string, numPairs)
for i := 0; i < numPairs; i += 1 {
result[i] = stringIndexedInto[pairs[i*2]:pairs[i*2+1]]
}
// convert indexes to strings
if len(result) == 0 {
err = fmt.Errorf("ExpectRegex didn't find regex '%v'.", regex)
}
return result, stringIndexedInto, err
}
func (expect *ExpectSubprocess) expectTimeoutRegexFind(regex string, timeout time.Duration) (result []string, out string, err error) {
t := make(chan bool)
go func() {
result, out, err = expect.ExpectRegexFindWithOutput(regex)
t <- false
}()
go func() {
time.Sleep(timeout)
err = fmt.Errorf("ExpectRegex timed out after %v finding '%v'.\nOutput:\n%s", timeout, regex, expect.Collect())
t <- true
}()
<-t
return result, out, err
}
func (expect *ExpectSubprocess) ExpectRegexFind(regex string) ([]string, error) {
result, _, err := expect.expectRegexFind(regex, false)
return result, err
}
func (expect *ExpectSubprocess) ExpectTimeoutRegexFind(regex string, timeout time.Duration) ([]string, error) {
result, _, err := expect.expectTimeoutRegexFind(regex, timeout)
return result, err
}
func (expect *ExpectSubprocess) ExpectRegexFindWithOutput(regex string) ([]string, string, error) {
return expect.expectRegexFind(regex, true)
}
func (expect *ExpectSubprocess) ExpectTimeoutRegexFindWithOutput(regex string, timeout time.Duration) ([]string, string, error) {
return expect.expectTimeoutRegexFind(regex, timeout)
}
func buildKMPTable(searchString string) []int {
pos := 2
cnd := 0
length := len(searchString)
var table []int
if length < 2 {
length = 2
}
table = make([]int, length)
table[0] = -1
table[1] = 0
for pos < len(searchString) {
if searchString[pos-1] == searchString[cnd] {
cnd += 1
table[pos] = cnd
pos += 1
} else if cnd > 0 {
cnd = table[cnd]
} else {
table[pos] = 0
pos += 1
}
}
return table
}
func (expect *ExpectSubprocess) ExpectTimeout(searchString string, timeout time.Duration) (e error) {
result := make(chan error)
go func() {
result <- expect.Expect(searchString)
}()
select {
case e = <-result:
case <-time.After(timeout):
e = fmt.Errorf("Expect timed out after %v waiting for '%v'.\nOutput:\n%s", timeout, searchString, expect.Collect())
}
return e
}
func (expect *ExpectSubprocess) Expect(searchString string) (e error) {
chunk := make([]byte, len(searchString)*2)
target := len(searchString)
if expect.outputBuffer != nil {
expect.outputBuffer = expect.outputBuffer[0:]
}
m := 0
i := 0
// Build KMP Table
table := buildKMPTable(searchString)
for {
n, err := expect.buf.Read(chunk)
if err != nil {
return err
}
if expect.outputBuffer != nil {
expect.outputBuffer = append(expect.outputBuffer, chunk[:n]...)
}
offset := m + i
for m+i-offset < n {
if searchString[i] == chunk[m+i-offset] {
i += 1
if i == target {
unreadIndex := m + i - offset
if len(chunk) > unreadIndex {
expect.buf.PutBack(chunk[unreadIndex:])
}
return nil
}
} else {
m += i - table[i]
if table[i] > -1 {
i = table[i]
} else {
i = 0
}
}
}
}
}
func (expect *ExpectSubprocess) Send(command string) error {
_, err := io.WriteString(expect.buf.f, command)
return err
}
func (expect *ExpectSubprocess) Capture() {
if expect.outputBuffer == nil {
expect.outputBuffer = make([]byte, 0)
}
}
func (expect *ExpectSubprocess) Collect() []byte {
collectOutput := make([]byte, len(expect.outputBuffer))
copy(collectOutput, expect.outputBuffer)
expect.outputBuffer = nil
return collectOutput
}
func (expect *ExpectSubprocess) SendLine(command string) error {
_, err := io.WriteString(expect.buf.f, command+"\r\n")
return err
}
func (expect *ExpectSubprocess) Interact() {
defer expect.Cmd.Wait()
io.Copy(os.Stdout, &expect.buf.b)
go io.Copy(os.Stdout, expect.buf.f)
go io.Copy(expect.buf.f, os.Stdin)
}
func (expect *ExpectSubprocess) ReadUntil(delim byte) ([]byte, error) {
join := make([]byte, 1, 512)
chunk := make([]byte, 255)
for {
n, err := expect.buf.Read(chunk)
if err != nil {
return join, err
}
for i := 0; i < n; i++ {
if chunk[i] == delim {
if len(chunk) > i+1 {
expect.buf.PutBack(chunk[i+1:])
}
return join, nil
} else {
join = append(join, chunk[i])
}
}
}
}
func (expect *ExpectSubprocess) Wait() error {
return expect.Cmd.Wait()
}
func (expect *ExpectSubprocess) ReadLine() (string, error) {
str, err := expect.ReadUntil('\n')
if err != nil {
return "", err
}
return string(str), nil
}
func _start(expect *ExpectSubprocess) (*ExpectSubprocess, error) {
f, err := pty.Start(expect.Cmd)
if err != nil {
return nil, err
}
expect.buf.f = f
return expect, nil
}
func _spawn(command string) (*ExpectSubprocess, error) {
wrapper := new(ExpectSubprocess)
wrapper.outputBuffer = nil
splitArgs, err := shell.Split(command)
if err != nil {
return nil, err
}
numArguments := len(splitArgs) - 1
if numArguments < 0 {
return nil, errors.New("gexpect: No command given to spawn")
}
path, err := exec.LookPath(splitArgs[0])
if err != nil {
return nil, err
}
if numArguments >= 1 {
wrapper.Cmd = exec.Command(path, splitArgs[1:]...)
} else {
wrapper.Cmd = exec.Command(path)
}
wrapper.buf = new(buffer)
return wrapper, nil
}