mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
test: don't use ptytest for client side of SSH session tests (#27681)
In our initial batches of test refactors, I left the SSH session tests using `ptytest` because I (erroneously) thought that we still needed a client side PTY when the SSH server creates a PTY. This is incorrect and plain in-process IO is fine on the client side. closes https://github.com/coder/internal/issues/1400 (again)<!-- If you have used AI to produce some or all of this PR, please ensure you have read our [AI Contribution guidelines](https://coder.com/docs/about/contributing/AI_CONTRIBUTING) before submitting. -->
This commit is contained in:
@@ -16,6 +16,7 @@ import (
|
||||
"github.com/acarl005/stripansi"
|
||||
"github.com/stretchr/testify/require"
|
||||
"go.uber.org/atomic"
|
||||
"golang.org/x/crypto/ssh"
|
||||
"golang.org/x/xerrors"
|
||||
|
||||
"github.com/coder/coder/v2/testutil"
|
||||
@@ -149,6 +150,14 @@ func NewPiped(t *testing.T) (*Expecter, io.Writer) {
|
||||
return e, w
|
||||
}
|
||||
|
||||
func NewAttachedToSSHSession(t *testing.T, session *ssh.Session) *Expecter {
|
||||
e, w := NewPiped(t)
|
||||
e.Rename("sshout")
|
||||
session.Stdout = w
|
||||
session.Stderr = w
|
||||
return e
|
||||
}
|
||||
|
||||
type Expecter struct {
|
||||
t *testing.T
|
||||
out *stdbuf
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"golang.org/x/crypto/ssh"
|
||||
"gvisor.dev/gvisor/pkg/context"
|
||||
|
||||
"cdr.dev/slog/v3"
|
||||
@@ -32,6 +33,20 @@ func NewWriterAttachedToInvocation(t *testing.T, logger slog.Logger, invocation
|
||||
}
|
||||
}
|
||||
|
||||
func NewWriterAttachedToSSHSession(t *testing.T, l slog.Logger, session *ssh.Session) *Writer {
|
||||
r, w := io.Pipe()
|
||||
session.Stdin = r
|
||||
// Close the pipe at the end of the test to ensure any goroutine in the Invocation that reads from stdin won't leak.
|
||||
t.Cleanup(func() {
|
||||
_ = w.Close()
|
||||
})
|
||||
return &Writer{
|
||||
t: t,
|
||||
w: w,
|
||||
l: l,
|
||||
}
|
||||
}
|
||||
|
||||
func (w *Writer) Write(r rune) {
|
||||
w.t.Helper()
|
||||
_, err := w.w.Write([]byte{byte(r)})
|
||||
|
||||
Reference in New Issue
Block a user