TestIntegration tests the inserter and publisher by running them with a real database.
(t *testing.T)
| 38 | // TestIntegration tests the inserter and publisher by running them with a real |
| 39 | // database. |
| 40 | func TestIntegration(t *testing.T) { |
| 41 | t.Parallel() |
| 42 | const eventCount = 3 |
| 43 | |
| 44 | ctx := testutil.Context(t, testutil.WaitLong) |
| 45 | log := slogtest.Make(t, nil) |
| 46 | db, _ := dbtestutil.NewDB(t) |
| 47 | |
| 48 | clock := quartz.NewMock(t) |
| 49 | deploymentID, licenseJWT := configureDeployment(ctx, t, db) |
| 50 | now := time.Now() |
| 51 | |
| 52 | var ( |
| 53 | calls int |
| 54 | handler func(req usagetypes.TallymanV1IngestRequest) any |
| 55 | ) |
| 56 | ingestURL := fakeServer(t, tallymanHandler(t, deploymentID.String(), licenseJWT, func(req usagetypes.TallymanV1IngestRequest) any { |
| 57 | calls++ |
| 58 | t.Logf("tallyman backend received call %d", calls) |
| 59 | |
| 60 | if handler == nil { |
| 61 | t.Errorf("handler is nil") |
| 62 | return usagetypes.TallymanV1IngestResponse{} |
| 63 | } |
| 64 | return handler(req) |
| 65 | })) |
| 66 | |
| 67 | inserter := usage.NewDBInserter( |
| 68 | usage.InserterWithClock(clock), |
| 69 | ) |
| 70 | // Insert an old event that should never be published. |
| 71 | clock.Set(now.Add(-31 * 24 * time.Hour)) |
| 72 | err := inserter.InsertDiscreteUsageEvent(ctx, db, usagetypes.DCManagedAgentsV1{ |
| 73 | Count: 31, |
| 74 | }) |
| 75 | require.NoError(t, err) |
| 76 | |
| 77 | // Insert the events we expect to be published. |
| 78 | clock.Set(now.Add(1 * time.Second)) |
| 79 | for i := 0; i < eventCount; i++ { |
| 80 | clock.Advance(time.Second) |
| 81 | err := inserter.InsertDiscreteUsageEvent(ctx, db, usagetypes.DCManagedAgentsV1{ |
| 82 | Count: uint64(i + 1), // nolint:gosec // these numbers are tiny and will not overflow |
| 83 | }) |
| 84 | require.NoErrorf(t, err, "collecting event %d", i) |
| 85 | } |
| 86 | |
| 87 | // Wrap the publisher's DB in a dbauthz to ensure that the publisher has |
| 88 | // enough permissions. |
| 89 | authzDB := dbauthz.New(db, rbac.NewAuthorizer(prometheus.NewRegistry()), log, coderdtest.AccessControlStorePointer()) |
| 90 | publisher := usage.NewTallymanPublisher(ctx, log, authzDB, coderdenttest.Keys, |
| 91 | usage.PublisherWithClock(clock), |
| 92 | usage.PublisherWithIngestURL(ingestURL), |
| 93 | ) |
| 94 | defer publisher.Close() |
| 95 | |
| 96 | // Start the publisher with a trap. |
| 97 | tickerTrap := clock.Trap().NewTicker() |
nothing calls this directly
no test coverage detected