tests: Extract expect configuration into struct

Signed-off-by: Marek Siarkowicz <siarkowicz@google.com>
This commit is contained in:
Marek Siarkowicz 2022-09-12 15:57:48 +02:00
parent b886bbc89f
commit 1038c2f45c

View File

@ -34,7 +34,7 @@ import (
const DEBUG_LINES_TAIL = 40
type ExpectProcess struct {
name string
cfg expectConfig
cmd *exec.Cmd
fpty *os.File
@ -58,15 +58,16 @@ func NewExpect(name string, arg ...string) (ep *ExpectProcess, err error) {
// NewExpectWithEnv creates a new process with user defined env variables for expect testing.
func NewExpectWithEnv(name string, args []string, env []string, serverProcessConfigName string) (ep *ExpectProcess, err error) {
cmd := exec.Command(name, args...)
cmd.Env = env
ep = &ExpectProcess{
name: serverProcessConfigName,
cmd: cmd,
StopSignal: syscall.SIGTERM,
cfg: expectConfig{
name: serverProcessConfigName,
cmd: name,
args: args,
env: env,
},
}
ep.cmd.Stderr = ep.cmd.Stdout
ep.cmd.Stdin = nil
ep.cmd = commandFromConfig(ep.cfg)
if ep.fpty, err = pty.Start(ep.cmd); err != nil {
return nil, err
@ -77,6 +78,21 @@ func NewExpectWithEnv(name string, args []string, env []string, serverProcessCon
return ep, nil
}
type expectConfig struct {
name string
cmd string
args []string
env []string
}
func commandFromConfig(config expectConfig) *exec.Cmd {
cmd := exec.Command(config.cmd, config.args...)
cmd.Env = config.env
cmd.Stderr = cmd.Stdout
cmd.Stdin = nil
return cmd
}
func (ep *ExpectProcess) Pid() int {
return ep.cmd.Process.Pid
}
@ -90,7 +106,7 @@ func (ep *ExpectProcess) read() {
ep.mu.Lock()
if l != "" {
if printDebugLines {
fmt.Printf("%s (%s) (%d): %s", ep.cmd.Path, ep.name, ep.cmd.Process.Pid, l)
fmt.Printf("%s (%s) (%d): %s", ep.cmd.Path, ep.cfg.name, ep.cmd.Process.Pid, l)
}
ep.lines = append(ep.lines, l)
ep.count++