Skip to content

Commit c696a6a

Browse files
committed
Connection timeout tests
1 parent ade9fa4 commit c696a6a

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

plugin_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"github.com/appleboy/easyssh-proxy"
99
"github.com/stretchr/testify/assert"
1010
"strings"
11+
12+
"time"
1113
)
1214

1315
func TestMissingHostOrUser(t *testing.T) {
@@ -457,6 +459,56 @@ func TestEnvOutput(t *testing.T) {
457459
assert.Equal(t, unindent(expected), unindent(buffer.String()))
458460
}
459461

462+
// TestConnectTimeoutRetry tests that when a network error occurs, the connect
463+
// is retried until it either succeeds or the configured timeout is hit.
464+
func TestConnectTimeoutRetry(t *testing.T) {
465+
start := time.Now()
466+
467+
plugin := Plugin{
468+
Config: Config{
469+
Host: []string{"localhost"},
470+
UserName: "drone-scp",
471+
Port: 2200,
472+
KeyPath: "./tests/.ssh/id_rsa",
473+
Script: []string{"exit"},
474+
RetryTimeout: 15 * time.Second,
475+
Sync: true,
476+
},
477+
}
478+
479+
err := plugin.Exec()
480+
assert.NotNil(t, err)
481+
482+
end := time.Now()
483+
assert.WithinDuration(t, start.Local().Add(15*time.Second), end, 2*time.Second)
484+
485+
}
486+
487+
// TestConnectTimeoutRetry tests that when a non-network error occurs, the connect
488+
// is not retried and instead returns the error immediately.
489+
func TestConnectTimeoutImmediate(t *testing.T) {
490+
start := time.Now()
491+
492+
plugin := Plugin{
493+
Config: Config{
494+
Host: []string{"localhost"},
495+
UserName: "drone-scp",
496+
Port: 22,
497+
Key: "",
498+
Script: []string{"exit"},
499+
RetryTimeout: 60 * time.Second,
500+
Sync: true,
501+
},
502+
}
503+
504+
err := plugin.Exec()
505+
assert.NotNil(t, err)
506+
507+
end := time.Now()
508+
assert.WithinDuration(t, start.Local().Add(time.Second), end, 2*time.Second)
509+
510+
}
511+
460512
func unindent(text string) string {
461513
return strings.TrimSpace(strings.Replace(text, "\t", "", -1))
462514
}

0 commit comments

Comments
 (0)