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"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
. "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
|
|
|
|
func Test(t *testing.T) { TestingT(t) }
|
|
|
|
|
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 {
|
|
|
|
Level Loglevel `yaml:"level"`
|
|
|
|
Formatter string `yaml:"formatter"`
|
|
|
|
Fields map[string]string `yaml:"fields"`
|
|
|
|
}{
|
|
|
|
Fields: map[string]string{"environment": "test"},
|
|
|
|
},
|
2014-11-04 17:41:32 +00:00
|
|
|
Loglevel: "info",
|
|
|
|
Storage: Storage{
|
|
|
|
"s3": Parameters{
|
|
|
|
"region": "us-east-1",
|
|
|
|
"bucket": "my-bucket",
|
|
|
|
"rootpath": "/registry",
|
2014-12-18 03:06:55 +00:00
|
|
|
"encrypt": true,
|
|
|
|
"secure": false,
|
2014-11-04 17:41:32 +00:00
|
|
|
"accesskey": "SAMPLEACCESSKEY",
|
|
|
|
"secretkey": "SUPERSECRET",
|
2014-12-18 03:06:55 +00:00
|
|
|
"host": nil,
|
|
|
|
"port": 42,
|
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>"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
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:
|
|
|
|
fields:
|
|
|
|
environment: test
|
2014-11-04 17:41:32 +00:00
|
|
|
loglevel: info
|
|
|
|
storage:
|
|
|
|
s3:
|
|
|
|
region: us-east-1
|
|
|
|
bucket: my-bucket
|
|
|
|
rootpath: /registry
|
|
|
|
encrypt: true
|
|
|
|
secure: false
|
|
|
|
accesskey: SAMPLEACCESSKEY
|
|
|
|
secretkey: SUPERSECRET
|
|
|
|
host: ~
|
2014-12-18 03:06:55 +00:00
|
|
|
port: 42
|
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>]
|
2014-12-13 02:05:37 +00:00
|
|
|
reporting:
|
|
|
|
bugsnag:
|
|
|
|
apikey: BugsnagApiKey
|
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
|
|
|
|
loglevel: info
|
|
|
|
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>]
|
2014-10-27 23:16:19 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
type ConfigSuite struct {
|
|
|
|
expectedConfig *Configuration
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ = Suite(new(ConfigSuite))
|
|
|
|
|
|
|
|
func (suite *ConfigSuite) SetUpTest(c *C) {
|
|
|
|
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
|
2014-10-27 23:16:19 +00:00
|
|
|
func (suite *ConfigSuite) TestMarshalRoundtrip(c *C) {
|
|
|
|
configBytes, err := yaml.Marshal(suite.expectedConfig)
|
|
|
|
c.Assert(err, IsNil)
|
2014-12-02 01:32:31 +00:00
|
|
|
config, err := Parse(bytes.NewReader(configBytes))
|
2014-10-27 23:16:19 +00:00
|
|
|
c.Assert(err, IsNil)
|
|
|
|
c.Assert(config, DeepEquals, suite.expectedConfig)
|
|
|
|
}
|
|
|
|
|
2014-12-17 22:18:58 +00:00
|
|
|
// TestParseSimple validates that configYamlV0_1 can be parsed into a struct
|
|
|
|
// matching configStruct
|
2014-10-27 23:16:19 +00:00
|
|
|
func (suite *ConfigSuite) TestParseSimple(c *C) {
|
2014-12-02 01:32:31 +00:00
|
|
|
config, err := Parse(bytes.NewReader([]byte(configYamlV0_1)))
|
2014-10-27 23:16:19 +00:00
|
|
|
c.Assert(err, IsNil)
|
|
|
|
c.Assert(config, DeepEquals, suite.expectedConfig)
|
|
|
|
}
|
|
|
|
|
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
|
2014-11-04 17:41:32 +00:00
|
|
|
func (suite *ConfigSuite) TestParseInmemory(c *C) {
|
|
|
|
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
|
2014-11-04 17:41:32 +00:00
|
|
|
|
2014-12-02 01:32:31 +00:00
|
|
|
config, err := Parse(bytes.NewReader([]byte(inmemoryConfigYamlV0_1)))
|
2014-11-04 17:41:32 +00:00
|
|
|
c.Assert(err, IsNil)
|
|
|
|
c.Assert(config, DeepEquals, suite.expectedConfig)
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
func (suite *ConfigSuite) TestParseIncomplete(c *C) {
|
|
|
|
incompleteConfigYaml := "version: 0.1"
|
|
|
|
_, err := Parse(bytes.NewReader([]byte(incompleteConfigYaml)))
|
|
|
|
c.Assert(err, NotNil)
|
|
|
|
|
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{}
|
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)))
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
c.Assert(config, DeepEquals, suite.expectedConfig)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestParseWithSameEnvStorage validates that providing environment variables
|
|
|
|
// that match the given storage type will only include environment-defined
|
|
|
|
// parameters and remove yaml-defined parameters
|
2014-10-27 23:16:19 +00:00
|
|
|
func (suite *ConfigSuite) TestParseWithSameEnvStorage(c *C) {
|
2014-12-17 22:18:58 +00:00
|
|
|
suite.expectedConfig.Storage = Storage{"s3": Parameters{"region": "us-east-1"}}
|
|
|
|
|
2014-10-27 23:16:19 +00:00
|
|
|
os.Setenv("REGISTRY_STORAGE", "s3")
|
|
|
|
os.Setenv("REGISTRY_STORAGE_S3_REGION", "us-east-1")
|
|
|
|
|
2014-12-02 01:32:31 +00:00
|
|
|
config, err := Parse(bytes.NewReader([]byte(configYamlV0_1)))
|
2014-10-27 23:16:19 +00:00
|
|
|
c.Assert(err, IsNil)
|
|
|
|
c.Assert(config, DeepEquals, suite.expectedConfig)
|
|
|
|
}
|
|
|
|
|
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
|
2014-10-27 23:16:19 +00:00
|
|
|
func (suite *ConfigSuite) TestParseWithDifferentEnvStorageParams(c *C) {
|
2014-11-04 17:41:32 +00:00
|
|
|
suite.expectedConfig.Storage.setParameter("region", "us-west-1")
|
2014-12-18 03:06:55 +00:00
|
|
|
suite.expectedConfig.Storage.setParameter("secure", true)
|
2014-11-04 17:41:32 +00:00
|
|
|
suite.expectedConfig.Storage.setParameter("newparam", "some Value")
|
2014-10-27 23:16:19 +00:00
|
|
|
|
|
|
|
os.Setenv("REGISTRY_STORAGE_S3_REGION", "us-west-1")
|
|
|
|
os.Setenv("REGISTRY_STORAGE_S3_SECURE", "true")
|
|
|
|
os.Setenv("REGISTRY_STORAGE_S3_NEWPARAM", "some Value")
|
|
|
|
|
2014-12-02 01:32:31 +00:00
|
|
|
config, err := Parse(bytes.NewReader([]byte(configYamlV0_1)))
|
2014-10-27 23:16:19 +00:00
|
|
|
c.Assert(err, IsNil)
|
|
|
|
c.Assert(config, DeepEquals, suite.expectedConfig)
|
|
|
|
}
|
|
|
|
|
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
|
2014-10-27 23:16:19 +00:00
|
|
|
func (suite *ConfigSuite) TestParseWithDifferentEnvStorageType(c *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)))
|
2014-10-27 23:16:19 +00:00
|
|
|
c.Assert(err, IsNil)
|
|
|
|
c.Assert(config, DeepEquals, suite.expectedConfig)
|
|
|
|
}
|
|
|
|
|
2015-01-21 20:02:51 +00:00
|
|
|
// TestParseWithExtraneousEnvStorageParams validates that environment variables
|
|
|
|
// that change parameters out of the scope of the specified storage type are
|
|
|
|
// ignored.
|
|
|
|
func (suite *ConfigSuite) TestParseWithExtraneousEnvStorageParams(c *C) {
|
|
|
|
os.Setenv("REGISTRY_STORAGE_FILESYSTEM_ROOTDIRECTORY", "/tmp/testroot")
|
|
|
|
|
|
|
|
config, err := Parse(bytes.NewReader([]byte(configYamlV0_1)))
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
c.Assert(config, DeepEquals, suite.expectedConfig)
|
|
|
|
}
|
|
|
|
|
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
|
2014-10-27 23:16:19 +00:00
|
|
|
func (suite *ConfigSuite) TestParseWithDifferentEnvStorageTypeAndParams(c *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)))
|
2014-10-27 23:16:19 +00:00
|
|
|
c.Assert(err, IsNil)
|
|
|
|
c.Assert(config, DeepEquals, suite.expectedConfig)
|
|
|
|
}
|
|
|
|
|
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
|
2014-10-27 23:16:19 +00:00
|
|
|
func (suite *ConfigSuite) TestParseWithSameEnvLoglevel(c *C) {
|
|
|
|
os.Setenv("REGISTRY_LOGLEVEL", "info")
|
|
|
|
|
2014-12-02 01:32:31 +00:00
|
|
|
config, err := Parse(bytes.NewReader([]byte(configYamlV0_1)))
|
2014-10-27 23:16:19 +00:00
|
|
|
c.Assert(err, IsNil)
|
|
|
|
c.Assert(config, DeepEquals, suite.expectedConfig)
|
|
|
|
}
|
|
|
|
|
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
|
2014-10-27 23:16:19 +00:00
|
|
|
func (suite *ConfigSuite) TestParseWithDifferentEnvLoglevel(c *C) {
|
2014-11-04 17:41:32 +00:00
|
|
|
suite.expectedConfig.Loglevel = "error"
|
2014-10-27 23:16:19 +00:00
|
|
|
|
|
|
|
os.Setenv("REGISTRY_LOGLEVEL", "error")
|
|
|
|
|
2014-12-02 01:32:31 +00:00
|
|
|
config, err := Parse(bytes.NewReader([]byte(configYamlV0_1)))
|
2014-10-27 23:16:19 +00:00
|
|
|
c.Assert(err, IsNil)
|
|
|
|
c.Assert(config, DeepEquals, suite.expectedConfig)
|
|
|
|
}
|
|
|
|
|
2014-12-17 22:18:58 +00:00
|
|
|
// TestParseInvalidLoglevel validates that the parser will fail to parse a
|
|
|
|
// configuration if the loglevel is malformed
|
|
|
|
func (suite *ConfigSuite) TestParseInvalidLoglevel(c *C) {
|
|
|
|
invalidConfigYaml := "version: 0.1\nloglevel: derp\nstorage: inmemory"
|
|
|
|
_, err := Parse(bytes.NewReader([]byte(invalidConfigYaml)))
|
|
|
|
c.Assert(err, NotNil)
|
|
|
|
|
|
|
|
os.Setenv("REGISTRY_LOGLEVEL", "derp")
|
|
|
|
|
|
|
|
_, err = Parse(bytes.NewReader([]byte(configYamlV0_1)))
|
|
|
|
c.Assert(err, NotNil)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestParseWithDifferentEnvReporting validates that environment variables
|
|
|
|
// properly override reporting parameters
|
2014-12-13 02:05:37 +00:00
|
|
|
func (suite *ConfigSuite) TestParseWithDifferentEnvReporting(c *C) {
|
|
|
|
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)))
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
c.Assert(config, DeepEquals, suite.expectedConfig)
|
|
|
|
}
|
|
|
|
|
2014-10-30 00:42:23 +00:00
|
|
|
// TestParseInvalidVersion validates that the parser will fail to parse a newer configuration
|
|
|
|
// version than the CurrentVersion
|
2014-10-27 23:16:19 +00:00
|
|
|
func (suite *ConfigSuite) TestParseInvalidVersion(c *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)
|
|
|
|
c.Assert(err, IsNil)
|
2014-12-02 01:32:31 +00:00
|
|
|
_, err = Parse(bytes.NewReader(configBytes))
|
2014-10-27 23:16:19 +00:00
|
|
|
c.Assert(err, NotNil)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
configCopy.Log.Fields = make(map[string]string, len(config.Log.Fields))
|
|
|
|
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},
|
|
|
|
NewRelic: NewRelicReporting{config.Reporting.NewRelic.LicenseKey, config.Reporting.NewRelic.Name},
|
|
|
|
}
|
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{}}
|
|
|
|
for _, v := range config.Notifications.Endpoints {
|
|
|
|
configCopy.Notifications.Endpoints = append(configCopy.Notifications.Endpoints, v)
|
|
|
|
}
|
|
|
|
|
2014-10-27 23:16:19 +00:00
|
|
|
return configCopy
|
|
|
|
}
|