diff --git a/cmd/build.go b/cmd/build.go index b5b8ec8eea167724dd02824b571b010972a8f105_Y21kL2J1aWxkLmdv..00dd885393fa2f611dd6fe246be70453a90b508b_Y21kL2J1aWxkLmdv 100644 --- a/cmd/build.go +++ b/cmd/build.go @@ -19,7 +19,7 @@ // Execute ... func (cmd *BuildCmd) Execute([]string) error { Logger.Info().Str("namespace", cmd.Args.Namespace).Msg("starting beaver") - config, err := runner.NewConfig(".", cmd.Args.Namespace) + config, err := runner.NewConfig(Logger, ".", cmd.Args.Namespace) if err != nil { Logger.Err(err).Msg("failed to prepare config") } diff --git a/runner/config.go b/runner/config.go index b5b8ec8eea167724dd02824b571b010972a8f105_cnVubmVyL2NvbmZpZy5nbw==..00dd885393fa2f611dd6fe246be70453a90b508b_cnVubmVyL2NvbmZpZy5nbw== 100644 --- a/runner/config.go +++ b/runner/config.go @@ -5,6 +5,7 @@ "fmt" "text/template" + "github.com/rs/zerolog" "github.com/spf13/viper" yaml "gopkg.in/yaml.v2" ) @@ -39,6 +40,7 @@ Kind string `mapstructure:"kind"` Spec Spec `mapstructure:"spec"` Namespace string + Logger zerolog.Logger } // NewConfig returns a *Config @@ -42,7 +44,7 @@ } // NewConfig returns a *Config -func NewConfig(configDir string, namespace string) (*Config, error) { +func NewConfig(logger zerolog.Logger, configDir string, namespace string) (*Config, error) { // we ONLY search for files named beaver.yml viper.SetConfigName("beaver") viper.AddConfigPath(configDir) diff --git a/runner/config_test.go b/runner/config_test.go index b5b8ec8eea167724dd02824b571b010972a8f105_cnVubmVyL2NvbmZpZ190ZXN0Lmdv..00dd885393fa2f611dd6fe246be70453a90b508b_cnVubmVyL2NvbmZpZ190ZXN0Lmdv 100644 --- a/runner/config_test.go +++ b/runner/config_test.go @@ -5,6 +5,7 @@ "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "orus.io/cloudcrane/beaver/testutils" ) func TestConfig(t *testing.T) { @@ -8,4 +9,5 @@ ) func TestConfig(t *testing.T) { + logger := testutils.GetLogger(t) configDir := "fixtures/" @@ -11,5 +13,5 @@ configDir := "fixtures/" - config, err := NewConfig(configDir, "ns1") + config, err := NewConfig(logger, configDir, "ns1") require.NoError(t, err) // first config.spec.variables entry name should be VAULT_KV in our test file assert.Equal(t, "VAULT_KV", config.Spec.Variables[0].Name) diff --git a/testutils/logger.go b/testutils/logger.go new file mode 100644 index 0000000000000000000000000000000000000000..00dd885393fa2f611dd6fe246be70453a90b508b_dGVzdHV0aWxzL2xvZ2dlci5nbw== --- /dev/null +++ b/testutils/logger.go @@ -0,0 +1,53 @@ +package testutils + +import ( + "os" + "testing" + + "github.com/rs/zerolog" +) + +// NewTestLogger creates a TestLogger +func NewTestLogger(tb testing.TB) *TestLogger { + return &TestLogger{tb} +} + +// TestLogger logs to a t.Log function +type TestLogger struct { + tb testing.TB +} + +// Logger returns a zerolog Logger +func (tl *TestLogger) Logger() zerolog.Logger { + level := zerolog.DebugLevel + if os.Getenv("TEST_TRACE") != "" { + level = zerolog.TraceLevel + } + if os.Getenv("TEST_QUIET") != "" { + level = zerolog.InfoLevel + } + return zerolog.New(zerolog.ConsoleWriter{Out: tl}). + With().Timestamp().Logger(). + Level(level) +} + +// Write writes the given string to t.Logf +func (tl *TestLogger) Write(m []byte) (int, error) { + tl.tb.Logf(string(m)) + return len(m), nil +} + +// SetTB changes the current TB, and returns a function to get back to the +// previous one +func (tl *TestLogger) SetTB(tb testing.TB) func() { + otb := tl.tb + tl.tb = tb + return func() { + tl.tb = otb + } +} + +// GetLogger returns a test Logger +func GetLogger(tb testing.TB) zerolog.Logger { + return NewTestLogger(tb).Logger() +}