2017-11-15 15:37:43 +00:00
|
|
|
package configuration
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
2023-12-07 18:00:27 +00:00
|
|
|
"testing"
|
2017-11-15 15:37:43 +00:00
|
|
|
|
2023-12-07 18:00:27 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2017-11-15 15:37:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type localConfiguration struct {
|
2023-07-17 21:40:32 +00:00
|
|
|
Version Version `yaml:"version"`
|
|
|
|
Log *Log `yaml:"log"`
|
|
|
|
Notifications []Notif `yaml:"notifications,omitempty"`
|
2017-11-15 15:37:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Log struct {
|
|
|
|
Formatter string `yaml:"formatter,omitempty"`
|
|
|
|
}
|
|
|
|
|
2023-07-17 21:40:32 +00:00
|
|
|
type Notif struct {
|
|
|
|
Name string `yaml:"name"`
|
|
|
|
}
|
|
|
|
|
2017-11-15 15:37:43 +00:00
|
|
|
var expectedConfig = localConfiguration{
|
|
|
|
Version: "0.1",
|
|
|
|
Log: &Log{
|
|
|
|
Formatter: "json",
|
|
|
|
},
|
2023-07-17 21:40:32 +00:00
|
|
|
Notifications: []Notif{
|
|
|
|
{Name: "foo"},
|
|
|
|
{Name: "bar"},
|
|
|
|
{Name: "car"},
|
|
|
|
},
|
2017-11-15 15:37:43 +00:00
|
|
|
}
|
|
|
|
|
2023-07-17 21:40:32 +00:00
|
|
|
const testConfig = `version: "0.1"
|
|
|
|
log:
|
|
|
|
formatter: "text"
|
|
|
|
notifications:
|
|
|
|
- name: "foo"
|
|
|
|
- name: "bar"
|
|
|
|
- name: "car"`
|
|
|
|
|
2023-12-07 18:00:27 +00:00
|
|
|
func TestParserOverwriteIninitializedPoiner(t *testing.T) {
|
2017-11-15 15:37:43 +00:00
|
|
|
config := localConfiguration{}
|
|
|
|
|
2023-12-07 18:00:27 +00:00
|
|
|
t.Setenv("REGISTRY_LOG_FORMATTER", "json")
|
2017-11-15 15:37:43 +00:00
|
|
|
|
|
|
|
p := NewParser("registry", []VersionedParseInfo{
|
|
|
|
{
|
|
|
|
Version: "0.1",
|
|
|
|
ParseAs: reflect.TypeOf(config),
|
|
|
|
ConversionFunc: func(c interface{}) (interface{}, error) {
|
|
|
|
return c, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2023-07-17 21:40:32 +00:00
|
|
|
err := p.Parse([]byte(testConfig), &config)
|
2023-12-07 18:00:27 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, expectedConfig, config)
|
2017-11-15 15:37:43 +00:00
|
|
|
}
|
|
|
|
|
2023-07-17 21:40:32 +00:00
|
|
|
const testConfig2 = `version: "0.1"
|
|
|
|
log:
|
|
|
|
formatter: "text"
|
|
|
|
notifications:
|
|
|
|
- name: "val1"
|
|
|
|
- name: "val2"
|
|
|
|
- name: "car"`
|
|
|
|
|
2023-12-07 18:00:27 +00:00
|
|
|
func TestParseOverwriteUnininitializedPoiner(t *testing.T) {
|
2017-11-15 15:37:43 +00:00
|
|
|
config := localConfiguration{}
|
|
|
|
|
2023-12-07 18:00:27 +00:00
|
|
|
t.Setenv("REGISTRY_LOG_FORMATTER", "json")
|
2017-11-15 15:37:43 +00:00
|
|
|
|
2023-07-17 21:40:32 +00:00
|
|
|
// override only first two notificationsvalues
|
|
|
|
// in the tetConfig: leave the last value unchanged.
|
2023-12-07 18:00:27 +00:00
|
|
|
t.Setenv("REGISTRY_NOTIFICATIONS_0_NAME", "foo")
|
|
|
|
t.Setenv("REGISTRY_NOTIFICATIONS_1_NAME", "bar")
|
2023-07-17 21:40:32 +00:00
|
|
|
|
2017-11-15 15:37:43 +00:00
|
|
|
p := NewParser("registry", []VersionedParseInfo{
|
|
|
|
{
|
|
|
|
Version: "0.1",
|
|
|
|
ParseAs: reflect.TypeOf(config),
|
|
|
|
ConversionFunc: func(c interface{}) (interface{}, error) {
|
|
|
|
return c, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2023-07-17 21:40:32 +00:00
|
|
|
err := p.Parse([]byte(testConfig2), &config)
|
2023-12-07 18:00:27 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, expectedConfig, config)
|
2017-11-15 15:37:43 +00:00
|
|
|
}
|