MCPcopy Create free account
hub / github.com/coder/coder / TestPublisherTallymanError

Function TestPublisherTallymanError

enterprise/coderd/usage/publisher_test.go:563–629  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

561}
562
563func TestPublisherTallymanError(t *testing.T) {
564 t.Parallel()
565 ctx := testutil.Context(t, testutil.WaitLong)
566 log := slogtest.Make(t, nil)
567 ctrl := gomock.NewController(t)
568 db := dbmock.NewMockStore(ctrl)
569 clock := quartz.NewMock(t)
570 now := time.Now()
571 clock.Set(now)
572
573 deploymentID, licenseJWT := configureMockDeployment(t, db)
574 const errorMessage = "tallyman error"
575 var calls int
576 ingestURL := fakeServer(t, tallymanHandler(t, deploymentID.String(), licenseJWT, func(req usagetypes.TallymanV1IngestRequest) any {
577 calls++
578 return usagetypes.TallymanV1Response{
579 Message: errorMessage,
580 }
581 }))
582
583 publisher := usage.NewTallymanPublisher(ctx, log, db, coderdenttest.Keys,
584 usage.PublisherWithClock(clock),
585 usage.PublisherWithIngestURL(ingestURL),
586 )
587 defer publisher.Close()
588
589 // Start the publisher with a trap.
590 tickerTrap := clock.Trap().NewTicker()
591 defer tickerTrap.Close()
592 startErr := make(chan error)
593 go func() {
594 err := publisher.Start()
595 testutil.RequireSend(ctx, t, startErr, err)
596 }()
597 tickerCall := tickerTrap.MustWait(ctx)
598 tickerCall.MustRelease(ctx)
599 require.NoError(t, testutil.RequireReceive(ctx, t, startErr))
600
601 // Mock events to be published.
602 events := []database.UsageEvent{
603 {
604 ID: uuid.New().String(),
605 EventType: string(usagetypes.UsageEventTypeDCManagedAgentsV1),
606 EventData: []byte(jsoninate(t, usagetypes.DCManagedAgentsV1{
607 Count: 1,
608 })),
609 },
610 }
611 db.EXPECT().SelectUsageEventsForPublishing(gomock.Any(), gomock.Any()).Return(events, nil).Times(1)
612 db.EXPECT().UpdateUsageEventsPostPublish(gomock.Any(), gomock.Any()).DoAndReturn(
613 func(ctx context.Context, params database.UpdateUsageEventsPostPublishParams) error {
614 assert.Equal(t, []string{events[0].ID}, params.IDs)
615 assert.Contains(t, params.FailureMessages[0], errorMessage)
616 assert.Equal(t, []bool{false}, params.SetPublishedAts)
617 return nil
618 },
619 ).Times(1)
620

Callers

nothing calls this directly

Calls 15

EXPECTMethod · 0.95
ContextFunction · 0.92
NewMockStoreFunction · 0.92
NewTallymanPublisherFunction · 0.92
PublisherWithClockFunction · 0.92
PublisherWithIngestURLFunction · 0.92
RequireSendFunction · 0.92
RequireReceiveFunction · 0.92
configureMockDeploymentFunction · 0.85
fakeServerFunction · 0.85
tallymanHandlerFunction · 0.85
jsoninateFunction · 0.85

Tested by

no test coverage detected