Code review comment for lp:~rogpeppe/juju-core/041-config-agent-version

Revision history for this message
Roger Peppe (rogpeppe) wrote :

Reviewers: mp+122016_code.launchpad.net,

Message:
Please take a look.

Description:
environs/config: add support for agent version.

Also adjust documentation for schema.Omit slightly
and add a test.

https://code.launchpad.net/~rogpeppe/juju-core/041-config-agent-version/+merge/122016

(do not edit description out of merge proposal)

Please review this at https://codereview.appspot.com/6499055/

Affected files:
   A [revision details]
   M environs/config/config.go
   M environs/config/config_test.go
   M schema/schema.go
   M schema/schema_test.go

Index: [revision details]
=== added file '[revision details]'
--- [revision details] 2012-01-01 00:00:00 +0000
+++ [revision details] 2012-01-01 00:00:00 +0000
@@ -0,0 +1,2 @@
+Old revision: <email address hidden>
+New revision: <email address hidden>

Index: schema/schema.go
=== modified file 'schema/schema.go'
--- schema/schema.go 2012-07-26 18:01:24 +0000
+++ schema/schema.go 2012-08-30 09:11:09 +0000
@@ -322,8 +322,8 @@
  // and returns with the underlying error.
  //
  // Fields in defaults will be set to the provided value if not present
-// in the coerced map. If the default value is schema.Omit, the field
-// missing field will be ommitted from the coerced map as well.
+// in the coerced map. If the default value is schema.Omit, the
+// missing field will be omitted from the coerced map.
  //
  // The coerced output value has type map[string]interface{}.
  func FieldMap(fields Fields, defaults Defaults) Checker {

Index: schema/schema_test.go
=== modified file 'schema/schema_test.go'
--- schema/schema_test.go 2012-07-26 18:08:04 +0000
+++ schema/schema_test.go 2012-08-30 09:11:09 +0000
@@ -285,6 +285,10 @@
   out, err :=
sch.Coerce(map[string]interface{}{"a": "A", "b": "B", "d": "D"}, aPath)
   c.Assert(err, IsNil)
   c.Assert(out, DeepEquals,
map[string]interface{}{"a": "A", "b": "B", "c": "C"})
+
+ out, err = sch.Coerce(map[string]interface{}{"a": "A", "d": "D"}, aPath)
+ c.Assert(err, IsNil)
+ c.Assert(out, DeepEquals, map[string]interface{}{"a": "A", "c": "C"})
  }

  func (s *S) TestFieldMapDefaultInvalid(c *C) {

Index: environs/config/config.go
=== modified file 'environs/config/config.go'
--- environs/config/config.go 2012-08-02 08:29:38 +0000
+++ environs/config/config.go 2012-08-30 09:11:09 +0000
@@ -47,6 +47,13 @@
    }
   }

+ // Check that the agent version parses ok if set.
+ if v, ok := c.m["agent-version"].(string); ok {
+ if _, err := version.Parse(v); err != nil {
+ return nil, fmt.Errorf("invalid agent version in environment
configuration: %q", v)
+ }
+ }
+
   // Copy unknown attributes onto the type-specific map.
   for k, v := range attrs {
    if _, ok := fields[k]; !ok {
@@ -76,6 +83,20 @@
   return c.m["authorized-keys"].(string)
  }

+// AgentVersion returns the proposed version number for the agent tools.
+// It returns the zero version if unset.
+func (c *Config) AgentVersion() version.Number {
+ v, ok := c.m["agent-version"].(string)
+ if !ok {
+ return version.Number{}
+ }
+ n, err := version.Parse(v)
+ if err != nil {
+ panic(err) // We should have checked it earlier.
+ }
+ return n
+}
+
  // UnknownAttrs returns a copy of the raw configuration attributes
  // that are supposedly specific to the environment type. They could
  // also be wrong attributes, though. Only the specific environment
@@ -112,12 +133,14 @@
   "default-series": schema.String(),
   "authorized-keys": schema.String(),
   "authorized-keys-path": schema.String(),
+ "agent-version": schema.String(),
  }

  var defaults = schema.Defaults{
   "default-series": version.Current.Series,
   "authorized-keys": "",
   "authorized-keys-path": "",
+ "agent-version": schema.Omit,
  }

  var checker = schema.FieldMap(fields, defaults)

Index: environs/config/config_test.go
=== modified file 'environs/config/config_test.go'
--- environs/config/config_test.go 2012-08-02 08:29:38 +0000
+++ environs/config/config_test.go 2012-08-30 09:11:09 +0000
@@ -70,6 +70,22 @@
    "",
   }, {
    attrs{
+ "type": "my-type",
+ "name": "my-name",
+ "authorized-keys": "my-keys",
+ "agent-version": "1.2.3",
+ },
+ "",
+ }, {
+ attrs{
+ "type": "my-type",
+ "name": "my-name",
+ "authorized-keys": "my-keys",
+ "agent-version": "2",
+ },
+ `invalid agent version in environment configuration: "2"`,
+ }, {
+ attrs{
     "name": "my-name",
    },
    "type: expected string, got nothing",
@@ -90,7 +106,8 @@
     "name": "",
    },
    "empty name in environment configuration",
- }}
+ },
+}

  func (*ConfigSuite) TestConfig(c *C) {
   homedir := c.MkDir()
@@ -114,7 +131,8 @@
    c.Assert(err, IsNil)
   }

- for _, test := range configTests {
+ for i, test := range configTests {
+ c.Logf("test %d", i)
    cfg, err := config.New(test.attrs)
    if test.err != "" {
     c.Assert(err, ErrorMatches, test.err)
@@ -127,7 +145,13 @@
    name, _ := test.attrs["name"].(string)
    c.Assert(cfg.Type(), Equals, typ)
    c.Assert(cfg.Name(), Equals, name)
-
+ if s := test.attrs["agent-version"]; s != nil {
+ vers, err := version.Parse(s.(string))
+ c.Assert(err, IsNil)
+ c.Assert(cfg.AgentVersion(), Equals, vers)
+ } else {
+ c.Assert(cfg.AgentVersion(), Equals, version.Number{})
+ }
    if series, _ := test.attrs["default-series"].(string); series != "" {
     c.Assert(cfg.DefaultSeries(), Equals, series)
    } else {

« Back to merge proposal