|
8 | 8 | "github.com/appleboy/easyssh-proxy"
|
9 | 9 | "github.com/stretchr/testify/assert"
|
10 | 10 | "strings"
|
| 11 | + |
| 12 | + "time" |
11 | 13 | )
|
12 | 14 |
|
13 | 15 | func TestMissingHostOrUser(t *testing.T) {
|
@@ -457,6 +459,56 @@ func TestEnvOutput(t *testing.T) {
|
457 | 459 | assert.Equal(t, unindent(expected), unindent(buffer.String()))
|
458 | 460 | }
|
459 | 461 |
|
| 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 | + |
460 | 512 | func unindent(text string) string {
|
461 | 513 | return strings.TrimSpace(strings.Replace(text, "\t", "", -1))
|
462 | 514 | }
|
0 commit comments