66 "encoding/json"
77 "net/http"
88 "net/http/httptest"
9+ "net/url"
910 "testing"
1011 "time"
1112
@@ -53,7 +54,7 @@ func TestIntegration(t *testing.T) {
5354 calls int
5455 handler func (req usagetypes.TallymanV1IngestRequest ) any
5556 )
56- ingestURL := fakeServer (t , tallymanHandler (t , deploymentID .String (), licenseJWT , func (req usagetypes.TallymanV1IngestRequest ) any {
57+ baseURL := fakeServer (t , tallymanHandler (t , deploymentID .String (), licenseJWT , func (req usagetypes.TallymanV1IngestRequest ) any {
5758 calls ++
5859 t .Logf ("tallyman backend received call %d" , calls )
5960
@@ -89,7 +90,7 @@ func TestIntegration(t *testing.T) {
8990 authzDB := dbauthz .New (db , rbac .NewAuthorizer (prometheus .NewRegistry ()), log , coderdtest .AccessControlStorePointer ())
9091 publisher := usage .NewTallymanPublisher (ctx , log , authzDB , coderdenttest .Keys ,
9192 usage .PublisherWithClock (clock ),
92- usage .PublisherWithIngestURL ( ingestURL ),
93+ usage .PublisherWithTallymanBaseURL ( baseURL ),
9394 )
9495 defer publisher .Close ()
9596
@@ -210,7 +211,7 @@ func TestPublisherNoEligibleLicenses(t *testing.T) {
210211 db .EXPECT ().GetDeploymentID (gomock .Any ()).Return (deploymentID .String (), nil ).Times (1 )
211212
212213 var calls int
213- ingestURL := fakeServer (t , tallymanHandler (t , deploymentID .String (), "" , func (req usagetypes.TallymanV1IngestRequest ) any {
214+ baseURL := fakeServer (t , tallymanHandler (t , deploymentID .String (), "" , func (req usagetypes.TallymanV1IngestRequest ) any {
214215 calls ++
215216 return usagetypes.TallymanV1IngestResponse {
216217 AcceptedEvents : []usagetypes.TallymanV1IngestAcceptedEvent {},
@@ -220,7 +221,7 @@ func TestPublisherNoEligibleLicenses(t *testing.T) {
220221
221222 publisher := usage .NewTallymanPublisher (ctx , log , db , coderdenttest .Keys ,
222223 usage .PublisherWithClock (clock ),
223- usage .PublisherWithIngestURL ( ingestURL ),
224+ usage .PublisherWithTallymanBaseURL ( baseURL ),
224225 )
225226 defer publisher .Close ()
226227
@@ -283,7 +284,7 @@ func TestPublisherClaimExpiry(t *testing.T) {
283284 now := time .Now ()
284285
285286 var calls int
286- ingestURL := fakeServer (t , tallymanHandler (t , deploymentID .String (), licenseJWT , func (req usagetypes.TallymanV1IngestRequest ) any {
287+ baseURL := fakeServer (t , tallymanHandler (t , deploymentID .String (), licenseJWT , func (req usagetypes.TallymanV1IngestRequest ) any {
287288 calls ++
288289 return tallymanAcceptAllHandler (req )
289290 }))
@@ -294,7 +295,7 @@ func TestPublisherClaimExpiry(t *testing.T) {
294295
295296 publisher := usage .NewTallymanPublisher (ctx , log , db , coderdenttest .Keys ,
296297 usage .PublisherWithClock (clock ),
297- usage .PublisherWithIngestURL ( ingestURL ),
298+ usage .PublisherWithTallymanBaseURL ( baseURL ),
298299 usage .PublisherWithInitialDelay (17 * time .Minute ),
299300 )
300301 defer publisher .Close ()
@@ -363,7 +364,7 @@ func TestPublisherMissingEvents(t *testing.T) {
363364 clock .Set (now )
364365
365366 var calls int
366- ingestURL := fakeServer (t , tallymanHandler (t , deploymentID .String (), licenseJWT , func (req usagetypes.TallymanV1IngestRequest ) any {
367+ baseURL := fakeServer (t , tallymanHandler (t , deploymentID .String (), licenseJWT , func (req usagetypes.TallymanV1IngestRequest ) any {
367368 calls ++
368369 return usagetypes.TallymanV1IngestResponse {
369370 AcceptedEvents : []usagetypes.TallymanV1IngestAcceptedEvent {},
@@ -373,7 +374,7 @@ func TestPublisherMissingEvents(t *testing.T) {
373374
374375 publisher := usage .NewTallymanPublisher (ctx , log , db , coderdenttest .Keys ,
375376 usage .PublisherWithClock (clock ),
376- usage .PublisherWithIngestURL ( ingestURL ),
377+ usage .PublisherWithTallymanBaseURL ( baseURL ),
377378 )
378379
379380 // Expect the publisher to call SelectUsageEventsForPublishing, followed by
@@ -507,14 +508,14 @@ func TestPublisherLicenseSelection(t *testing.T) {
507508 }, nil )
508509
509510 called := false
510- ingestURL := fakeServer (t , tallymanHandler (t , deploymentID .String (), expectedLicense , func (req usagetypes.TallymanV1IngestRequest ) any {
511+ baseURL := fakeServer (t , tallymanHandler (t , deploymentID .String (), expectedLicense , func (req usagetypes.TallymanV1IngestRequest ) any {
511512 called = true
512513 return tallymanAcceptAllHandler (req )
513514 }))
514515
515516 publisher := usage .NewTallymanPublisher (ctx , log , db , coderdenttest .Keys ,
516517 usage .PublisherWithClock (clock ),
517- usage .PublisherWithIngestURL ( ingestURL ),
518+ usage .PublisherWithTallymanBaseURL ( baseURL ),
518519 )
519520 defer publisher .Close ()
520521
@@ -573,7 +574,7 @@ func TestPublisherTallymanError(t *testing.T) {
573574 deploymentID , licenseJWT := configureMockDeployment (t , db )
574575 const errorMessage = "tallyman error"
575576 var calls int
576- ingestURL := fakeServer (t , tallymanHandler (t , deploymentID .String (), licenseJWT , func (req usagetypes.TallymanV1IngestRequest ) any {
577+ baseURL := fakeServer (t , tallymanHandler (t , deploymentID .String (), licenseJWT , func (req usagetypes.TallymanV1IngestRequest ) any {
577578 calls ++
578579 return usagetypes.TallymanV1Response {
579580 Message : errorMessage ,
@@ -582,7 +583,7 @@ func TestPublisherTallymanError(t *testing.T) {
582583
583584 publisher := usage .NewTallymanPublisher (ctx , log , db , coderdenttest .Keys ,
584585 usage .PublisherWithClock (clock ),
585- usage .PublisherWithIngestURL ( ingestURL ),
586+ usage .PublisherWithTallymanBaseURL ( baseURL ),
586587 )
587588 defer publisher .Close ()
588589
@@ -679,11 +680,13 @@ func configureMockDeployment(t *testing.T, db *dbmock.MockStore) (uuid.UUID, str
679680 return deploymentID , licenseRaw
680681}
681682
682- func fakeServer (t * testing.T , handler http.Handler ) string {
683+ func fakeServer (t * testing.T , handler http.Handler ) * url. URL {
683684 t .Helper ()
684685 server := httptest .NewServer (handler )
685686 t .Cleanup (server .Close )
686- return server .URL
687+ baseURL , err := url .Parse (server .URL )
688+ require .NoError (t , err )
689+ return baseURL
687690}
688691
689692func tallymanHandler (t * testing.T , expectDeploymentID string , expectLicenseJWT string , handler func (req usagetypes.TallymanV1IngestRequest ) any ) http.Handler {
0 commit comments