|
1 | 1 | package coderd_test
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "context" |
4 | 5 | "testing"
|
5 | 6 |
|
| 7 | + "github.com/stretchr/testify/assert" |
6 | 8 | "github.com/stretchr/testify/require"
|
7 | 9 |
|
8 | 10 | agentproto "github.com/coder/coder/v2/agent/proto"
|
9 | 11 | "github.com/coder/coder/v2/coderd/coderdtest"
|
10 | 12 | "github.com/coder/coder/v2/coderd/database"
|
| 13 | + "github.com/coder/coder/v2/coderd/database/dbauthz" |
11 | 14 | "github.com/coder/coder/v2/coderd/database/dbfake"
|
| 15 | + "github.com/coder/coder/v2/coderd/rbac" |
12 | 16 | "github.com/coder/coder/v2/codersdk/agentsdk"
|
13 | 17 | "github.com/coder/coder/v2/provisionersdk/proto"
|
14 | 18 | "github.com/coder/coder/v2/testutil"
|
15 | 19 | )
|
16 | 20 |
|
| 21 | +// Ported to RPC API from coderd/workspaceagents_test.go |
| 22 | +func TestWorkspaceAgentReportStats(t *testing.T) { |
| 23 | + t.Parallel() |
| 24 | + |
| 25 | + t.Run("OK", func(t *testing.T) { |
| 26 | + t.Parallel() |
| 27 | + |
| 28 | + client, db := coderdtest.NewWithDatabase(t, nil) |
| 29 | + user := coderdtest.CreateFirstUser(t, client) |
| 30 | + r := dbfake.WorkspaceBuild(t, db, database.Workspace{ |
| 31 | + OrganizationID: user.OrganizationID, |
| 32 | + OwnerID: user.UserID, |
| 33 | + }).WithAgent().Do() |
| 34 | + |
| 35 | + ac := agentsdk.New(client.URL) |
| 36 | + ac.SetSessionToken(r.AgentToken) |
| 37 | + conn, err := ac.ConnectRPC(context.Background()) |
| 38 | + require.NoError(t, err) |
| 39 | + defer func() { |
| 40 | + _ = conn.Close() |
| 41 | + }() |
| 42 | + agentAPI := agentproto.NewDRPCAgentClient(conn) |
| 43 | + |
| 44 | + _, err = agentAPI.UpdateStats(context.Background(), &agentproto.UpdateStatsRequest{ |
| 45 | + Stats: &agentproto.Stats{ |
| 46 | + ConnectionsByProto: map[string]int64{"TCP": 1}, |
| 47 | + ConnectionCount: 1, |
| 48 | + RxPackets: 1, |
| 49 | + RxBytes: 1, |
| 50 | + TxPackets: 1, |
| 51 | + TxBytes: 1, |
| 52 | + SessionCountVscode: 1, |
| 53 | + SessionCountJetbrains: 0, |
| 54 | + SessionCountReconnectingPty: 0, |
| 55 | + SessionCountSsh: 0, |
| 56 | + ConnectionMedianLatencyMs: 10, |
| 57 | + }, |
| 58 | + }) |
| 59 | + require.NoError(t, err) |
| 60 | + |
| 61 | + newWorkspace, err := client.Workspace(context.Background(), r.Workspace.ID) |
| 62 | + require.NoError(t, err) |
| 63 | + |
| 64 | + assert.True(t, |
| 65 | + newWorkspace.LastUsedAt.After(r.Workspace.LastUsedAt), |
| 66 | + "%s is not after %s", newWorkspace.LastUsedAt, r.Workspace.LastUsedAt, |
| 67 | + ) |
| 68 | + }) |
| 69 | + |
| 70 | + t.Run("FailDeleted", func(t *testing.T) { |
| 71 | + t.Parallel() |
| 72 | + |
| 73 | + owner, db := coderdtest.NewWithDatabase(t, nil) |
| 74 | + ownerUser := coderdtest.CreateFirstUser(t, owner) |
| 75 | + client, admin := coderdtest.CreateAnotherUser(t, owner, ownerUser.OrganizationID, rbac.RoleTemplateAdmin(), rbac.RoleUserAdmin()) |
| 76 | + r := dbfake.WorkspaceBuild(t, db, database.Workspace{ |
| 77 | + OrganizationID: admin.OrganizationIDs[0], |
| 78 | + OwnerID: admin.ID, |
| 79 | + }).WithAgent().Do() |
| 80 | + |
| 81 | + ac := agentsdk.New(client.URL) |
| 82 | + ac.SetSessionToken(r.AgentToken) |
| 83 | + conn, err := ac.ConnectRPC(context.Background()) |
| 84 | + require.NoError(t, err) |
| 85 | + defer func() { |
| 86 | + _ = conn.Close() |
| 87 | + }() |
| 88 | + agentAPI := agentproto.NewDRPCAgentClient(conn) |
| 89 | + |
| 90 | + _, err = agentAPI.UpdateStats(context.Background(), &agentproto.UpdateStatsRequest{ |
| 91 | + Stats: &agentproto.Stats{ |
| 92 | + ConnectionsByProto: map[string]int64{"TCP": 1}, |
| 93 | + ConnectionCount: 1, |
| 94 | + RxPackets: 1, |
| 95 | + RxBytes: 1, |
| 96 | + TxPackets: 1, |
| 97 | + TxBytes: 1, |
| 98 | + SessionCountVscode: 0, |
| 99 | + SessionCountJetbrains: 0, |
| 100 | + SessionCountReconnectingPty: 0, |
| 101 | + SessionCountSsh: 0, |
| 102 | + ConnectionMedianLatencyMs: 10, |
| 103 | + }, |
| 104 | + }) |
| 105 | + require.NoError(t, err) |
| 106 | + |
| 107 | + newWorkspace, err := client.Workspace(context.Background(), r.Workspace.ID) |
| 108 | + require.NoError(t, err) |
| 109 | + |
| 110 | + // nolint:gocritic // using db directly over creating a delete job |
| 111 | + err = db.UpdateWorkspaceDeletedByID(dbauthz.As(context.Background(), |
| 112 | + coderdtest.AuthzUserSubject(admin, ownerUser.OrganizationID)), |
| 113 | + database.UpdateWorkspaceDeletedByIDParams{ |
| 114 | + ID: newWorkspace.ID, |
| 115 | + Deleted: true, |
| 116 | + }) |
| 117 | + require.NoError(t, err) |
| 118 | + |
| 119 | + resp, err := agentAPI.UpdateStats(context.Background(), &agentproto.UpdateStatsRequest{ |
| 120 | + Stats: &agentproto.Stats{ |
| 121 | + ConnectionsByProto: map[string]int64{"TCP": 1}, |
| 122 | + ConnectionCount: 1, |
| 123 | + RxPackets: 1, |
| 124 | + RxBytes: 1, |
| 125 | + TxPackets: 1, |
| 126 | + TxBytes: 1, |
| 127 | + SessionCountVscode: 1, |
| 128 | + SessionCountJetbrains: 0, |
| 129 | + SessionCountReconnectingPty: 0, |
| 130 | + SessionCountSsh: 0, |
| 131 | + ConnectionMedianLatencyMs: 10, |
| 132 | + }, |
| 133 | + }) |
| 134 | + require.Nil(t, resp) |
| 135 | + require.ErrorContains(t, err, "agent is invalid") |
| 136 | + }) |
| 137 | +} |
| 138 | + |
17 | 139 | func TestAgentAPI_LargeManifest(t *testing.T) {
|
18 | 140 | t.Parallel()
|
19 | 141 | ctx := testutil.Context(t, testutil.WaitLong)
|
|
0 commit comments