@@ -7,12 +7,9 @@ package apiv1
7
7
import (
8
8
"context"
9
9
"database/sql"
10
- "reflect"
11
10
"testing"
12
11
"time"
13
12
14
- "github.com/gitpod-io/gitpod/usage/pkg/contentservice"
15
-
16
13
"github.com/gitpod-io/gitpod/common-go/baseserver"
17
14
v1 "github.com/gitpod-io/gitpod/usage-api/v1"
18
15
"github.com/gitpod-io/gitpod/usage/pkg/db"
@@ -375,182 +372,6 @@ func TestInstanceToUsageRecords(t *testing.T) {
375
372
}
376
373
}
377
374
378
- func TestReportGenerator_GenerateUsageReport (t * testing.T ) {
379
- startOfMay := time .Date (2022 , 05 , 1 , 0 , 00 , 00 , 00 , time .UTC )
380
- startOfJune := time .Date (2022 , 06 , 1 , 0 , 00 , 00 , 00 , time .UTC )
381
-
382
- teamID := uuid .New ()
383
- scenarioRunTime := time .Date (2022 , 05 , 31 , 23 , 00 , 00 , 00 , time .UTC )
384
-
385
- instances := []db.WorkspaceInstance {
386
- // Ran throughout the reconcile period
387
- dbtest .NewWorkspaceInstance (t , db.WorkspaceInstance {
388
- ID : uuid .New (),
389
- UsageAttributionID : db .NewTeamAttributionID (teamID .String ()),
390
- StartedTime : db .NewVarcharTime (time .Date (2022 , 05 , 1 , 00 , 01 , 00 , 00 , time .UTC )),
391
- StoppingTime : db .NewVarcharTime (time .Date (2022 , 06 , 1 , 1 , 0 , 0 , 0 , time .UTC )),
392
- }),
393
- // Still running
394
- dbtest .NewWorkspaceInstance (t , db.WorkspaceInstance {
395
- ID : uuid .New (),
396
- UsageAttributionID : db .NewTeamAttributionID (teamID .String ()),
397
- StartedTime : db .NewVarcharTime (time .Date (2022 , 05 , 30 , 00 , 01 , 00 , 00 , time .UTC )),
398
- }),
399
- // No creation time, invalid record, ignored
400
- dbtest .NewWorkspaceInstance (t , db.WorkspaceInstance {
401
- ID : uuid .New (),
402
- UsageAttributionID : db .NewTeamAttributionID (teamID .String ()),
403
- StoppingTime : db .NewVarcharTime (time .Date (2022 , 06 , 1 , 1 , 0 , 0 , 0 , time .UTC )),
404
- }),
405
- }
406
-
407
- conn := dbtest .ConnectForTests (t )
408
- dbtest .CreateWorkspaceInstances (t , conn , instances ... )
409
-
410
- nowFunc := func () time.Time { return scenarioRunTime }
411
- generator := & ReportGenerator {
412
- nowFunc : nowFunc ,
413
- conn : conn ,
414
- pricer : DefaultWorkspacePricer ,
415
- }
416
-
417
- report , err := generator .GenerateUsageReport (context .Background (), startOfMay , startOfJune )
418
- require .NoError (t , err )
419
-
420
- require .Equal (t , nowFunc (), report .GenerationTime )
421
- require .Equal (t , startOfMay , report .From )
422
- // require.Equal(t, startOfJune, report.To) TODO(gpl) This is not true anymore - does it really make sense to test for it?
423
- require .Len (t , report .InvalidSessions , 0 )
424
- require .Len (t , report .UsageRecords , 2 )
425
- }
426
-
427
- func TestReportGenerator_GenerateUsageReportTable (t * testing.T ) {
428
- teamID := uuid .New ()
429
- instanceID := uuid .New ()
430
-
431
- Must := func (ti db.VarcharTime , err error ) db.VarcharTime {
432
- if err != nil {
433
- t .Fatal (err )
434
- }
435
- return ti
436
- }
437
- Timestamp := func (timestampAsStr string ) db.VarcharTime {
438
- return Must (db .NewVarcharTimeFromStr (timestampAsStr ))
439
- }
440
- type Expectation struct {
441
- custom func (t * testing.T , report contentservice.UsageReport )
442
- usageRecords []db.WorkspaceInstanceUsage
443
- }
444
-
445
- type TestCase struct {
446
- name string
447
- from time.Time
448
- to time.Time
449
- runtime time.Time
450
- instances []db.WorkspaceInstance
451
- expectation Expectation
452
- }
453
- tests := []TestCase {
454
- {
455
- name : "real example taken from DB: runtime _before_ instance.startedTime" ,
456
- from : time .Date (2022 , 8 , 1 , 0 , 00 , 00 , 00 , time .UTC ),
457
- to : time .Date (2022 , 9 , 1 , 0 , 00 , 00 , 00 , time .UTC ),
458
- runtime : Timestamp ("2022-08-17T09:38:28Z" ).Time (),
459
- instances : []db.WorkspaceInstance {
460
- dbtest .NewWorkspaceInstance (t , db.WorkspaceInstance {
461
- ID : instanceID ,
462
- UsageAttributionID : db .NewTeamAttributionID (teamID .String ()),
463
- CreationTime : Timestamp ("2022-08-17T09:40:47.316Z" ),
464
- StartedTime : Timestamp ("2022-08-17T09:40:53.115Z" ),
465
- StoppingTime : Timestamp ("2022-08-17T09:42:36.292Z" ),
466
- StoppedTime : Timestamp ("2022-08-17T09:43:04.874Z" ),
467
- }),
468
- },
469
- expectation : Expectation {
470
- usageRecords : nil ,
471
- // usageRecords: []db.WorkspaceInstanceUsage{
472
- // {
473
- // InstanceID: instanceID,
474
- // AttributionID: db.NewTeamAttributionID(teamID.String()),
475
- // StartedAt: Timestamp("2022-08-17T09:40:53.115Z").Time(),
476
- // StoppedAt: sql.NullTime{ Time: Timestamp("2022-08-17T09:43:04.874Z").Time(), Valid: true },
477
- // WorkspaceClass: "default",
478
- // CreditsUsed: 3.0,
479
- // },
480
- // },
481
- },
482
- },
483
- {
484
- name : "same as above, but with runtime _after_ startedTime" ,
485
- from : time .Date (2022 , 8 , 1 , 0 , 00 , 00 , 00 , time .UTC ),
486
- to : time .Date (2022 , 9 , 1 , 0 , 00 , 00 , 00 , time .UTC ),
487
- runtime : Timestamp ("2022-08-17T09:41:00Z" ).Time (),
488
- instances : []db.WorkspaceInstance {
489
- dbtest .NewWorkspaceInstance (t , db.WorkspaceInstance {
490
- ID : instanceID ,
491
- UsageAttributionID : db .NewTeamAttributionID (teamID .String ()),
492
- CreationTime : Timestamp ("2022-08-17T09:40:47.316Z" ),
493
- StartedTime : Timestamp ("2022-08-17T09:40:53.115Z" ),
494
- StoppingTime : Timestamp ("2022-08-17T09:42:36.292Z" ),
495
- StoppedTime : Timestamp ("2022-08-17T09:43:04.874Z" ),
496
- }),
497
- },
498
- expectation : Expectation {
499
- usageRecords : []db.WorkspaceInstanceUsage {
500
- {
501
- InstanceID : instanceID ,
502
- AttributionID : db .NewTeamAttributionID (teamID .String ()),
503
- StartedAt : Timestamp ("2022-08-17T09:40:53.115Z" ).Time (),
504
- StoppedAt : sql.NullTime {Time : Timestamp ("2022-08-17T09:41:00Z" ).Time (), Valid : true },
505
- WorkspaceClass : "default" ,
506
- CreditsUsed : 0.019444444444444445 ,
507
- },
508
- },
509
- },
510
- },
511
- }
512
-
513
- for _ , test := range tests {
514
- t .Run (test .name , func (t * testing.T ) {
515
- conn := dbtest .ConnectForTests (t )
516
- dbtest .CreateWorkspaceInstances (t , conn , test .instances ... )
517
-
518
- nowFunc := func () time.Time { return test .runtime }
519
- generator := & ReportGenerator {
520
- nowFunc : nowFunc ,
521
- conn : conn ,
522
- pricer : DefaultWorkspacePricer ,
523
- }
524
-
525
- report , err := generator .GenerateUsageReport (context .Background (), test .from , test .to )
526
- require .NoError (t , err )
527
-
528
- require .Equal (t , test .runtime , report .GenerationTime )
529
- require .Equal (t , test .from , report .From )
530
- // require.Equal(t, test.to, report.To) TODO(gpl) This is not true anymore - does it really make sense to test for it?
531
-
532
- // These invariants should always be true:
533
- // 1. No negative usage
534
- for _ , rec := range report .UsageRecords {
535
- if rec .CreditsUsed < 0 {
536
- t .Error ("Got report with negative credits!" )
537
- }
538
- }
539
-
540
- if ! reflect .DeepEqual (test .expectation .usageRecords , report .UsageRecords ) {
541
- t .Errorf ("report.UsageRecords: expected %v but got %v" , test .expectation .usageRecords , report .UsageRecords )
542
- }
543
-
544
- // Custom expectations
545
- customTestFunction := test .expectation .custom
546
- if customTestFunction != nil {
547
- customTestFunction (t , report )
548
- require .NoError (t , err )
549
- }
550
- })
551
- }
552
- }
553
-
554
375
func TestUsageService_ReconcileUsageWithLedger (t * testing.T ) {
555
376
dbconn := dbtest .ConnectForTests (t )
556
377
from := time .Date (2022 , 05 , 1 , 0 , 00 , 00 , 00 , time .UTC )
@@ -673,11 +494,11 @@ func TestReconcileWithLedger(t *testing.T) {
673
494
WorkspaceId : instance .WorkspaceID ,
674
495
WorkspaceType : instance .Type ,
675
496
WorkspaceClass : instance .WorkspaceClass ,
676
- ContextURL : "" ,
497
+ ContextURL : instance . ContextURL ,
677
498
StartTime : db .TimeToISO8601 (instance .StartedTime .Time ()),
678
499
EndTime : "" ,
679
- UserName : "" ,
680
- UserAvatarURL : "" ,
500
+ UserName : instance . UserName ,
501
+ UserAvatarURL : instance . UserAvatarURL ,
681
502
}))
682
503
require .EqualValues (t , expectedUsage , inserts [0 ])
683
504
})
@@ -731,11 +552,11 @@ func TestReconcileWithLedger(t *testing.T) {
731
552
WorkspaceId : instance .WorkspaceID ,
732
553
WorkspaceType : instance .Type ,
733
554
WorkspaceClass : instance .WorkspaceClass ,
734
- ContextURL : "" ,
555
+ ContextURL : instance . ContextURL ,
735
556
StartTime : db .TimeToISO8601 (instance .StartedTime .Time ()),
736
557
EndTime : "" ,
737
- UserName : "" ,
738
- UserAvatarURL : "" ,
558
+ UserName : instance . UserName ,
559
+ UserAvatarURL : instance . UserAvatarURL ,
739
560
}))
740
561
require .EqualValues (t , expectedUsage , updates [0 ])
741
562
})
0 commit comments