From 75b7fcea25443c4315d1309f698291e8258bd8e2 Mon Sep 17 00:00:00 2001 From: Roman Glushko Date: Sun, 21 Jan 2024 19:11:15 +0200 Subject: [PATCH] #40: Covered the validation by tests --- pkg/config/provider.go | 2 +- pkg/config/provider_test.go | 20 ++++++++++++++++++++ pkg/config/testdata/provider.fullconfig.yaml | 2 +- pkg/config/testdata/provider.loggingnil.yaml | 15 +++++++++++++++ pkg/config/testdata/provider.telnil.yaml | 14 ++++++++++++++ pkg/telemetry/logging.go | 4 ++-- 6 files changed, 53 insertions(+), 4 deletions(-) create mode 100644 pkg/config/testdata/provider.loggingnil.yaml create mode 100644 pkg/config/testdata/provider.telnil.yaml diff --git a/pkg/config/provider.go b/pkg/config/provider.go index 7b88f49c..5231c595 100644 --- a/pkg/config/provider.go +++ b/pkg/config/provider.go @@ -71,7 +71,7 @@ func (p *Provider) Load(configPath string) (*Provider, error) { for _, err := range err.(validator.ValidationErrors) { namespace := strings.TrimLeft(err.Namespace(), "Config.") - errors = append(errors, fmt.Sprintf("- ❌ %v field is %v, %v provided", namespace, err.Tag(), err.Value())) + errors = append(errors, fmt.Sprintf("- ❌ %v field is %v, \"%v\" provided", namespace, err.Tag(), err.Value())) } // from here you can create your own error messages in whatever language you wish diff --git a/pkg/config/provider_test.go b/pkg/config/provider_test.go index 05f246fb..dc894264 100644 --- a/pkg/config/provider_test.go +++ b/pkg/config/provider_test.go @@ -36,6 +36,26 @@ func TestConfigProvider_ValidConfigLoaded(t *testing.T) { require.Len(t, models, 1) } +func TestConfigProvider_InvalidConfigLoaded(t *testing.T) { + var tests = []struct { + name string + configFile string + }{ + {"empty telemetry", "./testdata/provider.telnil.yaml"}, + {"empty logging", "./testdata/provider.loggingnil.yaml"}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + configProvider := NewProvider() + configProvider, err := configProvider.Load(tt.configFile) + + require.Error(t, err) + require.ErrorContains(t, err, "failed to validate config file") + }) + } +} + func TestConfigProvider_NoProvider(t *testing.T) { configProvider := NewProvider() _, err := configProvider.Load("./testdata/provider.nomodelprovider.yaml") diff --git a/pkg/config/testdata/provider.fullconfig.yaml b/pkg/config/testdata/provider.fullconfig.yaml index 3d960918..e83cfd5d 100644 --- a/pkg/config/testdata/provider.fullconfig.yaml +++ b/pkg/config/testdata/provider.fullconfig.yaml @@ -1,6 +1,6 @@ telemetry: logging: - level: INFO # DEBUG, INFO, WARNING, ERROR, FATAL + level: info # debug, info, warning, error, fatal encoding: json # console, json routers: diff --git a/pkg/config/testdata/provider.loggingnil.yaml b/pkg/config/testdata/provider.loggingnil.yaml new file mode 100644 index 00000000..eab73ea6 --- /dev/null +++ b/pkg/config/testdata/provider.loggingnil.yaml @@ -0,0 +1,15 @@ +telemetry: + logging: + +routers: + language: + - id: simplerouter + strategy: priority + models: + - id: openai-boring + openai: + model: gpt-3.5-turbo + api_key: "ABSC@124" + default_params: + temperature: 0 + diff --git a/pkg/config/testdata/provider.telnil.yaml b/pkg/config/testdata/provider.telnil.yaml new file mode 100644 index 00000000..0cf54c72 --- /dev/null +++ b/pkg/config/testdata/provider.telnil.yaml @@ -0,0 +1,14 @@ +telemetry: + +routers: + language: + - id: simplerouter + strategy: priority + models: + - id: openai-boring + openai: + model: gpt-3.5-turbo + api_key: "ABSC@124" + default_params: + temperature: 0 + diff --git a/pkg/telemetry/logging.go b/pkg/telemetry/logging.go index 1be3b318..dbeeb408 100644 --- a/pkg/telemetry/logging.go +++ b/pkg/telemetry/logging.go @@ -9,10 +9,10 @@ import ( type LogConfig struct { // Level is the minimum enabled logging level. - Level zapcore.Level `yaml:"level" validate:"required"` + Level zapcore.Level `yaml:"level"` // Encoding sets the logger's encoding. Valid values are "json", "console" - Encoding string `yaml:"encoding" validate:"required"` + Encoding string `yaml:"encoding"` // DisableCaller stops annotating logs with the calling function's file name and line number. // By default, all logs are annotated.