Skip to content

Commit

Permalink
Disable disk-based recording for TestWebAgentForward
Browse files Browse the repository at this point in the history
Also improve the correctness of web tests that use waitForOutput
by checking if the output was received even when the read fails
with an error.

Fixes #17918
  • Loading branch information
zmb3 committed Dec 21, 2022
1 parent 5376f1a commit b734b6b
Showing 1 changed file with 29 additions and 8 deletions.
37 changes: 29 additions & 8 deletions lib/web/apiserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ func newWebSuite(t *testing.T) *WebSuite {
type webSuiteConfig struct {
// AuthPreferenceSpec is custom initial AuthPreference spec for the test.
authPreferenceSpec *types.AuthPreferenceSpecV2

disableDiskBasedRecording bool
}

func newWebSuiteWithConfig(t *testing.T, cfg webSuiteConfig) *WebSuite {
Expand All @@ -190,17 +192,33 @@ func newWebSuiteWithConfig(t *testing.T, cfg webSuiteConfig) *WebSuite {
})
require.NoError(t, err)

s.server, err = auth.NewTestServer(auth.TestServerConfig{
authCfg := auth.TestServerConfig{
Auth: auth.TestAuthServerConfig{
ClusterName: "localhost",
Dir: t.TempDir(),
Clock: s.clock,
ClusterNetworkingConfig: networkingConfig,
AuthPreferenceSpec: cfg.authPreferenceSpec,

AuthPreferenceSpec: cfg.authPreferenceSpec,
},
})
}

if cfg.disableDiskBasedRecording {
authCfg.Auth.AuditLog = events.NewDiscardAuditLog()
}

s.server, err = auth.NewTestServer(authCfg)
require.NoError(t, err)

if cfg.disableDiskBasedRecording {
// use a sync recording mode because the disk-based uploader
// that runs in the background introduces races with test cleanup
recConfig := types.DefaultSessionRecordingConfig()
recConfig.SetMode(types.RecordAtNodeSync)
err := s.server.AuthServer.AuthServer.SetSessionRecordingConfig(context.Background(), recConfig)
require.NoError(t, err)
}

// Register the auth server, since test auth server doesn't start its own
// heartbeat.
err = s.server.Auth().UpsertAuthServer(&types.ServerV2{
Expand Down Expand Up @@ -1784,7 +1802,7 @@ func handleMFAWebauthnChallenge(t *testing.T, ws *websocket.Conn, dev *auth.Test

func TestWebAgentForward(t *testing.T) {
t.Parallel()
s := newWebSuite(t)
s := newWebSuiteWithConfig(t, webSuiteConfig{disableDiskBasedRecording: true})
ws, _, err := s.makeTerminal(t, s.authPack(t, "foo"))
require.NoError(t, err)
t.Cleanup(func() { require.NoError(t, ws.Close()) })
Expand Down Expand Up @@ -6084,13 +6102,16 @@ func waitForOutput(stream *terminalStream, substr string) error {
}

out := make([]byte, 100)
_, err := stream.Read(out)
n, err := stream.Read(out)

// check for the string before checking the error,
// as it's valid for n > 0 even when there is an error
if n > 0 && strings.Contains(removeSpace(string(out[:n])), substr) {
return nil
}
if err != nil {
return trace.Wrap(err)
}
if strings.Contains(removeSpace(string(out)), substr) {
return nil
}
}
}

Expand Down

0 comments on commit b734b6b

Please sign in to comment.