@@ -24,6 +24,8 @@ import (
24
24
"testing"
25
25
"time"
26
26
27
+ "github.com/coder/coder/pty"
28
+
27
29
scp "github.com/bramvdbogaerde/go-scp"
28
30
"github.com/google/uuid"
29
31
"github.com/pion/udp"
@@ -481,17 +483,10 @@ func TestAgent_TCPLocalForwarding(t *testing.T) {
481
483
}
482
484
}()
483
485
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 )
486
+ _ , proc := setupSSHCommand (t , []string {"-L" , fmt .Sprintf ("%d:127.0.0.1:%d" , randomPort , remotePort )}, []string {"sleep" , "5" })
492
487
493
488
go func () {
494
- err := cmd .Wait ()
489
+ err := proc .Wait ()
495
490
select {
496
491
case <- done :
497
492
default :
@@ -523,7 +518,7 @@ func TestAgent_TCPLocalForwarding(t *testing.T) {
523
518
524
519
<- done
525
520
526
- _ = cmd . Process .Kill ()
521
+ _ = proc .Kill ()
527
522
}
528
523
529
524
//nolint:paralleltest // This test reserves a port.
@@ -562,17 +557,10 @@ func TestAgent_TCPRemoteForwarding(t *testing.T) {
562
557
}
563
558
}()
564
559
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 )
560
+ _ , proc := setupSSHCommand (t , []string {"-R" , fmt .Sprintf ("127.0.0.1:%d:127.0.0.1:%d" , randomPort , localPort )}, []string {"sleep" , "5" })
573
561
574
562
go func () {
575
- err := cmd .Wait ()
563
+ err := proc .Wait ()
576
564
select {
577
565
case <- done :
578
566
default :
@@ -604,7 +592,7 @@ func TestAgent_TCPRemoteForwarding(t *testing.T) {
604
592
605
593
<- done
606
594
607
- _ = cmd . Process .Kill ()
595
+ _ = proc .Kill ()
608
596
}
609
597
610
598
func TestAgent_UnixLocalForwarding (t * testing.T ) {
@@ -641,17 +629,10 @@ func TestAgent_UnixLocalForwarding(t *testing.T) {
641
629
}
642
630
}()
643
631
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 )
632
+ _ , proc := setupSSHCommand (t , []string {"-L" , fmt .Sprintf ("%s:%s" , localSocketPath , remoteSocketPath )}, []string {"sleep" , "5" })
652
633
653
634
go func () {
654
- err := cmd .Wait ()
635
+ err := proc .Wait ()
655
636
select {
656
637
case <- done :
657
638
default :
@@ -676,7 +657,7 @@ func TestAgent_UnixLocalForwarding(t *testing.T) {
676
657
_ = conn .Close ()
677
658
<- done
678
659
679
- _ = cmd . Process .Kill ()
660
+ _ = proc .Kill ()
680
661
}
681
662
682
663
func TestAgent_UnixRemoteForwarding (t * testing.T ) {
@@ -713,17 +694,10 @@ func TestAgent_UnixRemoteForwarding(t *testing.T) {
713
694
}
714
695
}()
715
696
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 )
697
+ _ , proc := setupSSHCommand (t , []string {"-R" , fmt .Sprintf ("%s:%s" , remoteSocketPath , localSocketPath )}, []string {"sleep" , "5" })
724
698
725
699
go func () {
726
- err := cmd .Wait ()
700
+ err := proc .Wait ()
727
701
select {
728
702
case <- done :
729
703
default :
@@ -750,7 +724,7 @@ func TestAgent_UnixRemoteForwarding(t *testing.T) {
750
724
751
725
<- done
752
726
753
- _ = cmd . Process .Kill ()
727
+ _ = proc .Kill ()
754
728
}
755
729
756
730
func TestAgent_SFTP (t * testing.T ) {
@@ -1629,7 +1603,7 @@ func TestAgent_WriteVSCodeConfigs(t *testing.T) {
1629
1603
}, testutil .WaitShort , testutil .IntervalFast )
1630
1604
}
1631
1605
1632
- func setupSSHCommand (t * testing.T , beforeArgs []string , afterArgs []string ) * exec. Cmd {
1606
+ func setupSSHCommand (t * testing.T , beforeArgs []string , afterArgs []string ) ( * ptytest. PTYCmd , pty. Process ) {
1633
1607
//nolint:dogsled
1634
1608
agentConn , _ , _ , _ , _ := setupAgent (t , agentsdk.Manifest {}, 0 )
1635
1609
listener , err := net .Listen ("tcp" , "127.0.0.1:0" )
@@ -1671,7 +1645,8 @@ func setupSSHCommand(t *testing.T, beforeArgs []string, afterArgs []string) *exe
1671
1645
"host" ,
1672
1646
)
1673
1647
args = append (args , afterArgs ... )
1674
- return exec .Command ("ssh" , args ... )
1648
+ cmd := exec .Command ("ssh" , args ... )
1649
+ return ptytest .Start (t , cmd )
1675
1650
}
1676
1651
1677
1652
func setupSSHSession (t * testing.T , options agentsdk.Manifest ) * ssh.Session {
0 commit comments