Show how long each SSH and reconnecting PTY connection lasted, and let users find connections that share a client session ID. No schema changes. API (ssh_info): - Add client_session_id, the ID supplied by the client (the Coder CLI or a tool that launches it), if any. - Add a client_session_id:<32 hex chars> filter. UI: - Each row shows the connect → disconnect time range and the duration as badges. A connection with no disconnect shows "Ongoing", equal timestamps show an "Unknown" start, and a disconnect recorded before its connect is labeled as such. - The info icon opens a details popover instead of a hover tooltip. It uses the default popover styling and sentence-case labels, and lets users copy the client session ID and open "View related connections" for the same workspace. Closes CLIENT-1010
507 lines
19 KiB
Go
507 lines
19 KiB
Go
package coderd_test
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net"
|
|
"net/http"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/sqlc-dev/pqtype"
|
|
"github.com/stretchr/testify/require"
|
|
"google.golang.org/protobuf/types/known/timestamppb"
|
|
|
|
agentproto "github.com/coder/coder/v2/agent/proto"
|
|
"github.com/coder/coder/v2/coderd/coderdtest"
|
|
"github.com/coder/coder/v2/coderd/database"
|
|
"github.com/coder/coder/v2/coderd/database/dbauthz"
|
|
"github.com/coder/coder/v2/coderd/database/dbfake"
|
|
"github.com/coder/coder/v2/coderd/database/dbgen"
|
|
"github.com/coder/coder/v2/coderd/database/dbtestutil"
|
|
"github.com/coder/coder/v2/coderd/database/dbtime"
|
|
"github.com/coder/coder/v2/codersdk"
|
|
"github.com/coder/coder/v2/codersdk/agentsdk"
|
|
"github.com/coder/coder/v2/enterprise/coderd/coderdenttest"
|
|
entconnectionlog "github.com/coder/coder/v2/enterprise/coderd/connectionlog"
|
|
"github.com/coder/coder/v2/enterprise/coderd/license"
|
|
"github.com/coder/coder/v2/testutil"
|
|
)
|
|
|
|
func TestConnectionLogs(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
createWorkspace := func(t *testing.T, db database.Store) database.WorkspaceTable {
|
|
u := dbgen.User(t, db, database.User{})
|
|
o := dbgen.Organization(t, db, database.Organization{})
|
|
tpl := dbgen.Template(t, db, database.Template{
|
|
OrganizationID: o.ID,
|
|
CreatedBy: u.ID,
|
|
})
|
|
return dbgen.Workspace(t, db, database.WorkspaceTable{
|
|
ID: uuid.New(),
|
|
OwnerID: u.ID,
|
|
OrganizationID: o.ID,
|
|
AutomaticUpdates: database.AutomaticUpdatesNever,
|
|
TemplateID: tpl.ID,
|
|
})
|
|
}
|
|
|
|
t.Run("OK", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
ctx := context.Background()
|
|
client, db, _ := coderdenttest.NewWithDatabase(t, &coderdenttest.Options{
|
|
ConnectionLogging: true,
|
|
LicenseOptions: &coderdenttest.LicenseOptions{
|
|
Features: license.Features{
|
|
codersdk.FeatureAuditLog: 1,
|
|
codersdk.FeatureConnectionLog: 1,
|
|
},
|
|
},
|
|
})
|
|
|
|
ws := createWorkspace(t, db)
|
|
_ = dbgen.ConnectionLog(t, db, database.UpsertConnectionLogParams{
|
|
ConnectionMethod: database.ConnectionLogMethodSSH,
|
|
WorkspaceID: ws.ID,
|
|
OrganizationID: ws.OrganizationID,
|
|
WorkspaceOwnerID: ws.OwnerID,
|
|
})
|
|
|
|
logs, err := client.ConnectionLogs(ctx, codersdk.ConnectionLogsRequest{})
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, logs.ConnectionLogs, 1)
|
|
require.EqualValues(t, 1, logs.Count)
|
|
require.Equal(t, string(codersdk.ConnectionTypeSSH), logs.ConnectionLogs[0].Type)
|
|
require.Equal(t, codersdk.ConnectionLogMethodSSH, logs.ConnectionLogs[0].ConnectionMethod)
|
|
require.Empty(t, logs.ConnectionLogs[0].AppName)
|
|
require.Empty(t, logs.ConnectionLogs[0].AppDisplayName, "no display name is made up for a missing app")
|
|
})
|
|
|
|
// The filter semantics are covered by the database tests. This checks that
|
|
// method, app and type queries reach them.
|
|
t.Run("Filters", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
ctx := context.Background()
|
|
client, db, _ := coderdenttest.NewWithDatabase(t, &coderdenttest.Options{
|
|
ConnectionLogging: true,
|
|
LicenseOptions: &coderdenttest.LicenseOptions{
|
|
Features: license.Features{
|
|
codersdk.FeatureAuditLog: 1,
|
|
codersdk.FeatureConnectionLog: 1,
|
|
},
|
|
},
|
|
})
|
|
|
|
ws := createWorkspace(t, db)
|
|
const sessionID = "0123456789abcdef0123456789abcdef"
|
|
insert := func(method database.ConnectionLogMethod, appNameOrPort, clientSessionID string) uuid.UUID {
|
|
return dbgen.ConnectionLog(t, db, database.UpsertConnectionLogParams{
|
|
ClientSessionID: sql.NullString{String: clientSessionID, Valid: clientSessionID != ""},
|
|
ConnectionMethod: method,
|
|
AppNameOrPort: sql.NullString{String: appNameOrPort, Valid: appNameOrPort != ""},
|
|
WorkspaceID: ws.ID,
|
|
OrganizationID: ws.OrganizationID,
|
|
WorkspaceOwnerID: ws.OwnerID,
|
|
}).ID
|
|
}
|
|
sshCursor := insert(database.ConnectionLogMethodSSH, "cursor", sessionID)
|
|
ptyCursor := insert(database.ConnectionLogMethodReconnectingPTY, "cursor", sessionID)
|
|
_ = insert(database.ConnectionLogMethodSSH, "", "")
|
|
// A workspace app slugged like an IDE is still a workspace app.
|
|
workspaceCursor := insert(database.ConnectionLogMethodWorkspaceApp, "cursor", sessionID)
|
|
|
|
query := func(q string) []uuid.UUID {
|
|
logs, err := client.ConnectionLogs(ctx, codersdk.ConnectionLogsRequest{SearchQuery: q})
|
|
require.NoError(t, err, q)
|
|
require.EqualValues(t, len(logs.ConnectionLogs), logs.Count, "count must match the listed logs for %q", q)
|
|
var ids []uuid.UUID
|
|
for _, l := range logs.ConnectionLogs {
|
|
ids = append(ids, l.ID)
|
|
}
|
|
return ids
|
|
}
|
|
|
|
require.ElementsMatch(t, []uuid.UUID{sshCursor}, query("type:vscode"))
|
|
require.ElementsMatch(t, []uuid.UUID{sshCursor, ptyCursor}, query("app:cursor"))
|
|
require.ElementsMatch(t, []uuid.UUID{ptyCursor}, query("method:reconnecting_pty app:cursor"))
|
|
require.ElementsMatch(t, []uuid.UUID{sshCursor, ptyCursor, workspaceCursor}, query("client_session_id:"+sessionID+" workspace_id:"+ws.ID.String()))
|
|
require.Empty(t, query("client_session_id:"+sessionID+" workspace_id:"+uuid.NewString()))
|
|
|
|
_, err := client.ConnectionLogs(ctx, codersdk.ConnectionLogsRequest{SearchQuery: "method:vscode"})
|
|
var sdkErr *codersdk.Error
|
|
require.ErrorAs(t, err, &sdkErr)
|
|
require.Equal(t, http.StatusBadRequest, sdkErr.StatusCode())
|
|
})
|
|
|
|
t.Run("Empty", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
ctx := context.Background()
|
|
client, _, _ := coderdenttest.NewWithDatabase(t, &coderdenttest.Options{
|
|
ConnectionLogging: true,
|
|
LicenseOptions: &coderdenttest.LicenseOptions{
|
|
Features: license.Features{
|
|
codersdk.FeatureAuditLog: 1,
|
|
codersdk.FeatureConnectionLog: 1,
|
|
},
|
|
},
|
|
})
|
|
|
|
logs, err := client.ConnectionLogs(ctx, codersdk.ConnectionLogsRequest{})
|
|
require.NoError(t, err)
|
|
require.EqualValues(t, 0, logs.Count)
|
|
require.Len(t, logs.ConnectionLogs, 0)
|
|
})
|
|
|
|
t.Run("ByOrganizationIDAndName", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
ctx := context.Background()
|
|
client, db, _ := coderdenttest.NewWithDatabase(t, &coderdenttest.Options{
|
|
ConnectionLogging: true,
|
|
LicenseOptions: &coderdenttest.LicenseOptions{
|
|
Features: license.Features{
|
|
codersdk.FeatureAuditLog: 1,
|
|
codersdk.FeatureConnectionLog: 1,
|
|
},
|
|
},
|
|
})
|
|
|
|
org := dbgen.Organization(t, db, database.Organization{})
|
|
ws := createWorkspace(t, db)
|
|
_ = dbgen.ConnectionLog(t, db, database.UpsertConnectionLogParams{
|
|
ConnectionMethod: database.ConnectionLogMethodSSH,
|
|
WorkspaceID: ws.ID,
|
|
OrganizationID: org.ID,
|
|
WorkspaceOwnerID: ws.OwnerID,
|
|
})
|
|
_ = dbgen.ConnectionLog(t, db, database.UpsertConnectionLogParams{
|
|
ConnectionMethod: database.ConnectionLogMethodSSH,
|
|
WorkspaceID: ws.ID,
|
|
OrganizationID: ws.OrganizationID,
|
|
WorkspaceOwnerID: ws.OwnerID,
|
|
})
|
|
|
|
// By name
|
|
logs, err := client.ConnectionLogs(ctx, codersdk.ConnectionLogsRequest{
|
|
SearchQuery: fmt.Sprintf("organization:%s", org.Name),
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, logs.ConnectionLogs, 1)
|
|
require.Equal(t, org.ID, logs.ConnectionLogs[0].Organization.ID)
|
|
|
|
// By ID
|
|
logs, err = client.ConnectionLogs(ctx, codersdk.ConnectionLogsRequest{
|
|
SearchQuery: fmt.Sprintf("organization:%s", ws.OrganizationID),
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, logs.ConnectionLogs, 1)
|
|
require.EqualValues(t, 1, logs.Count)
|
|
require.Equal(t, ws.OrganizationID, logs.ConnectionLogs[0].Organization.ID)
|
|
})
|
|
|
|
t.Run("WebInfo", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
ctx := context.Background()
|
|
client, db, _ := coderdenttest.NewWithDatabase(t, &coderdenttest.Options{
|
|
ConnectionLogging: true,
|
|
LicenseOptions: &coderdenttest.LicenseOptions{
|
|
Features: license.Features{
|
|
codersdk.FeatureAuditLog: 1,
|
|
codersdk.FeatureConnectionLog: 1,
|
|
},
|
|
},
|
|
})
|
|
|
|
now := dbtime.Now()
|
|
connID := uuid.New()
|
|
ws := createWorkspace(t, db)
|
|
clog := dbgen.ConnectionLog(t, db, database.UpsertConnectionLogParams{
|
|
Time: now.Add(-time.Hour),
|
|
ConnectionMethod: database.ConnectionLogMethodWorkspaceApp,
|
|
WorkspaceID: ws.ID,
|
|
OrganizationID: ws.OrganizationID,
|
|
WorkspaceOwnerID: ws.OwnerID,
|
|
ConnectionID: uuid.NullUUID{UUID: connID, Valid: true},
|
|
UserAgent: sql.NullString{String: "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36", Valid: true},
|
|
UserID: uuid.NullUUID{UUID: ws.OwnerID, Valid: true},
|
|
AppNameOrPort: sql.NullString{String: "code-server", Valid: true},
|
|
})
|
|
|
|
logs, err := client.ConnectionLogs(ctx, codersdk.ConnectionLogsRequest{})
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, logs.ConnectionLogs, 1)
|
|
require.EqualValues(t, 1, logs.Count)
|
|
require.NotNil(t, logs.ConnectionLogs[0].WebInfo)
|
|
require.Equal(t, clog.AppNameOrPort.String, logs.ConnectionLogs[0].WebInfo.SlugOrPort)
|
|
require.Empty(t, logs.ConnectionLogs[0].AppName, "a destination is not a client app")
|
|
require.Empty(t, logs.ConnectionLogs[0].AppDisplayName)
|
|
require.Equal(t, clog.UserAgent.String, logs.ConnectionLogs[0].WebInfo.UserAgent)
|
|
require.Equal(t, ws.OwnerID, logs.ConnectionLogs[0].WebInfo.User.ID)
|
|
})
|
|
|
|
t.Run("WebInfoTunnel", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
ctx := context.Background()
|
|
client, db, _ := coderdenttest.NewWithDatabase(t, &coderdenttest.Options{
|
|
ConnectionLogging: true,
|
|
LicenseOptions: &coderdenttest.LicenseOptions{
|
|
Features: license.Features{
|
|
codersdk.FeatureAuditLog: 1,
|
|
codersdk.FeatureConnectionLog: 1,
|
|
},
|
|
},
|
|
})
|
|
|
|
now := dbtime.Now()
|
|
ws := createWorkspace(t, db)
|
|
// Tunnel events are written by coderd with the connecting
|
|
// user's identity; they must surface it via WebInfo.
|
|
clog := dbgen.ConnectionLog(t, db, database.UpsertConnectionLogParams{
|
|
Time: now.Add(-time.Hour),
|
|
ConnectionMethod: database.ConnectionLogMethodTunnel,
|
|
WorkspaceID: ws.ID,
|
|
OrganizationID: ws.OrganizationID,
|
|
WorkspaceOwnerID: ws.OwnerID,
|
|
UserAgent: sql.NullString{String: "coder-cli/2.0.0", Valid: true},
|
|
Code: sql.NullInt32{Int32: http.StatusSwitchingProtocols, Valid: true},
|
|
UserID: uuid.NullUUID{UUID: ws.OwnerID, Valid: true},
|
|
})
|
|
|
|
logs, err := client.ConnectionLogs(ctx, codersdk.ConnectionLogsRequest{})
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, logs.ConnectionLogs, 1)
|
|
require.EqualValues(t, 1, logs.Count)
|
|
require.Nil(t, logs.ConnectionLogs[0].SSHInfo)
|
|
require.NotNil(t, logs.ConnectionLogs[0].WebInfo)
|
|
require.Equal(t, clog.UserAgent.String, logs.ConnectionLogs[0].WebInfo.UserAgent)
|
|
require.NotNil(t, logs.ConnectionLogs[0].WebInfo.User)
|
|
require.Equal(t, ws.OwnerID, logs.ConnectionLogs[0].WebInfo.User.ID)
|
|
require.EqualValues(t, http.StatusSwitchingProtocols, logs.ConnectionLogs[0].WebInfo.StatusCode)
|
|
})
|
|
|
|
t.Run("SSHInfo", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
ctx := context.Background()
|
|
client, db, _ := coderdenttest.NewWithDatabase(t, &coderdenttest.Options{
|
|
ConnectionLogging: true,
|
|
LicenseOptions: &coderdenttest.LicenseOptions{
|
|
Features: license.Features{
|
|
codersdk.FeatureAuditLog: 1,
|
|
codersdk.FeatureConnectionLog: 1,
|
|
},
|
|
},
|
|
})
|
|
|
|
now := dbtime.Now()
|
|
connID := uuid.New()
|
|
ws := createWorkspace(t, db)
|
|
clog := dbgen.ConnectionLog(t, db, database.UpsertConnectionLogParams{
|
|
Time: now.Add(-time.Hour),
|
|
ConnectionMethod: database.ConnectionLogMethodSSH,
|
|
AppNameOrPort: sql.NullString{String: "cursor", Valid: true},
|
|
ClientSessionID: sql.NullString{String: "0123456789abcdef0123456789abcdef", Valid: true},
|
|
WorkspaceID: ws.ID,
|
|
OrganizationID: ws.OrganizationID,
|
|
WorkspaceOwnerID: ws.OwnerID,
|
|
ConnectionID: uuid.NullUUID{UUID: connID, Valid: true},
|
|
})
|
|
|
|
logs, err := client.ConnectionLogs(ctx, codersdk.ConnectionLogsRequest{})
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, logs.ConnectionLogs, 1)
|
|
require.NotNil(t, logs.ConnectionLogs[0].SSHInfo)
|
|
require.Empty(t, logs.ConnectionLogs[0].WebInfo)
|
|
require.Empty(t, logs.ConnectionLogs[0].SSHInfo.ExitCode)
|
|
require.Empty(t, logs.ConnectionLogs[0].SSHInfo.DisconnectTime)
|
|
require.Empty(t, logs.ConnectionLogs[0].SSHInfo.DisconnectReason)
|
|
require.Equal(t, "0123456789abcdef0123456789abcdef", logs.ConnectionLogs[0].SSHInfo.ClientSessionID)
|
|
|
|
// Mark log as closed
|
|
updatedClog := dbgen.ConnectionLog(t, db, database.UpsertConnectionLogParams{
|
|
Time: now,
|
|
OrganizationID: clog.OrganizationID,
|
|
ConnectionMethod: clog.ConnectionMethod,
|
|
WorkspaceID: clog.WorkspaceID,
|
|
WorkspaceOwnerID: clog.WorkspaceOwnerID,
|
|
WorkspaceName: clog.WorkspaceName,
|
|
AgentName: clog.AgentName,
|
|
Code: sql.NullInt32{
|
|
Int32: 0,
|
|
Valid: false,
|
|
},
|
|
IP: pqtype.Inet{IPNet: net.IPNet{
|
|
IP: net.ParseIP("192.168.0.1"),
|
|
Mask: net.CIDRMask(8, 32),
|
|
}, Valid: true},
|
|
|
|
ConnectionID: clog.ConnectionID,
|
|
ConnectionStatus: database.ConnectionStatusDisconnected,
|
|
DisconnectReason: sql.NullString{
|
|
String: "example close reason",
|
|
Valid: true,
|
|
},
|
|
})
|
|
|
|
logs, err = client.ConnectionLogs(ctx, codersdk.ConnectionLogsRequest{})
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, logs.ConnectionLogs, 1)
|
|
require.EqualValues(t, 1, logs.Count)
|
|
require.NotNil(t, logs.ConnectionLogs[0].SSHInfo)
|
|
require.Nil(t, logs.ConnectionLogs[0].WebInfo)
|
|
require.Equal(t, string(codersdk.ConnectionTypeVSCode), logs.ConnectionLogs[0].Type)
|
|
require.Equal(t, codersdk.ConnectionLogMethodSSH, logs.ConnectionLogs[0].ConnectionMethod)
|
|
require.Equal(t, "cursor", logs.ConnectionLogs[0].AppName)
|
|
require.Equal(t, "Cursor", logs.ConnectionLogs[0].AppDisplayName)
|
|
require.Equal(t, clog.ConnectionID.UUID, logs.ConnectionLogs[0].SSHInfo.ConnectionID)
|
|
require.True(t, logs.ConnectionLogs[0].SSHInfo.DisconnectTime.Equal(now))
|
|
require.Equal(t, updatedClog.DisconnectReason.String, logs.ConnectionLogs[0].SSHInfo.DisconnectReason)
|
|
})
|
|
|
|
// The frontend compares the raw strings to detect a disconnect recorded
|
|
// without its connect, so both timestamps must serialize identically.
|
|
t.Run("SSHInfoMissingConnect", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
ctx := testutil.Context(t, testutil.WaitShort)
|
|
client, db, _ := coderdenttest.NewWithDatabase(t, &coderdenttest.Options{
|
|
ConnectionLogging: true,
|
|
LicenseOptions: &coderdenttest.LicenseOptions{
|
|
Features: license.Features{
|
|
codersdk.FeatureConnectionLog: 1,
|
|
},
|
|
},
|
|
})
|
|
|
|
ws := createWorkspace(t, db)
|
|
_ = dbgen.ConnectionLog(t, db, database.UpsertConnectionLogParams{
|
|
// Sub-millisecond precision catches any rounding on one side.
|
|
Time: dbtime.Now().Add(-123 * time.Microsecond),
|
|
ConnectionMethod: database.ConnectionLogMethodSSH,
|
|
WorkspaceID: ws.ID,
|
|
OrganizationID: ws.OrganizationID,
|
|
WorkspaceOwnerID: ws.OwnerID,
|
|
ConnectionID: uuid.NullUUID{UUID: uuid.New(), Valid: true},
|
|
ConnectionStatus: database.ConnectionStatusDisconnected,
|
|
})
|
|
|
|
res, err := client.Request(ctx, http.MethodGet, "/api/v2/connectionlog", nil)
|
|
require.NoError(t, err)
|
|
defer res.Body.Close()
|
|
require.Equal(t, http.StatusOK, res.StatusCode)
|
|
|
|
var body struct {
|
|
ConnectionLogs []struct {
|
|
ConnectTime string `json:"connect_time"`
|
|
SSHInfo struct {
|
|
DisconnectTime string `json:"disconnect_time"`
|
|
} `json:"ssh_info"`
|
|
} `json:"connection_logs"`
|
|
}
|
|
require.NoError(t, json.NewDecoder(res.Body).Decode(&body))
|
|
require.Len(t, body.ConnectionLogs, 1)
|
|
log := body.ConnectionLogs[0]
|
|
require.NotEmpty(t, log.ConnectTime)
|
|
require.Equal(t, log.ConnectTime, log.SSHInfo.DisconnectTime)
|
|
})
|
|
}
|
|
|
|
func TestConnectionLogsWorkspaceShutdown(t *testing.T) {
|
|
t.Parallel()
|
|
store, _ := dbtestutil.NewDB(t)
|
|
batcher := entconnectionlog.NewDBBatcher(t.Context(), store, testutil.Logger(t), entconnectionlog.WithBatchSize(1))
|
|
t.Cleanup(func() { require.NoError(t, batcher.Close()) })
|
|
client, db, user := coderdenttest.NewWithDatabase(t, &coderdenttest.Options{
|
|
Options: &coderdtest.Options{Database: store, ConnectionLogger: batcher},
|
|
ConnectionLogging: true,
|
|
LicenseOptions: &coderdenttest.LicenseOptions{Features: license.Features{
|
|
codersdk.FeatureAuditLog: 1, codersdk.FeatureConnectionLog: 1,
|
|
}},
|
|
})
|
|
for _, transition := range []database.WorkspaceTransition{database.WorkspaceTransitionStop, database.WorkspaceTransitionDelete} {
|
|
for _, session := range []struct{ name, id string }{
|
|
{name: "SessionID", id: "0123456789abcdef0123456789abcdef"},
|
|
{name: "NoSessionID"},
|
|
} {
|
|
t.Run(string(transition)+"/"+session.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
ctx := testutil.Context(t, testutil.WaitLong)
|
|
workspace := dbfake.WorkspaceBuild(t, db, database.WorkspaceTable{
|
|
OrganizationID: user.OrganizationID, OwnerID: user.UserID,
|
|
}).WithAgent().Do()
|
|
agentClient := agentsdk.New(client.URL, agentsdk.WithFixedToken(workspace.AgentToken))
|
|
// The agent role runs the connection monitor, which must keep the
|
|
// RPC open while the shutdown build runs.
|
|
rpc, _, err := agentClient.ConnectRPC212WithRole(ctx, "agent")
|
|
require.NoError(t, err)
|
|
defer rpc.DRPCConn().Close()
|
|
connectionID := uuid.New()
|
|
_, err = rpc.ReportConnection(ctx, &agentproto.ReportConnectionRequest{Connection: &agentproto.Connection{
|
|
Id: connectionID[:], Action: agentproto.Connection_CONNECT, Type: agentproto.Connection_SSH,
|
|
Timestamp: timestamppb.Now(), Ip: "127.0.0.1", ClientSessionId: session.id,
|
|
}})
|
|
require.NoError(t, err)
|
|
// The existing agent connection reports a generic shutdown after a
|
|
// STOP or DELETE job starts. Coderd enriches it before persistence.
|
|
dbfake.WorkspaceBuild(t, db, workspace.Workspace).Seed(database.WorkspaceBuild{
|
|
BuildNumber: workspace.Build.BuildNumber + 1, Transition: transition, Reason: database.BuildReasonAutostop,
|
|
}).Starting().Do()
|
|
// Two connection time updates after the shutdown build exists
|
|
// guarantee that the monitor has checked the build at least once.
|
|
shutdownCreated := dbtime.Now()
|
|
var updates int
|
|
var lastConnectedAt time.Time
|
|
require.Eventually(t, func() bool {
|
|
agent, err := db.GetWorkspaceAgentByID(dbauthz.AsSystemRestricted(ctx), workspace.Agents[0].ID)
|
|
if err != nil || !agent.LastConnectedAt.Valid {
|
|
return false
|
|
}
|
|
if at := agent.LastConnectedAt.Time; at.After(shutdownCreated) && !at.Equal(lastConnectedAt) {
|
|
lastConnectedAt = at
|
|
updates++
|
|
}
|
|
return updates >= 2
|
|
}, testutil.WaitLong, testutil.IntervalFast)
|
|
reason := string(codersdk.DisconnectReasonServerShutdown)
|
|
_, err = rpc.ReportConnection(ctx, &agentproto.ReportConnectionRequest{Connection: &agentproto.Connection{
|
|
Id: connectionID[:], Action: agentproto.Connection_DISCONNECT, Type: agentproto.Connection_SSH,
|
|
Timestamp: timestamppb.Now(), Reason: &reason, ClientSessionId: session.id,
|
|
}})
|
|
require.NoError(t, err)
|
|
require.Eventually(t, func() bool {
|
|
rows, err := db.GetConnectionLogsOffset(dbauthz.AsSystemRestricted(ctx), database.GetConnectionLogsOffsetParams{WorkspaceID: workspace.Workspace.ID, ConnectionID: connectionID, LimitOpt: 1})
|
|
if err != nil {
|
|
return false
|
|
}
|
|
for _, row := range rows {
|
|
log := row.ConnectionLog
|
|
if log.ConnectionID.UUID != connectionID || !log.DisconnectTime.Valid {
|
|
continue
|
|
}
|
|
return log.WorkspaceID == workspace.Workspace.ID &&
|
|
log.DisconnectReason.String == string(codersdk.DisconnectReasonWorkspaceStopped) &&
|
|
log.ClientSessionID.String == session.id && log.ClientSessionID.Valid == (session.id != "")
|
|
}
|
|
return false
|
|
}, testutil.WaitLong, testutil.IntervalFast)
|
|
})
|
|
}
|
|
}
|
|
}
|