Merge lp:~niemeyer/juju-core/fix-tools-marshaling into lp:~juju/juju-core/trunk

Proposed by Gustavo Niemeyer
Status: Merged
Merged at revision: 577
Proposed branch: lp:~niemeyer/juju-core/fix-tools-marshaling
Merge into: lp:~juju/juju-core/trunk
Diff against target: 84 lines (+43/-2)
2 files modified
state/state.go (+20/-0)
state/tools_test.go (+23/-2)
To merge this branch: bzr merge lp:~niemeyer/juju-core/fix-tools-marshaling
Reviewer Review Type Date Requested Status
The Go Language Gophers Pending
Review via email: mp+126363@code.launchpad.net

Description of the change

state: fix tools marshaling

https://codereview.appspot.com/6573050/

To post a comment you must log in.
Revision history for this message
Gustavo Niemeyer (niemeyer) wrote :

Reviewers: mp+126363_code.launchpad.net,

Message:
Please take a look.

Description:
state: fix tools marshaling

https://code.launchpad.net/~niemeyer/juju-core/fix-tools-marshaling/+merge/126363

(do not edit description out of merge proposal)

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

Affected files:
   A [revision details]
   M state/state.go
   M state/tools_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: state/state.go
=== modified file 'state/state.go'
--- state/state.go 2012-09-25 17:34:03 +0000
+++ state/state.go 2012-09-26 02:02:34 +0000
@@ -18,6 +18,7 @@
   "regexp"
  )

+// TODO(niemeyer): This must not be exported.
  type D []bson.DocElem

  // Tools describes a particular set of juju tools and where to find them.
@@ -26,6 +27,25 @@
   URL string
  }

+type toolsDoc struct {
+ Version version.Binary
+ URL string
+}
+
+func (t *Tools) GetBSON() (interface{}, error) {
+ return &toolsDoc{t.Binary, t.URL}, nil
+}
+
+func (t *Tools) SetBSON(raw bson.Raw) error {
+ var doc toolsDoc
+ if err := raw.Unmarshal(&doc); err != nil {
+ return err
+ }
+ t.Binary = doc.Version
+ t.URL = doc.URL
+ return nil
+}
+
  var (
   validService =
regexp.MustCompile("^[a-z][a-z0-9]*(-[a-z0-9]*[a-z][a-z0-9]*)*$")
   validUnit =
regexp.MustCompile("^[a-z][a-z0-9]*(-[a-z0-9]*[a-z][a-z0-9]*)*/[0-9]+$")

Index: state/tools_test.go
=== modified file 'state/tools_test.go'
--- state/tools_test.go 2012-09-21 20:31:39 +0000
+++ state/tools_test.go 2012-09-26 02:02:34 +0000
@@ -2,6 +2,7 @@

  import (
   "fmt"
+ "labix.org/v2/mgo/bson"
   . "launchpad.net/gocheck"
   "launchpad.net/juju-core/state"
   "launchpad.net/juju-core/version"
@@ -30,8 +31,6 @@
  }

  func testAgentTools(c *C, obj tooler, agent string) {
- c.Skip("Marshalling of agent tools is currently broken")
-
   // object starts with zero'd tools.
   t, err := obj.AgentTools()
   c.Assert(err, IsNil)
@@ -70,3 +69,25 @@
   c.Assert(err, IsNil)
   testAgentTools(c, unit, `unit "wordpress/0"`)
  }
+
+func (s *ToolsSuite) TestMarshalUnmarshal(c *C) {
+ tools := newTools("7.8.9-foo-bar", "http://arble.tgz")
+ data, err := bson.Marshal(&tools)
+ c.Assert(err, IsNil)
+
+ // Check the exact document.
+ want := bson.M{
+ "version": tools.Binary.String(),
+ "url": tools.URL,
+ }
+ got := bson.M{}
+ err = bson.Unmarshal(data, &got)
+ c.Assert(err, IsNil)
+ c.Assert(got, DeepEquals, want)
+
+ // Check that it unpacks properly too.
+ var t state.Tools
+ err = bson.Unmarshal(data, &t)
+ c.Assert(err, IsNil)
+ c.Assert(t, Equals, *tools)
+}

Revision history for this message
Gustavo Niemeyer (niemeyer) wrote :

*** Submitted:

state: fix tools marshaling

R=dfc
CC=
https://codereview.appspot.com/6573050

https://codereview.appspot.com/6573050/

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'state/state.go'
2--- state/state.go 2012-09-25 17:34:03 +0000
3+++ state/state.go 2012-09-26 02:04:24 +0000
4@@ -18,6 +18,7 @@
5 "regexp"
6 )
7
8+// TODO(niemeyer): This must not be exported.
9 type D []bson.DocElem
10
11 // Tools describes a particular set of juju tools and where to find them.
12@@ -26,6 +27,25 @@
13 URL string
14 }
15
16+type toolsDoc struct {
17+ Version version.Binary
18+ URL string
19+}
20+
21+func (t *Tools) GetBSON() (interface{}, error) {
22+ return &toolsDoc{t.Binary, t.URL}, nil
23+}
24+
25+func (t *Tools) SetBSON(raw bson.Raw) error {
26+ var doc toolsDoc
27+ if err := raw.Unmarshal(&doc); err != nil {
28+ return err
29+ }
30+ t.Binary = doc.Version
31+ t.URL = doc.URL
32+ return nil
33+}
34+
35 var (
36 validService = regexp.MustCompile("^[a-z][a-z0-9]*(-[a-z0-9]*[a-z][a-z0-9]*)*$")
37 validUnit = regexp.MustCompile("^[a-z][a-z0-9]*(-[a-z0-9]*[a-z][a-z0-9]*)*/[0-9]+$")
38
39=== modified file 'state/tools_test.go'
40--- state/tools_test.go 2012-09-21 20:31:39 +0000
41+++ state/tools_test.go 2012-09-26 02:04:24 +0000
42@@ -2,6 +2,7 @@
43
44 import (
45 "fmt"
46+ "labix.org/v2/mgo/bson"
47 . "launchpad.net/gocheck"
48 "launchpad.net/juju-core/state"
49 "launchpad.net/juju-core/version"
50@@ -30,8 +31,6 @@
51 }
52
53 func testAgentTools(c *C, obj tooler, agent string) {
54- c.Skip("Marshalling of agent tools is currently broken")
55-
56 // object starts with zero'd tools.
57 t, err := obj.AgentTools()
58 c.Assert(err, IsNil)
59@@ -70,3 +69,25 @@
60 c.Assert(err, IsNil)
61 testAgentTools(c, unit, `unit "wordpress/0"`)
62 }
63+
64+func (s *ToolsSuite) TestMarshalUnmarshal(c *C) {
65+ tools := newTools("7.8.9-foo-bar", "http://arble.tgz")
66+ data, err := bson.Marshal(&tools)
67+ c.Assert(err, IsNil)
68+
69+ // Check the exact document.
70+ want := bson.M{
71+ "version": tools.Binary.String(),
72+ "url": tools.URL,
73+ }
74+ got := bson.M{}
75+ err = bson.Unmarshal(data, &got)
76+ c.Assert(err, IsNil)
77+ c.Assert(got, DeepEquals, want)
78+
79+ // Check that it unpacks properly too.
80+ var t state.Tools
81+ err = bson.Unmarshal(data, &t)
82+ c.Assert(err, IsNil)
83+ c.Assert(t, Equals, *tools)
84+}

Subscribers

People subscribed via source and target branches