Skip to content

Commit

Permalink
Chore: Make test tracer noop and return no errors (grafana#50797)
Browse files Browse the repository at this point in the history
  • Loading branch information
zserge authored Jun 15, 2022
1 parent 94d5850 commit ae9491c
Show file tree
Hide file tree
Showing 28 changed files with 55 additions and 189 deletions.
3 changes: 1 addition & 2 deletions pkg/api/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,7 @@ func getContextHandler(t *testing.T, cfg *setting.Cfg) *contexthandler.ContextHa
userAuthTokenSvc := auth.NewFakeUserAuthTokenService()
renderSvc := &fakeRenderService{}
authJWTSvc := models.NewFakeJWTService()
tracer, err := tracing.InitializeTracerForTest()
require.NoError(t, err)
tracer := tracing.InitializeTracerForTest()
authProxy := authproxy.ProvideAuthProxy(cfg, remoteCacheSvc, loginservice.LoginServiceMock{}, sqlStore)
loginService := &logintest.LoginServiceFake{}
authenticator := &logintest.AuthenticatorFake{}
Expand Down
28 changes: 10 additions & 18 deletions pkg/api/pluginproxy/ds_proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ import (
func TestDataSourceProxy_routeRule(t *testing.T) {
cfg := &setting.Cfg{}
httpClientProvider := httpclient.NewProvider()
tracer, err := tracing.InitializeTracerForTest()
require.NoError(t, err)
tracer := tracing.InitializeTracerForTest()

t.Run("Plugin with routes", func(t *testing.T) {
routes := []*plugins.Route{
Expand Down Expand Up @@ -628,8 +627,7 @@ func TestDataSourceProxy_requestHandling(t *testing.T) {
}, ds
}

tracer, err := tracing.InitializeTracerForTest()
require.NoError(t, err)
tracer := tracing.InitializeTracerForTest()

t.Run("When response header Set-Cookie is not set should remove proxied Set-Cookie header", func(t *testing.T) {
ctx, ds := setUp(t)
Expand Down Expand Up @@ -765,13 +763,12 @@ func TestNewDataSourceProxy_InvalidURL(t *testing.T) {
Url: "://host/root",
}
cfg := &setting.Cfg{}
tracer, err := tracing.InitializeTracerForTest()
require.NoError(t, err)
tracer := tracing.InitializeTracerForTest()
var routes []*plugins.Route
secretsStore := kvstore.SetupTestService(t)
secretsService := secretsManager.SetupTestService(t, fakes.NewFakeSecretsStore())
dsService := datasourceservice.ProvideService(nil, secretsService, secretsStore, cfg, featuremgmt.WithFeatures(), acmock.New(), acmock.NewMockedPermissionsService())
_, err = NewDataSourceProxy(&ds, routes, &ctx, "api/method", cfg, httpclient.NewProvider(), &oauthtoken.Service{}, dsService, tracer)
_, err := NewDataSourceProxy(&ds, routes, &ctx, "api/method", cfg, httpclient.NewProvider(), &oauthtoken.Service{}, dsService, tracer)
require.Error(t, err)
assert.True(t, strings.HasPrefix(err.Error(), `validation of data source URL "://host/root" failed`))
}
Expand All @@ -786,14 +783,13 @@ func TestNewDataSourceProxy_ProtocolLessURL(t *testing.T) {
Url: "127.0.01:5432",
}
cfg := &setting.Cfg{}
tracer, err := tracing.InitializeTracerForTest()
require.NoError(t, err)
tracer := tracing.InitializeTracerForTest()

var routes []*plugins.Route
secretsStore := kvstore.SetupTestService(t)
secretsService := secretsManager.SetupTestService(t, fakes.NewFakeSecretsStore())
dsService := datasourceservice.ProvideService(nil, secretsService, secretsStore, cfg, featuremgmt.WithFeatures(), acmock.New(), acmock.NewMockedPermissionsService())
_, err = NewDataSourceProxy(&ds, routes, &ctx, "api/method", cfg, httpclient.NewProvider(), &oauthtoken.Service{}, dsService, tracer)
_, err := NewDataSourceProxy(&ds, routes, &ctx, "api/method", cfg, httpclient.NewProvider(), &oauthtoken.Service{}, dsService, tracer)

require.NoError(t, err)
}
Expand All @@ -804,8 +800,7 @@ func TestNewDataSourceProxy_MSSQL(t *testing.T) {
Context: &web.Context{},
SignedInUser: &models.SignedInUser{OrgRole: models.ROLE_EDITOR},
}
tracer, err := tracing.InitializeTracerForTest()
require.NoError(t, err)
tracer := tracing.InitializeTracerForTest()

tcs := []struct {
description string
Expand Down Expand Up @@ -858,8 +853,7 @@ func getDatasourceProxiedRequest(t *testing.T, ctx *models.ReqContext, cfg *sett
Type: "custom",
Url: "http://host/root/",
}
tracer, err := tracing.InitializeTracerForTest()
require.NoError(t, err)
tracer := tracing.InitializeTracerForTest()

var routes []*plugins.Route
secretsStore := kvstore.SetupTestService(t)
Expand Down Expand Up @@ -978,8 +972,7 @@ func createAuthTest(t *testing.T, secretsStore kvstore.SecretsKVStore, dsType st

func runDatasourceAuthTest(t *testing.T, secretsService secrets.Service, secretsStore kvstore.SecretsKVStore, cfg *setting.Cfg, test *testCase) {
ctx := &models.ReqContext{}
tracer, err := tracing.InitializeTracerForTest()
require.NoError(t, err)
tracer := tracing.InitializeTracerForTest()

var routes []*plugins.Route
dsService := datasourceservice.ProvideService(nil, secretsService, secretsStore, cfg, featuremgmt.WithFeatures(), acmock.New(), acmock.NewMockedPermissionsService())
Expand Down Expand Up @@ -1011,8 +1004,7 @@ func Test_PathCheck(t *testing.T) {
Method: http.MethodGet,
},
}
tracer, err := tracing.InitializeTracerForTest()
require.NoError(t, err)
tracer := tracing.InitializeTracerForTest()

setUp := func() (*models.ReqContext, *http.Request) {
req, err := http.NewRequest("GET", "http://localhost/asd", nil)
Expand Down
36 changes: 12 additions & 24 deletions pkg/bus/bus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ type testQuery struct {
}

func TestEventPublish(t *testing.T) {
tracer, err := tracing.InitializeTracerForTest()
require.NoError(t, err)
bus := ProvideBus(tracer)
bus := ProvideBus(tracing.InitializeTracerForTest())

var invoked bool

Expand All @@ -25,25 +23,21 @@ func TestEventPublish(t *testing.T) {
return nil
})

err = bus.Publish(context.Background(), &testQuery{})
err := bus.Publish(context.Background(), &testQuery{})
require.NoError(t, err, "unable to publish event")

require.True(t, invoked)
}

func TestEventPublish_NoRegisteredListener(t *testing.T) {
tracer, err := tracing.InitializeTracerForTest()
require.NoError(t, err)
bus := ProvideBus(tracer)
bus := ProvideBus(tracing.InitializeTracerForTest())

err = bus.Publish(context.Background(), &testQuery{})
err := bus.Publish(context.Background(), &testQuery{})
require.NoError(t, err, "unable to publish event")
}

func TestEventCtxPublishCtx(t *testing.T) {
tracer, err := tracing.InitializeTracerForTest()
require.NoError(t, err)
bus := ProvideBus(tracer)
bus := ProvideBus(tracing.InitializeTracerForTest())

var invoked bool

Expand All @@ -52,25 +46,21 @@ func TestEventCtxPublishCtx(t *testing.T) {
return nil
})

err = bus.Publish(context.Background(), &testQuery{})
err := bus.Publish(context.Background(), &testQuery{})
require.NoError(t, err, "unable to publish event")

require.True(t, invoked)
}

func TestEventPublishCtx_NoRegisteredListener(t *testing.T) {
tracer, err := tracing.InitializeTracerForTest()
require.NoError(t, err)
bus := ProvideBus(tracer)
bus := ProvideBus(tracing.InitializeTracerForTest())

err = bus.Publish(context.Background(), &testQuery{})
err := bus.Publish(context.Background(), &testQuery{})
require.NoError(t, err, "unable to publish event")
}

func TestEventPublishCtx(t *testing.T) {
tracer, err := tracing.InitializeTracerForTest()
require.NoError(t, err)
bus := ProvideBus(tracer)
bus := ProvideBus(tracing.InitializeTracerForTest())

var invoked bool

Expand All @@ -79,16 +69,14 @@ func TestEventPublishCtx(t *testing.T) {
return nil
})

err = bus.Publish(context.Background(), &testQuery{})
err := bus.Publish(context.Background(), &testQuery{})
require.NoError(t, err, "unable to publish event")

require.True(t, invoked)
}

func TestEventCtxPublish(t *testing.T) {
tracer, err := tracing.InitializeTracerForTest()
require.NoError(t, err)
bus := ProvideBus(tracer)
bus := ProvideBus(tracing.InitializeTracerForTest())

var invoked bool

Expand All @@ -97,7 +85,7 @@ func TestEventCtxPublish(t *testing.T) {
return nil
})

err = bus.Publish(context.Background(), &testQuery{})
err := bus.Publish(context.Background(), &testQuery{})
require.NoError(t, err, "unable to publish event")

require.True(t, invoked)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ func TestHTTPClientProvider(t *testing.T) {
t.Cleanup(func() {
newProviderFunc = origNewProviderFunc
})
tracer, err := tracing.InitializeTracerForTest()
require.NoError(t, err)
tracer := tracing.InitializeTracerForTest()
_ = New(&setting.Cfg{SigV4AuthEnabled: false}, &validations.OSSPluginRequestValidator{}, tracer)
require.Len(t, providerOpts, 1)
o := providerOpts[0]
Expand All @@ -47,8 +46,7 @@ func TestHTTPClientProvider(t *testing.T) {
t.Cleanup(func() {
newProviderFunc = origNewProviderFunc
})
tracer, err := tracing.InitializeTracerForTest()
require.NoError(t, err)
tracer := tracing.InitializeTracerForTest()
_ = New(&setting.Cfg{SigV4AuthEnabled: true}, &validations.OSSPluginRequestValidator{}, tracer)
require.Len(t, providerOpts, 1)
o := providerOpts[0]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ import (
)

func TestTracingMiddleware(t *testing.T) {
tracer, err := tracing.InitializeTracerForTest()
require.NoError(t, err)
tracer := tracing.InitializeTracerForTest()

t.Run("GET request that returns 200 OK should start and capture span", func(t *testing.T) {
finalRoundTripper := httpclient.RoundTripperFunc(func(req *http.Request) (*http.Response, error) {
Expand Down
17 changes: 2 additions & 15 deletions pkg/infra/tracing/test_helper.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,7 @@
package tracing

func InitializeTracerForTest() (Tracer, error) {
ots := &Opentelemetry{
enabled: "jaeger",
}
err := ots.initOpentelemetryTracer()
if err != nil {
return ots, err
}
return ots, err
}

func InitializeForBus() Tracer {
ots := &Opentelemetry{
enabled: "jaeger",
}
func InitializeTracerForTest() Tracer {
ots := &Opentelemetry{enabled: noopExporter}
_ = ots.initOpentelemetryTracer()
return ots
}
3 changes: 1 addition & 2 deletions pkg/middleware/middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -640,10 +640,9 @@ func getContextHandler(t *testing.T, cfg *setting.Cfg, mockSQLStore *mockstore.S
userAuthTokenSvc := auth.NewFakeUserAuthTokenService()
renderSvc := &fakeRenderService{}
authJWTSvc := models.NewFakeJWTService()
tracer, err := tracing.InitializeTracerForTest()
tracer := tracing.InitializeTracerForTest()
authProxy := authproxy.ProvideAuthProxy(cfg, remoteCacheSvc, loginService, mockSQLStore)
authenticator := &logintest.AuthenticatorFake{ExpectedUser: &models.User{}}
require.NoError(t, err)
return contexthandler.ProvideService(cfg, userAuthTokenSvc, authJWTSvc, remoteCacheSvc, renderSvc, mockSQLStore, tracer, authProxy, loginService, authenticator)
}

Expand Down
3 changes: 1 addition & 2 deletions pkg/services/alerting/engine_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ func TestIntegrationEngineTimeouts(t *testing.T) {
t.Skip("skipping integration test")
}
usMock := &usagestats.UsageStatsMock{T: t}
tracer, err := tracing.InitializeTracerForTest()
require.NoError(t, err)
tracer := tracing.InitializeTracerForTest()
engine := ProvideAlertEngine(nil, nil, nil, usMock, ossencryption.ProvideService(), nil, tracer, nil, setting.NewCfg(), nil, nil)
setting.AlertingNotificationTimeout = 30 * time.Second
setting.AlertingMaxAttempts = 3
Expand Down
3 changes: 1 addition & 2 deletions pkg/services/alerting/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,7 @@ func (a *AlertStoreMock) SetAlertState(_ context.Context, _ *models.SetAlertStat

func TestEngineProcessJob(t *testing.T) {
usMock := &usagestats.UsageStatsMock{T: t}
tracer, err := tracing.InitializeTracerForTest()
require.NoError(t, err)
tracer := tracing.InitializeTracerForTest()

store := &AlertStoreMock{}
engine := ProvideAlertEngine(nil, nil, nil, usMock, ossencryption.ProvideService(), nil, tracer, store, setting.NewCfg(), nil, nil)
Expand Down
3 changes: 1 addition & 2 deletions pkg/services/contexthandler/auth_proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ func getContextHandler(t *testing.T) *ContextHandler {
userAuthTokenSvc := auth.NewFakeUserAuthTokenService()
renderSvc := &fakeRenderService{}
authJWTSvc := models.NewFakeJWTService()
tracer, err := tracing.InitializeTracerForTest()
require.NoError(t, err)
tracer := tracing.InitializeTracerForTest()

loginService := loginservice.LoginServiceMock{ExpectedUser: &models.User{Id: userID}}
authProxy := authproxy.ProvideAuthProxy(cfg, remoteCacheSvc, loginService, &FakeGetSignUserStore{})
Expand Down
3 changes: 1 addition & 2 deletions pkg/services/ngalert/notifier/channels/email_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,7 @@ func TestEmailNotifierIntegration(t *testing.T) {
func createCoreEmailService(t *testing.T) *notifications.NotificationService {
t.Helper()

tracer, err := tracing.InitializeTracerForTest()
require.NoError(t, err)
tracer := tracing.InitializeTracerForTest()
bus := bus.ProvideBus(tracer)

cfg := setting.NewCfg()
Expand Down
3 changes: 1 addition & 2 deletions pkg/services/notifications/notifications_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ import (

func newBus(t *testing.T) bus.Bus {
t.Helper()
tracer, err := tracing.InitializeTracerForTest()
require.NoError(t, err)
tracer := tracing.InitializeTracerForTest()
return bus.ProvideBus(tracer)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -473,8 +473,7 @@ type scenarioContext struct {
func scenario(t *testing.T, desc string, input scenarioInput, f func(ctx *scenarioContext)) {
t.Helper()

tracer, err := tracing.InitializeTracerForTest()
require.NoError(t, err)
tracer := tracing.InitializeTracerForTest()

sCtx := &scenarioContext{
t: t,
Expand Down
5 changes: 1 addition & 4 deletions pkg/services/sqlstore/sqlstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -542,10 +542,7 @@ func initTestDB(migration registry.DatabaseMigrator, opts ...InitTestDBOpt) (*SQ
engine.DatabaseTZ = time.UTC
engine.TZLocation = time.UTC

tracer, err := tracing.InitializeTracerForTest()
if err != nil {
return nil, err
}
tracer := tracing.InitializeTracerForTest()
bus := bus.ProvideBus(tracer)
testSQLStore, err = newSQLStore(cfg, localcache.New(5*time.Minute, 10*time.Minute), engine, migration, bus, tracer, opts...)
if err != nil {
Expand Down
5 changes: 1 addition & 4 deletions pkg/services/teamguardian/manager/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"testing"

"github.com/grafana/grafana/pkg/infra/tracing"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/teamguardian/database"
"github.com/stretchr/testify/mock"
Expand Down Expand Up @@ -33,11 +32,9 @@ func TestUpdateTeam(t *testing.T) {

t.Run("Given an editor and a team he isn't a member of", func(t *testing.T) {
t.Run("Should not be able to update the team", func(t *testing.T) {
_, err := tracing.InitializeTracerForTest()
require.NoError(t, err)
ctx := context.Background()
store.On("GetTeamMembers", ctx, mock.Anything).Return([]*models.TeamMemberDTO{}, nil).Once()
err = teamGuardianService.CanAdmin(ctx, testTeam.OrgId, testTeam.Id, &editor)
err := teamGuardianService.CanAdmin(ctx, testTeam.OrgId, testTeam.Id, &editor)
require.Equal(t, models.ErrNotAllowedToUpdateTeam, err)
})
})
Expand Down
3 changes: 0 additions & 3 deletions pkg/tests/api/alerting/api_admin_configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/prometheus/common/model"
"github.com/stretchr/testify/require"

"github.com/grafana/grafana/pkg/infra/tracing"
"github.com/grafana/grafana/pkg/models"
apimodels "github.com/grafana/grafana/pkg/services/ngalert/api/tooling/definitions"
ngmodels "github.com/grafana/grafana/pkg/services/ngalert/models"
Expand All @@ -22,8 +21,6 @@ import (

func TestAdminConfiguration_SendingToExternalAlertmanagers(t *testing.T) {
const disableOrgID int64 = 3
_, err := tracing.InitializeTracerForTest()
require.NoError(t, err)
dir, path := testinfra.CreateGrafDir(t, testinfra.GrafanaOpts{
DisableLegacyAlerting: true,
EnableUnifiedAlerting: true,
Expand Down
Loading

0 comments on commit ae9491c

Please sign in to comment.