@@ -1647,67 +1647,85 @@ func TestSSH(t *testing.T) {
1647
1647
}
1648
1648
})
1649
1649
1650
- t .Run ("SSHHostPrefix " , func (t * testing.T ) {
1650
+ t .Run ("SSHHost " , func (t * testing.T ) {
1651
1651
t .Parallel ()
1652
- client , workspace , agentToken := setupWorkspaceForAgent (t )
1653
- _ , _ = tGoContext (t , func (ctx context.Context ) {
1654
- // Run this async so the SSH command has to wait for
1655
- // the build and agent to connect!
1656
- _ = agenttest .New (t , client .URL , agentToken )
1657
- <- ctx .Done ()
1658
- })
1659
1652
1660
- clientOutput , clientInput := io .Pipe ()
1661
- serverOutput , serverInput := io .Pipe ()
1662
- defer func () {
1663
- for _ , c := range []io.Closer {clientOutput , clientInput , serverOutput , serverInput } {
1664
- _ = c .Close ()
1665
- }
1666
- }()
1653
+ testCases := []struct {
1654
+ name , hostnameFormat string
1655
+ flags []string
1656
+ }{
1657
+ {"Prefix" , "coder.dummy.com--%s--%s" , []string {"--ssh-host-prefix" , "coder.dummy.com--" }},
1658
+ {"Suffix" , "%s--%s.coder" , []string {"--hostname-suffix" , "coder" }},
1659
+ {"Both" , "%s--%s.coder" , []string {"--hostname-suffix" , "coder" , "--ssh-host-prefix" , "coder.dummy.com--" }},
1660
+ }
1661
+ for _ , tc := range testCases {
1662
+ t .Run (tc .name , func (t * testing.T ) {
1663
+ t .Parallel ()
1667
1664
1668
- ctx , cancel := context .WithTimeout (context .Background (), testutil .WaitLong )
1669
- defer cancel ()
1665
+ client , workspace , agentToken := setupWorkspaceForAgent (t )
1666
+ _ , _ = tGoContext (t , func (ctx context.Context ) {
1667
+ // Run this async so the SSH command has to wait for
1668
+ // the build and agent to connect!
1669
+ _ = agenttest .New (t , client .URL , agentToken )
1670
+ <- ctx .Done ()
1671
+ })
1670
1672
1671
- user , err := client .User (ctx , codersdk .Me )
1672
- require .NoError (t , err )
1673
+ clientOutput , clientInput := io .Pipe ()
1674
+ serverOutput , serverInput := io .Pipe ()
1675
+ defer func () {
1676
+ for _ , c := range []io.Closer {clientOutput , clientInput , serverOutput , serverInput } {
1677
+ _ = c .Close ()
1678
+ }
1679
+ }()
1673
1680
1674
- inv , root := clitest .New (t , "ssh" , "--stdio" , "--ssh-host-prefix" , "coder.dummy.com--" , fmt .Sprintf ("coder.dummy.com--%s--%s" , user .Username , workspace .Name ))
1675
- clitest .SetupConfig (t , client , root )
1676
- inv .Stdin = clientOutput
1677
- inv .Stdout = serverInput
1678
- inv .Stderr = io .Discard
1681
+ ctx , cancel := context .WithTimeout (context .Background (), testutil .WaitLong )
1682
+ defer cancel ()
1679
1683
1680
- cmdDone := tGo (t , func () {
1681
- err := inv .WithContext (ctx ).Run ()
1682
- assert .NoError (t , err )
1683
- })
1684
+ user , err := client .User (ctx , codersdk .Me )
1685
+ require .NoError (t , err )
1684
1686
1685
- conn , channels , requests , err := ssh .NewClientConn (& stdioConn {
1686
- Reader : serverOutput ,
1687
- Writer : clientInput ,
1688
- }, "" , & ssh.ClientConfig {
1689
- // #nosec
1690
- HostKeyCallback : ssh .InsecureIgnoreHostKey (),
1691
- })
1692
- require .NoError (t , err )
1693
- defer conn .Close ()
1687
+ args := []string {"ssh" , "--stdio" }
1688
+ args = append (args , tc .flags ... )
1689
+ args = append (args , fmt .Sprintf (tc .hostnameFormat , user .Username , workspace .Name ))
1690
+ inv , root := clitest .New (t , args ... )
1691
+ clitest .SetupConfig (t , client , root )
1692
+ inv .Stdin = clientOutput
1693
+ inv .Stdout = serverInput
1694
+ inv .Stderr = io .Discard
1694
1695
1695
- sshClient := ssh . NewClient ( conn , channels , requests )
1696
- session , err := sshClient . NewSession ()
1697
- require .NoError (t , err )
1698
- defer session . Close ( )
1696
+ cmdDone := tGo ( t , func () {
1697
+ err := inv . WithContext ( ctx ). Run ()
1698
+ assert .NoError (t , err )
1699
+ } )
1699
1700
1700
- command := "sh -c exit"
1701
- if runtime . GOOS == "windows" {
1702
- command = "cmd.exe /c exit"
1703
- }
1704
- err = session . Run ( command )
1705
- require . NoError ( t , err )
1706
- err = sshClient . Close ( )
1707
- require .NoError (t , err )
1708
- _ = clientOutput .Close ()
1701
+ conn , channels , requests , err := ssh . NewClientConn ( & stdioConn {
1702
+ Reader : serverOutput ,
1703
+ Writer : clientInput ,
1704
+ }, "" , & ssh. ClientConfig {
1705
+ // #nosec
1706
+ HostKeyCallback : ssh . InsecureIgnoreHostKey (),
1707
+ } )
1708
+ require .NoError (t , err )
1709
+ defer conn .Close ()
1709
1710
1710
- <- cmdDone
1711
+ sshClient := ssh .NewClient (conn , channels , requests )
1712
+ session , err := sshClient .NewSession ()
1713
+ require .NoError (t , err )
1714
+ defer session .Close ()
1715
+
1716
+ command := "sh -c exit"
1717
+ if runtime .GOOS == "windows" {
1718
+ command = "cmd.exe /c exit"
1719
+ }
1720
+ err = session .Run (command )
1721
+ require .NoError (t , err )
1722
+ err = sshClient .Close ()
1723
+ require .NoError (t , err )
1724
+ _ = clientOutput .Close ()
1725
+
1726
+ <- cmdDone
1727
+ })
1728
+ }
1711
1729
})
1712
1730
}
1713
1731
0 commit comments