Merge lp:~chipaca/ubuntu-push/networkmanager into lp:ubuntu-push

Proposed by John Lenton
Status: Merged
Approved by: John Lenton
Approved revision: 15
Merged at revision: 11
Proposed branch: lp:~chipaca/ubuntu-push/networkmanager
Merge into: lp:ubuntu-push
Prerequisite: lp:~chipaca/ubuntu-push/simple-bus-interface
Diff against target: 285 lines (+240/-3)
5 files modified
bus/testing/testing_bus.go (+2/-2)
bus/testing/testing_endpoint.go (+1/-1)
networkmanager/networkmanager.go (+85/-0)
networkmanager/networkmanager_test.go (+100/-0)
networkmanager/state.go (+52/-0)
To merge this branch: bzr merge lp:~chipaca/ubuntu-push/networkmanager
Reviewer Review Type Date Requested Status
Samuele Pedroni Approve
Review via email: mp+202242@code.launchpad.net

Commit message

A simplified, testable, Network Manager wrapper

Description of the change

A very small, partial, wrapper around network manager.

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

Merged simple-bus-interface into networkmanager.

Revision history for this message
Samuele Pedroni (pedronis) wrote :

+ return NetworkManager{conn, log}

the struct is big enough that that should return a pointer and the methods be on pointers

218 +func (nm State) String() string {

nm should be state there

the const group ins state, NetworkManager and New need docs

according to coverage this is not hit by tests:

67 + func() { close(ch) })

13. By John Lenton

Merged simple-bus-interface into networkmanager.

14. By John Lenton

addressed issues raised by pedronis during peer review

15. By John Lenton

Merged simple-bus-interface into networkmanager.

Revision history for this message
Samuele Pedroni (pedronis) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'bus/testing/testing_bus.go'
--- bus/testing/testing_bus.go 2014-01-20 13:45:52 +0000
+++ bus/testing/testing_bus.go 2014-01-20 13:45:52 +0000
@@ -33,13 +33,13 @@
3333
34type testingBus struct {34type testingBus struct {
35 TestCond condition.Interface35 TestCond condition.Interface
36 TestEndp *testingEndpoint36 TestEndp bus.Endpoint
37}37}
3838
39// Build a bus.Bus that takes a condition to determine whether it should work,39// Build a bus.Bus that takes a condition to determine whether it should work,
40// as well as a condition and series of return values for the testing40// as well as a condition and series of return values for the testing
41// bus.Endpoint it builds.41// bus.Endpoint it builds.
42func NewTestingBus(clientTC condition.Interface, busTC condition.Interface, retvals ...interface{}) *testingBus {42func NewTestingBus(clientTC condition.Interface, busTC condition.Interface, retvals ...interface{}) bus.Bus {
43 return &testingBus{clientTC, NewTestingEndpoint(busTC, retvals...)}43 return &testingBus{clientTC, NewTestingEndpoint(busTC, retvals...)}
44}44}
4545
4646
=== modified file 'bus/testing/testing_endpoint.go'
--- bus/testing/testing_endpoint.go 2014-01-20 13:45:52 +0000
+++ bus/testing/testing_endpoint.go 2014-01-20 13:45:52 +0000
@@ -35,7 +35,7 @@
35//35//
36// NOTE: Call() always returns the first return value; Watch() will provide36// NOTE: Call() always returns the first return value; Watch() will provide
37// each of them intern, irrespective of whether Call has been called.37// each of them intern, irrespective of whether Call has been called.
38func NewTestingEndpoint(cond condition.Interface, retvals ...interface{}) *testingEndpoint {38func NewTestingEndpoint(cond condition.Interface, retvals ...interface{}) bus.Endpoint {
39 return &testingEndpoint{cond, retvals}39 return &testingEndpoint{cond, retvals}
40}40}
4141
4242
=== added directory 'networkmanager'
=== added file 'networkmanager/networkmanager.go'
--- networkmanager/networkmanager.go 1970-01-01 00:00:00 +0000
+++ networkmanager/networkmanager.go 2014-01-20 13:45:52 +0000
@@ -0,0 +1,85 @@
1/*
2 Copyright 2013-2014 Canonical Ltd.
3
4 This program is free software: you can redistribute it and/or modify it
5 under the terms of the GNU General Public License version 3, as published
6 by the Free Software Foundation.
7
8 This program is distributed in the hope that it will be useful, but
9 WITHOUT ANY WARRANTY; without even the implied warranties of
10 MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11 PURPOSE. See the GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License along
14 with this program. If not, see <http://www.gnu.org/licenses/>.
15*/
16
17// Package networkmanager wraps a couple of NetworkManager's DBus API points:
18// the org.freedesktop.NetworkManager.state call, and listening for the
19// StateChange signal.
20package networkmanager
21
22import (
23 "launchpad.net/ubuntu-push/bus"
24 "launchpad.net/ubuntu-push/logger"
25)
26
27// NetworkManager lives on a well-knwon bus.Address
28var BusAddress bus.Address = bus.Address{
29 Interface: "org.freedesktop.NetworkManager",
30 Path: "/org/freedesktop/NetworkManager",
31 Name: "org.freedesktop.NetworkManager",
32}
33
34/*****************************************************************
35 * NetworkManager (and its implementation)
36 */
37
38type NetworkManager interface {
39 // GetState fetches and returns NetworkManager's current state
40 GetState() State
41 // WatchState listens for changes to NetworkManager's state, and sends
42 // them out over the channel returned.
43 WatchState() (<-chan State, error)
44}
45
46type networkManager struct {
47 bus bus.Endpoint
48 log logger.Logger
49}
50
51// New returns a new NetworkManager that'll use the provided bus.Endpoint
52func New(endp bus.Endpoint, log logger.Logger) NetworkManager {
53 return &networkManager{endp, log}
54}
55
56var _ NetworkManager = &networkManager{}
57
58/*
59 public methods
60*/
61
62func (nm *networkManager) GetState() State {
63 s, err := nm.bus.Call("state")
64
65 if err != nil {
66 nm.log.Errorf("Failed gettting current state: %s", err)
67 nm.log.Debugf("Defaulting state to Unknown")
68 return Unknown
69 }
70
71 return State(s.(uint32))
72}
73
74func (nm *networkManager) WatchState() (<-chan State, error) {
75 ch := make(chan State)
76 err := nm.bus.WatchSignal("StateChanged",
77 func(n interface{}) { ch <- State(n.(uint32)) },
78 func() { close(ch) })
79 if err != nil {
80 nm.log.Debugf("Failed to set up the watch: %s", err)
81 return nil, err
82 }
83
84 return ch, nil
85}
086
=== added file 'networkmanager/networkmanager_test.go'
--- networkmanager/networkmanager_test.go 1970-01-01 00:00:00 +0000
+++ networkmanager/networkmanager_test.go 2014-01-20 13:45:52 +0000
@@ -0,0 +1,100 @@
1/*
2 Copyright 2013-2014 Canonical Ltd.
3
4 This program is free software: you can redistribute it and/or modify it
5 under the terms of the GNU General Public License version 3, as published
6 by the Free Software Foundation.
7
8 This program is distributed in the hope that it will be useful, but
9 WITHOUT ANY WARRANTY; without even the implied warranties of
10 MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11 PURPOSE. See the GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License along
14 with this program. If not, see <http://www.gnu.org/licenses/>.
15*/
16
17package networkmanager
18
19import (
20 "io/ioutil"
21 . "launchpad.net/gocheck"
22 testingbus "launchpad.net/ubuntu-push/bus/testing"
23 "launchpad.net/ubuntu-push/logger"
24 "launchpad.net/ubuntu-push/testing/condition"
25 "testing"
26)
27
28// hook up gocheck
29func Test(t *testing.T) { TestingT(t) }
30
31type NMSuite struct{}
32
33var _ = Suite(&NMSuite{})
34
35var nullog = logger.NewSimpleLogger(ioutil.Discard, "error")
36
37// TestNames checks that networkmanager.State objects serialize
38// correctly, to a point.
39func (s *NMSuite) TestNames(c *C) {
40 var i State
41 for i = 0; i < _max_state; i += 10 {
42 c.Check(names[i], Equals, i.String())
43 }
44 i = _max_state
45 c.Check(i.String(), Equals, "Unknown")
46}
47
48// TestNew doesn't test much at all. If this fails, all is wrong in the world.
49func (s *NMSuite) TestNew(c *C) {
50 nm := New(testingbus.NewTestingEndpoint(condition.Work(true)), nullog)
51 c.Check(nm, NotNil)
52}
53
54// GetState returns the right state when everything works
55func (s *NMSuite) TestGetState(c *C) {
56 nm := New(testingbus.NewTestingEndpoint(condition.Work(true), uint32(ConnectedGlobal)), nullog)
57 state := nm.GetState()
58 c.Check(state, Equals, ConnectedGlobal)
59}
60
61// GetState returns the right state when dbus fails
62func (s *NMSuite) TestGetStateFail(c *C) {
63 nm := New(testingbus.NewTestingEndpoint(condition.Work(false), uint32(ConnectedGlobal)), nullog)
64 state := nm.GetState()
65 c.Check(state, Equals, Unknown)
66}
67
68// GetState returns the right state when dbus works but delivers rubbish
69func (s *NMSuite) TestGetStateRubbish(c *C) {
70 nm := New(testingbus.NewTestingEndpoint(condition.Work(false), 42), nullog)
71 state := nm.GetState()
72 c.Check(state, Equals, Unknown)
73}
74
75// WatchState sends a stream of States over the channel
76func (s *NMSuite) TestWatchState(c *C) {
77 tc := testingbus.NewTestingEndpoint(condition.Work(true), uint32(Unknown), uint32(Asleep), uint32(ConnectedGlobal))
78 nm := New(tc, nullog)
79 ch, err := nm.WatchState()
80 c.Check(err, IsNil)
81 l := []State{<-ch, <-ch, <-ch}
82 c.Check(l, DeepEquals, []State{Unknown, Asleep, ConnectedGlobal})
83}
84
85// WatchState returns on error if the dbus call fails
86func (s *NMSuite) TestWatchStateFails(c *C) {
87 nm := New(testingbus.NewTestingEndpoint(condition.Work(false)), nullog)
88 _, err := nm.WatchState()
89 c.Check(err, NotNil)
90}
91
92// WatchState calls close on its channel when the watch bails
93func (s *NMSuite) TestWatchClosesOnWatchBail(c *C) {
94 tc := testingbus.NewTestingEndpoint(condition.Work(true))
95 nm := New(tc, nullog)
96 ch, err := nm.WatchState()
97 c.Check(err, IsNil)
98 _, ok := <-ch
99 c.Check(ok, Equals, false)
100}
0101
=== added file 'networkmanager/state.go'
--- networkmanager/state.go 1970-01-01 00:00:00 +0000
+++ networkmanager/state.go 2014-01-20 13:45:52 +0000
@@ -0,0 +1,52 @@
1/*
2 Copyright 2013-2014 Canonical Ltd.
3
4 This program is free software: you can redistribute it and/or modify it
5 under the terms of the GNU General Public License version 3, as published
6 by the Free Software Foundation.
7
8 This program is distributed in the hope that it will be useful, but
9 WITHOUT ANY WARRANTY; without even the implied warranties of
10 MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11 PURPOSE. See the GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License along
14 with this program. If not, see <http://www.gnu.org/licenses/>.
15*/
16
17package networkmanager
18
19type State uint32
20
21// the NetworkManager states, as per
22// https://wiki.gnome.org/Projects/NetworkManager/DBusInterface/LatestDBusAPI
23const (
24 Unknown State = iota * 10
25 Asleep
26 Disconnected
27 Disconnecting
28 Connecting
29 ConnectedLocal
30 ConnectedSite
31 ConnectedGlobal
32 _max_state
33)
34
35var names = map[State]string{
36 Unknown: "Unknown",
37 Asleep: "Asleep",
38 Disconnected: "Disconnected",
39 Disconnecting: "Disconnecting",
40 Connecting: "Connecting",
41 ConnectedLocal: "Connected Local",
42 ConnectedSite: "Connected Site",
43 ConnectedGlobal: "Connected Global",
44}
45
46// give its state a descriptive stringification
47func (state State) String() string {
48 if s, ok := names[state]; ok {
49 return s
50 }
51 return names[Unknown]
52}

Subscribers

People subscribed via source and target branches