test: ensure that all dependencies in a test agent use the test logger (#14996)

This commit is contained in:
R.B. Boyer 2022-10-24 17:02:38 -05:00 committed by GitHub
parent a80a49aed9
commit da70daba43
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 20 additions and 13 deletions

View File

@ -51,6 +51,14 @@ func NewTestACLAgent(t *testing.T, name string, hcl string, resolveAuthz authzRe
dataDir := testutil.TempDir(t, "acl-agent") dataDir := testutil.TempDir(t, "acl-agent")
logBuffer := testutil.NewLogBuffer(t) logBuffer := testutil.NewLogBuffer(t)
logger := hclog.NewInterceptLogger(&hclog.LoggerOptions{
Name: name,
Level: testutil.TestLogLevel,
Output: logBuffer,
TimeFormat: "04:05.000",
})
loader := func(source config.Source) (config.LoadResult, error) { loader := func(source config.Source) (config.LoadResult, error) {
dataDir := fmt.Sprintf(`data_dir = "%s"`, dataDir) dataDir := fmt.Sprintf(`data_dir = "%s"`, dataDir)
opts := config.LoadOpts{ opts := config.LoadOpts{
@ -63,15 +71,9 @@ func NewTestACLAgent(t *testing.T, name string, hcl string, resolveAuthz authzRe
} }
return result, err return result, err
} }
bd, err := NewBaseDeps(loader, logBuffer) bd, err := NewBaseDeps(loader, logBuffer, logger)
require.NoError(t, err) require.NoError(t, err)
bd.Logger = hclog.NewInterceptLogger(&hclog.LoggerOptions{
Name: name,
Level: testutil.TestLogLevel,
Output: logBuffer,
TimeFormat: "04:05.000",
})
bd.MetricsConfig = &lib.MetricsConfig{ bd.MetricsConfig = &lib.MetricsConfig{
Handler: metrics.NewInmemSink(1*time.Second, time.Minute), Handler: metrics.NewInmemSink(1*time.Second, time.Minute),
} }

View File

@ -53,7 +53,7 @@ type BaseDeps struct {
type ConfigLoader func(source config.Source) (config.LoadResult, error) type ConfigLoader func(source config.Source) (config.LoadResult, error)
func NewBaseDeps(configLoader ConfigLoader, logOut io.Writer) (BaseDeps, error) { func NewBaseDeps(configLoader ConfigLoader, logOut io.Writer, providedLogger hclog.InterceptLogger) (BaseDeps, error) {
d := BaseDeps{} d := BaseDeps{}
result, err := configLoader(nil) result, err := configLoader(nil)
if err != nil { if err != nil {
@ -63,10 +63,15 @@ func NewBaseDeps(configLoader ConfigLoader, logOut io.Writer) (BaseDeps, error)
cfg := result.RuntimeConfig cfg := result.RuntimeConfig
logConf := cfg.Logging logConf := cfg.Logging
logConf.Name = logging.Agent logConf.Name = logging.Agent
if providedLogger != nil {
d.Logger = providedLogger
} else {
d.Logger, err = logging.Setup(logConf, logOut) d.Logger, err = logging.Setup(logConf, logOut)
if err != nil { if err != nil {
return d, err return d, err
} }
}
grpcLogInitOnce.Do(func() { grpcLogInitOnce.Do(func() {
grpclog.SetLoggerV2(logging.NewGRPCLogger(cfg.Logging.LogLevel, d.Logger)) grpclog.SetLoggerV2(logging.NewGRPCLogger(cfg.Logging.LogLevel, d.Logger))

View File

@ -219,7 +219,7 @@ func (a *TestAgent) Start(t *testing.T) error {
} }
return result, err return result, err
} }
bd, err := NewBaseDeps(loader, logOutput) bd, err := NewBaseDeps(loader, logOutput, logger)
if err != nil { if err != nil {
return fmt.Errorf("failed to create base deps: %w", err) return fmt.Errorf("failed to create base deps: %w", err)
} }

View File

@ -199,7 +199,7 @@ func (c *cmd) run(args []string) int {
return 1 return 1
} }
bd, err := agent.NewBaseDeps(loader, logGate) bd, err := agent.NewBaseDeps(loader, logGate, nil)
if err != nil { if err != nil {
ui.Error(err.Error()) ui.Error(err.Error())
return 1 return 1