1
1
package telemetry_test
2
2
3
3
import (
4
+ "context"
4
5
"database/sql"
5
6
"encoding/json"
6
7
"net/http"
@@ -115,7 +116,7 @@ func TestTelemetry(t *testing.T) {
115
116
_ = dbgen .WorkspaceAgentMemoryResourceMonitor (t , db , database.WorkspaceAgentMemoryResourceMonitor {})
116
117
_ = dbgen .WorkspaceAgentVolumeResourceMonitor (t , db , database.WorkspaceAgentVolumeResourceMonitor {})
117
118
118
- _ , snapshot := collectSnapshot (t , db , nil )
119
+ _ , snapshot := collectSnapshot (ctx , t , db , nil )
119
120
require .Len (t , snapshot .ProvisionerJobs , 1 )
120
121
require .Len (t , snapshot .Licenses , 1 )
121
122
require .Len (t , snapshot .Templates , 1 )
@@ -168,17 +169,19 @@ func TestTelemetry(t *testing.T) {
168
169
})
169
170
t .Run ("HashedEmail" , func (t * testing.T ) {
170
171
t .Parallel ()
172
+ ctx := testutil .Context (t , testutil .WaitMedium )
171
173
db := dbmem .New ()
172
174
_ = dbgen .User (t , db , database.User {
173
175
Email : "kyle@coder.com" ,
174
176
})
175
- _ , snapshot := collectSnapshot (t , db , nil )
177
+ _ , snapshot := collectSnapshot (ctx , t , db , nil )
176
178
require .Len (t , snapshot .Users , 1 )
177
179
require .Equal (t , snapshot .Users [0 ].EmailHashed , "bb44bf07cf9a2db0554bba63a03d822c927deae77df101874496df5a6a3e896d@coder.com" )
178
180
})
179
181
t .Run ("HashedModule" , func (t * testing.T ) {
180
182
t .Parallel ()
181
183
db , _ := dbtestutil .NewDB (t )
184
+ ctx := testutil .Context (t , testutil .WaitMedium )
182
185
pj := dbgen .ProvisionerJob (t , db , nil , database.ProvisionerJob {})
183
186
_ = dbgen .WorkspaceModule (t , db , database.WorkspaceModule {
184
187
JobID : pj .ID ,
@@ -190,7 +193,7 @@ func TestTelemetry(t *testing.T) {
190
193
Source : "https://internal-url.com/some-module" ,
191
194
Version : "1.0.0" ,
192
195
})
193
- _ , snapshot := collectSnapshot (t , db , nil )
196
+ _ , snapshot := collectSnapshot (ctx , t , db , nil )
194
197
require .Len (t , snapshot .WorkspaceModules , 2 )
195
198
modules := snapshot .WorkspaceModules
196
199
sort .Slice (modules , func (i , j int ) bool {
@@ -286,11 +289,11 @@ func TestTelemetry(t *testing.T) {
286
289
db , _ := dbtestutil .NewDB (t )
287
290
288
291
// 1. No org sync settings
289
- deployment , _ := collectSnapshot (t , db , nil )
292
+ deployment , _ := collectSnapshot (ctx , t , db , nil )
290
293
require .False (t , * deployment .IDPOrgSync )
291
294
292
295
// 2. Org sync settings set in server flags
293
- deployment , _ = collectSnapshot (t , db , func (opts telemetry.Options ) telemetry.Options {
296
+ deployment , _ = collectSnapshot (ctx , t , db , func (opts telemetry.Options ) telemetry.Options {
294
297
opts .DeploymentConfig = & codersdk.DeploymentValues {
295
298
OIDC : codersdk.OIDCConfig {
296
299
OrganizationField : "organizations" ,
@@ -312,16 +315,17 @@ func TestTelemetry(t *testing.T) {
312
315
AssignDefault : true ,
313
316
})
314
317
require .NoError (t , err )
315
- deployment , _ = collectSnapshot (t , db , nil )
318
+ deployment , _ = collectSnapshot (ctx , t , db , nil )
316
319
require .True (t , * deployment .IDPOrgSync )
317
320
})
318
321
}
319
322
320
323
// nolint:paralleltest
321
324
func TestTelemetryInstallSource (t * testing.T ) {
322
325
t .Setenv ("CODER_TELEMETRY_INSTALL_SOURCE" , "aws_marketplace" )
326
+ ctx := testutil .Context (t , testutil .WaitMedium )
323
327
db := dbmem .New ()
324
- deployment , _ := collectSnapshot (t , db , nil )
328
+ deployment , _ := collectSnapshot (ctx , t , db , nil )
325
329
require .Equal (t , "aws_marketplace" , deployment .InstallSource )
326
330
}
327
331
@@ -436,7 +440,7 @@ func TestRecordTelemetryStatus(t *testing.T) {
436
440
}
437
441
}
438
442
439
- func mockTelemetryServer (t * testing.T ) (* url.URL , chan * telemetry.Deployment , chan * telemetry.Snapshot ) {
443
+ func mockTelemetryServer (ctx context. Context , t * testing.T ) (* url.URL , chan * telemetry.Deployment , chan * telemetry.Snapshot ) {
440
444
t .Helper ()
441
445
deployment := make (chan * telemetry.Deployment , 64 )
442
446
snapshot := make (chan * telemetry.Snapshot , 64 )
@@ -446,7 +450,11 @@ func mockTelemetryServer(t *testing.T) (*url.URL, chan *telemetry.Deployment, ch
446
450
dd := & telemetry.Deployment {}
447
451
err := json .NewDecoder (r .Body ).Decode (dd )
448
452
require .NoError (t , err )
449
- deployment <- dd
453
+ ok := testutil .AssertSend (ctx , t , deployment , dd )
454
+ if ! ok {
455
+ w .WriteHeader (http .StatusInternalServerError )
456
+ return
457
+ }
450
458
// Ensure the header is sent only after deployment is sent
451
459
w .WriteHeader (http .StatusAccepted )
452
460
})
@@ -455,7 +463,11 @@ func mockTelemetryServer(t *testing.T) (*url.URL, chan *telemetry.Deployment, ch
455
463
ss := & telemetry.Snapshot {}
456
464
err := json .NewDecoder (r .Body ).Decode (ss )
457
465
require .NoError (t , err )
458
- snapshot <- ss
466
+ ok := testutil .AssertSend (ctx , t , snapshot , ss )
467
+ if ! ok {
468
+ w .WriteHeader (http .StatusInternalServerError )
469
+ return
470
+ }
459
471
// Ensure the header is sent only after snapshot is sent
460
472
w .WriteHeader (http .StatusAccepted )
461
473
})
@@ -467,10 +479,15 @@ func mockTelemetryServer(t *testing.T) (*url.URL, chan *telemetry.Deployment, ch
467
479
return serverURL , deployment , snapshot
468
480
}
469
481
470
- func collectSnapshot (t * testing.T , db database.Store , addOptionsFn func (opts telemetry.Options ) telemetry.Options ) (* telemetry.Deployment , * telemetry.Snapshot ) {
482
+ func collectSnapshot (
483
+ ctx context.Context ,
484
+ t * testing.T ,
485
+ db database.Store ,
486
+ addOptionsFn func (opts telemetry.Options ) telemetry.Options ,
487
+ ) (* telemetry.Deployment , * telemetry.Snapshot ) {
471
488
t .Helper ()
472
489
473
- serverURL , deployment , snapshot := mockTelemetryServer (t )
490
+ serverURL , deployment , snapshot := mockTelemetryServer (ctx , t )
474
491
475
492
options := telemetry.Options {
476
493
Database : db ,
@@ -485,5 +502,6 @@ func collectSnapshot(t *testing.T, db database.Store, addOptionsFn func(opts tel
485
502
reporter , err := telemetry .New (options )
486
503
require .NoError (t , err )
487
504
t .Cleanup (reporter .Close )
488
- return <- deployment , <- snapshot
505
+
506
+ return testutil .RequireReceive (ctx , t , deployment ), testutil .RequireReceive (ctx , t , snapshot )
489
507
}
0 commit comments