Skip to content

Commit

Permalink
Fix odo log flake
Browse files Browse the repository at this point in the history
  • Loading branch information
Aditi Sharma committed Aug 11, 2020
1 parent bf474b1 commit 2e1bb20
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 5 deletions.
64 changes: 64 additions & 0 deletions tests/helper/helper_generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package helper
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -177,6 +179,68 @@ func WatchNonRetCmdStdOut(cmdStr string, timeout time.Duration, check func(outpu
}
}

// RunCmdWithMatchOutputFromBuffer starts the command, and command stdout is attached to buffer.
// we read data from buffer line by line, and if expected string is matcher it reutrns true
// It is diffrent from WaitforCmdOut which gives stdout in one go usinf session.Out.Contents()
// for commands like odo log -f which streams continuous data and does not terminate by their own
// we need to read the stream data from buffer.
func RunCmdWithMatchOutputFromBuffer(timeoutAfter time.Duration, matchString, program string, args ...string) (bool, error) {
var buf bytes.Buffer

command := exec.Command(program, args...)
command.Stdout = &buf

timeoutCh := time.After(timeoutAfter)
matchOutputCh := make(chan bool)
errorCh := make(chan error)

_, err := fmt.Fprintln(GinkgoWriter, runningCmd(command))
if err != nil {
return false, err
}

err = command.Start()
if err != nil {
return false, err
}

// go routine which is reading data from buffer until expected string matched
go func() {
for {
line, err := buf.ReadString('\n')
if err != nil && err != io.EOF {
errorCh <- err
}
if len(line) > 0 {
_, err = fmt.Fprintln(GinkgoWriter, line)
if err != nil {
errorCh <- err
}
if strings.Contains(line, matchString) {
matchOutputCh <- true
}
}

}
}()

// if we do not expected output timeout.
for {
select {

case <-timeoutCh:
return false, errors.New("Timeout waiting for the conditon")

case <-matchOutputCh:
return true, nil

case <-errorCh:
return false, <-errorCh
}
}

}

// GetUserHomeDir gets the user home directory
func GetUserHomeDir() string {
homeDir, err := os.UserHomeDir()
Expand Down
12 changes: 7 additions & 5 deletions tests/integration/devfile/cmd_devfile_log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,10 @@ var _ = Describe("odo devfile log command tests", func() {
output := helper.CmdShouldPass("odo", "log")
Expect(output).To(ContainSubstring("ODO_COMMAND_RUN"))

// test with follow flag
output = helper.CmdShouldRunWithTimeout(1*time.Second, "odo", "log", "-f")
Expect(output).To(ContainSubstring("program=devrun"))
// Test odo log -f
match, err := helper.RunCmdWithMatchOutputFromBuffer(30*time.Second, "program=devrun", "odo", "log", "-f")
Expect(err).To(BeNil())
Expect(match).To(BeTrue())

})

Expand All @@ -83,8 +84,9 @@ var _ = Describe("odo devfile log command tests", func() {
Expect(output).To(ContainSubstring("ODO_COMMAND_DEBUG"))

// test with follow flag
output = helper.CmdShouldRunWithTimeout(1*time.Second, "odo", "log", "-f")
Expect(output).To(ContainSubstring("program=debugrun"))
match, err := helper.RunCmdWithMatchOutputFromBuffer(30*time.Second, "program=debugrun", "odo", "log", "-f")
Expect(err).To(BeNil())
Expect(match).To(BeTrue())

})

Expand Down

0 comments on commit 2e1bb20

Please sign in to comment.