From 9f84df588d5f2d9326e40419a1ac9a0263b6f49d Mon Sep 17 00:00:00 2001 From: Patrick Falk Nielsen Date: Fri, 29 Sep 2023 08:08:02 +0200 Subject: [PATCH 1/3] fixes #155 - add WithStandardTransportDialTimeout --- driver/options/transport.go | 17 ++++++++++ driver/options/transport_test.go | 53 ++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) diff --git a/driver/options/transport.go b/driver/options/transport.go index b7b1b5e..6ed4d06 100644 --- a/driver/options/transport.go +++ b/driver/options/transport.go @@ -1,6 +1,8 @@ package options import ( + "time" + "github.com/scrapli/scrapligo/transport" "github.com/scrapli/scrapligo/util" ) @@ -66,3 +68,18 @@ func WithTermWidth(i int) util.Option { return nil } } + +// WithStandardTransportDialTimeout allows for modifying TimeoutSocket when using standard transport, +// this modifies the timeout for initial connections +func WithStandardTransportDialTimeout(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 + } +} diff --git a/driver/options/transport_test.go b/driver/options/transport_test.go index cff4b12..c77cb23 100644 --- a/driver/options/transport_test.go +++ b/driver/options/transport_test.go @@ -3,6 +3,7 @@ package options_test import ( "errors" "testing" + "time" "github.com/scrapli/scrapligo/driver/generic" "github.com/scrapli/scrapligo/driver/options" @@ -227,3 +228,55 @@ func TestWithTermWidth(t *testing.T) { t.Run(testName, f) } } + +func testStandardTransportDialTimeout(testName string, testCase *optionsDurationTestCase) func(t *testing.T) { + return func(t *testing.T) { + t.Logf("%s: starting", testName) + + err := options.WithStandardTransportDialTimeout(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 TestStandardTransportDialTimeout(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 := testStandardTransportDialTimeout(testName, testCase) + + t.Run(testName, f) + } +} From 98f5b13a3d5d57bd8f8021640a7b918e8a230619 Mon Sep 17 00:00:00 2001 From: Patrick Falk Nielsen Date: Mon, 2 Oct 2023 09:31:16 +0200 Subject: [PATCH 2/3] fix: change function name + docstring --- driver/options/transport.go | 11 ++++++++--- driver/options/transport_test.go | 8 ++++---- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/driver/options/transport.go b/driver/options/transport.go index 6ed4d06..09ac23c 100644 --- a/driver/options/transport.go +++ b/driver/options/transport.go @@ -69,9 +69,14 @@ func WithTermWidth(i int) util.Option { } } -// WithStandardTransportDialTimeout allows for modifying TimeoutSocket when using standard transport, -// this modifies the timeout for initial connections -func WithStandardTransportDialTimeout(t time.Duration) util.Option { +// 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 { diff --git a/driver/options/transport_test.go b/driver/options/transport_test.go index c77cb23..8fcc7a1 100644 --- a/driver/options/transport_test.go +++ b/driver/options/transport_test.go @@ -229,11 +229,11 @@ func TestWithTermWidth(t *testing.T) { } } -func testStandardTransportDialTimeout(testName string, testCase *optionsDurationTestCase) func(t *testing.T) { +func testTransportTimeoutSocket(testName string, testCase *optionsDurationTestCase) func(t *testing.T) { return func(t *testing.T) { t.Logf("%s: starting", testName) - err := options.WithStandardTransportDialTimeout(testCase.d)(testCase.o) + err := options.WithTimeoutSocket(testCase.d)(testCase.o) if err != nil { if errors.Is(err, util.ErrIgnoredOption) && !testCase.isignored { t.Fatalf( @@ -258,7 +258,7 @@ func testStandardTransportDialTimeout(testName string, testCase *optionsDuration } } -func TestStandardTransportDialTimeout(t *testing.T) { +func TestTransportTimeoutSocket(t *testing.T) { cases := map[string]*optionsDurationTestCase{ "set-timeout": { description: "simple set option test", @@ -275,7 +275,7 @@ func TestStandardTransportDialTimeout(t *testing.T) { } for testName, testCase := range cases { - f := testStandardTransportDialTimeout(testName, testCase) + f := testTransportTimeoutSocket(testName, testCase) t.Run(testName, f) } From ef5500c57f4127cfcdef730d8d8db045f1327354 Mon Sep 17 00:00:00 2001 From: Carl Montanari Date: Mon, 2 Oct 2023 08:10:16 -0700 Subject: [PATCH 3/3] chore: appease lll --- driver/options/transport_test.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/driver/options/transport_test.go b/driver/options/transport_test.go index 8fcc7a1..5a4513f 100644 --- a/driver/options/transport_test.go +++ b/driver/options/transport_test.go @@ -229,7 +229,10 @@ func TestWithTermWidth(t *testing.T) { } } -func testTransportTimeoutSocket(testName string, testCase *optionsDurationTestCase) func(t *testing.T) { +func testTransportTimeoutSocket( + testName string, + testCase *optionsDurationTestCase, +) func(t *testing.T) { return func(t *testing.T) { t.Logf("%s: starting", testName)