Skip to content

Commit

Permalink
Fix bouncer initialization in test
Browse files Browse the repository at this point in the history
  • Loading branch information
hslatman committed Oct 1, 2024
1 parent c6b235f commit 89de874
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 22 deletions.
33 changes: 12 additions & 21 deletions internal/bouncer/bouncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package bouncer

import (
"context"
"fmt"
"net"
"net/url"
"regexp"
Expand All @@ -13,32 +12,29 @@ import (
"github.com/crowdsecurity/crowdsec/pkg/models"
"github.com/google/go-cmp/cmp"
"github.com/jarcoal/httpmock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zaptest"
)

func new(t *testing.T) (*Bouncer, error) {
func newBouncer(t *testing.T) (*Bouncer, error) {
t.Helper()

key := "apiKey"
host := "http://127.0.0.1:8080/"
tickerInterval := "10s"
logger := zaptest.NewLogger(t)

bouncer, err := New(key, host, tickerInterval, logger)
if err != nil {
return nil, err
}
require.NoError(t, err)

bouncer.EnableStreaming()

// the code below mimicks the bouncer.streamingBouncer.Init() functionality
bouncer.streamingBouncer.Stream = make(chan *models.DecisionsStreamResponse)

apiURL, err := url.Parse(bouncer.streamingBouncer.APIUrl)
if err != nil {
return nil, fmt.Errorf("local API Url %q: %w", bouncer.streamingBouncer.APIUrl, err)
}
require.NoError(t, err, "local API Url %q", bouncer.streamingBouncer.APIUrl)

transport := &apiclient.APIKeyTransport{
APIKey: bouncer.streamingBouncer.APIKey,
Transport: httpmock.DefaultTransport, // crucial for httpmock to work correctly
Expand All @@ -49,14 +45,12 @@ func new(t *testing.T) (*Bouncer, error) {
// Transport in the APIKeyTransport and waiting a bit before the bouncer is ran. This results in
// the goal of ensuring the bouncer gets mocked decisions.
bouncer.streamingBouncer.APIClient, err = apiclient.NewDefaultClient(apiURL, "v1", bouncer.streamingBouncer.UserAgent, transport.Client())
if err != nil {
return nil, fmt.Errorf("api client init: %w", err)
}
require.NoError(t, err)

bouncer.streamingBouncer.TickerIntervalDuration, err = time.ParseDuration(bouncer.streamingBouncer.TickerInterval)
if err != nil {
return nil, fmt.Errorf("unable to parse duration %q: %w", bouncer.streamingBouncer.TickerInterval, err)
}
require.NoError(t, err)

bouncer.metricsProvider, err = newMetricsProvider(bouncer.streamingBouncer.APIClient, bouncer.updateMetrics, time.Minute)

// initialization of the bouncer finished; running is responsibility of the caller

Expand Down Expand Up @@ -130,11 +124,8 @@ func decisions() *models.DecisionsStreamResponse {
}

func TestStreamingBouncer(t *testing.T) {

b, err := new(t)
if err != nil {
t.Fatal(err)
}
b, err := newBouncer(t)
require.NoError(t, err)

// activate httpmock so that responses can be mocked
httpmock.Activate()
Expand Down Expand Up @@ -235,5 +226,5 @@ func TestStreamingBouncer(t *testing.T) {
func Test_generateInstanceID(t *testing.T) {
id, err := generateInstanceID(time.Now())
require.NoError(t, err)
assert.Len(t, id, 8)
require.Len(t, id, 8)
}
2 changes: 1 addition & 1 deletion internal/bouncer/decisions.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (b *Bouncer) startProcessingDecisions(ctx context.Context) {

for {
select {
case <-b.ctx.Done():
case <-ctx.Done():
b.logger.Info("processing new and deleted decisions stopped", b.zapField())
return
case decisions := <-b.streamingBouncer.Stream:
Expand Down

0 comments on commit 89de874

Please sign in to comment.