@@ -13,98 +13,156 @@ import (
13
13
"github.com/coder/coder/v2/coderd/database"
14
14
"github.com/coder/coder/v2/coderd/database/dbfake"
15
15
"github.com/coder/coder/v2/coderd/database/dbtime"
16
+ "github.com/coder/coder/v2/coderd/rbac"
16
17
"github.com/coder/coder/v2/codersdk/agentsdk"
17
18
"github.com/coder/coder/v2/provisionersdk/proto"
18
19
"github.com/coder/coder/v2/testutil"
19
20
)
20
21
21
22
// Ported to RPC API from coderd/workspaceagents_test.go
22
23
func TestWorkspaceAgentReportStats (t * testing.T ) {
23
- t .Parallel ()
24
+ for _ , tc := range []struct {
25
+ name string
26
+ apiKeyScope rbac.ScopeName
27
+ }{
28
+ {
29
+ name : "empty (backwards compat)" ,
30
+ apiKeyScope : "" ,
31
+ },
32
+ {
33
+ name : "all" ,
34
+ apiKeyScope : rbac .ScopeAll ,
35
+ },
36
+ {
37
+ name : "no_user_data" ,
38
+ apiKeyScope : rbac .ScopeNoUserData ,
39
+ },
40
+ {
41
+ name : "application_connect" ,
42
+ apiKeyScope : rbac .ScopeApplicationConnect ,
43
+ },
44
+ } {
45
+ t .Run (tc .name , func (t * testing.T ) {
46
+ t .Parallel ()
24
47
25
- tickCh := make (chan time.Time )
26
- flushCh := make (chan int , 1 )
27
- client , db := coderdtest .NewWithDatabase (t , & coderdtest.Options {
28
- WorkspaceUsageTrackerFlush : flushCh ,
29
- WorkspaceUsageTrackerTick : tickCh ,
30
- })
31
- user := coderdtest .CreateFirstUser (t , client )
32
- r := dbfake .WorkspaceBuild (t , db , database.WorkspaceTable {
33
- OrganizationID : user .OrganizationID ,
34
- OwnerID : user .UserID ,
35
- LastUsedAt : dbtime .Now ().Add (- time .Minute ),
36
- }).WithAgent ().Do ()
48
+ tickCh := make (chan time.Time )
49
+ flushCh := make (chan int , 1 )
50
+ client , db := coderdtest .NewWithDatabase (t , & coderdtest.Options {
51
+ WorkspaceUsageTrackerFlush : flushCh ,
52
+ WorkspaceUsageTrackerTick : tickCh ,
53
+ })
54
+ user := coderdtest .CreateFirstUser (t , client )
55
+ r := dbfake .WorkspaceBuild (t , db , database.WorkspaceTable {
56
+ OrganizationID : user .OrganizationID ,
57
+ OwnerID : user .UserID ,
58
+ LastUsedAt : dbtime .Now ().Add (- time .Minute ),
59
+ }).WithAgent (
60
+ func (agent []* proto.Agent ) []* proto.Agent {
61
+ for _ , a := range agent {
62
+ a .ApiKeyScope = string (tc .apiKeyScope )
63
+ }
37
64
38
- ac := agentsdk .New (client .URL )
39
- ac .SetSessionToken (r .AgentToken )
40
- conn , err := ac .ConnectRPC (context .Background ())
41
- require .NoError (t , err )
42
- defer func () {
43
- _ = conn .Close ()
44
- }()
45
- agentAPI := agentproto .NewDRPCAgentClient (conn )
65
+ return agent
66
+ },
67
+ ).Do ()
46
68
47
- _ , err = agentAPI .UpdateStats (context .Background (), & agentproto.UpdateStatsRequest {
48
- Stats : & agentproto.Stats {
49
- ConnectionsByProto : map [string ]int64 {"TCP" : 1 },
50
- ConnectionCount : 1 ,
51
- RxPackets : 1 ,
52
- RxBytes : 1 ,
53
- TxPackets : 1 ,
54
- TxBytes : 1 ,
55
- SessionCountVscode : 1 ,
56
- SessionCountJetbrains : 0 ,
57
- SessionCountReconnectingPty : 0 ,
58
- SessionCountSsh : 0 ,
59
- ConnectionMedianLatencyMs : 10 ,
60
- },
61
- })
62
- require .NoError (t , err )
69
+ ac := agentsdk .New (client .URL )
70
+ ac .SetSessionToken (r .AgentToken )
71
+ conn , err := ac .ConnectRPC (context .Background ())
72
+ require .NoError (t , err )
73
+ defer func () {
74
+ _ = conn .Close ()
75
+ }()
76
+ agentAPI := agentproto .NewDRPCAgentClient (conn )
77
+
78
+ _ , err = agentAPI .UpdateStats (context .Background (), & agentproto.UpdateStatsRequest {
79
+ Stats : & agentproto.Stats {
80
+ ConnectionsByProto : map [string ]int64 {"TCP" : 1 },
81
+ ConnectionCount : 1 ,
82
+ RxPackets : 1 ,
83
+ RxBytes : 1 ,
84
+ TxPackets : 1 ,
85
+ TxBytes : 1 ,
86
+ SessionCountVscode : 1 ,
87
+ SessionCountJetbrains : 0 ,
88
+ SessionCountReconnectingPty : 0 ,
89
+ SessionCountSsh : 0 ,
90
+ ConnectionMedianLatencyMs : 10 ,
91
+ },
92
+ })
93
+ require .NoError (t , err )
63
94
64
- tickCh <- dbtime .Now ()
65
- count := <- flushCh
66
- require .Equal (t , 1 , count , "expected one flush with one id" )
95
+ tickCh <- dbtime .Now ()
96
+ count := <- flushCh
97
+ require .Equal (t , 1 , count , "expected one flush with one id" )
67
98
68
- newWorkspace , err := client .Workspace (context .Background (), r .Workspace .ID )
69
- require .NoError (t , err )
99
+ newWorkspace , err := client .Workspace (context .Background (), r .Workspace .ID )
100
+ require .NoError (t , err )
70
101
71
- assert .True (t ,
72
- newWorkspace .LastUsedAt .After (r .Workspace .LastUsedAt ),
73
- "%s is not after %s" , newWorkspace .LastUsedAt , r .Workspace .LastUsedAt ,
74
- )
102
+ assert .True (t ,
103
+ newWorkspace .LastUsedAt .After (r .Workspace .LastUsedAt ),
104
+ "%s is not after %s" , newWorkspace .LastUsedAt , r .Workspace .LastUsedAt ,
105
+ )
106
+ })
107
+ }
75
108
}
76
109
77
110
func TestAgentAPI_LargeManifest (t * testing.T ) {
78
- t .Parallel ()
79
- ctx := testutil .Context (t , testutil .WaitLong )
80
- client , store := coderdtest .NewWithDatabase (t , nil )
81
- adminUser := coderdtest .CreateFirstUser (t , client )
82
- n := 512000
83
- longScript := make ([]byte , n )
84
- for i := range longScript {
85
- longScript [i ] = 'q'
111
+ for _ , tc := range []struct {
112
+ name string
113
+ apiKeyScope rbac.ScopeName
114
+ }{
115
+ {
116
+ name : "empty (backwards compat)" ,
117
+ apiKeyScope : "" ,
118
+ },
119
+ {
120
+ name : "all" ,
121
+ apiKeyScope : rbac .ScopeAll ,
122
+ },
123
+ {
124
+ name : "no_user_data" ,
125
+ apiKeyScope : rbac .ScopeNoUserData ,
126
+ },
127
+ {
128
+ name : "application_connect" ,
129
+ apiKeyScope : rbac .ScopeApplicationConnect ,
130
+ },
131
+ } {
132
+ t .Run (tc .name , func (t * testing.T ) {
133
+ t .Parallel ()
134
+ ctx := testutil .Context (t , testutil .WaitLong )
135
+ client , store := coderdtest .NewWithDatabase (t , nil )
136
+ adminUser := coderdtest .CreateFirstUser (t , client )
137
+ n := 512000
138
+ longScript := make ([]byte , n )
139
+ for i := range longScript {
140
+ longScript [i ] = 'q'
141
+ }
142
+ r := dbfake .WorkspaceBuild (t , store , database.WorkspaceTable {
143
+ OrganizationID : adminUser .OrganizationID ,
144
+ OwnerID : adminUser .UserID ,
145
+ }).WithAgent (func (agents []* proto.Agent ) []* proto.Agent {
146
+ agents [0 ].Scripts = []* proto.Script {
147
+ {
148
+ Script : string (longScript ),
149
+ },
150
+ }
151
+ agents [0 ].ApiKeyScope = string (tc .apiKeyScope )
152
+ return agents
153
+ }).Do ()
154
+ ac := agentsdk .New (client .URL )
155
+ ac .SetSessionToken (r .AgentToken )
156
+ conn , err := ac .ConnectRPC (ctx )
157
+ defer func () {
158
+ _ = conn .Close ()
159
+ }()
160
+ require .NoError (t , err )
161
+ agentAPI := agentproto .NewDRPCAgentClient (conn )
162
+ manifest , err := agentAPI .GetManifest (ctx , & agentproto.GetManifestRequest {})
163
+ require .NoError (t , err )
164
+ require .Len (t , manifest .Scripts , 1 )
165
+ require .Len (t , manifest .Scripts [0 ].Script , n )
166
+ })
86
167
}
87
- r := dbfake .WorkspaceBuild (t , store , database.WorkspaceTable {
88
- OrganizationID : adminUser .OrganizationID ,
89
- OwnerID : adminUser .UserID ,
90
- }).WithAgent (func (agents []* proto.Agent ) []* proto.Agent {
91
- agents [0 ].Scripts = []* proto.Script {
92
- {
93
- Script : string (longScript ),
94
- },
95
- }
96
- return agents
97
- }).Do ()
98
- ac := agentsdk .New (client .URL )
99
- ac .SetSessionToken (r .AgentToken )
100
- conn , err := ac .ConnectRPC (ctx )
101
- defer func () {
102
- _ = conn .Close ()
103
- }()
104
- require .NoError (t , err )
105
- agentAPI := agentproto .NewDRPCAgentClient (conn )
106
- manifest , err := agentAPI .GetManifest (ctx , & agentproto.GetManifestRequest {})
107
- require .NoError (t , err )
108
- require .Len (t , manifest .Scripts , 1 )
109
- require .Len (t , manifest .Scripts [0 ].Script , n )
110
168
}
0 commit comments