Skip to content

Commit

Permalink
Merge pull request #156 from patrickfnielsen/option-for-timeoutsocket
Browse files Browse the repository at this point in the history
Fixes #155 - add WithStandardTransportDialTimeout
  • Loading branch information
carlmontanari authored Oct 2, 2023
2 parents d036ae1 + ef5500c commit 071f5bf
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
22 changes: 22 additions & 0 deletions driver/options/transport.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package options

import (
"time"

"github.com/scrapli/scrapligo/transport"
"github.com/scrapli/scrapligo/util"
)
Expand Down Expand Up @@ -66,3 +68,23 @@ func WithTermWidth(i int) util.Option {
return nil
}
}

// WithTimeoutSocket allows for modifying the TimeoutSocket parameter for the underlying transport.
// When using "system" (default) transport, this value governs the `ConnectTimeout` and
// `ServerAliveInterval` ssh options.
// When using the "standard" (crypto/ssh) transport, this modifies the timeout for initial
// connections.
// For the "telnet" transport, this value modifies the timeout for initial control character
// processing.
func WithTimeoutSocket(t time.Duration) util.Option {
return func(o interface{}) error {
a, ok := o.(*transport.Args)
if !ok {
return util.ErrIgnoredOption
}

a.TimeoutSocket = t

return nil
}
}
56 changes: 56 additions & 0 deletions driver/options/transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package options_test
import (
"errors"
"testing"
"time"

"github.com/scrapli/scrapligo/driver/generic"
"github.com/scrapli/scrapligo/driver/options"
Expand Down Expand Up @@ -227,3 +228,58 @@ func TestWithTermWidth(t *testing.T) {
t.Run(testName, f)
}
}

func testTransportTimeoutSocket(
testName string,
testCase *optionsDurationTestCase,
) func(t *testing.T) {
return func(t *testing.T) {
t.Logf("%s: starting", testName)

err := options.WithTimeoutSocket(testCase.d)(testCase.o)
if err != nil {
if errors.Is(err, util.ErrIgnoredOption) && !testCase.isignored {
t.Fatalf(
"%s: option should be ignored, but returned different error",
testName,
)
}

return
}

oo, _ := testCase.o.(*transport.Args)

if !cmp.Equal(oo.TimeoutSocket, testCase.d) {
t.Fatalf(
"%s: actual and expected dial timeout do not match\nactual: %s\nexpected:%s",
testName,
oo.TimeoutSocket,
testCase.d,
)
}
}
}

func TestTransportTimeoutSocket(t *testing.T) {
cases := map[string]*optionsDurationTestCase{
"set-timeout": {
description: "simple set option test",
d: 123 * time.Second,
o: &transport.Args{},
isignored: false,
},
"ignored": {
description: "skipped due to ignored type",
d: 123 * time.Second,
o: &generic.Driver{},
isignored: true,
},
}

for testName, testCase := range cases {
f := testTransportTimeoutSocket(testName, testCase)

t.Run(testName, f)
}
}

0 comments on commit 071f5bf

Please sign in to comment.