Files
odo/tests/helper/helper_interactive.go
Anand Kumar Singh 3eb92b6a47 POC for odo interactive testing (#5466)
* POC for odo interactive testing

Signed-off-by: anandrkskd <anandrkskd@gmail.com>

* add interactive tests to make target test-integration

Signed-off-by: anandrkskd <anandrkskd@gmail.com>

* [WIP] use func to pass test instructions

Signed-off-by: anandrkskd <anandrkskd@gmail.com>

* cleanup comments/test

Signed-off-by: anandrkskd <anandrkskd@gmail.com>

* cleanup minor changes

Signed-off-by: anandrkskd <anandrkskd@gmail.com>

* fix unit test failure

Signed-off-by: anandrkskd <anandrkskd@gmail.com>

* skip for windows

Signed-off-by: anandrkskd <anandrkskd@gmail.com>

* skip for windows by not compiling, cleanup

Signed-off-by: anandrkskd <anandrkskd@gmail.com>

* cleanup, and add the make target to test files

Signed-off-by: anandrkskd <anandrkskd@gmail.com>

* incorporate review, adding comments, cleanup

Signed-off-by: anandrkskd <anandrkskd@gmail.com>

* fix failing make validate

Signed-off-by: anandrkskd <anandrkskd@gmail.com>

* update test dependency, and possible fix for windows

Signed-off-by: anandrkskd <anandrkskd@gmail.com>

* Revert "update test dependency, and possible fix for windows"

This reverts commit 55580b7dc5.

* Final cleanup

Signed-off-by: anandrkskd <anandrkskd@gmail.com>
2022-03-02 13:50:26 -05:00

67 lines
1.6 KiB
Go

//go:build linux || darwin || dragonfly || solaris || openbsd || netbsd || freebsd
// +build linux darwin dragonfly solaris openbsd netbsd freebsd
package helper
import (
"bytes"
"log"
"os/exec"
"github.com/Netflix/go-expect"
"github.com/hinshun/vt10x"
"github.com/kr/pty"
. "github.com/onsi/gomega"
)
// RunInteractive runs the command in interactive mode and returns the output, and error
// It takes command as array of strings, and a function `test` that contains steps to run the test as an argument
func RunInteractive(command []string, test func(*expect.Console, *bytes.Buffer)) (string, error) {
ptm, pts, err := pty.Open()
if err != nil {
log.Fatal(err)
}
term := vt10x.New(vt10x.WithWriter(pts))
c, err := expect.NewConsole(expect.WithStdin(ptm), expect.WithStdout(term), expect.WithCloser(pts, ptm))
if err != nil {
log.Fatal(err)
}
defer c.Close()
// execute the command
cmd := exec.Command(command[0], command[1:]...)
// setup stdin, stdout and stderr
cmd.Stdin = c.Tty()
cmd.Stdout = c.Tty()
cmd.Stderr = c.Tty()
err = cmd.Start()
if err != nil {
log.Fatal(err)
}
buf := new(bytes.Buffer)
test(c, buf)
err = cmd.Wait()
if err != nil {
log.Fatal(err)
}
// Close the slave end of the pty, and read the remaining bytes from the master end.
c.Tty().Close()
return buf.String(), err
}
func SendLine(c *expect.Console, line string) {
_, err := c.SendLine(line)
Expect(err).ShouldNot(HaveOccurred())
}
func ExpectString(c *expect.Console, line string) string {
res, err := c.ExpectString(line)
Expect(err).ShouldNot(HaveOccurred())
return res
}