@@ -45,6 +45,7 @@ import (
45
45
"github.com/coder/coder/coderd/httpapi"
46
46
"github.com/coder/coder/codersdk"
47
47
"github.com/coder/coder/codersdk/agentsdk"
48
+ "github.com/coder/coder/pty"
48
49
"github.com/coder/coder/pty/ptytest"
49
50
"github.com/coder/coder/tailnet"
50
51
"github.com/coder/coder/tailnet/tailnettest"
@@ -481,17 +482,10 @@ func TestAgent_TCPLocalForwarding(t *testing.T) {
481
482
}
482
483
}()
483
484
484
- pty := ptytest .New (t )
485
-
486
- cmd := setupSSHCommand (t , []string {"-L" , fmt .Sprintf ("%d:127.0.0.1:%d" , randomPort , remotePort )}, []string {"sleep" , "5" })
487
- cmd .Stdin = pty .Input ()
488
- cmd .Stdout = pty .Output ()
489
- cmd .Stderr = pty .Output ()
490
- err = cmd .Start ()
491
- require .NoError (t , err )
485
+ _ , proc := setupSSHCommand (t , []string {"-L" , fmt .Sprintf ("%d:127.0.0.1:%d" , randomPort , remotePort )}, []string {"sleep" , "5" })
492
486
493
487
go func () {
494
- err := cmd .Wait ()
488
+ err := proc .Wait ()
495
489
select {
496
490
case <- done :
497
491
default :
@@ -523,7 +517,7 @@ func TestAgent_TCPLocalForwarding(t *testing.T) {
523
517
524
518
<- done
525
519
526
- _ = cmd . Process .Kill ()
520
+ _ = proc .Kill ()
527
521
}
528
522
529
523
//nolint:paralleltest // This test reserves a port.
@@ -562,17 +556,10 @@ func TestAgent_TCPRemoteForwarding(t *testing.T) {
562
556
}
563
557
}()
564
558
565
- pty := ptytest .New (t )
566
-
567
- cmd := setupSSHCommand (t , []string {"-R" , fmt .Sprintf ("127.0.0.1:%d:127.0.0.1:%d" , randomPort , localPort )}, []string {"sleep" , "5" })
568
- cmd .Stdin = pty .Input ()
569
- cmd .Stdout = pty .Output ()
570
- cmd .Stderr = pty .Output ()
571
- err = cmd .Start ()
572
- require .NoError (t , err )
559
+ _ , proc := setupSSHCommand (t , []string {"-R" , fmt .Sprintf ("127.0.0.1:%d:127.0.0.1:%d" , randomPort , localPort )}, []string {"sleep" , "5" })
573
560
574
561
go func () {
575
- err := cmd .Wait ()
562
+ err := proc .Wait ()
576
563
select {
577
564
case <- done :
578
565
default :
@@ -604,7 +591,7 @@ func TestAgent_TCPRemoteForwarding(t *testing.T) {
604
591
605
592
<- done
606
593
607
- _ = cmd . Process .Kill ()
594
+ _ = proc .Kill ()
608
595
}
609
596
610
597
func TestAgent_UnixLocalForwarding (t * testing.T ) {
@@ -641,17 +628,10 @@ func TestAgent_UnixLocalForwarding(t *testing.T) {
641
628
}
642
629
}()
643
630
644
- pty := ptytest .New (t )
645
-
646
- cmd := setupSSHCommand (t , []string {"-L" , fmt .Sprintf ("%s:%s" , localSocketPath , remoteSocketPath )}, []string {"sleep" , "5" })
647
- cmd .Stdin = pty .Input ()
648
- cmd .Stdout = pty .Output ()
649
- cmd .Stderr = pty .Output ()
650
- err = cmd .Start ()
651
- require .NoError (t , err )
631
+ _ , proc := setupSSHCommand (t , []string {"-L" , fmt .Sprintf ("%s:%s" , localSocketPath , remoteSocketPath )}, []string {"sleep" , "5" })
652
632
653
633
go func () {
654
- err := cmd .Wait ()
634
+ err := proc .Wait ()
655
635
select {
656
636
case <- done :
657
637
default :
@@ -676,7 +656,7 @@ func TestAgent_UnixLocalForwarding(t *testing.T) {
676
656
_ = conn .Close ()
677
657
<- done
678
658
679
- _ = cmd . Process .Kill ()
659
+ _ = proc .Kill ()
680
660
}
681
661
682
662
func TestAgent_UnixRemoteForwarding (t * testing.T ) {
@@ -713,17 +693,10 @@ func TestAgent_UnixRemoteForwarding(t *testing.T) {
713
693
}
714
694
}()
715
695
716
- pty := ptytest .New (t )
717
-
718
- cmd := setupSSHCommand (t , []string {"-R" , fmt .Sprintf ("%s:%s" , remoteSocketPath , localSocketPath )}, []string {"sleep" , "5" })
719
- cmd .Stdin = pty .Input ()
720
- cmd .Stdout = pty .Output ()
721
- cmd .Stderr = pty .Output ()
722
- err = cmd .Start ()
723
- require .NoError (t , err )
696
+ _ , proc := setupSSHCommand (t , []string {"-R" , fmt .Sprintf ("%s:%s" , remoteSocketPath , localSocketPath )}, []string {"sleep" , "5" })
724
697
725
698
go func () {
726
- err := cmd .Wait ()
699
+ err := proc .Wait ()
727
700
select {
728
701
case <- done :
729
702
default :
@@ -733,12 +706,15 @@ func TestAgent_UnixRemoteForwarding(t *testing.T) {
733
706
734
707
// It's possible that the socket is created but the server is not ready to
735
708
// accept connections yet. We need to retry until we can connect.
709
+ //
710
+ // Note that we wait long here because if the tailnet connection has trouble
711
+ // connecting, it could take 5 seconds or more to reconnect.
736
712
var conn net.Conn
737
713
require .Eventually (t , func () bool {
738
714
var err error
739
715
conn , err = net .Dial ("unix" , remoteSocketPath )
740
716
return err == nil
741
- }, testutil .WaitShort , testutil .IntervalFast )
717
+ }, testutil .WaitLong , testutil .IntervalFast )
742
718
defer conn .Close ()
743
719
_ , err = conn .Write ([]byte ("test" ))
744
720
require .NoError (t , err )
@@ -750,7 +726,7 @@ func TestAgent_UnixRemoteForwarding(t *testing.T) {
750
726
751
727
<- done
752
728
753
- _ = cmd . Process .Kill ()
729
+ _ = proc .Kill ()
754
730
}
755
731
756
732
func TestAgent_SFTP (t * testing.T ) {
@@ -1645,7 +1621,7 @@ func TestAgent_WriteVSCodeConfigs(t *testing.T) {
1645
1621
}, testutil .WaitShort , testutil .IntervalFast )
1646
1622
}
1647
1623
1648
- func setupSSHCommand (t * testing.T , beforeArgs []string , afterArgs []string ) * exec. Cmd {
1624
+ func setupSSHCommand (t * testing.T , beforeArgs []string , afterArgs []string ) ( * ptytest. PTYCmd , pty. Process ) {
1649
1625
//nolint:dogsled
1650
1626
agentConn , _ , _ , _ , _ := setupAgent (t , agentsdk.Manifest {}, 0 )
1651
1627
listener , err := net .Listen ("tcp" , "127.0.0.1:0" )
@@ -1687,7 +1663,8 @@ func setupSSHCommand(t *testing.T, beforeArgs []string, afterArgs []string) *exe
1687
1663
"host" ,
1688
1664
)
1689
1665
args = append (args , afterArgs ... )
1690
- return exec .Command ("ssh" , args ... )
1666
+ cmd := exec .Command ("ssh" , args ... )
1667
+ return ptytest .Start (t , cmd )
1691
1668
}
1692
1669
1693
1670
func setupSSHSession (t * testing.T , options agentsdk.Manifest ) * ssh.Session {
@@ -1775,7 +1752,9 @@ func setupAgent(t *testing.T, metadata agentsdk.Manifest, ptyTimeout time.Durati
1775
1752
t .Cleanup (func () {
1776
1753
_ = agentConn .Close ()
1777
1754
})
1778
- ctx , cancel := context .WithTimeout (context .Background (), testutil .WaitMedium )
1755
+ // Ideally we wouldn't wait too long here, but sometimes the the
1756
+ // networking needs more time to resolve itself.
1757
+ ctx , cancel := context .WithTimeout (context .Background (), testutil .WaitLong )
1779
1758
defer cancel ()
1780
1759
if ! agentConn .AwaitReachable (ctx ) {
1781
1760
t .Fatal ("agent not reachable" )
0 commit comments