configuration.Parse should take io.Reader

This commit is contained in:
Stephen J Day 2014-12-01 17:32:31 -08:00
parent e6e0219065
commit dd8eb6a8df
2 changed files with 29 additions and 15 deletions

View file

@ -2,6 +2,8 @@ package configuration
import ( import (
"fmt" "fmt"
"io"
"io/ioutil"
"os" "os"
"regexp" "regexp"
"strconv" "strconv"
@ -21,6 +23,13 @@ type Configuration struct {
// Storage is the configuration for the registry's storage driver // Storage is the configuration for the registry's storage driver
Storage Storage `yaml:"storage"` 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 // v0_1Configuration is a Version 0.1 Configuration struct
@ -178,16 +187,21 @@ type Parameters map[string]string
// following the scheme below: // following the scheme below:
// Configuration.Abc may be replaced by the value of REGISTRY_ABC, // 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 // 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 { var untypedConfig struct {
Version Version Version Version
} }
var config *Configuration var config *Configuration
err := yaml.Unmarshal(in, &untypedConfig) if err := yaml.Unmarshal(in, &untypedConfig); err != nil {
if err != nil {
return nil, err return nil, err
} }
if untypedConfig.Version == "" { if untypedConfig.Version == "" {
return nil, fmt.Errorf("Please specify a configuration version. Current version is %s", CurrentVersion) return nil, fmt.Errorf("Please specify a configuration version. Current version is %s", CurrentVersion)
} }

View file

@ -1,12 +1,12 @@
package configuration package configuration
import ( import (
"bytes"
"os" "os"
"testing" "testing"
"gopkg.in/yaml.v2"
. "gopkg.in/check.v1" . "gopkg.in/check.v1"
"gopkg.in/yaml.v2"
) )
// Hook up gocheck into the "go test" runner // Hook up gocheck into the "go test" runner
@ -72,14 +72,14 @@ func (suite *ConfigSuite) SetUpTest(c *C) {
func (suite *ConfigSuite) TestMarshalRoundtrip(c *C) { func (suite *ConfigSuite) TestMarshalRoundtrip(c *C) {
configBytes, err := yaml.Marshal(suite.expectedConfig) configBytes, err := yaml.Marshal(suite.expectedConfig)
c.Assert(err, IsNil) c.Assert(err, IsNil)
config, err := Parse(configBytes) config, err := Parse(bytes.NewReader(configBytes))
c.Assert(err, IsNil) c.Assert(err, IsNil)
c.Assert(config, DeepEquals, suite.expectedConfig) c.Assert(config, DeepEquals, suite.expectedConfig)
} }
// TestParseSimple validates that configYamlV0_1 can be parsed into a struct matching configStruct // TestParseSimple validates that configYamlV0_1 can be parsed into a struct matching configStruct
func (suite *ConfigSuite) TestParseSimple(c *C) { 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(err, IsNil)
c.Assert(config, DeepEquals, suite.expectedConfig) c.Assert(config, DeepEquals, suite.expectedConfig)
} }
@ -89,7 +89,7 @@ func (suite *ConfigSuite) TestParseSimple(c *C) {
func (suite *ConfigSuite) TestParseInmemory(c *C) { func (suite *ConfigSuite) TestParseInmemory(c *C) {
suite.expectedConfig.Storage = Storage{"inmemory": Parameters{}} 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(err, IsNil)
c.Assert(config, DeepEquals, suite.expectedConfig) 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")
os.Setenv("REGISTRY_STORAGE_S3_REGION", "us-east-1") 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(err, IsNil)
c.Assert(config, DeepEquals, suite.expectedConfig) 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_SECURE", "true")
os.Setenv("REGISTRY_STORAGE_S3_NEWPARAM", "some Value") 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(err, IsNil)
c.Assert(config, DeepEquals, suite.expectedConfig) c.Assert(config, DeepEquals, suite.expectedConfig)
} }
@ -129,7 +129,7 @@ func (suite *ConfigSuite) TestParseWithDifferentEnvStorageType(c *C) {
os.Setenv("REGISTRY_STORAGE", "inmemory") 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(err, IsNil)
c.Assert(config, DeepEquals, suite.expectedConfig) 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")
os.Setenv("REGISTRY_STORAGE_FILESYSTEM_ROOTDIRECTORY", "/tmp/testroot") 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(err, IsNil)
c.Assert(config, DeepEquals, suite.expectedConfig) c.Assert(config, DeepEquals, suite.expectedConfig)
} }
@ -154,7 +154,7 @@ func (suite *ConfigSuite) TestParseWithDifferentEnvStorageTypeAndParams(c *C) {
func (suite *ConfigSuite) TestParseWithSameEnvLoglevel(c *C) { func (suite *ConfigSuite) TestParseWithSameEnvLoglevel(c *C) {
os.Setenv("REGISTRY_LOGLEVEL", "info") 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(err, IsNil)
c.Assert(config, DeepEquals, suite.expectedConfig) c.Assert(config, DeepEquals, suite.expectedConfig)
} }
@ -166,7 +166,7 @@ func (suite *ConfigSuite) TestParseWithDifferentEnvLoglevel(c *C) {
os.Setenv("REGISTRY_LOGLEVEL", "error") 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(err, IsNil)
c.Assert(config, DeepEquals, suite.expectedConfig) 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) suite.expectedConfig.Version = MajorMinorVersion(CurrentVersion.Major(), CurrentVersion.Minor()+1)
configBytes, err := yaml.Marshal(suite.expectedConfig) configBytes, err := yaml.Marshal(suite.expectedConfig)
c.Assert(err, IsNil) c.Assert(err, IsNil)
_, err = Parse(configBytes) _, err = Parse(bytes.NewReader(configBytes))
c.Assert(err, NotNil) c.Assert(err, NotNil)
} }