e2e/expect: In case of sut process failure, print last 40lines of logs.

This commit is contained in:
Piotr Tabor 2021-03-10 21:55:46 +01:00
parent a47c18d30a
commit 67491a00ea

View File

@ -29,6 +29,8 @@ import (
"github.com/creack/pty"
)
const DEBUG_LINES_TAIL=40
type ExpectProcess struct {
cmd *exec.Cmd
fpty *os.File
@ -96,6 +98,8 @@ func (ep *ExpectProcess) read() {
// ExpectFunc returns the first line satisfying the function f.
func (ep *ExpectProcess) ExpectFunc(f func(string) bool) (string, error) {
lastLinesBuffer := make([]string, 0)
ep.mu.Lock()
for {
for len(ep.lines) == 0 && ep.err == nil {
@ -106,13 +110,19 @@ func (ep *ExpectProcess) ExpectFunc(f func(string) bool) (string, error) {
}
l := ep.lines[0]
ep.lines = ep.lines[1:]
lastLinesBuffer = append(lastLinesBuffer, l)
if l:=len(lastLinesBuffer); l>DEBUG_LINES_TAIL {
lastLinesBuffer = lastLinesBuffer[l-DEBUG_LINES_TAIL:l-1]
}
if f(l) {
ep.mu.Unlock()
return l, nil
}
}
ep.mu.Unlock()
return "", ep.err
return "", fmt.Errorf("Match not found." +
" Set EXPECT_DEBUG for more info Err: %v, last lines:\n%s\n\n",
ep.err, strings.Join(lastLinesBuffer, ""))
}
// Expect returns the first line containing the given string.