2014-10-27 23:16:19 +00:00
|
|
|
package configuration
|
|
|
|
|
|
|
|
import (
|
2014-12-02 01:32:31 +00:00
|
|
|
"bytes"
|
2015-01-28 23:55:18 +00:00
|
|
|
"net/http"
|
2014-10-27 23:16:19 +00:00
|
|
|
"os"
|
2015-08-17 20:41:50 +00:00
|
|
|
"reflect"
|
|
|
|
"strings"
|
2014-10-27 23:16:19 +00:00
|
|
|
"testing"
|
2018-07-30 23:15:35 +00:00
|
|
|
"time"
|
2014-10-27 23:16:19 +00:00
|
|
|
|
2023-04-29 15:08:47 +00:00
|
|
|
"gopkg.in/check.v1"
|
2014-12-02 01:32:31 +00:00
|
|
|
"gopkg.in/yaml.v2"
|
2014-10-27 23:16:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Hook up gocheck into the "go test" runner
|
2023-04-29 15:08:47 +00:00
|
|
|
func Test(t *testing.T) { check.TestingT(t) }
|
2014-10-27 23:16:19 +00:00
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
// configStruct is a canonical example configuration, which should map to configYamlV0_1
|
2014-10-27 23:16:19 +00:00
|
|
|
var configStruct = Configuration{
|
2015-03-25 00:12:04 +00:00
|
|
|
Version: "0.1",
|
|
|
|
Log: struct {
|
2016-09-14 00:23:27 +00:00
|
|
|
AccessLog struct {
|
|
|
|
Disabled bool `yaml:"disabled,omitempty"`
|
|
|
|
} `yaml:"accesslog,omitempty"`
|
2023-06-02 07:46:53 +00:00
|
|
|
Level Loglevel `yaml:"level,omitempty"`
|
|
|
|
Formatter string `yaml:"formatter,omitempty"`
|
|
|
|
Fields map[string]interface{} `yaml:"fields,omitempty"`
|
|
|
|
Hooks []LogHook `yaml:"hooks,omitempty"`
|
|
|
|
ReportCaller bool `yaml:"reportcaller,omitempty"`
|
2015-03-25 00:12:04 +00:00
|
|
|
}{
|
2018-08-14 22:23:04 +00:00
|
|
|
Level: "info",
|
2015-04-01 16:46:35 +00:00
|
|
|
Fields: map[string]interface{}{"environment": "test"},
|
2015-03-25 00:12:04 +00:00
|
|
|
},
|
2014-11-04 17:41:32 +00:00
|
|
|
Storage: Storage{
|
2022-08-04 20:01:48 +00:00
|
|
|
"somedriver": Parameters{
|
|
|
|
"string1": "string-value1",
|
|
|
|
"string2": "string-value2",
|
|
|
|
"bool1": true,
|
|
|
|
"bool2": false,
|
|
|
|
"nil1": nil,
|
|
|
|
"int1": 42,
|
|
|
|
"url1": "https://foo.example.com",
|
|
|
|
"path1": "/some-path",
|
2014-10-27 23:16:19 +00:00
|
|
|
},
|
|
|
|
},
|
2014-12-18 20:30:19 +00:00
|
|
|
Auth: Auth{
|
|
|
|
"silly": Parameters{
|
|
|
|
"realm": "silly",
|
|
|
|
"service": "silly",
|
|
|
|
},
|
|
|
|
},
|
2014-12-13 02:05:37 +00:00
|
|
|
Reporting: Reporting{
|
|
|
|
Bugsnag: BugsnagReporting{
|
|
|
|
APIKey: "BugsnagApiKey",
|
|
|
|
},
|
|
|
|
},
|
2015-01-28 23:55:18 +00:00
|
|
|
Notifications: Notifications{
|
|
|
|
Endpoints: []Endpoint{
|
|
|
|
{
|
|
|
|
Name: "endpoint-1",
|
|
|
|
URL: "http://example.com",
|
|
|
|
Headers: http.Header{
|
|
|
|
"Authorization": []string{"Bearer <example>"},
|
|
|
|
},
|
2016-09-12 22:07:49 +00:00
|
|
|
IgnoredMediaTypes: []string{"application/octet-stream"},
|
2018-01-18 10:26:54 +00:00
|
|
|
Ignore: Ignore{
|
|
|
|
MediaTypes: []string{"application/octet-stream"},
|
|
|
|
Actions: []string{"pull"},
|
|
|
|
},
|
2015-01-28 23:55:18 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2023-03-31 11:16:29 +00:00
|
|
|
Catalog: Catalog{
|
|
|
|
MaxEntries: 1000,
|
|
|
|
},
|
2015-03-20 15:19:07 +00:00
|
|
|
HTTP: struct {
|
2018-07-30 23:15:35 +00:00
|
|
|
Addr string `yaml:"addr,omitempty"`
|
|
|
|
Net string `yaml:"net,omitempty"`
|
|
|
|
Host string `yaml:"host,omitempty"`
|
|
|
|
Prefix string `yaml:"prefix,omitempty"`
|
|
|
|
Secret string `yaml:"secret,omitempty"`
|
|
|
|
RelativeURLs bool `yaml:"relativeurls,omitempty"`
|
|
|
|
DrainTimeout time.Duration `yaml:"draintimeout,omitempty"`
|
2016-02-23 01:49:23 +00:00
|
|
|
TLS struct {
|
2021-02-18 21:31:23 +00:00
|
|
|
Certificate string `yaml:"certificate,omitempty"`
|
|
|
|
Key string `yaml:"key,omitempty"`
|
|
|
|
ClientCAs []string `yaml:"clientcas,omitempty"`
|
|
|
|
MinimumTLS string `yaml:"minimumtls,omitempty"`
|
|
|
|
CipherSuites []string `yaml:"ciphersuites,omitempty"`
|
|
|
|
LetsEncrypt struct {
|
2021-07-23 21:25:09 +00:00
|
|
|
CacheFile string `yaml:"cachefile,omitempty"`
|
|
|
|
Email string `yaml:"email,omitempty"`
|
|
|
|
Hosts []string `yaml:"hosts,omitempty"`
|
|
|
|
DirectoryURL string `yaml:"directoryurl,omitempty"`
|
2016-06-13 18:30:42 +00:00
|
|
|
} `yaml:"letsencrypt,omitempty"`
|
2015-03-20 15:19:07 +00:00
|
|
|
} `yaml:"tls,omitempty"`
|
2015-08-10 21:20:52 +00:00
|
|
|
Headers http.Header `yaml:"headers,omitempty"`
|
|
|
|
Debug struct {
|
2017-11-17 00:43:38 +00:00
|
|
|
Addr string `yaml:"addr,omitempty"`
|
|
|
|
Prometheus struct {
|
|
|
|
Enabled bool `yaml:"enabled,omitempty"`
|
|
|
|
Path string `yaml:"path,omitempty"`
|
|
|
|
} `yaml:"prometheus,omitempty"`
|
2015-03-20 15:19:07 +00:00
|
|
|
} `yaml:"debug,omitempty"`
|
2016-07-14 19:48:03 +00:00
|
|
|
HTTP2 struct {
|
|
|
|
Disabled bool `yaml:"disabled,omitempty"`
|
|
|
|
} `yaml:"http2,omitempty"`
|
2015-03-20 15:19:07 +00:00
|
|
|
}{
|
|
|
|
TLS: struct {
|
2021-02-18 21:31:23 +00:00
|
|
|
Certificate string `yaml:"certificate,omitempty"`
|
|
|
|
Key string `yaml:"key,omitempty"`
|
|
|
|
ClientCAs []string `yaml:"clientcas,omitempty"`
|
|
|
|
MinimumTLS string `yaml:"minimumtls,omitempty"`
|
|
|
|
CipherSuites []string `yaml:"ciphersuites,omitempty"`
|
|
|
|
LetsEncrypt struct {
|
2021-07-23 21:25:09 +00:00
|
|
|
CacheFile string `yaml:"cachefile,omitempty"`
|
|
|
|
Email string `yaml:"email,omitempty"`
|
|
|
|
Hosts []string `yaml:"hosts,omitempty"`
|
|
|
|
DirectoryURL string `yaml:"directoryurl,omitempty"`
|
2016-06-13 18:30:42 +00:00
|
|
|
} `yaml:"letsencrypt,omitempty"`
|
2015-03-20 15:19:07 +00:00
|
|
|
}{
|
|
|
|
ClientCAs: []string{"/path/to/ca.pem"},
|
|
|
|
},
|
2015-08-10 21:20:52 +00:00
|
|
|
Headers: http.Header{
|
|
|
|
"X-Content-Type-Options": []string{"nosniff"},
|
|
|
|
},
|
2016-07-14 19:48:03 +00:00
|
|
|
HTTP2: struct {
|
|
|
|
Disabled bool `yaml:"disabled,omitempty"`
|
|
|
|
}{
|
|
|
|
Disabled: false,
|
|
|
|
},
|
2015-03-20 15:19:07 +00:00
|
|
|
},
|
2023-08-04 07:04:43 +00:00
|
|
|
Redis: struct {
|
|
|
|
Addr string `yaml:"addr,omitempty"`
|
|
|
|
Username string `yaml:"username,omitempty"`
|
|
|
|
Password string `yaml:"password,omitempty"`
|
|
|
|
DB int `yaml:"db,omitempty"`
|
|
|
|
TLS struct {
|
|
|
|
Enabled bool `yaml:"enabled,omitempty"`
|
|
|
|
} `yaml:"tls,omitempty"`
|
|
|
|
DialTimeout time.Duration `yaml:"dialtimeout,omitempty"`
|
|
|
|
ReadTimeout time.Duration `yaml:"readtimeout,omitempty"`
|
|
|
|
WriteTimeout time.Duration `yaml:"writetimeout,omitempty"`
|
|
|
|
Pool struct {
|
|
|
|
MaxIdle int `yaml:"maxidle,omitempty"`
|
|
|
|
MaxActive int `yaml:"maxactive,omitempty"`
|
|
|
|
IdleTimeout time.Duration `yaml:"idletimeout,omitempty"`
|
|
|
|
} `yaml:"pool,omitempty"`
|
|
|
|
}{
|
|
|
|
Addr: "localhost:6379",
|
|
|
|
Username: "alice",
|
|
|
|
Password: "123456",
|
|
|
|
DB: 1,
|
|
|
|
Pool: struct {
|
|
|
|
MaxIdle int `yaml:"maxidle,omitempty"`
|
|
|
|
MaxActive int `yaml:"maxactive,omitempty"`
|
|
|
|
IdleTimeout time.Duration `yaml:"idletimeout,omitempty"`
|
|
|
|
}{
|
|
|
|
MaxIdle: 16,
|
|
|
|
MaxActive: 64,
|
|
|
|
IdleTimeout: time.Second * 300,
|
|
|
|
},
|
|
|
|
DialTimeout: time.Millisecond * 10,
|
|
|
|
ReadTimeout: time.Millisecond * 10,
|
|
|
|
WriteTimeout: time.Millisecond * 10,
|
|
|
|
},
|
2014-10-27 23:16:19 +00:00
|
|
|
}
|
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
// configYamlV0_1 is a Version 0.1 yaml document representing configStruct
|
|
|
|
var configYamlV0_1 = `
|
2014-10-27 23:16:19 +00:00
|
|
|
version: 0.1
|
2015-03-25 00:12:04 +00:00
|
|
|
log:
|
2018-08-14 22:23:04 +00:00
|
|
|
level: info
|
2015-03-25 00:12:04 +00:00
|
|
|
fields:
|
|
|
|
environment: test
|
2014-11-04 17:41:32 +00:00
|
|
|
storage:
|
2022-08-04 20:01:48 +00:00
|
|
|
somedriver:
|
|
|
|
string1: string-value1
|
|
|
|
string2: string-value2
|
|
|
|
bool1: true
|
|
|
|
bool2: false
|
|
|
|
nil1: ~
|
|
|
|
int1: 42
|
|
|
|
url1: "https://foo.example.com"
|
|
|
|
path1: "/some-path"
|
2014-12-18 20:30:19 +00:00
|
|
|
auth:
|
|
|
|
silly:
|
|
|
|
realm: silly
|
|
|
|
service: silly
|
2015-01-28 23:55:18 +00:00
|
|
|
notifications:
|
|
|
|
endpoints:
|
|
|
|
- name: endpoint-1
|
|
|
|
url: http://example.com
|
|
|
|
headers:
|
|
|
|
Authorization: [Bearer <example>]
|
2016-09-12 22:07:49 +00:00
|
|
|
ignoredmediatypes:
|
|
|
|
- application/octet-stream
|
2018-01-18 10:26:54 +00:00
|
|
|
ignore:
|
|
|
|
mediatypes:
|
|
|
|
- application/octet-stream
|
|
|
|
actions:
|
|
|
|
- pull
|
2014-12-13 02:05:37 +00:00
|
|
|
reporting:
|
|
|
|
bugsnag:
|
|
|
|
apikey: BugsnagApiKey
|
2015-03-20 15:19:07 +00:00
|
|
|
http:
|
|
|
|
clientcas:
|
|
|
|
- /path/to/ca.pem
|
2015-08-10 21:20:52 +00:00
|
|
|
headers:
|
|
|
|
X-Content-Type-Options: [nosniff]
|
2023-08-04 07:04:43 +00:00
|
|
|
redis:
|
|
|
|
addr: localhost:6379
|
|
|
|
username: alice
|
|
|
|
password: 123456
|
|
|
|
db: 1
|
|
|
|
pool:
|
|
|
|
maxidle: 16
|
|
|
|
maxactive: 64
|
|
|
|
idletimeout: 300s
|
|
|
|
dialtimeout: 10ms
|
|
|
|
readtimeout: 10ms
|
|
|
|
writetimeout: 10ms
|
2014-11-04 17:41:32 +00:00
|
|
|
`
|
2014-10-27 23:16:19 +00:00
|
|
|
|
2014-12-17 22:18:58 +00:00
|
|
|
// inmemoryConfigYamlV0_1 is a Version 0.1 yaml document specifying an inmemory
|
|
|
|
// storage driver with no parameters
|
2014-11-17 23:44:07 +00:00
|
|
|
var inmemoryConfigYamlV0_1 = `
|
2014-11-04 17:41:32 +00:00
|
|
|
version: 0.1
|
2018-08-14 22:23:04 +00:00
|
|
|
log:
|
|
|
|
level: info
|
2014-11-04 17:41:32 +00:00
|
|
|
storage: inmemory
|
2014-12-18 20:30:19 +00:00
|
|
|
auth:
|
|
|
|
silly:
|
|
|
|
realm: silly
|
|
|
|
service: silly
|
2015-01-28 23:55:18 +00:00
|
|
|
notifications:
|
|
|
|
endpoints:
|
|
|
|
- name: endpoint-1
|
|
|
|
url: http://example.com
|
|
|
|
headers:
|
|
|
|
Authorization: [Bearer <example>]
|
2016-09-12 22:07:49 +00:00
|
|
|
ignoredmediatypes:
|
|
|
|
- application/octet-stream
|
2018-01-18 10:26:54 +00:00
|
|
|
ignore:
|
|
|
|
mediatypes:
|
|
|
|
- application/octet-stream
|
|
|
|
actions:
|
|
|
|
- pull
|
2015-08-10 21:20:52 +00:00
|
|
|
http:
|
|
|
|
headers:
|
|
|
|
X-Content-Type-Options: [nosniff]
|
2014-10-27 23:16:19 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
type ConfigSuite struct {
|
|
|
|
expectedConfig *Configuration
|
|
|
|
}
|
|
|
|
|
2023-04-29 15:08:47 +00:00
|
|
|
var _ = check.Suite(new(ConfigSuite))
|
2014-10-27 23:16:19 +00:00
|
|
|
|
2023-04-29 15:08:47 +00:00
|
|
|
func (suite *ConfigSuite) SetUpTest(c *check.C) {
|
2014-10-27 23:16:19 +00:00
|
|
|
os.Clearenv()
|
|
|
|
suite.expectedConfig = copyConfig(configStruct)
|
|
|
|
}
|
|
|
|
|
2014-12-17 22:18:58 +00:00
|
|
|
// TestMarshalRoundtrip validates that configStruct can be marshaled and
|
|
|
|
// unmarshaled without changing any parameters
|
2023-04-29 15:08:47 +00:00
|
|
|
func (suite *ConfigSuite) TestMarshalRoundtrip(c *check.C) {
|
2014-10-27 23:16:19 +00:00
|
|
|
configBytes, err := yaml.Marshal(suite.expectedConfig)
|
2023-04-29 15:08:47 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2014-12-02 01:32:31 +00:00
|
|
|
config, err := Parse(bytes.NewReader(configBytes))
|
2018-08-14 22:23:04 +00:00
|
|
|
c.Log(string(configBytes))
|
2023-04-29 15:08:47 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(config, check.DeepEquals, suite.expectedConfig)
|
2014-10-27 23:16:19 +00:00
|
|
|
}
|
|
|
|
|
2014-12-17 22:18:58 +00:00
|
|
|
// TestParseSimple validates that configYamlV0_1 can be parsed into a struct
|
|
|
|
// matching configStruct
|
2023-04-29 15:08:47 +00:00
|
|
|
func (suite *ConfigSuite) TestParseSimple(c *check.C) {
|
2014-12-02 01:32:31 +00:00
|
|
|
config, err := Parse(bytes.NewReader([]byte(configYamlV0_1)))
|
2023-04-29 15:08:47 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(config, check.DeepEquals, suite.expectedConfig)
|
2014-10-27 23:16:19 +00:00
|
|
|
}
|
|
|
|
|
2014-12-17 22:18:58 +00:00
|
|
|
// TestParseInmemory validates that configuration yaml with storage provided as
|
|
|
|
// a string can be parsed into a Configuration struct with no storage parameters
|
2023-04-29 15:08:47 +00:00
|
|
|
func (suite *ConfigSuite) TestParseInmemory(c *check.C) {
|
2014-11-04 17:41:32 +00:00
|
|
|
suite.expectedConfig.Storage = Storage{"inmemory": Parameters{}}
|
2014-12-13 02:05:37 +00:00
|
|
|
suite.expectedConfig.Reporting = Reporting{}
|
2015-03-25 00:12:04 +00:00
|
|
|
suite.expectedConfig.Log.Fields = nil
|
2023-08-04 07:04:43 +00:00
|
|
|
suite.expectedConfig.Redis = struct {
|
|
|
|
Addr string `yaml:"addr,omitempty"`
|
|
|
|
Username string `yaml:"username,omitempty"`
|
|
|
|
Password string `yaml:"password,omitempty"`
|
|
|
|
DB int `yaml:"db,omitempty"`
|
|
|
|
TLS struct {
|
|
|
|
Enabled bool `yaml:"enabled,omitempty"`
|
|
|
|
} `yaml:"tls,omitempty"`
|
|
|
|
DialTimeout time.Duration `yaml:"dialtimeout,omitempty"`
|
|
|
|
ReadTimeout time.Duration `yaml:"readtimeout,omitempty"`
|
|
|
|
WriteTimeout time.Duration `yaml:"writetimeout,omitempty"`
|
|
|
|
Pool struct {
|
|
|
|
MaxIdle int `yaml:"maxidle,omitempty"`
|
|
|
|
MaxActive int `yaml:"maxactive,omitempty"`
|
|
|
|
IdleTimeout time.Duration `yaml:"idletimeout,omitempty"`
|
|
|
|
} `yaml:"pool,omitempty"`
|
|
|
|
}{}
|
2014-11-04 17:41:32 +00:00
|
|
|
|
2014-12-02 01:32:31 +00:00
|
|
|
config, err := Parse(bytes.NewReader([]byte(inmemoryConfigYamlV0_1)))
|
2023-04-29 15:08:47 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(config, check.DeepEquals, suite.expectedConfig)
|
2014-11-04 17:41:32 +00:00
|
|
|
}
|
|
|
|
|
2014-12-17 22:18:58 +00:00
|
|
|
// TestParseIncomplete validates that an incomplete yaml configuration cannot
|
|
|
|
// be parsed without providing environment variables to fill in the missing
|
|
|
|
// components.
|
2023-04-29 15:08:47 +00:00
|
|
|
func (suite *ConfigSuite) TestParseIncomplete(c *check.C) {
|
2014-12-17 22:18:58 +00:00
|
|
|
incompleteConfigYaml := "version: 0.1"
|
|
|
|
_, err := Parse(bytes.NewReader([]byte(incompleteConfigYaml)))
|
2023-04-29 15:08:47 +00:00
|
|
|
c.Assert(err, check.NotNil)
|
2014-12-17 22:18:58 +00:00
|
|
|
|
2015-03-25 00:12:04 +00:00
|
|
|
suite.expectedConfig.Log.Fields = nil
|
2014-12-17 22:18:58 +00:00
|
|
|
suite.expectedConfig.Storage = Storage{"filesystem": Parameters{"rootdirectory": "/tmp/testroot"}}
|
2014-12-18 20:30:19 +00:00
|
|
|
suite.expectedConfig.Auth = Auth{"silly": Parameters{"realm": "silly"}}
|
2014-12-17 22:18:58 +00:00
|
|
|
suite.expectedConfig.Reporting = Reporting{}
|
2015-01-28 23:55:18 +00:00
|
|
|
suite.expectedConfig.Notifications = Notifications{}
|
2015-08-10 21:20:52 +00:00
|
|
|
suite.expectedConfig.HTTP.Headers = nil
|
2023-08-04 07:04:43 +00:00
|
|
|
suite.expectedConfig.Redis = struct {
|
|
|
|
Addr string `yaml:"addr,omitempty"`
|
|
|
|
Username string `yaml:"username,omitempty"`
|
|
|
|
Password string `yaml:"password,omitempty"`
|
|
|
|
DB int `yaml:"db,omitempty"`
|
|
|
|
TLS struct {
|
|
|
|
Enabled bool `yaml:"enabled,omitempty"`
|
|
|
|
} `yaml:"tls,omitempty"`
|
|
|
|
DialTimeout time.Duration `yaml:"dialtimeout,omitempty"`
|
|
|
|
ReadTimeout time.Duration `yaml:"readtimeout,omitempty"`
|
|
|
|
WriteTimeout time.Duration `yaml:"writetimeout,omitempty"`
|
|
|
|
Pool struct {
|
|
|
|
MaxIdle int `yaml:"maxidle,omitempty"`
|
|
|
|
MaxActive int `yaml:"maxactive,omitempty"`
|
|
|
|
IdleTimeout time.Duration `yaml:"idletimeout,omitempty"`
|
|
|
|
} `yaml:"pool,omitempty"`
|
|
|
|
}{}
|
2014-12-17 22:18:58 +00:00
|
|
|
|
2015-08-17 20:41:50 +00:00
|
|
|
// Note: this also tests that REGISTRY_STORAGE and
|
|
|
|
// REGISTRY_STORAGE_FILESYSTEM_ROOTDIRECTORY can be used together
|
2014-12-17 22:18:58 +00:00
|
|
|
os.Setenv("REGISTRY_STORAGE", "filesystem")
|
|
|
|
os.Setenv("REGISTRY_STORAGE_FILESYSTEM_ROOTDIRECTORY", "/tmp/testroot")
|
2014-12-18 20:30:19 +00:00
|
|
|
os.Setenv("REGISTRY_AUTH", "silly")
|
|
|
|
os.Setenv("REGISTRY_AUTH_SILLY_REALM", "silly")
|
2014-12-17 22:18:58 +00:00
|
|
|
|
|
|
|
config, err := Parse(bytes.NewReader([]byte(incompleteConfigYaml)))
|
2023-04-29 15:08:47 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(config, check.DeepEquals, suite.expectedConfig)
|
2014-12-17 22:18:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TestParseWithSameEnvStorage validates that providing environment variables
|
|
|
|
// that match the given storage type will only include environment-defined
|
|
|
|
// parameters and remove yaml-defined parameters
|
2023-04-29 15:08:47 +00:00
|
|
|
func (suite *ConfigSuite) TestParseWithSameEnvStorage(c *check.C) {
|
2022-08-04 20:01:48 +00:00
|
|
|
suite.expectedConfig.Storage = Storage{"somedriver": Parameters{"region": "us-east-1"}}
|
2014-12-17 22:18:58 +00:00
|
|
|
|
2022-08-04 20:01:48 +00:00
|
|
|
os.Setenv("REGISTRY_STORAGE", "somedriver")
|
|
|
|
os.Setenv("REGISTRY_STORAGE_SOMEDRIVER_REGION", "us-east-1")
|
2014-10-27 23:16:19 +00:00
|
|
|
|
2014-12-02 01:32:31 +00:00
|
|
|
config, err := Parse(bytes.NewReader([]byte(configYamlV0_1)))
|
2023-04-29 15:08:47 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(config, check.DeepEquals, suite.expectedConfig)
|
2014-10-27 23:16:19 +00:00
|
|
|
}
|
|
|
|
|
2014-10-30 00:42:23 +00:00
|
|
|
// TestParseWithDifferentEnvStorageParams validates that providing environment variables that change
|
|
|
|
// and add to the given storage parameters will change and add parameters to the parsed
|
|
|
|
// Configuration struct
|
2023-04-29 15:08:47 +00:00
|
|
|
func (suite *ConfigSuite) TestParseWithDifferentEnvStorageParams(c *check.C) {
|
2022-08-04 20:01:48 +00:00
|
|
|
suite.expectedConfig.Storage.setParameter("string1", "us-west-1")
|
|
|
|
suite.expectedConfig.Storage.setParameter("bool1", true)
|
2014-11-04 17:41:32 +00:00
|
|
|
suite.expectedConfig.Storage.setParameter("newparam", "some Value")
|
2014-10-27 23:16:19 +00:00
|
|
|
|
2022-08-04 20:01:48 +00:00
|
|
|
os.Setenv("REGISTRY_STORAGE_SOMEDRIVER_STRING1", "us-west-1")
|
|
|
|
os.Setenv("REGISTRY_STORAGE_SOMEDRIVER_BOOL1", "true")
|
|
|
|
os.Setenv("REGISTRY_STORAGE_SOMEDRIVER_NEWPARAM", "some Value")
|
2014-10-27 23:16:19 +00:00
|
|
|
|
2014-12-02 01:32:31 +00:00
|
|
|
config, err := Parse(bytes.NewReader([]byte(configYamlV0_1)))
|
2023-04-29 15:08:47 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(config, check.DeepEquals, suite.expectedConfig)
|
2014-10-27 23:16:19 +00:00
|
|
|
}
|
|
|
|
|
2014-10-30 00:42:23 +00:00
|
|
|
// TestParseWithDifferentEnvStorageType validates that providing an environment variable that
|
|
|
|
// changes the storage type will be reflected in the parsed Configuration struct
|
2023-04-29 15:08:47 +00:00
|
|
|
func (suite *ConfigSuite) TestParseWithDifferentEnvStorageType(c *check.C) {
|
2014-11-04 17:41:32 +00:00
|
|
|
suite.expectedConfig.Storage = Storage{"inmemory": Parameters{}}
|
2014-10-27 23:16:19 +00:00
|
|
|
|
|
|
|
os.Setenv("REGISTRY_STORAGE", "inmemory")
|
|
|
|
|
2014-12-02 01:32:31 +00:00
|
|
|
config, err := Parse(bytes.NewReader([]byte(configYamlV0_1)))
|
2023-04-29 15:08:47 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(config, check.DeepEquals, suite.expectedConfig)
|
2014-10-27 23:16:19 +00:00
|
|
|
}
|
|
|
|
|
2014-10-30 00:42:23 +00:00
|
|
|
// TestParseWithDifferentEnvStorageTypeAndParams validates that providing an environment variable
|
|
|
|
// that changes the storage type will be reflected in the parsed Configuration struct and that
|
|
|
|
// environment storage parameters will also be included
|
2023-04-29 15:08:47 +00:00
|
|
|
func (suite *ConfigSuite) TestParseWithDifferentEnvStorageTypeAndParams(c *check.C) {
|
2014-11-04 17:41:32 +00:00
|
|
|
suite.expectedConfig.Storage = Storage{"filesystem": Parameters{}}
|
|
|
|
suite.expectedConfig.Storage.setParameter("rootdirectory", "/tmp/testroot")
|
2014-10-27 23:16:19 +00:00
|
|
|
|
|
|
|
os.Setenv("REGISTRY_STORAGE", "filesystem")
|
|
|
|
os.Setenv("REGISTRY_STORAGE_FILESYSTEM_ROOTDIRECTORY", "/tmp/testroot")
|
|
|
|
|
2014-12-02 01:32:31 +00:00
|
|
|
config, err := Parse(bytes.NewReader([]byte(configYamlV0_1)))
|
2023-04-29 15:08:47 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(config, check.DeepEquals, suite.expectedConfig)
|
2014-10-27 23:16:19 +00:00
|
|
|
}
|
|
|
|
|
2014-10-30 00:42:23 +00:00
|
|
|
// TestParseWithSameEnvLoglevel validates that providing an environment variable defining the log
|
|
|
|
// level to the same as the one provided in the yaml will not change the parsed Configuration struct
|
2023-04-29 15:08:47 +00:00
|
|
|
func (suite *ConfigSuite) TestParseWithSameEnvLoglevel(c *check.C) {
|
2014-10-27 23:16:19 +00:00
|
|
|
os.Setenv("REGISTRY_LOGLEVEL", "info")
|
|
|
|
|
2014-12-02 01:32:31 +00:00
|
|
|
config, err := Parse(bytes.NewReader([]byte(configYamlV0_1)))
|
2023-04-29 15:08:47 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(config, check.DeepEquals, suite.expectedConfig)
|
2014-10-27 23:16:19 +00:00
|
|
|
}
|
|
|
|
|
2014-10-30 00:42:23 +00:00
|
|
|
// TestParseWithDifferentEnvLoglevel validates that providing an environment variable defining the
|
|
|
|
// log level will override the value provided in the yaml document
|
2023-04-29 15:08:47 +00:00
|
|
|
func (suite *ConfigSuite) TestParseWithDifferentEnvLoglevel(c *check.C) {
|
2018-08-14 22:23:04 +00:00
|
|
|
suite.expectedConfig.Log.Level = "error"
|
2014-10-27 23:16:19 +00:00
|
|
|
|
2018-08-14 22:23:04 +00:00
|
|
|
os.Setenv("REGISTRY_LOG_LEVEL", "error")
|
2014-10-27 23:16:19 +00:00
|
|
|
|
2014-12-02 01:32:31 +00:00
|
|
|
config, err := Parse(bytes.NewReader([]byte(configYamlV0_1)))
|
2023-04-29 15:08:47 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(config, check.DeepEquals, suite.expectedConfig)
|
2014-10-27 23:16:19 +00:00
|
|
|
}
|
|
|
|
|
2014-12-17 22:18:58 +00:00
|
|
|
// TestParseInvalidLoglevel validates that the parser will fail to parse a
|
|
|
|
// configuration if the loglevel is malformed
|
2023-04-29 15:08:47 +00:00
|
|
|
func (suite *ConfigSuite) TestParseInvalidLoglevel(c *check.C) {
|
2014-12-17 22:18:58 +00:00
|
|
|
invalidConfigYaml := "version: 0.1\nloglevel: derp\nstorage: inmemory"
|
|
|
|
_, err := Parse(bytes.NewReader([]byte(invalidConfigYaml)))
|
2023-04-29 15:08:47 +00:00
|
|
|
c.Assert(err, check.NotNil)
|
2014-12-17 22:18:58 +00:00
|
|
|
|
|
|
|
os.Setenv("REGISTRY_LOGLEVEL", "derp")
|
|
|
|
|
|
|
|
_, err = Parse(bytes.NewReader([]byte(configYamlV0_1)))
|
2023-04-29 15:08:47 +00:00
|
|
|
c.Assert(err, check.NotNil)
|
2014-12-17 22:18:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TestParseWithDifferentEnvReporting validates that environment variables
|
|
|
|
// properly override reporting parameters
|
2023-04-29 15:08:47 +00:00
|
|
|
func (suite *ConfigSuite) TestParseWithDifferentEnvReporting(c *check.C) {
|
2014-12-13 02:05:37 +00:00
|
|
|
suite.expectedConfig.Reporting.Bugsnag.APIKey = "anotherBugsnagApiKey"
|
|
|
|
suite.expectedConfig.Reporting.Bugsnag.Endpoint = "localhost:8080"
|
|
|
|
suite.expectedConfig.Reporting.NewRelic.LicenseKey = "NewRelicLicenseKey"
|
|
|
|
suite.expectedConfig.Reporting.NewRelic.Name = "some NewRelic NAME"
|
|
|
|
|
|
|
|
os.Setenv("REGISTRY_REPORTING_BUGSNAG_APIKEY", "anotherBugsnagApiKey")
|
|
|
|
os.Setenv("REGISTRY_REPORTING_BUGSNAG_ENDPOINT", "localhost:8080")
|
|
|
|
os.Setenv("REGISTRY_REPORTING_NEWRELIC_LICENSEKEY", "NewRelicLicenseKey")
|
|
|
|
os.Setenv("REGISTRY_REPORTING_NEWRELIC_NAME", "some NewRelic NAME")
|
|
|
|
|
|
|
|
config, err := Parse(bytes.NewReader([]byte(configYamlV0_1)))
|
2023-04-29 15:08:47 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(config, check.DeepEquals, suite.expectedConfig)
|
2014-12-13 02:05:37 +00:00
|
|
|
}
|
|
|
|
|
2014-10-30 00:42:23 +00:00
|
|
|
// TestParseInvalidVersion validates that the parser will fail to parse a newer configuration
|
|
|
|
// version than the CurrentVersion
|
2023-04-29 15:08:47 +00:00
|
|
|
func (suite *ConfigSuite) TestParseInvalidVersion(c *check.C) {
|
2014-11-04 17:41:32 +00:00
|
|
|
suite.expectedConfig.Version = MajorMinorVersion(CurrentVersion.Major(), CurrentVersion.Minor()+1)
|
2014-10-27 23:16:19 +00:00
|
|
|
configBytes, err := yaml.Marshal(suite.expectedConfig)
|
2023-04-29 15:08:47 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2014-12-02 01:32:31 +00:00
|
|
|
_, err = Parse(bytes.NewReader(configBytes))
|
2023-04-29 15:08:47 +00:00
|
|
|
c.Assert(err, check.NotNil)
|
2014-10-27 23:16:19 +00:00
|
|
|
}
|
|
|
|
|
2015-08-17 20:41:50 +00:00
|
|
|
// TestParseExtraneousVars validates that environment variables referring to
|
|
|
|
// nonexistent variables don't cause side effects.
|
2023-04-29 15:08:47 +00:00
|
|
|
func (suite *ConfigSuite) TestParseExtraneousVars(c *check.C) {
|
2015-08-17 20:41:50 +00:00
|
|
|
suite.expectedConfig.Reporting.Bugsnag.Endpoint = "localhost:8080"
|
|
|
|
|
|
|
|
// A valid environment variable
|
|
|
|
os.Setenv("REGISTRY_REPORTING_BUGSNAG_ENDPOINT", "localhost:8080")
|
|
|
|
|
|
|
|
// Environment variables which shouldn't set config items
|
|
|
|
os.Setenv("registry_REPORTING_NEWRELIC_LICENSEKEY", "NewRelicLicenseKey")
|
|
|
|
os.Setenv("REPORTING_NEWRELIC_NAME", "some NewRelic NAME")
|
|
|
|
os.Setenv("REGISTRY_DUCKS", "quack")
|
|
|
|
os.Setenv("REGISTRY_REPORTING_ASDF", "ghjk")
|
|
|
|
|
|
|
|
config, err := Parse(bytes.NewReader([]byte(configYamlV0_1)))
|
2023-04-29 15:08:47 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(config, check.DeepEquals, suite.expectedConfig)
|
2015-08-17 20:41:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TestParseEnvVarImplicitMaps validates that environment variables can set
|
|
|
|
// values in maps that don't already exist.
|
2023-04-29 15:08:47 +00:00
|
|
|
func (suite *ConfigSuite) TestParseEnvVarImplicitMaps(c *check.C) {
|
2015-08-17 20:41:50 +00:00
|
|
|
readonly := make(map[string]interface{})
|
|
|
|
readonly["enabled"] = true
|
|
|
|
|
|
|
|
maintenance := make(map[string]interface{})
|
|
|
|
maintenance["readonly"] = readonly
|
|
|
|
|
|
|
|
suite.expectedConfig.Storage["maintenance"] = maintenance
|
|
|
|
|
|
|
|
os.Setenv("REGISTRY_STORAGE_MAINTENANCE_READONLY_ENABLED", "true")
|
|
|
|
|
|
|
|
config, err := Parse(bytes.NewReader([]byte(configYamlV0_1)))
|
2023-04-29 15:08:47 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(config, check.DeepEquals, suite.expectedConfig)
|
2015-08-17 20:41:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TestParseEnvWrongTypeMap validates that incorrectly attempting to unmarshal a
|
|
|
|
// string over existing map fails.
|
2023-04-29 15:08:47 +00:00
|
|
|
func (suite *ConfigSuite) TestParseEnvWrongTypeMap(c *check.C) {
|
2022-08-04 20:01:48 +00:00
|
|
|
os.Setenv("REGISTRY_STORAGE_SOMEDRIVER", "somestring")
|
2015-08-17 20:41:50 +00:00
|
|
|
|
|
|
|
_, err := Parse(bytes.NewReader([]byte(configYamlV0_1)))
|
2023-04-29 15:08:47 +00:00
|
|
|
c.Assert(err, check.NotNil)
|
2015-08-17 20:41:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TestParseEnvWrongTypeStruct validates that incorrectly attempting to
|
|
|
|
// unmarshal a string into a struct fails.
|
2023-04-29 15:08:47 +00:00
|
|
|
func (suite *ConfigSuite) TestParseEnvWrongTypeStruct(c *check.C) {
|
2015-08-17 20:41:50 +00:00
|
|
|
os.Setenv("REGISTRY_STORAGE_LOG", "somestring")
|
|
|
|
|
|
|
|
_, err := Parse(bytes.NewReader([]byte(configYamlV0_1)))
|
2023-04-29 15:08:47 +00:00
|
|
|
c.Assert(err, check.NotNil)
|
2015-08-17 20:41:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TestParseEnvWrongTypeSlice validates that incorrectly attempting to
|
|
|
|
// unmarshal a string into a slice fails.
|
2023-04-29 15:08:47 +00:00
|
|
|
func (suite *ConfigSuite) TestParseEnvWrongTypeSlice(c *check.C) {
|
2015-08-17 20:41:50 +00:00
|
|
|
os.Setenv("REGISTRY_LOG_HOOKS", "somestring")
|
|
|
|
|
|
|
|
_, err := Parse(bytes.NewReader([]byte(configYamlV0_1)))
|
2023-04-29 15:08:47 +00:00
|
|
|
c.Assert(err, check.NotNil)
|
2015-08-17 20:41:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TestParseEnvMany tests several environment variable overrides.
|
|
|
|
// The result is not checked - the goal of this test is to detect panics
|
|
|
|
// from misuse of reflection.
|
2023-04-29 15:08:47 +00:00
|
|
|
func (suite *ConfigSuite) TestParseEnvMany(c *check.C) {
|
2015-08-17 20:41:50 +00:00
|
|
|
os.Setenv("REGISTRY_VERSION", "0.1")
|
|
|
|
os.Setenv("REGISTRY_LOG_LEVEL", "debug")
|
|
|
|
os.Setenv("REGISTRY_LOG_FORMATTER", "json")
|
|
|
|
os.Setenv("REGISTRY_LOG_HOOKS", "json")
|
|
|
|
os.Setenv("REGISTRY_LOG_FIELDS", "abc: xyz")
|
|
|
|
os.Setenv("REGISTRY_LOG_HOOKS", "- type: asdf")
|
|
|
|
os.Setenv("REGISTRY_LOGLEVEL", "debug")
|
2022-08-04 20:01:48 +00:00
|
|
|
os.Setenv("REGISTRY_STORAGE", "somedriver")
|
2015-08-17 20:41:50 +00:00
|
|
|
os.Setenv("REGISTRY_AUTH_PARAMS", "param1: value1")
|
|
|
|
os.Setenv("REGISTRY_AUTH_PARAMS_VALUE2", "value2")
|
|
|
|
os.Setenv("REGISTRY_AUTH_PARAMS_VALUE2", "value2")
|
|
|
|
|
|
|
|
_, err := Parse(bytes.NewReader([]byte(configYamlV0_1)))
|
2023-04-29 15:08:47 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2015-08-17 20:41:50 +00:00
|
|
|
}
|
|
|
|
|
2023-04-29 15:08:47 +00:00
|
|
|
func checkStructs(c *check.C, t reflect.Type, structsChecked map[string]struct{}) {
|
2015-08-17 20:41:50 +00:00
|
|
|
for t.Kind() == reflect.Ptr || t.Kind() == reflect.Map || t.Kind() == reflect.Slice {
|
|
|
|
t = t.Elem()
|
|
|
|
}
|
|
|
|
|
|
|
|
if t.Kind() != reflect.Struct {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if _, present := structsChecked[t.String()]; present {
|
|
|
|
// Already checked this type
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
structsChecked[t.String()] = struct{}{}
|
|
|
|
|
|
|
|
byUpperCase := make(map[string]int)
|
|
|
|
for i := 0; i < t.NumField(); i++ {
|
|
|
|
sf := t.Field(i)
|
|
|
|
|
|
|
|
// Check that the yaml tag does not contain an _.
|
|
|
|
yamlTag := sf.Tag.Get("yaml")
|
|
|
|
if strings.Contains(yamlTag, "_") {
|
|
|
|
c.Fatalf("yaml field name includes _ character: %s", yamlTag)
|
|
|
|
}
|
|
|
|
upper := strings.ToUpper(sf.Name)
|
|
|
|
if _, present := byUpperCase[upper]; present {
|
|
|
|
c.Fatalf("field name collision in configuration object: %s", sf.Name)
|
|
|
|
}
|
|
|
|
byUpperCase[upper] = i
|
|
|
|
|
|
|
|
checkStructs(c, sf.Type, structsChecked)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestValidateConfigStruct makes sure that the config struct has no members
|
|
|
|
// with yaml tags that would be ambiguous to the environment variable parser.
|
2023-04-29 15:08:47 +00:00
|
|
|
func (suite *ConfigSuite) TestValidateConfigStruct(c *check.C) {
|
2015-08-17 20:41:50 +00:00
|
|
|
structsChecked := make(map[string]struct{})
|
|
|
|
checkStructs(c, reflect.TypeOf(Configuration{}), structsChecked)
|
|
|
|
}
|
|
|
|
|
2014-10-27 23:16:19 +00:00
|
|
|
func copyConfig(config Configuration) *Configuration {
|
|
|
|
configCopy := new(Configuration)
|
|
|
|
|
2014-11-04 17:41:32 +00:00
|
|
|
configCopy.Version = MajorMinorVersion(config.Version.Major(), config.Version.Minor())
|
|
|
|
configCopy.Loglevel = config.Loglevel
|
2015-03-25 00:12:04 +00:00
|
|
|
configCopy.Log = config.Log
|
2023-03-31 11:16:29 +00:00
|
|
|
configCopy.Catalog = config.Catalog
|
2015-04-01 16:46:35 +00:00
|
|
|
configCopy.Log.Fields = make(map[string]interface{}, len(config.Log.Fields))
|
2015-03-25 00:12:04 +00:00
|
|
|
for k, v := range config.Log.Fields {
|
|
|
|
configCopy.Log.Fields[k] = v
|
|
|
|
}
|
|
|
|
|
2014-11-04 17:41:32 +00:00
|
|
|
configCopy.Storage = Storage{config.Storage.Type(): Parameters{}}
|
|
|
|
for k, v := range config.Storage.Parameters() {
|
|
|
|
configCopy.Storage.setParameter(k, v)
|
2014-10-27 23:16:19 +00:00
|
|
|
}
|
2014-12-13 02:05:37 +00:00
|
|
|
configCopy.Reporting = Reporting{
|
|
|
|
Bugsnag: BugsnagReporting{config.Reporting.Bugsnag.APIKey, config.Reporting.Bugsnag.ReleaseStage, config.Reporting.Bugsnag.Endpoint},
|
2015-04-14 20:16:10 +00:00
|
|
|
NewRelic: NewRelicReporting{config.Reporting.NewRelic.LicenseKey, config.Reporting.NewRelic.Name, config.Reporting.NewRelic.Verbose},
|
2014-12-13 02:05:37 +00:00
|
|
|
}
|
2014-10-27 23:16:19 +00:00
|
|
|
|
2014-12-18 20:30:19 +00:00
|
|
|
configCopy.Auth = Auth{config.Auth.Type(): Parameters{}}
|
|
|
|
for k, v := range config.Auth.Parameters() {
|
|
|
|
configCopy.Auth.setParameter(k, v)
|
|
|
|
}
|
|
|
|
|
2015-01-28 23:55:18 +00:00
|
|
|
configCopy.Notifications = Notifications{Endpoints: []Endpoint{}}
|
2019-02-05 00:01:04 +00:00
|
|
|
configCopy.Notifications.Endpoints = append(configCopy.Notifications.Endpoints, config.Notifications.Endpoints...)
|
2015-01-28 23:55:18 +00:00
|
|
|
|
2015-08-10 21:20:52 +00:00
|
|
|
configCopy.HTTP.Headers = make(http.Header)
|
|
|
|
for k, v := range config.HTTP.Headers {
|
|
|
|
configCopy.HTTP.Headers[k] = v
|
|
|
|
}
|
|
|
|
|
2023-08-04 07:04:43 +00:00
|
|
|
configCopy.Redis = config.Redis
|
|
|
|
|
2014-10-27 23:16:19 +00:00
|
|
|
return configCopy
|
|
|
|
}
|