From 25a13d3611970c1e9605a8d232ae2198c3f0e15b Mon Sep 17 00:00:00 2001 From: Alex Vanin Date: Tue, 1 Jun 2021 12:47:01 +0300 Subject: [PATCH] [#493] cmd/node: Add .env file for tests Signed-off-by: Alex Vanin --- cmd/neofs-node/config/logger/config_test.go | 26 +++++------- cmd/neofs-node/config/test/config.go | 45 ++++++++++++++++++++- config/example/node.env | 1 + 3 files changed, 56 insertions(+), 16 deletions(-) create mode 100644 config/example/node.env diff --git a/cmd/neofs-node/config/logger/config_test.go b/cmd/neofs-node/config/logger/config_test.go index fbdea6555..faf2c7997 100644 --- a/cmd/neofs-node/config/logger/config_test.go +++ b/cmd/neofs-node/config/logger/config_test.go @@ -1,34 +1,30 @@ package loggerconfig_test import ( - "os" "testing" "github.com/nspcc-dev/neofs-node/cmd/neofs-node/config" - "github.com/nspcc-dev/neofs-node/cmd/neofs-node/config/internal" loggerconfig "github.com/nspcc-dev/neofs-node/cmd/neofs-node/config/logger" configtest "github.com/nspcc-dev/neofs-node/cmd/neofs-node/config/test" "github.com/stretchr/testify/require" ) func TestLoggerSection_Level(t *testing.T) { - configtest.ForEachFileType("../../../../config/example/node", func(c *config.Config) { + t.Run("defaults", func(t *testing.T) { + v := loggerconfig.Level(configtest.EmptyConfig()) + require.Equal(t, loggerconfig.LevelDefault, v) + }) + + const path = "../../../../config/example/node" + + var fileConfigTest = func(c *config.Config) { v := loggerconfig.Level(c) require.Equal(t, "debug", v) - }) + } - v := loggerconfig.Level(configtest.EmptyConfig()) - require.Equal(t, loggerconfig.LevelDefault, v) + configtest.ForEachFileType(path, fileConfigTest) t.Run("ENV", func(t *testing.T) { - // TODO: read from file - err := os.Setenv( - internal.Env("logger", "level"), - "debug", - ) - require.NoError(t, err) - - v := loggerconfig.Level(configtest.EmptyConfig()) - require.Equal(t, "debug", v) + configtest.ForEnvFileType(path, fileConfigTest) }) } diff --git a/cmd/neofs-node/config/test/config.go b/cmd/neofs-node/config/test/config.go index a1b98ef8d..e56f6d61b 100644 --- a/cmd/neofs-node/config/test/config.go +++ b/cmd/neofs-node/config/test/config.go @@ -1,15 +1,30 @@ package configtest -import "github.com/nspcc-dev/neofs-node/cmd/neofs-node/config" +import ( + "bufio" + "os" + "strings" + + "github.com/nspcc-dev/neofs-node/cmd/neofs-node/config" +) func fromFile(path string) *config.Config { var p config.Prm + os.Clearenv() // ENVs have priority over config files so we do this in tests + return config.New(p, config.WithConfigFile(path), ) } +func fromEnvFile(path string) *config.Config { + var p config.Prm + + loadEnv(path) // github.com/joho/godotenv can do that as well + return config.New(p) +} + func forEachFile(paths []string, f func(*config.Config)) { for i := range paths { f(fromFile(paths[i])) @@ -26,9 +41,37 @@ func ForEachFileType(pref string, f func(*config.Config)) { }, f) } +// ForEnvFileType creates config from `.env` file. +func ForEnvFileType(pref string, f func(*config.Config)) { + f(fromEnvFile(pref + ".env")) +} + // EmptyConfig returns config without any values and sections. func EmptyConfig() *config.Config { var p config.Prm return config.New(p) } + +// loadEnv reads .env file, parses `X=Y` records and sets OS ENVs. +func loadEnv(path string) { + f, err := os.Open(path) + if err != nil { + panic("can't open .env file") + } + + defer f.Close() + + scanner := bufio.NewScanner(f) + for scanner.Scan() { + pair := strings.Split(scanner.Text(), "=") + if len(pair) != 2 { + continue + } + + err = os.Setenv(pair[0], pair[1]) + if err != nil { + panic("can't set environment variable") + } + } +} diff --git a/config/example/node.env b/config/example/node.env new file mode 100644 index 000000000..f23bc7d9e --- /dev/null +++ b/config/example/node.env @@ -0,0 +1 @@ +NEOFS_LOGGER_LEVEL=debug \ No newline at end of file