2017-11-15 16:37:43 +01:00
|
|
|
package configuration
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"reflect"
|
|
|
|
|
2023-04-29 17:08:47 +02:00
|
|
|
"gopkg.in/check.v1"
|
2017-11-15 16:37:43 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type localConfiguration struct {
|
2023-07-17 23:40:32 +02:00
|
|
|
Version Version `yaml:"version"`
|
|
|
|
Log *Log `yaml:"log"`
|
|
|
|
Notifications []Notif `yaml:"notifications,omitempty"`
|
2017-11-15 16:37:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type Log struct {
|
|
|
|
Formatter string `yaml:"formatter,omitempty"`
|
|
|
|
}
|
|
|
|
|
2023-07-17 23:40:32 +02:00
|
|
|
type Notif struct {
|
|
|
|
Name string `yaml:"name"`
|
|
|
|
}
|
|
|
|
|
2017-11-15 16:37:43 +01:00
|
|
|
var expectedConfig = localConfiguration{
|
|
|
|
Version: "0.1",
|
|
|
|
Log: &Log{
|
|
|
|
Formatter: "json",
|
|
|
|
},
|
2023-07-17 23:40:32 +02:00
|
|
|
Notifications: []Notif{
|
|
|
|
{Name: "foo"},
|
|
|
|
{Name: "bar"},
|
|
|
|
{Name: "car"},
|
|
|
|
},
|
2017-11-15 16:37:43 +01:00
|
|
|
}
|
|
|
|
|
2023-07-17 23:40:32 +02:00
|
|
|
const testConfig = `version: "0.1"
|
|
|
|
log:
|
|
|
|
formatter: "text"
|
|
|
|
notifications:
|
|
|
|
- name: "foo"
|
|
|
|
- name: "bar"
|
|
|
|
- name: "car"`
|
|
|
|
|
2017-11-15 16:37:43 +01:00
|
|
|
type ParserSuite struct{}
|
|
|
|
|
2023-04-29 17:08:47 +02:00
|
|
|
var _ = check.Suite(new(ParserSuite))
|
2017-11-15 16:37:43 +01:00
|
|
|
|
2023-04-29 17:08:47 +02:00
|
|
|
func (suite *ParserSuite) TestParserOverwriteIninitializedPoiner(c *check.C) {
|
2017-11-15 16:37:43 +01:00
|
|
|
config := localConfiguration{}
|
|
|
|
|
|
|
|
os.Setenv("REGISTRY_LOG_FORMATTER", "json")
|
|
|
|
defer os.Unsetenv("REGISTRY_LOG_FORMATTER")
|
|
|
|
|
|
|
|
p := NewParser("registry", []VersionedParseInfo{
|
|
|
|
{
|
|
|
|
Version: "0.1",
|
|
|
|
ParseAs: reflect.TypeOf(config),
|
|
|
|
ConversionFunc: func(c interface{}) (interface{}, error) {
|
|
|
|
return c, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2023-07-17 23:40:32 +02:00
|
|
|
err := p.Parse([]byte(testConfig), &config)
|
2023-04-29 17:08:47 +02:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(config, check.DeepEquals, expectedConfig)
|
2017-11-15 16:37:43 +01:00
|
|
|
}
|
|
|
|
|
2023-07-17 23:40:32 +02:00
|
|
|
const testConfig2 = `version: "0.1"
|
|
|
|
log:
|
|
|
|
formatter: "text"
|
|
|
|
notifications:
|
|
|
|
- name: "val1"
|
|
|
|
- name: "val2"
|
|
|
|
- name: "car"`
|
|
|
|
|
2023-04-29 17:08:47 +02:00
|
|
|
func (suite *ParserSuite) TestParseOverwriteUnininitializedPoiner(c *check.C) {
|
2017-11-15 16:37:43 +01:00
|
|
|
config := localConfiguration{}
|
|
|
|
|
|
|
|
os.Setenv("REGISTRY_LOG_FORMATTER", "json")
|
|
|
|
defer os.Unsetenv("REGISTRY_LOG_FORMATTER")
|
|
|
|
|
2023-07-17 23:40:32 +02:00
|
|
|
// override only first two notificationsvalues
|
|
|
|
// in the tetConfig: leave the last value unchanged.
|
|
|
|
os.Setenv("REGISTRY_NOTIFICATIONS_0_NAME", "foo")
|
|
|
|
defer os.Unsetenv("REGISTRY_NOTIFICATIONS_0_NAME")
|
|
|
|
os.Setenv("REGISTRY_NOTIFICATIONS_1_NAME", "bar")
|
|
|
|
defer os.Unsetenv("REGISTRY_NOTIFICATIONS_1_NAME")
|
|
|
|
|
2017-11-15 16:37:43 +01:00
|
|
|
p := NewParser("registry", []VersionedParseInfo{
|
|
|
|
{
|
|
|
|
Version: "0.1",
|
|
|
|
ParseAs: reflect.TypeOf(config),
|
|
|
|
ConversionFunc: func(c interface{}) (interface{}, error) {
|
|
|
|
return c, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2023-07-17 23:40:32 +02:00
|
|
|
err := p.Parse([]byte(testConfig2), &config)
|
2023-04-29 17:08:47 +02:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(config, check.DeepEquals, expectedConfig)
|
2017-11-15 16:37:43 +01:00
|
|
|
}
|