TestProxyDialTimeoutInRun tests timeout detection in Run method
(t *testing.T)
| 590 | |
| 591 | // TestProxyDialTimeoutInRun tests timeout detection in Run method |
| 592 | func TestProxyDialTimeoutInRun(t *testing.T) { |
| 593 | ssh := &MakeConfig{ |
| 594 | Server: "example.com", |
| 595 | User: "testuser", |
| 596 | Port: "22", |
| 597 | KeyPath: "./tests/.ssh/id_rsa", |
| 598 | Timeout: 2 * time.Second, |
| 599 | Proxy: DefaultConfig{ |
| 600 | User: "testuser", |
| 601 | Server: "127.0.0.1", // Assume localhost SSH exists |
| 602 | Port: "22", |
| 603 | KeyPath: "./tests/.ssh/id_rsa", |
| 604 | Timeout: 2 * time.Second, |
| 605 | }, |
| 606 | } |
| 607 | |
| 608 | // Mock a scenario where Connect() returns ErrProxyDialTimeout |
| 609 | // by temporarily changing the target to a non-routable address |
| 610 | ssh.Server = "10.255.255.1" |
| 611 | |
| 612 | start := time.Now() |
| 613 | outStr, errStr, isTimeout, err := ssh.Run("whoami") |
| 614 | elapsed := time.Since(start) |
| 615 | |
| 616 | // Should timeout within reasonable bounds |
| 617 | assert.True(t, elapsed < 5*time.Second, "Should timeout within 5 seconds, took %v", elapsed) |
| 618 | |
| 619 | // Should return empty output |
| 620 | assert.Equal(t, "", outStr) |
| 621 | assert.Equal(t, "", errStr) |
| 622 | |
| 623 | // Should have error |
| 624 | assert.NotNil(t, err) |
| 625 | |
| 626 | // If it's specifically a proxy dial timeout, isTimeout should be true |
| 627 | if errors.Is(err, ErrProxyDialTimeout) { |
| 628 | assert.True(t, isTimeout, "isTimeout should be true for proxy dial timeout") |
| 629 | } |
| 630 | } |
| 631 | |
| 632 | // TestProxyGoroutineLeak tests that no goroutines are leaked when proxy dial times out |
| 633 | func TestProxyGoroutineLeak(t *testing.T) { |