@@ -68,15 +68,20 @@ func TestSSH(t *testing.T) {
68
68
t .Parallel ()
69
69
t .Run ("ImmediateExit" , func (t * testing.T ) {
70
70
t .Parallel ()
71
+
71
72
client , workspace , agentToken := setupWorkspaceForSSH (t )
72
73
cmd , root := clitest .New (t , "ssh" , workspace .Name )
73
74
clitest .SetupConfig (t , client , root )
74
75
pty := ptytest .New (t )
75
76
cmd .SetIn (pty .Input ())
76
77
cmd .SetErr (pty .Output ())
77
78
cmd .SetOut (pty .Output ())
79
+
80
+ ctx , cancel := context .WithTimeout (context .Background (), 10 * time .Second )
81
+ defer cancel ()
82
+
78
83
cmdDone := tGo (t , func () {
79
- err := cmd .Execute ( )
84
+ err := cmd .ExecuteContext ( ctx )
80
85
assert .NoError (t , err )
81
86
})
82
87
pty .ExpectMatch ("Waiting" )
@@ -85,9 +90,9 @@ func TestSSH(t *testing.T) {
85
90
agentCloser := agent .New (agentClient .ListenWorkspaceAgent , & agent.Options {
86
91
Logger : slogtest .Make (t , nil ).Leveled (slog .LevelDebug ),
87
92
})
88
- t . Cleanup ( func () {
93
+ defer func () {
89
94
_ = agentCloser .Close ()
90
- })
95
+ }( )
91
96
92
97
// Shells on Mac, Windows, and Linux all exit shells with the "exit" command.
93
98
pty .WriteLine ("exit" )
@@ -113,14 +118,22 @@ func TestSSH(t *testing.T) {
113
118
114
119
clientOutput , clientInput := io .Pipe ()
115
120
serverOutput , serverInput := io .Pipe ()
121
+ defer func () {
122
+ for _ , c := range []io.Closer {clientOutput , clientInput , serverOutput , serverInput } {
123
+ _ = c .Close ()
124
+ }
125
+ }()
126
+
127
+ ctx , cancel := context .WithTimeout (context .Background (), 10 * time .Second )
128
+ defer cancel ()
116
129
117
130
cmd , root := clitest .New (t , "ssh" , "--stdio" , workspace .Name )
118
131
clitest .SetupConfig (t , client , root )
119
132
cmd .SetIn (clientOutput )
120
133
cmd .SetOut (serverInput )
121
134
cmd .SetErr (io .Discard )
122
135
cmdDone := tGo (t , func () {
123
- err := cmd .Execute ( )
136
+ err := cmd .ExecuteContext ( ctx )
124
137
assert .NoError (t , err )
125
138
})
126
139
@@ -132,9 +145,13 @@ func TestSSH(t *testing.T) {
132
145
HostKeyCallback : ssh .InsecureIgnoreHostKey (),
133
146
})
134
147
require .NoError (t , err )
148
+ defer conn .Close ()
149
+
135
150
sshClient := ssh .NewClient (conn , channels , requests )
136
151
session , err := sshClient .NewSession ()
137
152
require .NoError (t , err )
153
+ defer session .Close ()
154
+
138
155
command := "sh -c exit"
139
156
if runtime .GOOS == "windows" {
140
157
command = "cmd.exe /c exit"
@@ -198,6 +215,9 @@ func TestSSH(t *testing.T) {
198
215
}
199
216
})
200
217
218
+ ctx , cancel := context .WithTimeout (context .Background (), 10 * time .Second )
219
+ defer cancel ()
220
+
201
221
cmd , root := clitest .New (t ,
202
222
"ssh" ,
203
223
workspace .Name ,
@@ -210,7 +230,7 @@ func TestSSH(t *testing.T) {
210
230
cmd .SetOut (pty .Output ())
211
231
cmd .SetErr (io .Discard )
212
232
cmdDone := tGo (t , func () {
213
- err := cmd .Execute ( )
233
+ err := cmd .ExecuteContext ( ctx )
214
234
assert .NoError (t , err )
215
235
})
216
236
0 commit comments