Skip to content

Commit

Permalink
Merge pull request #202 from shaazmik/dev
Browse files Browse the repository at this point in the history
FailedWhenContains with SendWithCallbacks
  • Loading branch information
carlmontanari authored Oct 17, 2024
2 parents c98e523 + 3ef514d commit f6dbfb5
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 5 deletions.
17 changes: 16 additions & 1 deletion driver/generic/sendwithcallbacks.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,25 @@ func (d *Driver) SendWithCallbacks(
input string,
callbacks []*Callback,
timeout time.Duration,
opts ...util.Option,
) (*response.Response, error) {
d.Logger.Info("SendWithCallbacks requested")

r := response.NewResponse(input, d.Transport.GetHost(), d.Transport.GetPort(), nil)
driverOpts, err := NewOperation(opts...)
if err != nil {
return nil, err
}

if len(driverOpts.FailedWhenContains) == 0 {
driverOpts.FailedWhenContains = d.FailedWhenContains
}

r := response.NewResponse(
input,
d.Transport.GetHost(),
d.Transport.GetPort(),
driverOpts.FailedWhenContains,
)

if input != "" {
err := d.Channel.WriteAndReturn([]byte(input), false)
Expand Down
32 changes: 29 additions & 3 deletions driver/generic/sendwithcallbacks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type sendWithCallbacksTestCase struct {
payloadFile string
initialInput string
callbacks []*generic.Callback
failed bool
}

func testSendWithCallbacks(
Expand All @@ -37,10 +38,9 @@ func testSendWithCallbacks(
)
}

if r.Failed != nil {
if r.Failed != nil && !testCase.failed {
t.Fatalf(
"%s: response object indicates failure,"+
" this shouldn't happenf or send with callbacks",
"%s: response object indicates failure, this shouldn't happen",
testName,
)
}
Expand Down Expand Up @@ -91,6 +91,32 @@ func TestSendWithCallbacks(t *testing.T) {
},
},
},
"send-with-callbacks-failed-when-contains": {
description: "simple send with callbacks test w/ failed when contains output",
payloadFile: "send-with-callbacks-failed-when-contains.txt",
initialInput: "",
callbacks: []*generic.Callback{
{
Callback: func(d *generic.Driver, _ string) error {
_, err := d.Channel.SendInput("configure terminal")

return err
},
Contains: "C3560CX#",
Name: "callback-one",
ResetOutput: true,
},
{
Callback: func(d *generic.Driver, _ string) error {
return d.Channel.WriteAndReturn([]byte("show version"), false)
},
ContainsRe: regexp.MustCompile(`(?im)^c3560cx\(config\)#`),
Name: "callback-two",
Complete: true,
},
},
failed: true,
},
}

for testName, testCase := range cases {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
configure terminal


show version

Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
C3560CX#configure terminal
Enter configuration commands, one per line. End with CNTL/Z.
C3560CX(config)#show version
^
% Invalid input detected at '^' marker.

C3560CX(config)#
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ C3560CX#configure terminal
Enter configuration commands, one per line. End with CNTL/Z.
C3560CX(config)#show version
^
% Invalid input detected at '^' marker.
NOTHING TO SEE HERE -- dont want the test to have a failed result, so just making this show no failed when contains thing :)

C3560CX(config)#
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ require (
)

require golang.org/x/sys v0.23.0 // indirect

0 comments on commit f6dbfb5

Please sign in to comment.