Merge lp:~chipaca/ubuntu-push/nm-primary-connection into lp:ubuntu-push

Proposed by John Lenton
Status: Superseded
Proposed branch: lp:~chipaca/ubuntu-push/nm-primary-connection
Merge into: lp:ubuntu-push
Diff against target: 194 lines (+124/-4)
3 files modified
bus/endpoint.go (+5/-1)
bus/networkmanager/networkmanager.go (+50/-1)
bus/networkmanager/networkmanager_test.go (+69/-2)
To merge this branch: bzr merge lp:~chipaca/ubuntu-push/nm-primary-connection
Reviewer Review Type Date Requested Status
Ubuntu Push Hackers Pending
Review via email: mp+213520@code.launchpad.net

This proposal has been superseded by a proposal from 2014-04-01.

Description of the change

Add the bits to NetworkManager to track the PrimaryConnection.

Also caught a bug in a test of GetState, and fixed it.

To post a comment you must log in.
95. By John Lenton

added a couple more tests to WatchPrimaryConnection

96. By John Lenton

whoops, test name clash

97. By John Lenton

changes *of* PrimaryConnection, not changes *to* it. I think.

Unmerged revisions

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'bus/endpoint.go'
2--- bus/endpoint.go 2014-02-21 16:17:28 +0000
3+++ bus/endpoint.go 2014-03-31 17:25:45 +0000
4@@ -175,7 +175,11 @@
5
6 // unpackOneMsg unpacks the value from the response msg
7 func (endp *endpoint) unpackOneMsg(msg *dbus.Message, member string) []interface{} {
8- return msg.AllArgs()
9+ var varmap map[string]dbus.Variant
10+ if err := msg.Args(&varmap); err != nil {
11+ return msg.AllArgs()
12+ }
13+ return []interface{}{varmap}
14 }
15
16 // unpackMessages unpacks the value from the watch
17
18=== modified file 'bus/networkmanager/networkmanager.go'
19--- bus/networkmanager/networkmanager.go 2014-01-21 13:21:19 +0000
20+++ bus/networkmanager/networkmanager.go 2014-03-31 17:25:45 +0000
21@@ -20,6 +20,8 @@
22 package networkmanager
23
24 import (
25+ "launchpad.net/go-dbus/v1"
26+
27 "launchpad.net/ubuntu-push/bus"
28 "launchpad.net/ubuntu-push/logger"
29 )
30@@ -41,6 +43,12 @@
31 // WatchState listens for changes to NetworkManager's state, and sends
32 // them out over the channel returned.
33 WatchState() (<-chan State, error)
34+ // GetPrimaryConnection fetches and returns NetworkManager's current
35+ // primary connection.
36+ GetPrimaryConnection() string
37+ // WatchPrimaryConnection listens for changes to NetworkManager's
38+ // Primary Connection, and sends it out over the channel returned.
39+ WatchPrimaryConnection() (<-chan string, error)
40 }
41
42 type networkManager struct {
43@@ -68,7 +76,13 @@
44 return Unknown
45 }
46
47- return State(s.(uint32))
48+ v, ok := s.(uint32)
49+ if !ok {
50+ nm.log.Errorf("Got weird state: %#v", s)
51+ return Unknown
52+ }
53+
54+ return State(v)
55 }
56
57 func (nm *networkManager) WatchState() (<-chan State, error) {
58@@ -83,3 +97,38 @@
59
60 return ch, nil
61 }
62+
63+func (nm *networkManager) GetPrimaryConnection() string {
64+ s, err := nm.bus.GetProperty("PrimaryConnection")
65+ if err != nil {
66+ nm.log.Errorf("Failed gettting current primary connection: %s", err)
67+ nm.log.Debugf("Defaulting primary connection to empty")
68+ return ""
69+ }
70+
71+ v, ok := s.(dbus.ObjectPath)
72+ if !ok {
73+ nm.log.Errorf("got weird PrimaryConnection: %#v", s)
74+ return ""
75+ }
76+
77+ return string(v)
78+}
79+
80+func (nm *networkManager) WatchPrimaryConnection() (<-chan string, error) {
81+ ch := make(chan string)
82+ err := nm.bus.WatchSignal("PropertiesChanged",
83+ func(ppsi ...interface{}) {
84+ pps := ppsi[0].(map[string]dbus.Variant)
85+ v, ok := pps["PrimaryConnection"]
86+ if ok {
87+ ch <- string(v.Value.(dbus.ObjectPath))
88+ }
89+ }, func() { close(ch) })
90+ if err != nil {
91+ nm.log.Debugf("Failed to set up the watch: %s", err)
92+ return nil, err
93+ }
94+
95+ return ch, nil
96+}
97
98=== modified file 'bus/networkmanager/networkmanager_test.go'
99--- bus/networkmanager/networkmanager_test.go 2014-02-05 18:17:26 +0000
100+++ bus/networkmanager/networkmanager_test.go 2014-03-31 17:25:45 +0000
101@@ -17,12 +17,15 @@
102 package networkmanager
103
104 import (
105+ "testing"
106+
107+ "launchpad.net/go-dbus/v1"
108 . "launchpad.net/gocheck"
109+
110 testingbus "launchpad.net/ubuntu-push/bus/testing"
111 "launchpad.net/ubuntu-push/logger"
112 helpers "launchpad.net/ubuntu-push/testing"
113 "launchpad.net/ubuntu-push/testing/condition"
114- "testing"
115 )
116
117 // hook up gocheck
118@@ -71,7 +74,7 @@
119
120 // GetState returns the right state when dbus works but delivers rubbish values
121 func (s *NMSuite) TestGetStateRubbishValues(c *C) {
122- nm := New(testingbus.NewTestingEndpoint(nil, condition.Work(false), 42), s.log)
123+ nm := New(testingbus.NewTestingEndpoint(nil, condition.Work(true), "Unknown"), s.log)
124 state := nm.GetState()
125 c.Check(state, Equals, Unknown)
126 }
127@@ -109,3 +112,67 @@
128 _, ok := <-ch
129 c.Check(ok, Equals, false)
130 }
131+
132+// GetPrimaryConnection returns the right state when everything works
133+func (s *NMSuite) TestGetPrimaryConnection(c *C) {
134+ nm := New(testingbus.NewTestingEndpoint(nil, condition.Work(true), dbus.ObjectPath("/a/1")), s.log)
135+ con := nm.GetPrimaryConnection()
136+ c.Check(con, Equals, "/a/1")
137+}
138+
139+// GetPrimaryConnection returns the right state when dbus fails
140+func (s *NMSuite) TestGetPrimaryConnectionFail(c *C) {
141+ nm := New(testingbus.NewTestingEndpoint(nil, condition.Work(false)), s.log)
142+ con := nm.GetPrimaryConnection()
143+ c.Check(con, Equals, "")
144+}
145+
146+// GetPrimaryConnection returns the right state when dbus works but delivers rubbish values
147+func (s *NMSuite) TestGetPrimaryConnectionRubbishValues(c *C) {
148+ nm := New(testingbus.NewTestingEndpoint(nil, condition.Work(true), "broken"), s.log)
149+ con := nm.GetPrimaryConnection()
150+ c.Check(con, Equals, "")
151+}
152+
153+// GetPrimaryConnection returns the right state when dbus works but delivers a rubbish structure
154+func (s *NMSuite) TestGetPrimaryConnectionRubbishStructure(c *C) {
155+ nm := New(testingbus.NewMultiValuedTestingEndpoint(nil, condition.Work(true), []interface{}{}), s.log)
156+ con := nm.GetPrimaryConnection()
157+ c.Check(con, Equals, "")
158+}
159+
160+func mkPriConMap(priCon string) map[string]dbus.Variant {
161+ m := make(map[string]dbus.Variant)
162+ m["PrimaryConnection"] = dbus.Variant{dbus.ObjectPath(priCon)}
163+ return m
164+}
165+
166+// WatchPrimaryConnection sends a stream of Connections over the channel
167+func (s *NMSuite) TestWatchPrimaryConnection(c *C) {
168+ tc := testingbus.NewTestingEndpoint(nil, condition.Work(true),
169+ mkPriConMap("/a/1"),
170+ mkPriConMap("/b/2"),
171+ mkPriConMap("/c/3"))
172+ nm := New(tc, s.log)
173+ ch, err := nm.WatchPrimaryConnection()
174+ c.Check(err, IsNil)
175+ l := []string{<-ch, <-ch, <-ch}
176+ c.Check(l, DeepEquals, []string{"/a/1", "/b/2", "/c/3"})
177+}
178+
179+// WatchPrimaryConnection returns on error if the dbus call fails
180+func (s *NMSuite) TestWatchPrimaryConnectionFails(c *C) {
181+ nm := New(testingbus.NewTestingEndpoint(nil, condition.Work(false)), s.log)
182+ _, err := nm.WatchPrimaryConnection()
183+ c.Check(err, NotNil)
184+}
185+
186+// WatchPrimaryConnection calls close on its channel when the watch bails
187+func (s *NMSuite) TestWatchClosesOnWatchBail(c *C) {
188+ tc := testingbus.NewTestingEndpoint(nil, condition.Work(true))
189+ nm := New(tc, s.log)
190+ ch, err := nm.WatchPrimaryConnection()
191+ c.Check(err, IsNil)
192+ _, ok := <-ch
193+ c.Check(ok, Equals, false)
194+}

Subscribers

People subscribed via source and target branches