@@ -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 :
@@ -753,7 +726,7 @@ func TestAgent_UnixRemoteForwarding(t *testing.T) {
753
726
754
727
<- done
755
728
756
- _ = cmd . Process .Kill ()
729
+ _ = proc .Kill ()
757
730
}
758
731
759
732
func TestAgent_SFTP (t * testing.T ) {
@@ -1648,7 +1621,7 @@ func TestAgent_WriteVSCodeConfigs(t *testing.T) {
1648
1621
}, testutil .WaitShort , testutil .IntervalFast )
1649
1622
}
1650
1623
1651
- 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 ) {
1652
1625
//nolint:dogsled
1653
1626
agentConn , _ , _ , _ , _ := setupAgent (t , agentsdk.Manifest {}, 0 )
1654
1627
listener , err := net .Listen ("tcp" , "127.0.0.1:0" )
@@ -1690,7 +1663,8 @@ func setupSSHCommand(t *testing.T, beforeArgs []string, afterArgs []string) *exe
1690
1663
"host" ,
1691
1664
)
1692
1665
args = append (args , afterArgs ... )
1693
- return exec .Command ("ssh" , args ... )
1666
+ cmd := exec .Command ("ssh" , args ... )
1667
+ return ptytest .Start (t , cmd )
1694
1668
}
1695
1669
1696
1670
func setupSSHSession (t * testing.T , options agentsdk.Manifest ) * ssh.Session {
@@ -1778,7 +1752,9 @@ func setupAgent(t *testing.T, metadata agentsdk.Manifest, ptyTimeout time.Durati
1778
1752
t .Cleanup (func () {
1779
1753
_ = agentConn .Close ()
1780
1754
})
1781
- 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 )
1782
1758
defer cancel ()
1783
1759
if ! agentConn .AwaitReachable (ctx ) {
1784
1760
t .Fatal ("agent not reachable" )
0 commit comments