Skip to content
Snippets Groups Projects
Commit 00dd885393fa authored by Florent Aide's avatar Florent Aide
Browse files

runner config now needs a logger

parent b5b8ec8eea16
No related branches found
No related tags found
No related merge requests found
......@@ -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")
}
......
......@@ -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)
......
......@@ -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)
......
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()
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment