Skip to content

Commit ddaf7bd

Browse files
committed
[sshcmd] Implement expect in ssh plugin
1 parent 7d75a9b commit ddaf7bd

1 file changed

Lines changed: 18 additions & 2 deletions

File tree

plugins/teststeps/sshcmd/sshcmd.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"io"
2323
"io/ioutil"
2424
"net"
25+
"regexp"
2526
"strconv"
2627
"strings"
2728

@@ -57,6 +58,7 @@ type SSHCmd struct {
5758
Password *test.Param
5859
Executable *test.Param
5960
Args []test.Param
61+
Expect *test.Param
6062
}
6163

6264
// Name returns the plugin name.
@@ -184,11 +186,24 @@ func (ts *SSHCmd) Run(cancel, pause <-chan struct{}, ch test.TestStepChannels, p
184186
errCh <- innerErr
185187
}()
186188

187-
// TODO should stdout/stderr go to events?
188189
select {
189190
case err := <-errCh:
190191
log.Infof("Stdout of command '%s' is '%s'", cmd, stdout.Bytes())
191-
if err != nil {
192+
if err == nil {
193+
// Execute expectations
194+
expect := ts.Expect.Raw()
195+
if expect == "" {
196+
log.Warningf("no expectations specified")
197+
} else {
198+
re := regexp.MustCompile(expect)
199+
matches := re.FindAll(stdout.Bytes(), -1)
200+
if len(matches) > 0 {
201+
log.Infof("match for regex \"%s\" found", expect)
202+
} else {
203+
return fmt.Errorf("match for %s not found for target %v", expect, target)
204+
}
205+
}
206+
} else {
192207
log.Warningf("Stderr of command '%s' is '%s'", cmd, stderr.Bytes())
193208
}
194209
return err
@@ -237,6 +252,7 @@ func (ts *SSHCmd) validateAndPopulate(params test.TestStepParameters) error {
237252
return errors.New("invalid or missing 'executable' parameter, must be exactly one string")
238253
}
239254
ts.Args = params.Get("args")
255+
ts.Expect = params.GetOne("expect")
240256
return nil
241257
}
242258

0 commit comments

Comments
 (0)