MCPcopy Index your code
hub / github.com/coder/coder / TestCoderConnectStdio

Function TestCoderConnectStdio

cli/ssh_internal_test.go:266–322  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

264}
265
266func TestCoderConnectStdio(t *testing.T) {
267 t.Parallel()
268
269 ctx := testutil.Context(t, testutil.WaitShort)
270 logger := slogtest.Make(t, nil).Leveled(slog.LevelDebug)
271 stack := newCloserStack(ctx, logger, quartz.NewMock(t))
272
273 clientOutput, clientInput := io.Pipe()
274 serverOutput, serverInput := io.Pipe()
275 defer func() {
276 for _, c := range []io.Closer{clientOutput, clientInput, serverOutput, serverInput} {
277 _ = c.Close()
278 }
279 }()
280
281 server := newSSHServer("127.0.0.1:0")
282 ln, err := net.Listen("tcp", server.server.Addr)
283 require.NoError(t, err)
284
285 go func() {
286 _ = server.Serve(ln)
287 }()
288 t.Cleanup(func() {
289 _ = server.Close()
290 })
291
292 stdioDone := make(chan struct{})
293 go func() {
294 err = runCoderConnectStdio(ctx, ln.Addr().String(), clientOutput, serverInput, stack, logger)
295 assert.NoError(t, err)
296 close(stdioDone)
297 }()
298
299 conn, channels, requests, err := ssh.NewClientConn(&testutil.ReaderWriterConn{
300 Reader: serverOutput,
301 Writer: clientInput,
302 }, "", &ssh.ClientConfig{
303 // #nosec
304 HostKeyCallback: ssh.InsecureIgnoreHostKey(),
305 })
306 require.NoError(t, err)
307 defer conn.Close()
308
309 sshClient := ssh.NewClient(conn, channels, requests)
310 session, err := sshClient.NewSession()
311 require.NoError(t, err)
312 defer session.Close()
313
314 // We're not connected to a real shell
315 err = session.Run("")
316 require.NoError(t, err)
317 err = sshClient.Close()
318 require.NoError(t, err)
319 _ = clientOutput.Close()
320
321 <-stdioDone
322}
323

Callers

nothing calls this directly

Calls 13

CloseMethod · 0.95
ContextFunction · 0.92
newCloserStackFunction · 0.85
newSSHServerFunction · 0.85
runCoderConnectStdioFunction · 0.85
NewSessionMethod · 0.80
CloseMethod · 0.65
ListenMethod · 0.65
CleanupMethod · 0.65
RunMethod · 0.65
ServeMethod · 0.45
StringMethod · 0.45

Tested by

no test coverage detected