Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #155 - add WithStandardTransportDialTimeout #156

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}
Loading