Skip to content

Commit

Permalink
test: reduce logging in go tests (#3562)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonas-jonas authored Oct 10, 2023
1 parent e9ed14f commit 05de3a2
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 12 deletions.
21 changes: 14 additions & 7 deletions driver/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,13 +180,14 @@ func NewRegistryFromDSN(ctx context.Context, c *config.Config, l *logrusx.Logger
}

type options struct {
skipNetworkInit bool
config *config.Config
replaceTracer func(*otelx.Tracer) *otelx.Tracer
inspect func(Registry) error
extraMigrations []fs.FS
replacementStrategies []NewStrategy
extraHooks map[string]func(config.SelfServiceHook) any
skipNetworkInit bool
config *config.Config
replaceTracer func(*otelx.Tracer) *otelx.Tracer
inspect func(Registry) error
extraMigrations []fs.FS
replacementStrategies []NewStrategy
extraHooks map[string]func(config.SelfServiceHook) any
disableMigrationLogging bool
}

type RegistryOption func(*options)
Expand Down Expand Up @@ -236,6 +237,12 @@ func WithExtraMigrations(m ...fs.FS) RegistryOption {
}
}

func WithDisabledMigrationLogging() RegistryOption {
return func(o *options) {
o.disableMigrationLogging = true
}
}

func newOptions(os []RegistryOption) *options {
o := new(options)
for _, f := range os {
Expand Down
2 changes: 1 addition & 1 deletion driver/registry_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,7 @@ func (m *RegistryDefault) Init(ctx context.Context, ctxer contextx.Contextualize
m.Logger().WithError(err).Warnf("Unable to open database, retrying.")
return errors.WithStack(err)
}
p, err := sql.NewPersister(ctx, m, c, o.extraMigrations...)
p, err := sql.NewPersister(ctx, m, c, sql.WithExtraMigrations(o.extraMigrations...), sql.WithDisabledLogging(o.disableMigrationLogging))
if err != nil {
m.Logger().WithError(err).Warnf("Unable to initialize persister, retrying.")
return err
Expand Down
2 changes: 1 addition & 1 deletion internal/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func NewRegistryDefaultWithDSN(t testing.TB, dsn string) (*config.Config, *drive
reg, err := driver.NewRegistryFromDSN(ctx, c, logrusx.New("", ""))
require.NoError(t, err)
reg.Config().MustSet(ctx, "dev", true)
require.NoError(t, reg.Init(context.Background(), &contextx.Default{}, driver.SkipNetworkInit))
require.NoError(t, reg.Init(context.Background(), &contextx.Default{}, driver.SkipNetworkInit, driver.WithDisabledMigrationLogging()))
require.NoError(t, reg.Persister().MigrateUp(context.Background())) // always migrate up

actual, err := reg.Persister().DetermineNetwork(context.Background())
Expand Down
36 changes: 33 additions & 3 deletions persistence/sql/persister.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/gofrs/uuid"
"github.com/laher/mergefs"
"github.com/pkg/errors"
"github.com/sirupsen/logrus/hooks/test"

"github.com/ory/kratos/driver/config"
"github.com/ory/kratos/identity"
Expand All @@ -23,6 +24,7 @@ import (
"github.com/ory/kratos/session"
"github.com/ory/kratos/x"
"github.com/ory/x/contextx"
"github.com/ory/x/logrusx"
"github.com/ory/x/networkx"
"github.com/ory/x/popx"
)
Expand Down Expand Up @@ -54,17 +56,45 @@ type (
}
)

func NewPersister(ctx context.Context, r persisterDependencies, c *pop.Connection, extraMigrations ...fs.FS) (*Persister, error) {
type persisterOptions struct {
extraMigrations []fs.FS
disableLogging bool
}

type persisterOption func(o *persisterOptions)

func WithExtraMigrations(fss ...fs.FS) persisterOption {
return func(o *persisterOptions) {
o.extraMigrations = fss
}
}

func WithDisabledLogging(v bool) persisterOption {
return func(o *persisterOptions) {
o.disableLogging = v
}
}

func NewPersister(ctx context.Context, r persisterDependencies, c *pop.Connection, opts ...persisterOption) (*Persister, error) {
o := &persisterOptions{}
for _, f := range opts {
f(o)
}
logger := r.Logger()
if o.disableLogging {
inner, _ := test.NewNullLogger()
logger = logrusx.New("kratos", "", logrusx.UseLogger(inner))
}
m, err := popx.NewMigrationBox(
mergefs.Merge(
append(
[]fs.FS{
migrations, networkx.Migrations,
},
extraMigrations...,
o.extraMigrations...,
)...,
),
popx.NewMigrator(c, r.Logger(), r.Tracer(ctx), 0),
popx.NewMigrator(c, logger, r.Tracer(ctx), 0),
)
if err != nil {
return nil, err
Expand Down

0 comments on commit 05de3a2

Please sign in to comment.