Merge lp:~mikemc/ubuntu-sso-client/fix-1003692 into lp:ubuntu-sso-client

Proposed by Mike McCracken
Status: Merged
Approved by: Mike McCracken
Approved revision: 962
Merged at revision: 962
Proposed branch: lp:~mikemc/ubuntu-sso-client/fix-1003692
Merge into: lp:ubuntu-sso-client
Diff against target: 169 lines (+48/-93)
2 files modified
ubuntu_sso/networkstate/linux.py (+4/-1)
ubuntu_sso/networkstate/tests/test_linux.py (+44/-92)
To merge this branch: bzr merge lp:~mikemc/ubuntu-sso-client/fix-1003692
Reviewer Review Type Date Requested Status
Manuel de la Peña (community) Approve
Alejandro J. Cura (community) Approve
Review via email: mp+107159@code.launchpad.net

Commit message

- Fixes "TypeError in got_state()" message and incorrect network detection on linux ( LP #1003692 )

Description of the change

- Fixes "TypeError in got_state()" message and incorrect network detection on linux ( LP #1003692 )

The linux version of is_machine_connected had an internal callback "got_state()", which expected an int while the NetworkManagerState will only ever give it one of {ONLINE, OFFLINE, UNKNOWN}. This branch fixes that callback.

The branch also adds tests for calling the callback with those three state values, and removes tests for calling the callback with int constants from NetworkManager that it will not be called with - they're only used inside NetworkManagerState.

Finally, it adds coverage in the tests of the NetworkManagerState class for three states from networkmanager that were not tested before - asleep, asleep_old, and unknown.

Note that the new tests say that the code should handle the "NetworkManager gave us state NM_UNKNOWN" case by returning "OFFLINE", not "UNKNOWN". This is to match the behavior of the code and its only client: syncdaemon's interaction_interfaces.py, where the state callback just compares the state to ONLINE and does treat UNKNOWN as offline, eg. in case of NetworkManager DBus error.

Tests pass on linux.

To post a comment you must log in.
Revision history for this message
Alejandro J. Cura (alecu) wrote :

Code looks good, all tests pass on Linux. +1

review: Approve
Revision history for this message
Manuel de la Peña (mandel) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'ubuntu_sso/networkstate/linux.py'
2--- ubuntu_sso/networkstate/linux.py 2012-04-23 12:22:03 +0000
3+++ ubuntu_sso/networkstate/linux.py 2012-05-24 05:14:40 +0000
4@@ -117,7 +117,10 @@
5
6 def got_state(state):
7 """The state was retrieved from the Network Manager."""
8- result = int(state) in NM_STATE_CONNECTED_LIST
9+ if type(state) is not type(ONLINE):
10+ logger.exception("bad callback argument in is_machine_connected")
11+ raise NetworkFailException()
12+ result = (state == ONLINE)
13 d.callback(result)
14
15 # pylint: disable=W0702, W0703
16
17=== modified file 'ubuntu_sso/networkstate/tests/test_linux.py'
18--- ubuntu_sso/networkstate/tests/test_linux.py 2012-04-19 19:29:37 +0000
19+++ ubuntu_sso/networkstate/tests/test_linux.py 2012-05-24 05:14:40 +0000
20@@ -178,98 +178,38 @@
21 self.assertFalse(nms.state_signal.removed)
22
23 @inlineCallbacks
24- def test_is_machine_connected_nm_state_connected_old(self):
25- """Fake the NetworkManagerState with connected return."""
26- self.patch(FakeNetworkManagerState, "connection_state",
27- NM_STATE_CONNECTED_OLD)
28- d = yield is_machine_connected()
29- self.assertTrue(d)
30-
31- @inlineCallbacks
32- def test_is_machine_connected_nm_state_connected_global(self):
33- """Fake the NetworkManagerState with connected return."""
34- self.patch(FakeNetworkManagerState, "connection_state",
35- NM_STATE_CONNECTED_GLOBAL)
36- d = yield is_machine_connected()
37- self.assertTrue(d)
38-
39- @inlineCallbacks
40- def test_is_machine_disconnected_nm_state_nm_state_unknown(self):
41- """Fake the NetworkManagerState with disconnected return."""
42- self.patch(FakeNetworkManagerState, "connection_state",
43- NM_STATE_UNKNOWN)
44- d = yield is_machine_connected()
45- self.assertFalse(d)
46-
47- @inlineCallbacks
48- def test_is_machine_disconnected_nm_state_asleep_old(self):
49- """Fake the NetworkManagerState with disconnected return."""
50- self.patch(FakeNetworkManagerState, "connection_state",
51- NM_STATE_ASLEEP_OLD)
52- d = yield is_machine_connected()
53- self.assertFalse(d)
54-
55- @inlineCallbacks
56- def test_is_machine_disconnected_nm_state_asleep(self):
57- """Fake the NetworkManagerState with disconnected return."""
58- self.patch(FakeNetworkManagerState, "connection_state",
59- NM_STATE_ASLEEP)
60- d = yield is_machine_connected()
61- self.assertFalse(d)
62-
63- @inlineCallbacks
64- def test_is_machine_disconnected_nm_state_connecting_old(self):
65- """Fake the NetworkManagerState with disconnected return."""
66- self.patch(FakeNetworkManagerState, "connection_state",
67- NM_STATE_CONNECTING_OLD)
68- d = yield is_machine_connected()
69- self.assertFalse(d)
70-
71- @inlineCallbacks
72- def test_is_machine_disconnected_nm_state_connecting(self):
73- """Fake the NetworkManagerState with disconnected return."""
74- self.patch(FakeNetworkManagerState, "connection_state",
75- NM_STATE_CONNECTING)
76- d = yield is_machine_connected()
77- self.assertFalse(d)
78-
79- @inlineCallbacks
80- def test_is_machine_disconnected_nm_state_connected_local(self):
81- """Fake the NetworkManagerState with disconnected return."""
82- self.patch(FakeNetworkManagerState, "connection_state",
83- NM_STATE_CONNECTED_LOCAL)
84- d = yield is_machine_connected()
85- self.assertFalse(d)
86-
87- @inlineCallbacks
88- def test_is_machine_disconnected_nm_state_connected_site(self):
89- """Fake the NetworkManagerState with disconnected return."""
90- self.patch(FakeNetworkManagerState, "connection_state",
91- NM_STATE_CONNECTED_SITE)
92- d = yield is_machine_connected()
93- self.assertFalse(d)
94-
95- @inlineCallbacks
96- def test_is_machine_disconnected_nm_state_disconnected_old(self):
97- """Fake the NetworkManagerState with disconnected return."""
98- self.patch(FakeNetworkManagerState, "connection_state",
99- NM_STATE_DISCONNECTED_OLD)
100- d = yield is_machine_connected()
101- self.assertFalse(d)
102-
103- @inlineCallbacks
104- def test_is_machine_disconnected_nm_state_disconnected(self):
105- """Fake the NetworkManagerState with disconnected return."""
106- self.patch(FakeNetworkManagerState, "connection_state",
107- NM_STATE_DISCONNECTED)
108- d = yield is_machine_connected()
109- self.assertFalse(d)
110-
111- @inlineCallbacks
112- def test_is_machine_defer_error(self):
113- """Fake the NetworkManagerState with disconnected return."""
114- self.patch(FakeNetworkManagerState, "connection_state",
115- 'no-int')
116+ def test_is_machine_connected_nm_state_online(self):
117+ """Callback given ONLINE should mean we are online"""
118+ self.patch(FakeNetworkManagerState, "connection_state",
119+ ONLINE)
120+ d = yield is_machine_connected()
121+ self.assertTrue(d)
122+
123+ @inlineCallbacks
124+ def test_is_machine_connected_nm_state_offline(self):
125+ """Callback given OFFLINE should mean we are offline"""
126+ self.patch(FakeNetworkManagerState, "connection_state",
127+ OFFLINE)
128+ d = yield is_machine_connected()
129+ self.assertFalse(d)
130+
131+ @inlineCallbacks
132+ def test_is_machine_connected_nm_state_unknown(self):
133+ """Callback given ONLINE should mean we are not online"""
134+ self.patch(FakeNetworkManagerState, "connection_state",
135+ UNKNOWN)
136+ d = yield is_machine_connected()
137+ self.assertFalse(d)
138+
139+ @inlineCallbacks
140+ def test_is_machine_connected_callback_error(self):
141+ """Test bad argument to is_machine_connected's internal callback.
142+
143+ Passing anything other than ONLINE/OFFLINE/UNKNOWN should
144+ cause an exception.
145+ """
146+ self.patch(FakeNetworkManagerState, "connection_state",
147+ NM_STATE_CONNECTED_GLOBAL)
148 yield self.assertFailure(is_machine_connected(), NetworkFailException)
149
150
151@@ -361,6 +301,18 @@
152 super(NetworkManagerStateTestCase, self).setUp()
153 self.connect_proxy()
154
155+ def test_nm_asleep(self):
156+ """Asleep status should mean offline."""
157+ self.check_nm_state(self.assertOffline, NM_STATE_ASLEEP)
158+
159+ def test_nm_asleep_old(self):
160+ """Asleep, old status, should mean offline."""
161+ self.check_nm_state(self.assertOffline, NM_STATE_ASLEEP_OLD)
162+
163+ def test_nm_unknown(self):
164+ """Unknown status should be treated the same as OFFLINE."""
165+ self.check_nm_state(self.assertOffline, NM_STATE_UNKNOWN)
166+
167 def test_nm_online_old(self):
168 """Check the connected, old status, case."""
169 self.check_nm_state(self.assertOnline, NM_STATE_CONNECTED_OLD)

Subscribers

People subscribed via source and target branches