Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 2 additions & 16 deletions internal/adhoc/adhoc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ import (

"github.com/grafana/synthetic-monitoring-agent/internal/feature"
"github.com/grafana/synthetic-monitoring-agent/internal/k6runner"
"github.com/grafana/synthetic-monitoring-agent/internal/model"
"github.com/grafana/synthetic-monitoring-agent/internal/prober"
"github.com/grafana/synthetic-monitoring-agent/internal/prober/logger"
"github.com/grafana/synthetic-monitoring-agent/internal/pusher"
"github.com/grafana/synthetic-monitoring-agent/internal/testhelper"
sm "github.com/grafana/synthetic-monitoring-agent/pkg/pb/synthetic_monitoring"
)

Expand Down Expand Up @@ -352,20 +352,6 @@ func (p *testProber) Probe(ctx context.Context, target string, registry *prometh
return true, 1
}

// Add mock secrets store
type testSecretStore struct{}

func (s testSecretStore) GetSecretCredentials(ctx context.Context, tenantId model.GlobalID) (*sm.SecretStore, error) {
if tenantId == 0 {
return nil, errors.New("invalid tenant ID")
}

return &sm.SecretStore{
Url: "http://example.com",
Token: "test-token",
}, nil
}

func TestDefaultRunnerFactory(t *testing.T) {
t.Parallel()

Expand All @@ -379,7 +365,7 @@ func TestDefaultRunnerFactory(t *testing.T) {

// Initialize the mockRunner and secretStore
mockRunner := &testK6Runner{}
secretStore := &testSecretStore{}
secretStore := &testhelper.TestSecretStore{}

testcases := map[string]struct {
request *sm.AdHocRequest
Expand Down
8 changes: 1 addition & 7 deletions internal/prober/browser/browser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func TestNewProber(t *testing.T) {
for name, tc := range testcases {
t.Run(name, func(t *testing.T) {
var runner noopRunner
var store noopSecretStore
var store testhelper.NoopSecretStore
p, err := NewProber(ctx, tc.check, logger, runner, store)
if tc.expectFailure {
require.Error(t, err)
Expand All @@ -83,9 +83,3 @@ func (noopRunner) WithLogger(logger *zerolog.Logger) k6runner.Runner {
func (noopRunner) Run(ctx context.Context, script k6runner.Script, secretStore k6runner.SecretStore) (*k6runner.RunResponse, error) {
return &k6runner.RunResponse{}, nil
}

type noopSecretStore struct{}

func (n noopSecretStore) GetSecretCredentials(ctx context.Context, tenantID model.GlobalID) (*sm.SecretStore, error) {
return &sm.SecretStore{}, nil
}
8 changes: 1 addition & 7 deletions internal/prober/multihttp/multihttp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func TestNewProber(t *testing.T) {
for name, tc := range testcases {
t.Run(name, func(t *testing.T) {
var runner noopRunner
var store noopSecretStore
var store testhelper.NoopSecretStore
checkId := tc.check.Id
reservedHeaders := http.Header{}
reservedHeaders.Add("x-sm-id", fmt.Sprintf("%d-%d", checkId, checkId))
Expand Down Expand Up @@ -158,9 +158,3 @@ func (noopRunner) WithLogger(logger *zerolog.Logger) k6runner.Runner {
func (noopRunner) Run(ctx context.Context, script k6runner.Script, secretStore k6runner.SecretStore) (*k6runner.RunResponse, error) {
return &k6runner.RunResponse{}, nil
}

type noopSecretStore struct{}

func (n noopSecretStore) GetSecretCredentials(ctx context.Context, tenantID model.GlobalID) (*sm.SecretStore, error) {
return &sm.SecretStore{}, nil
}
2 changes: 1 addition & 1 deletion internal/prober/multihttp/script_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -851,7 +851,7 @@ func TestSettingsToScript(t *testing.T) {

k6path := testhelper.K6Path(t)
runner := k6runner.New(k6runner.RunnerOpts{Uri: k6path})
store := noopSecretStore{}
store := testhelper.NoopSecretStore{}

logger := testhelper.Logger(t)
prober, err := NewProber(ctx, check, logger, runner, http.Header{}, &store)
Expand Down
9 changes: 2 additions & 7 deletions internal/prober/prober_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/grafana/synthetic-monitoring-agent/internal/feature"
"github.com/grafana/synthetic-monitoring-agent/internal/model"
"github.com/grafana/synthetic-monitoring-agent/internal/testhelper"
sm "github.com/grafana/synthetic-monitoring-agent/pkg/pb/synthetic_monitoring"
"github.com/rs/zerolog"
"github.com/stretchr/testify/require"
Expand All @@ -15,7 +16,7 @@ func TestProberFactoryCoverage(t *testing.T) {
// This test will assert that the prober factory is handling all the
// known check types (as defined in the synthetic_monitoring package).

var store noopSecretStore
var store testhelper.NoopSecretStore
pf := NewProberFactory(nil, 0, feature.Collection{}, &store)
ctx := context.Background()
testLogger := zerolog.New(zerolog.NewTestWriter(t))
Expand All @@ -28,9 +29,3 @@ func TestProberFactoryCoverage(t *testing.T) {
require.NotErrorIs(t, err, errUnsupportedCheckType)
}
}

type noopSecretStore struct{}

func (n noopSecretStore) GetSecretCredentials(ctx context.Context, tenantID model.GlobalID) (*sm.SecretStore, error) {
return &sm.SecretStore{}, nil
}
9 changes: 2 additions & 7 deletions internal/prober/scripted/scripted_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/grafana/synthetic-monitoring-agent/internal/k6runner"
"github.com/grafana/synthetic-monitoring-agent/internal/model"
"github.com/grafana/synthetic-monitoring-agent/internal/testhelper"
sm "github.com/grafana/synthetic-monitoring-agent/pkg/pb/synthetic_monitoring"
"github.com/rs/zerolog"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -57,7 +58,7 @@ func TestNewProber(t *testing.T) {
for name, tc := range testcases {
t.Run(name, func(t *testing.T) {
var runner noopRunner
var store noopSecretStore
var store testhelper.NoopSecretStore
p, err := NewProber(ctx, tc.check, logger, runner, store)
if tc.expectFailure {
require.Error(t, err)
Expand All @@ -83,12 +84,6 @@ func (noopRunner) Run(ctx context.Context, script k6runner.Script, secretStore k
return &k6runner.RunResponse{}, nil
}

type noopSecretStore struct{}

func (n noopSecretStore) GetSecretCredentials(ctx context.Context, tenantID model.GlobalID) (*sm.SecretStore, error) {
return &sm.SecretStore{}, nil
}

func testContext(t *testing.T) (context.Context, func()) {
if deadline, ok := t.Deadline(); ok {
return context.WithDeadline(context.Background(), deadline)
Expand Down
12 changes: 3 additions & 9 deletions internal/scraper/scraper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ func setupScriptedProbe(ctx context.Context, t *testing.T) (prober.Prober, model
}
}

var store noopSecretStore
var store testhelper.NoopSecretStore

prober, err := scripted.NewProber(
ctx,
Expand Down Expand Up @@ -499,7 +499,7 @@ func setupMultiHTTPProbe(ctx context.Context, t *testing.T) (prober.Prober, mode
}

var runner k6runner.Runner
var store noopSecretStore
var store testhelper.NoopSecretStore

if k6Path := os.Getenv("K6_PATH"); k6Path != "" {
runner = k6runner.New(k6runner.RunnerOpts{Uri: k6Path})
Expand Down Expand Up @@ -544,7 +544,7 @@ func setupBrowserProbe(ctx context.Context, t *testing.T) (prober.Prober, model.
}

var runner k6runner.Runner
var store noopSecretStore
var store testhelper.NoopSecretStore

if k6Path := os.Getenv("K6_PATH"); k6Path != "" {
runner = k6runner.New(k6runner.RunnerOpts{Uri: k6Path})
Expand Down Expand Up @@ -2025,9 +2025,3 @@ func TestTickWithOffset(t *testing.T) {
})
}
}

type noopSecretStore struct{}

func (n noopSecretStore) GetSecretCredentials(ctx context.Context, tenantID model.GlobalID) (*sm.SecretStore, error) {
return &sm.SecretStore{}, nil
}
28 changes: 28 additions & 0 deletions internal/testhelper/testhelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ package testhelper

import (
"context"
"errors"
"os"
"path/filepath"
"runtime"
"testing"
"time"

"github.com/grafana/synthetic-monitoring-agent/internal/model"
sm "github.com/grafana/synthetic-monitoring-agent/pkg/pb/synthetic_monitoring"
"github.com/rs/zerolog"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -74,3 +77,28 @@ func K6Path(t *testing.T) string {

return k6path
}

// NoopSecretStore is a test implementation of the SecretProvider interface
// that returns an empty secret store. Use this in tests when you need a
// secret store but don't care about the actual secrets.
type NoopSecretStore struct{}

func (n NoopSecretStore) GetSecretCredentials(ctx context.Context, tenantID model.GlobalID) (*sm.SecretStore, error) {
return &sm.SecretStore{}, nil
}

// TestSecretStore is a test implementation of the SecretProvider interface
// that returns a mock secret store with test credentials. Use this in tests
// when you need to test behavior that depends on having actual secret values.
type TestSecretStore struct{}

func (s TestSecretStore) GetSecretCredentials(ctx context.Context, tenantId model.GlobalID) (*sm.SecretStore, error) {
if tenantId == 0 {
return nil, errors.New("invalid tenant ID")
}

return &sm.SecretStore{
Url: "http://example.com",
Token: "test-token",
}, nil
}
Loading