fix: do terminal emulation in reconnecting pty tests (#9114)

It looks like it is possible for screen to use control sequences instead
of literal newlines which fails the tests.

This reuses the existing readUntil function used in other pty tests.
This commit is contained in:
Asher
2023-08-16 13:02:03 -08:00
committed by GitHub
parent 74999305b6
commit 02ee724d9f
4 changed files with 97 additions and 109 deletions

View File

@ -12,7 +12,6 @@ import (
"net/http/httputil"
"net/url"
"path"
"regexp"
"runtime"
"strconv"
"strings"
@ -32,10 +31,6 @@ import (
"github.com/coder/coder/testutil"
)
const ansi = "[\u001B\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[a-zA-Z\\d]*)*)?\u0007)|(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PRZcf-ntqry=><~]))"
var re = regexp.MustCompile(ansi)
// Run runs the entire workspace app test suite against deployments minted
// by the provided factory.
//
@ -70,8 +65,8 @@ func Run(t *testing.T, appHostIsPrimary bool, factory DeploymentFactory) {
testReconnectingPTY(ctx, t, client, codersdk.WorkspaceAgentReconnectingPTYOpts{
AgentID: appDetails.Agent.ID,
Reconnect: uuid.New(),
Height: 80,
Width: 80,
Height: 100,
Width: 100,
Command: "bash",
})
})
@ -104,8 +99,8 @@ func Run(t *testing.T, appHostIsPrimary bool, factory DeploymentFactory) {
testReconnectingPTY(ctx, t, unauthedAppClient, codersdk.WorkspaceAgentReconnectingPTYOpts{
AgentID: appDetails.Agent.ID,
Reconnect: uuid.New(),
Height: 80,
Width: 80,
Height: 100,
Width: 100,
Command: "bash",
SignedToken: issueRes.SignedToken,
})
@ -1407,16 +1402,6 @@ func (r *fakeStatsReporter) Report(_ context.Context, stats []workspaceapps.Stat
}
func testReconnectingPTY(ctx context.Context, t *testing.T, client *codersdk.Client, opts codersdk.WorkspaceAgentReconnectingPTYOpts) {
hasLine := func(scanner *bufio.Scanner, matcher func(string) bool) bool {
for scanner.Scan() {
line := scanner.Text()
t.Logf("bash tty stdout = %s", re.ReplaceAllString(line, ""))
if matcher(line) {
return true
}
}
return false
}
matchEchoCommand := func(line string) bool {
return strings.Contains(line, "echo test")
}
@ -1437,13 +1422,12 @@ func testReconnectingPTY(ctx context.Context, t *testing.T, client *codersdk.Cli
// First attempt to resize the TTY.
// The websocket will close if it fails!
data, err := json.Marshal(codersdk.ReconnectingPTYRequest{
Height: 250,
Width: 250,
Height: 80,
Width: 80,
})
require.NoError(t, err)
_, err = conn.Write(data)
require.NoError(t, err)
scanner := bufio.NewScanner(conn)
// Brief pause to reduce the likelihood that we send keystrokes while
// the shell is simultaneously sending a prompt.
@ -1456,8 +1440,8 @@ func testReconnectingPTY(ctx context.Context, t *testing.T, client *codersdk.Cli
_, err = conn.Write(data)
require.NoError(t, err)
require.True(t, hasLine(scanner, matchEchoCommand), "find echo command")
require.True(t, hasLine(scanner, matchEchoOutput), "find echo output")
require.NoError(t, testutil.ReadUntil(ctx, t, conn, matchEchoCommand), "find echo command")
require.NoError(t, testutil.ReadUntil(ctx, t, conn, matchEchoOutput), "find echo output")
// Exit should cause the connection to close.
data, err = json.Marshal(codersdk.ReconnectingPTYRequest{
@ -1468,12 +1452,9 @@ func testReconnectingPTY(ctx context.Context, t *testing.T, client *codersdk.Cli
require.NoError(t, err)
// Once for the input and again for the output.
require.True(t, hasLine(scanner, matchExitCommand), "find exit command")
require.True(t, hasLine(scanner, matchExitOutput), "find exit output")
require.NoError(t, testutil.ReadUntil(ctx, t, conn, matchExitCommand), "find exit command")
require.NoError(t, testutil.ReadUntil(ctx, t, conn, matchExitOutput), "find exit output")
// Ensure the connection closes.
for scanner.Scan() {
line := scanner.Text()
t.Logf("bash tty stdout = %s", re.ReplaceAllString(line, ""))
}
require.ErrorIs(t, testutil.ReadUntil(ctx, t, conn, nil), io.EOF)
}