@@ -15,11 +15,14 @@ import (
15
15
16
16
"cdr.dev/slog/sloggers/slogtest"
17
17
18
+ "github.com/coder/coder/coderd/coderdtest"
18
19
"github.com/coder/coder/coderd/database"
19
20
"github.com/coder/coder/coderd/database/dbfake"
20
21
"github.com/coder/coder/coderd/database/dbgen"
21
22
"github.com/coder/coder/coderd/prometheusmetrics"
22
23
"github.com/coder/coder/codersdk"
24
+ "github.com/coder/coder/provisioner/echo"
25
+ "github.com/coder/coder/provisionersdk/proto"
23
26
"github.com/coder/coder/tailnet"
24
27
"github.com/coder/coder/tailnet/tailnettest"
25
28
"github.com/coder/coder/testutil"
@@ -249,14 +252,53 @@ func TestWorkspaces(t *testing.T) {
249
252
func TestAgents (t * testing.T ) {
250
253
t .Parallel ()
251
254
252
- // given
253
- db := dbfake .New ()
255
+ // Build a sample workspace with test agent and fake application
256
+ client , _ , api := coderdtest .NewWithAPI (t , & coderdtest.Options {IncludeProvisionerDaemon : true })
257
+ db := api .Database
258
+
259
+ user := coderdtest .CreateFirstUser (t , client )
260
+ version := coderdtest .CreateTemplateVersion (t , client , user .OrganizationID , & echo.Responses {
261
+ Parse : echo .ParseComplete ,
262
+ ProvisionPlan : echo .ProvisionComplete ,
263
+ ProvisionApply : []* proto.Provision_Response {{
264
+ Type : & proto.Provision_Response_Complete {
265
+ Complete : & proto.Provision_Complete {
266
+ Resources : []* proto.Resource {{
267
+ Name : "example" ,
268
+ Type : "aws_instance" ,
269
+ Agents : []* proto.Agent {{
270
+ Id : uuid .NewString (),
271
+ Name : "testagent" ,
272
+ Directory : t .TempDir (),
273
+ Auth : & proto.Agent_Token {
274
+ Token : uuid .NewString (),
275
+ },
276
+ Apps : []* proto.App {
277
+ {
278
+ Slug : "fake-app" ,
279
+ DisplayName : "Fake application" ,
280
+ SharingLevel : proto .AppSharingLevel_OWNER ,
281
+ // Hopefully this IP and port doesn't exist.
282
+ Url : "http://127.1.0.1:65535" ,
283
+ },
284
+ },
285
+ }},
286
+ }},
287
+ },
288
+ },
289
+ }},
290
+ })
291
+ template := coderdtest .CreateTemplate (t , client , user .OrganizationID , version .ID )
292
+ coderdtest .AwaitTemplateVersionJob (t , client , version .ID )
293
+ workspace := coderdtest .CreateWorkspace (t , client , user .OrganizationID , template .ID )
294
+ coderdtest .AwaitWorkspaceBuildJob (t , client , workspace .LatestBuild .ID )
254
295
296
+ // given
255
297
coordinator := tailnet .NewCoordinator ()
256
298
coordinatorPtr := atomic.Pointer [tailnet.Coordinator ]{}
257
299
coordinatorPtr .Store (& coordinator )
258
300
derpMap := tailnettest .RunDERPAndSTUN (t )
259
- agentInactiveDisconnectTimeout := 1 * time .Hour
301
+ agentInactiveDisconnectTimeout := 1 * time .Hour // don't need to focus on this value in tests
260
302
registry := prometheus .NewRegistry ()
261
303
262
304
// when
@@ -265,6 +307,10 @@ func TestAgents(t *testing.T) {
265
307
266
308
// then
267
309
require .NoError (t , err )
310
+
311
+ var agentsUp bool
312
+ var agentsConnections bool
313
+ var agentsApps bool
268
314
require .Eventually (t , func () bool {
269
315
metrics , err := registry .Gather ()
270
316
assert .NoError (t , err )
@@ -273,9 +319,34 @@ func TestAgents(t *testing.T) {
273
319
return false
274
320
}
275
321
276
- for _ , metric := range metrics [0 ].Metric {
277
- panic (metric )
322
+ for _ , metric := range metrics {
323
+ switch metric .GetName () {
324
+ case "coderd_agents_up" :
325
+ assert .Equal (t , "testuser" , metric .Metric [0 ].Label [0 ].GetValue ()) // Username
326
+ assert .Equal (t , workspace .Name , metric .Metric [0 ].Label [1 ].GetValue ()) // Workspace name
327
+ assert .Equal (t , 1 , int (metric .Metric [0 ].Gauge .GetValue ())) // Metric value
328
+ agentsUp = true
329
+ case "coderd_agents_connections" :
330
+ assert .Equal (t , "testagent" , metric .Metric [0 ].Label [0 ].GetValue ()) // Agent name
331
+ assert .Equal (t , "created" , metric .Metric [0 ].Label [1 ].GetValue ()) // Lifecycle state
332
+ assert .Equal (t , "connecting" , metric .Metric [0 ].Label [2 ].GetValue ()) // Status
333
+ assert .Equal (t , "unknown" , metric .Metric [0 ].Label [3 ].GetValue ()) // Tailnet node
334
+ assert .Equal (t , "testuser" , metric .Metric [0 ].Label [4 ].GetValue ()) // Username
335
+ assert .Equal (t , workspace .Name , metric .Metric [0 ].Label [5 ].GetValue ()) // Workspace name
336
+ assert .Equal (t , 1 , int (metric .Metric [0 ].Gauge .GetValue ())) // Metric value
337
+ agentsConnections = true
338
+ case "coderd_agents_apps" :
339
+ assert .Equal (t , "testagent" , metric .Metric [0 ].Label [0 ].GetValue ()) // Agent name
340
+ assert .Equal (t , "Fake application" , metric .Metric [0 ].Label [1 ].GetValue ()) // App name
341
+ assert .Equal (t , "disabled" , metric .Metric [0 ].Label [2 ].GetValue ()) // Health
342
+ assert .Equal (t , "testuser" , metric .Metric [0 ].Label [3 ].GetValue ()) // Username
343
+ assert .Equal (t , workspace .Name , metric .Metric [0 ].Label [4 ].GetValue ()) // Workspace name
344
+ assert .Equal (t , 1 , int (metric .Metric [0 ].Gauge .GetValue ())) // Metric value
345
+ agentsApps = true
346
+ default :
347
+ require .FailNowf (t , "unexpected metric collected" , "metric: %s" , metric .GetName ())
348
+ }
278
349
}
279
- return true
350
+ return agentsUp && agentsConnections && agentsApps
280
351
}, testutil .WaitShort , testutil .IntervalFast )
281
352
}
0 commit comments