From dd8eb6a8df76d82b5dc22f378640c33421ea0691 Mon Sep 17 00:00:00 2001 From: Stephen J Day Date: Mon, 1 Dec 2014 17:32:31 -0800 Subject: [PATCH] configuration.Parse should take io.Reader --- configuration/configuration.go | 20 +++++++++++++++++--- configuration/configuration_test.go | 24 ++++++++++++------------ 2 files changed, 29 insertions(+), 15 deletions(-) diff --git a/configuration/configuration.go b/configuration/configuration.go index 85d74d95..2d7e476b 100644 --- a/configuration/configuration.go +++ b/configuration/configuration.go @@ -2,6 +2,8 @@ package configuration import ( "fmt" + "io" + "io/ioutil" "os" "regexp" "strconv" @@ -21,6 +23,13 @@ type Configuration struct { // Storage is the configuration for the registry's storage driver Storage Storage `yaml:"storage"` + + // HTTP contains configuration parameters for the registry's http + // interface. + HTTP struct { + // Addr specifies the bind address for the registry instance. + Addr string `yaml:"addr"` + } `yaml:"http"` } // v0_1Configuration is a Version 0.1 Configuration struct @@ -178,16 +187,21 @@ type Parameters map[string]string // following the scheme below: // Configuration.Abc may be replaced by the value of REGISTRY_ABC, // Configuration.Abc.Xyz may be replaced by the value of REGISTRY_ABC_XYZ, and so forth -func Parse(in []byte) (*Configuration, error) { +func Parse(rd io.Reader) (*Configuration, error) { + in, err := ioutil.ReadAll(rd) + if err != nil { + return nil, err + } + var untypedConfig struct { Version Version } var config *Configuration - err := yaml.Unmarshal(in, &untypedConfig) - if err != nil { + if err := yaml.Unmarshal(in, &untypedConfig); err != nil { return nil, err } + if untypedConfig.Version == "" { return nil, fmt.Errorf("Please specify a configuration version. Current version is %s", CurrentVersion) } diff --git a/configuration/configuration_test.go b/configuration/configuration_test.go index 31d15b7a..0e227653 100644 --- a/configuration/configuration_test.go +++ b/configuration/configuration_test.go @@ -1,12 +1,12 @@ package configuration import ( + "bytes" "os" "testing" - "gopkg.in/yaml.v2" - . "gopkg.in/check.v1" + "gopkg.in/yaml.v2" ) // Hook up gocheck into the "go test" runner @@ -72,14 +72,14 @@ func (suite *ConfigSuite) SetUpTest(c *C) { func (suite *ConfigSuite) TestMarshalRoundtrip(c *C) { configBytes, err := yaml.Marshal(suite.expectedConfig) c.Assert(err, IsNil) - config, err := Parse(configBytes) + config, err := Parse(bytes.NewReader(configBytes)) c.Assert(err, IsNil) c.Assert(config, DeepEquals, suite.expectedConfig) } // TestParseSimple validates that configYamlV0_1 can be parsed into a struct matching configStruct func (suite *ConfigSuite) TestParseSimple(c *C) { - config, err := Parse([]byte(configYamlV0_1)) + config, err := Parse(bytes.NewReader([]byte(configYamlV0_1))) c.Assert(err, IsNil) c.Assert(config, DeepEquals, suite.expectedConfig) } @@ -89,7 +89,7 @@ func (suite *ConfigSuite) TestParseSimple(c *C) { func (suite *ConfigSuite) TestParseInmemory(c *C) { suite.expectedConfig.Storage = Storage{"inmemory": Parameters{}} - config, err := Parse([]byte(inmemoryConfigYamlV0_1)) + config, err := Parse(bytes.NewReader([]byte(inmemoryConfigYamlV0_1))) c.Assert(err, IsNil) c.Assert(config, DeepEquals, suite.expectedConfig) } @@ -100,7 +100,7 @@ func (suite *ConfigSuite) TestParseWithSameEnvStorage(c *C) { os.Setenv("REGISTRY_STORAGE", "s3") os.Setenv("REGISTRY_STORAGE_S3_REGION", "us-east-1") - config, err := Parse([]byte(configYamlV0_1)) + config, err := Parse(bytes.NewReader([]byte(configYamlV0_1))) c.Assert(err, IsNil) c.Assert(config, DeepEquals, suite.expectedConfig) } @@ -117,7 +117,7 @@ func (suite *ConfigSuite) TestParseWithDifferentEnvStorageParams(c *C) { os.Setenv("REGISTRY_STORAGE_S3_SECURE", "true") os.Setenv("REGISTRY_STORAGE_S3_NEWPARAM", "some Value") - config, err := Parse([]byte(configYamlV0_1)) + config, err := Parse(bytes.NewReader([]byte(configYamlV0_1))) c.Assert(err, IsNil) c.Assert(config, DeepEquals, suite.expectedConfig) } @@ -129,7 +129,7 @@ func (suite *ConfigSuite) TestParseWithDifferentEnvStorageType(c *C) { os.Setenv("REGISTRY_STORAGE", "inmemory") - config, err := Parse([]byte(configYamlV0_1)) + config, err := Parse(bytes.NewReader([]byte(configYamlV0_1))) c.Assert(err, IsNil) c.Assert(config, DeepEquals, suite.expectedConfig) } @@ -144,7 +144,7 @@ func (suite *ConfigSuite) TestParseWithDifferentEnvStorageTypeAndParams(c *C) { os.Setenv("REGISTRY_STORAGE", "filesystem") os.Setenv("REGISTRY_STORAGE_FILESYSTEM_ROOTDIRECTORY", "/tmp/testroot") - config, err := Parse([]byte(configYamlV0_1)) + config, err := Parse(bytes.NewReader([]byte(configYamlV0_1))) c.Assert(err, IsNil) c.Assert(config, DeepEquals, suite.expectedConfig) } @@ -154,7 +154,7 @@ func (suite *ConfigSuite) TestParseWithDifferentEnvStorageTypeAndParams(c *C) { func (suite *ConfigSuite) TestParseWithSameEnvLoglevel(c *C) { os.Setenv("REGISTRY_LOGLEVEL", "info") - config, err := Parse([]byte(configYamlV0_1)) + config, err := Parse(bytes.NewReader([]byte(configYamlV0_1))) c.Assert(err, IsNil) c.Assert(config, DeepEquals, suite.expectedConfig) } @@ -166,7 +166,7 @@ func (suite *ConfigSuite) TestParseWithDifferentEnvLoglevel(c *C) { os.Setenv("REGISTRY_LOGLEVEL", "error") - config, err := Parse([]byte(configYamlV0_1)) + config, err := Parse(bytes.NewReader([]byte(configYamlV0_1))) c.Assert(err, IsNil) c.Assert(config, DeepEquals, suite.expectedConfig) } @@ -177,7 +177,7 @@ func (suite *ConfigSuite) TestParseInvalidVersion(c *C) { suite.expectedConfig.Version = MajorMinorVersion(CurrentVersion.Major(), CurrentVersion.Minor()+1) configBytes, err := yaml.Marshal(suite.expectedConfig) c.Assert(err, IsNil) - _, err = Parse(configBytes) + _, err = Parse(bytes.NewReader(configBytes)) c.Assert(err, NotNil) }