Skip to content

Commit b341266

Browse files
committed
ported workspaceagent test
1 parent 48b31d3 commit b341266

File tree

1 file changed

+122
-0
lines changed

1 file changed

+122
-0
lines changed

coderd/workspaceagentsrpc_test.go

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,141 @@
11
package coderd_test
22

33
import (
4+
"context"
45
"testing"
56

7+
"github.com/stretchr/testify/assert"
68
"github.com/stretchr/testify/require"
79

810
agentproto "github.com/coder/coder/v2/agent/proto"
911
"github.com/coder/coder/v2/coderd/coderdtest"
1012
"github.com/coder/coder/v2/coderd/database"
13+
"github.com/coder/coder/v2/coderd/database/dbauthz"
1114
"github.com/coder/coder/v2/coderd/database/dbfake"
15+
"github.com/coder/coder/v2/coderd/rbac"
1216
"github.com/coder/coder/v2/codersdk/agentsdk"
1317
"github.com/coder/coder/v2/provisionersdk/proto"
1418
"github.com/coder/coder/v2/testutil"
1519
)
1620

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+
17139
func TestAgentAPI_LargeManifest(t *testing.T) {
18140
t.Parallel()
19141
ctx := testutil.Context(t, testutil.WaitLong)

0 commit comments

Comments
 (0)