2025-02-20 14:52:01 +02:00
|
|
|
package agentapi_test
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2025-07-15 14:36:06 +10:00
|
|
|
"database/sql"
|
2025-02-20 14:52:01 +02:00
|
|
|
"sync/atomic"
|
|
|
|
|
"testing"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
"go.uber.org/mock/gomock"
|
2026-10-19 23:06:41 +03:00
|
|
|
"golang.org/x/xerrors"
|
2025-02-20 14:52:01 +02:00
|
|
|
"google.golang.org/protobuf/types/known/timestamppb"
|
|
|
|
|
|
|
|
|
|
agentproto "github.com/coder/coder/v2/agent/proto"
|
|
|
|
|
"github.com/coder/coder/v2/coderd/agentapi"
|
2025-07-15 14:36:06 +10:00
|
|
|
"github.com/coder/coder/v2/coderd/connectionlog"
|
2025-02-20 14:52:01 +02:00
|
|
|
"github.com/coder/coder/v2/coderd/database"
|
|
|
|
|
"github.com/coder/coder/v2/coderd/database/db2sdk"
|
|
|
|
|
"github.com/coder/coder/v2/coderd/database/dbmock"
|
|
|
|
|
"github.com/coder/coder/v2/coderd/database/dbtime"
|
2026-10-16 00:21:40 -08:00
|
|
|
"github.com/coder/coder/v2/coderd/util/ptr"
|
2026-10-19 23:06:41 +03:00
|
|
|
"github.com/coder/coder/v2/codersdk"
|
2025-02-20 14:52:01 +02:00
|
|
|
)
|
|
|
|
|
|
2025-07-15 14:36:06 +10:00
|
|
|
func TestConnectionLog(t *testing.T) {
|
2025-02-20 14:52:01 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
owner = database.User{
|
|
|
|
|
ID: uuid.New(),
|
|
|
|
|
Username: "cool-user",
|
|
|
|
|
}
|
|
|
|
|
workspace = database.Workspace{
|
|
|
|
|
ID: uuid.New(),
|
|
|
|
|
OrganizationID: uuid.New(),
|
|
|
|
|
OwnerID: owner.ID,
|
|
|
|
|
Name: "cool-workspace",
|
|
|
|
|
}
|
|
|
|
|
agent = database.WorkspaceAgent{
|
|
|
|
|
ID: uuid.New(),
|
|
|
|
|
}
|
2026-10-19 23:06:41 +03:00
|
|
|
// agentBuildID is the build that created the agent.
|
|
|
|
|
agentBuildID = uuid.New()
|
2025-02-20 14:52:01 +02:00
|
|
|
)
|
|
|
|
|
|
2026-10-19 23:06:41 +03:00
|
|
|
type testCase struct {
|
2026-09-27 23:38:36 -07:00
|
|
|
name string
|
|
|
|
|
id uuid.UUID
|
|
|
|
|
action *agentproto.Connection_Action
|
|
|
|
|
typ *agentproto.Connection_Type
|
2026-10-14 13:54:17 +03:00
|
|
|
reqMethod agentproto.Connection_Method
|
|
|
|
|
reqAppName string
|
2026-10-12 15:56:40 +03:00
|
|
|
method database.ConnectionLogMethod
|
|
|
|
|
appName string
|
2026-09-27 23:38:36 -07:00
|
|
|
time time.Time
|
|
|
|
|
ip string
|
|
|
|
|
status int32
|
|
|
|
|
reason string
|
|
|
|
|
clientSessionID string
|
2026-10-16 00:21:40 -08:00
|
|
|
rxBytes *int64
|
|
|
|
|
txBytes *int64
|
2026-10-19 23:06:41 +03:00
|
|
|
build *database.WorkspaceBuild
|
|
|
|
|
jobStatus database.ProvisionerJobStatus
|
|
|
|
|
buildErr error
|
|
|
|
|
jobErr error
|
|
|
|
|
wantReason string
|
|
|
|
|
}
|
|
|
|
|
tests := []testCase{
|
2025-02-20 14:52:01 +02:00
|
|
|
{
|
2026-09-27 23:38:36 -07:00
|
|
|
name: "SSH Connect",
|
|
|
|
|
id: uuid.New(),
|
|
|
|
|
action: agentproto.Connection_CONNECT.Enum(),
|
|
|
|
|
typ: agentproto.Connection_SSH.Enum(),
|
2026-10-12 15:56:40 +03:00
|
|
|
method: database.ConnectionLogMethodSSH,
|
2026-09-27 23:38:36 -07:00
|
|
|
time: dbtime.Now(),
|
|
|
|
|
ip: "127.0.0.1",
|
|
|
|
|
status: 200,
|
|
|
|
|
clientSessionID: "0123456789abcdef0123456789abcdef",
|
2025-02-20 14:52:01 +02:00
|
|
|
},
|
|
|
|
|
{
|
2026-10-12 15:56:40 +03:00
|
|
|
name: "VS Code Connect",
|
|
|
|
|
id: uuid.New(),
|
|
|
|
|
action: agentproto.Connection_CONNECT.Enum(),
|
|
|
|
|
typ: agentproto.Connection_VSCODE.Enum(),
|
|
|
|
|
method: database.ConnectionLogMethodSSH,
|
|
|
|
|
appName: "vscode",
|
|
|
|
|
time: dbtime.Now(),
|
|
|
|
|
ip: "8.8.8.8",
|
2025-02-20 14:52:01 +02:00
|
|
|
},
|
|
|
|
|
{
|
2026-10-12 15:56:40 +03:00
|
|
|
name: "JetBrains Connect",
|
|
|
|
|
id: uuid.New(),
|
|
|
|
|
action: agentproto.Connection_CONNECT.Enum(),
|
|
|
|
|
typ: agentproto.Connection_JETBRAINS.Enum(),
|
|
|
|
|
method: database.ConnectionLogMethodSSH,
|
|
|
|
|
appName: "jetbrains",
|
|
|
|
|
time: dbtime.Now(),
|
2025-10-16 01:56:43 +11:00
|
|
|
// Sometimes, JetBrains clients report as localhost, see
|
|
|
|
|
// https://github.com/coder/coder/issues/20194
|
|
|
|
|
ip: "localhost",
|
2025-02-20 14:52:01 +02:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "Reconnecting PTY Connect",
|
|
|
|
|
id: uuid.New(),
|
|
|
|
|
action: agentproto.Connection_CONNECT.Enum(),
|
|
|
|
|
typ: agentproto.Connection_RECONNECTING_PTY.Enum(),
|
2026-10-12 15:56:40 +03:00
|
|
|
method: database.ConnectionLogMethodReconnectingPTY,
|
|
|
|
|
time: dbtime.Now(),
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
// SSH handlers reported unfamiliar apps as unspecified.
|
|
|
|
|
name: "Unspecified Connect",
|
|
|
|
|
id: uuid.New(),
|
|
|
|
|
action: agentproto.Connection_CONNECT.Enum(),
|
|
|
|
|
typ: agentproto.Connection_TYPE_UNSPECIFIED.Enum(),
|
|
|
|
|
method: database.ConnectionLogMethodSSH,
|
2025-07-15 14:36:06 +10:00
|
|
|
time: dbtime.Now(),
|
2025-02-20 14:52:01 +02:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "SSH Disconnect",
|
|
|
|
|
id: uuid.New(),
|
|
|
|
|
action: agentproto.Connection_DISCONNECT.Enum(),
|
|
|
|
|
typ: agentproto.Connection_SSH.Enum(),
|
2026-10-12 15:56:40 +03:00
|
|
|
method: database.ConnectionLogMethodSSH,
|
2025-07-15 14:36:06 +10:00
|
|
|
time: dbtime.Now(),
|
2025-02-20 14:52:01 +02:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "SSH Disconnect",
|
|
|
|
|
id: uuid.New(),
|
|
|
|
|
action: agentproto.Connection_DISCONNECT.Enum(),
|
|
|
|
|
typ: agentproto.Connection_SSH.Enum(),
|
2026-10-12 15:56:40 +03:00
|
|
|
method: database.ConnectionLogMethodSSH,
|
2025-07-15 14:36:06 +10:00
|
|
|
time: dbtime.Now(),
|
2025-02-20 14:52:01 +02:00
|
|
|
status: 500,
|
|
|
|
|
reason: "because error says so",
|
|
|
|
|
},
|
2026-10-12 15:56:40 +03:00
|
|
|
{
|
|
|
|
|
name: "VS Code Disconnect",
|
|
|
|
|
id: uuid.New(),
|
|
|
|
|
action: agentproto.Connection_DISCONNECT.Enum(),
|
|
|
|
|
typ: agentproto.Connection_VSCODE.Enum(),
|
|
|
|
|
method: database.ConnectionLogMethodSSH,
|
|
|
|
|
appName: "vscode",
|
|
|
|
|
time: dbtime.Now(),
|
|
|
|
|
},
|
2026-10-14 13:54:17 +03:00
|
|
|
{
|
|
|
|
|
name: "Method And App Connect",
|
|
|
|
|
id: uuid.New(),
|
|
|
|
|
action: agentproto.Connection_CONNECT.Enum(),
|
|
|
|
|
typ: agentproto.Connection_TYPE_UNSPECIFIED.Enum(),
|
|
|
|
|
reqMethod: agentproto.Connection_METHOD_SSH,
|
|
|
|
|
reqAppName: "Some-IDE",
|
|
|
|
|
method: database.ConnectionLogMethodSSH,
|
|
|
|
|
appName: "some_ide",
|
|
|
|
|
time: dbtime.Now(),
|
|
|
|
|
},
|
2026-10-16 00:21:40 -08:00
|
|
|
{
|
|
|
|
|
name: "Bytes Transferred Disconnect",
|
|
|
|
|
id: uuid.New(),
|
|
|
|
|
action: agentproto.Connection_DISCONNECT.Enum(),
|
|
|
|
|
typ: agentproto.Connection_SSH.Enum(),
|
|
|
|
|
method: database.ConnectionLogMethodSSH,
|
|
|
|
|
time: dbtime.Now(),
|
|
|
|
|
rxBytes: new(int64(10)),
|
|
|
|
|
txBytes: new(int64(50)),
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "Zero Bytes Transferred Disconnect",
|
|
|
|
|
id: uuid.New(),
|
|
|
|
|
action: agentproto.Connection_DISCONNECT.Enum(),
|
|
|
|
|
typ: agentproto.Connection_SSH.Enum(),
|
|
|
|
|
method: database.ConnectionLogMethodSSH,
|
|
|
|
|
time: dbtime.Now(),
|
|
|
|
|
rxBytes: new(int64(0)),
|
|
|
|
|
txBytes: new(int64(0)),
|
|
|
|
|
},
|
2025-02-20 14:52:01 +02:00
|
|
|
}
|
2026-10-19 23:06:41 +03:00
|
|
|
for _, transition := range []database.WorkspaceTransition{
|
|
|
|
|
database.WorkspaceTransitionStart, database.WorkspaceTransitionStop, database.WorkspaceTransitionDelete,
|
|
|
|
|
} {
|
|
|
|
|
for _, status := range []database.ProvisionerJobStatus{
|
|
|
|
|
database.ProvisionerJobStatusPending, database.ProvisionerJobStatusRunning,
|
|
|
|
|
database.ProvisionerJobStatusSucceeded, database.ProvisionerJobStatusCanceling,
|
|
|
|
|
database.ProvisionerJobStatusCanceled, database.ProvisionerJobStatusFailed,
|
|
|
|
|
} {
|
|
|
|
|
tt := testCase{
|
|
|
|
|
name: string(transition) + "/" + string(status), id: uuid.New(),
|
|
|
|
|
action: agentproto.Connection_DISCONNECT.Enum(), typ: agentproto.Connection_SSH.Enum(), method: database.ConnectionLogMethodSSH,
|
|
|
|
|
time: dbtime.Now(), reason: string(codersdk.DisconnectReasonServerShutdown),
|
|
|
|
|
clientSessionID: "0123456789abcdef0123456789abcdef",
|
|
|
|
|
build: &database.WorkspaceBuild{
|
|
|
|
|
ID: uuid.New(), JobID: uuid.New(), Transition: transition,
|
|
|
|
|
Reason: database.BuildReasonAutostop, CreatedAt: dbtime.Now().Add(-time.Minute),
|
|
|
|
|
}, jobStatus: status,
|
|
|
|
|
}
|
|
|
|
|
// Any newer build replaced the agent once its job runs.
|
|
|
|
|
if status == database.ProvisionerJobStatusRunning || status == database.ProvisionerJobStatusSucceeded {
|
|
|
|
|
tt.wantReason = string(codersdk.DisconnectReasonWorkspaceStopped)
|
|
|
|
|
}
|
|
|
|
|
tests = append(tests, tt)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for _, reason := range []codersdk.DisconnectReason{
|
|
|
|
|
codersdk.DisconnectReasonClientClosed, codersdk.DisconnectReasonNetworkError, codersdk.DisconnectReasonGraceful,
|
|
|
|
|
} {
|
|
|
|
|
tests = append(tests, testCase{
|
|
|
|
|
name: "Preserve/" + string(reason), id: uuid.New(),
|
|
|
|
|
action: agentproto.Connection_DISCONNECT.Enum(), typ: agentproto.Connection_SSH.Enum(), method: database.ConnectionLogMethodSSH,
|
|
|
|
|
time: dbtime.Now(), reason: string(reason),
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
queryErr := xerrors.New("database unavailable")
|
|
|
|
|
tests = append(tests,
|
|
|
|
|
testCase{
|
|
|
|
|
name: "BuildLookupError", id: uuid.New(),
|
|
|
|
|
action: agentproto.Connection_DISCONNECT.Enum(), typ: agentproto.Connection_SSH.Enum(), method: database.ConnectionLogMethodSSH,
|
|
|
|
|
time: dbtime.Now(), reason: string(codersdk.DisconnectReasonServerShutdown), buildErr: queryErr,
|
|
|
|
|
},
|
|
|
|
|
testCase{
|
|
|
|
|
name: "JobLookupError", id: uuid.New(),
|
|
|
|
|
action: agentproto.Connection_DISCONNECT.Enum(), typ: agentproto.Connection_JETBRAINS.Enum(), method: database.ConnectionLogMethodSSH, appName: "jetbrains",
|
|
|
|
|
time: dbtime.Now(), reason: string(codersdk.DisconnectReasonServerShutdown),
|
|
|
|
|
build: &database.WorkspaceBuild{ID: uuid.New(), JobID: uuid.New(), Transition: database.WorkspaceTransitionStop}, jobErr: queryErr,
|
|
|
|
|
},
|
|
|
|
|
testCase{
|
|
|
|
|
// The agent's own build is still the latest, so no build ended it.
|
|
|
|
|
name: "AgentBuildIsLatest", id: uuid.New(),
|
|
|
|
|
action: agentproto.Connection_DISCONNECT.Enum(), typ: agentproto.Connection_SSH.Enum(), method: database.ConnectionLogMethodSSH,
|
|
|
|
|
time: dbtime.Now(), reason: string(codersdk.DisconnectReasonServerShutdown),
|
|
|
|
|
build: &database.WorkspaceBuild{ID: agentBuildID, JobID: uuid.New(), Transition: database.WorkspaceTransitionStart, CreatedAt: dbtime.Now().Add(-time.Hour)},
|
|
|
|
|
},
|
|
|
|
|
testCase{
|
|
|
|
|
name: "LaterStopBuild", id: uuid.New(),
|
|
|
|
|
action: agentproto.Connection_DISCONNECT.Enum(), typ: agentproto.Connection_SSH.Enum(), method: database.ConnectionLogMethodSSH,
|
|
|
|
|
time: dbtime.Now(), reason: string(codersdk.DisconnectReasonServerShutdown),
|
|
|
|
|
build: &database.WorkspaceBuild{ID: uuid.New(), JobID: uuid.New(), Transition: database.WorkspaceTransitionStop, CreatedAt: dbtime.Now().Add(time.Minute)},
|
|
|
|
|
},
|
|
|
|
|
testCase{
|
|
|
|
|
name: "ConnectWithShutdownReason", id: uuid.New(),
|
|
|
|
|
action: agentproto.Connection_CONNECT.Enum(), typ: agentproto.Connection_SSH.Enum(), method: database.ConnectionLogMethodSSH,
|
|
|
|
|
time: dbtime.Now(), reason: string(codersdk.DisconnectReasonServerShutdown),
|
|
|
|
|
},
|
|
|
|
|
testCase{
|
|
|
|
|
name: "JetBrainsShutdown", id: uuid.New(),
|
|
|
|
|
action: agentproto.Connection_DISCONNECT.Enum(), typ: agentproto.Connection_JETBRAINS.Enum(), method: database.ConnectionLogMethodSSH, appName: "jetbrains",
|
|
|
|
|
time: dbtime.Now(), reason: string(codersdk.DisconnectReasonServerShutdown),
|
|
|
|
|
build: &database.WorkspaceBuild{ID: uuid.New(), JobID: uuid.New(), Transition: database.WorkspaceTransitionStop}, jobStatus: database.ProvisionerJobStatusRunning,
|
|
|
|
|
wantReason: string(codersdk.DisconnectReasonWorkspaceStopped),
|
|
|
|
|
},
|
|
|
|
|
testCase{
|
|
|
|
|
name: "ReconnectingPTYShutdown", id: uuid.New(),
|
|
|
|
|
action: agentproto.Connection_DISCONNECT.Enum(), typ: agentproto.Connection_RECONNECTING_PTY.Enum(), method: database.ConnectionLogMethodReconnectingPTY,
|
|
|
|
|
time: dbtime.Now(), reason: string(codersdk.DisconnectReasonServerShutdown),
|
|
|
|
|
clientSessionID: "0123456789abcdef0123456789abcdef",
|
|
|
|
|
build: &database.WorkspaceBuild{ID: uuid.New(), JobID: uuid.New(), Transition: database.WorkspaceTransitionDelete}, jobStatus: database.ProvisionerJobStatusSucceeded,
|
|
|
|
|
wantReason: string(codersdk.DisconnectReasonWorkspaceStopped),
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
|
2025-02-20 14:52:01 +02:00
|
|
|
for _, tt := range tests {
|
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
|
|
|
|
|
2025-07-15 14:36:06 +10:00
|
|
|
connLogger := connectionlog.NewFake()
|
2025-02-20 14:52:01 +02:00
|
|
|
|
|
|
|
|
mDB := dbmock.NewMockStore(gomock.NewController(t))
|
|
|
|
|
mDB.EXPECT().GetWorkspaceByAgentID(gomock.Any(), agent.ID).Return(workspace, nil)
|
|
|
|
|
|
2026-10-19 23:06:41 +03:00
|
|
|
if *tt.action == agentproto.Connection_DISCONNECT && tt.reason == string(codersdk.DisconnectReasonServerShutdown) {
|
|
|
|
|
build := database.WorkspaceBuild{}
|
|
|
|
|
if tt.build != nil {
|
|
|
|
|
build = *tt.build
|
|
|
|
|
}
|
|
|
|
|
mDB.EXPECT().GetLatestWorkspaceBuildByWorkspaceID(gomock.Any(), workspace.ID).Return(build, tt.buildErr)
|
|
|
|
|
if tt.buildErr == nil && !build.CreatedAt.After(tt.time) && build.ID != agentBuildID {
|
|
|
|
|
mDB.EXPECT().GetProvisionerJobByID(gomock.Any(), build.JobID).Return(database.ProvisionerJob{JobStatus: tt.jobStatus}, tt.jobErr)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-15 14:36:06 +10:00
|
|
|
api := &agentapi.ConnLogAPI{
|
|
|
|
|
ConnectionLogger: asAtomicPointer[connectionlog.ConnectionLogger](connLogger),
|
|
|
|
|
Database: mDB,
|
2026-03-20 15:42:05 -05:00
|
|
|
AgentID: agent.ID,
|
|
|
|
|
AgentName: agent.Name,
|
2026-10-19 23:06:41 +03:00
|
|
|
WorkspaceBuildID: agentBuildID,
|
2026-03-20 15:42:05 -05:00
|
|
|
Workspace: &agentapi.CachedWorkspaceFields{},
|
2025-02-20 14:52:01 +02:00
|
|
|
}
|
2026-10-12 15:56:40 +03:00
|
|
|
_, err := api.ReportConnection(context.Background(), &agentproto.ReportConnectionRequest{
|
2025-02-20 14:52:01 +02:00
|
|
|
Connection: &agentproto.Connection{
|
2026-10-14 13:54:17 +03:00
|
|
|
Id: tt.id[:],
|
|
|
|
|
Action: *tt.action,
|
|
|
|
|
Type: *tt.typ,
|
|
|
|
|
Timestamp: timestamppb.New(tt.time),
|
|
|
|
|
Ip: tt.ip,
|
|
|
|
|
StatusCode: tt.status,
|
|
|
|
|
Reason: &tt.reason,
|
|
|
|
|
ClientSessionId: tt.clientSessionID,
|
|
|
|
|
AppName: tt.reqAppName,
|
|
|
|
|
ConnectionMethod: tt.reqMethod,
|
2026-10-16 00:21:40 -08:00
|
|
|
RxBytes: tt.rxBytes,
|
|
|
|
|
TxBytes: tt.txBytes,
|
2025-02-20 14:52:01 +02:00
|
|
|
},
|
|
|
|
|
})
|
2026-10-12 15:56:40 +03:00
|
|
|
require.NoError(t, err)
|
2026-10-19 23:06:41 +03:00
|
|
|
if tt.wantReason == "" {
|
|
|
|
|
tt.wantReason = tt.reason
|
|
|
|
|
}
|
2025-02-20 14:52:01 +02:00
|
|
|
|
2025-10-16 01:56:43 +11:00
|
|
|
expectedIPRaw := tt.ip
|
|
|
|
|
if expectedIPRaw == "localhost" {
|
|
|
|
|
expectedIPRaw = "127.0.0.1"
|
|
|
|
|
}
|
|
|
|
|
expectedIP := database.ParseIP(expectedIPRaw)
|
2026-10-12 15:56:40 +03:00
|
|
|
// Only disconnects carry a status code.
|
|
|
|
|
var expectedCode sql.NullInt32
|
|
|
|
|
if *tt.action == agentproto.Connection_DISCONNECT {
|
|
|
|
|
expectedCode = sql.NullInt32{Int32: tt.status, Valid: true}
|
|
|
|
|
}
|
2025-10-16 01:56:43 +11:00
|
|
|
|
2026-10-12 15:56:40 +03:00
|
|
|
logs := connLogger.ConnectionLogs()
|
|
|
|
|
require.Len(t, logs, 1)
|
|
|
|
|
require.Equal(t, database.UpsertConnectionLogParams{
|
|
|
|
|
// The ID is generated per report.
|
|
|
|
|
ID: logs[0].ID,
|
2025-07-15 14:36:06 +10:00
|
|
|
Time: dbtime.Time(tt.time).In(time.UTC),
|
|
|
|
|
OrganizationID: workspace.OrganizationID,
|
|
|
|
|
WorkspaceOwnerID: workspace.OwnerID,
|
|
|
|
|
WorkspaceID: workspace.ID,
|
|
|
|
|
WorkspaceName: workspace.Name,
|
|
|
|
|
AgentName: agent.Name,
|
|
|
|
|
UserID: uuid.NullUUID{
|
|
|
|
|
UUID: uuid.Nil,
|
|
|
|
|
Valid: false,
|
|
|
|
|
},
|
|
|
|
|
ConnectionStatus: agentProtoConnectionActionToConnectionLog(t, *tt.action),
|
2026-10-12 15:56:40 +03:00
|
|
|
Code: expectedCode,
|
|
|
|
|
IP: expectedIP,
|
|
|
|
|
ConnectionMethod: tt.method,
|
|
|
|
|
AppNameOrPort: sql.NullString{String: tt.appName, Valid: tt.appName != ""},
|
2025-07-15 14:36:06 +10:00
|
|
|
DisconnectReason: sql.NullString{
|
2026-10-19 23:06:41 +03:00
|
|
|
String: tt.wantReason,
|
|
|
|
|
Valid: tt.wantReason != "",
|
2025-07-15 14:36:06 +10:00
|
|
|
},
|
|
|
|
|
ConnectionID: uuid.NullUUID{
|
|
|
|
|
UUID: tt.id,
|
|
|
|
|
Valid: tt.id != uuid.Nil,
|
|
|
|
|
},
|
2026-09-27 23:38:36 -07:00
|
|
|
ClientSessionID: sql.NullString{
|
|
|
|
|
String: tt.clientSessionID,
|
|
|
|
|
Valid: tt.clientSessionID != "",
|
|
|
|
|
},
|
2026-10-16 00:21:40 -08:00
|
|
|
RxBytes: sql.NullInt64{
|
|
|
|
|
Int64: ptr.NilToEmpty(tt.rxBytes),
|
|
|
|
|
Valid: tt.rxBytes != nil,
|
|
|
|
|
},
|
|
|
|
|
TxBytes: sql.NullInt64{
|
|
|
|
|
Int64: ptr.NilToEmpty(tt.txBytes),
|
|
|
|
|
Valid: tt.txBytes != nil,
|
|
|
|
|
},
|
2026-10-12 15:56:40 +03:00
|
|
|
}, logs[0])
|
2025-02-20 14:52:01 +02:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-12 15:56:40 +03:00
|
|
|
// An agent connection type this coderd does not know is rejected rather than
|
|
|
|
|
// logged with a guessed method.
|
|
|
|
|
func TestConnectionLogRejectsUnknownType(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
|
|
connLogger := connectionlog.NewFake()
|
|
|
|
|
api := &agentapi.ConnLogAPI{
|
|
|
|
|
ConnectionLogger: asAtomicPointer[connectionlog.ConnectionLogger](connLogger),
|
|
|
|
|
Database: dbmock.NewMockStore(gomock.NewController(t)),
|
|
|
|
|
AgentID: uuid.New(),
|
|
|
|
|
Workspace: &agentapi.CachedWorkspaceFields{},
|
|
|
|
|
}
|
|
|
|
|
id := uuid.New()
|
|
|
|
|
_, err := api.ReportConnection(context.Background(), &agentproto.ReportConnectionRequest{
|
|
|
|
|
Connection: &agentproto.Connection{
|
|
|
|
|
Id: id[:],
|
|
|
|
|
Action: agentproto.Connection_CONNECT,
|
|
|
|
|
Type: agentproto.Connection_Type(1000),
|
|
|
|
|
Timestamp: timestamppb.New(dbtime.Now()),
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
require.Error(t, err)
|
|
|
|
|
require.Empty(t, connLogger.ConnectionLogs())
|
2025-02-20 14:52:01 +02:00
|
|
|
}
|
|
|
|
|
|
2025-07-15 14:36:06 +10:00
|
|
|
func agentProtoConnectionActionToConnectionLog(t *testing.T, action agentproto.Connection_Action) database.ConnectionStatus {
|
|
|
|
|
a, err := db2sdk.ConnectionLogStatusFromAgentProtoConnectionAction(action)
|
2025-02-20 14:52:01 +02:00
|
|
|
require.NoError(t, err)
|
2025-07-15 14:36:06 +10:00
|
|
|
return a
|
2025-02-20 14:52:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func asAtomicPointer[T any](v T) *atomic.Pointer[T] {
|
|
|
|
|
var p atomic.Pointer[T]
|
|
|
|
|
p.Store(&v)
|
|
|
|
|
return &p
|
|
|
|
|
}
|