Merge lp:~shanepatrickfagan/ubuntu-sso-client/nm-state-bug-fix into lp:ubuntu-sso-client

Proposed by Shane Fagan
Status: Rejected
Rejected by: dobey
Proposed branch: lp:~shanepatrickfagan/ubuntu-sso-client/nm-state-bug-fix
Merge into: lp:ubuntu-sso-client
Diff against target: 305 lines (+189/-17) (has conflicts)
2 files modified
ubuntu_sso/networkstate/linux.py (+43/-13)
ubuntu_sso/networkstate/tests/test_linux.py (+146/-4)
Text conflict in ubuntu_sso/networkstate/tests/test_linux.py
To merge this branch: bzr merge lp:~shanepatrickfagan/ubuntu-sso-client/nm-state-bug-fix
Reviewer Review Type Date Requested Status
dobey (community) Disapprove
Natalia Bidart (community) Needs Fixing
Review via email: mp+63136@code.launchpad.net

Description of the change

Fixes the network manager states change in 11.10. I had to change stuff around to get it working and I dont know if I approached it entirely correctly but it is working on 11.10 and 11.04 for me. To test just do a sudo python setup.py and kill the cp and see if it connects correctly.

To post a comment you must log in.
719. By Shane Fagan

took away that weird code I wrote

Revision history for this message
Natalia Bidart (nataliabidart) wrote :

The code makes sense, though in order to get the Approve we need the following:

* unit tests for both branches of the newly added if
* self.NM_STATE_UNKNOWN should be defined for both versions of the protocol
* the protocol version number should not be a literal string but a constant

review: Needs Fixing
720. By Shane Fagan

fixed a small oversight

Revision history for this message
Natalia Bidart (nataliabidart) wrote :

Also, test suite is not passing OK:

nessita@dali:~/canonical/ussoc/review_nm-state-bug-fix$ ./run-tests
Running test suite for ubuntu_sso
Xlib: extension "RANDR" missing on display ":99".
Traceback (most recent call last):
  File "/usr/bin/u1trial", line 269, in <module>
    main()
  File "/usr/bin/u1trial", line 265, in main
    TestRunner(force_gc=options.force_gc).run(testpath, options)
  File "/usr/bin/u1trial", line 182, in run
    options.ignored_modules, options.ignored_paths)
  File "/usr/bin/u1trial", line 161, in _collect_tests
    module_suite = self._load_unittest(filepath)
  File "/usr/bin/u1trial", line 89, in _load_unittest
    module = __import__(modpath, None, None, [""])
  File "/home/nessita/canonical/ussoc/review_nm-state-bug-fix/ubuntu_sso/networkstate/tests/test_linux.py", line 24, in <module>
    from ubuntu_sso.networkstate.linux import (DBUS_UNKNOWN_SERVICE,
ImportError: cannot import name NM_STATE_DISCONNECTED

review: Needs Fixing
Revision history for this message
dobey (dobey) wrote :

47 + if str(nm_proxy.Get(NM_DBUS_INTERFACE,
48 + "Version")) >= "0.8.9997":

This doesn't work. Python doesn't compare these two values as tuples of integers here. Instead, it compares them as strings. Which means, any single character in the ASCII charset that comes after the character '0' is going to return True. Also, you should use a lower number for the check, like 0.8.99 perhaps.

review: Needs Fixing
Revision history for this message
Shane Fagan (shanepatrickfagan) wrote :

@dobey well the way I did it originally I did .digit and compared them as ints would that work better?

Revision history for this message
Shane Fagan (shanepatrickfagan) wrote :

Oh and that version was the one that brought the changes in Ubuntu I dont know if the earlier versions would work in the same way so I thought better be safe than sorry about the version number.

Revision history for this message
dobey (dobey) wrote :

You should check the upstream history to see when the API change was made, and what the version number was at that time, and use that version number (or the next one after, as it may have only been bumped at release time).

And for comparison, as alecu said in IRC, you need to get the string split into a tuple of integers (and strings, if there are any non-numeric characters in the version like a/b/pre/etc...), check that the length of each tuple is >= 2, and then compare the tuples. Also need to ensure that the int() values are actually correct, and not just the ASCII numeric value (ie "0" becomes 0).

721. By Shane Fagan

made the code a lot cleaner and did the suggestions that the guys said but still no tests. Will do them next

722. By Shane Fagan

fixed nearly all of the tests and added methods for everything

Revision history for this message
dobey (dobey) wrote :

A much better fix for this was proposed by mterry and approved. Rejecting this branch now.

review: Disapprove

Unmerged revisions

722. By Shane Fagan

fixed nearly all of the tests and added methods for everything

721. By Shane Fagan

made the code a lot cleaner and did the suggestions that the guys said but still no tests. Will do them next

720. By Shane Fagan

fixed a small oversight

719. By Shane Fagan

took away that weird code I wrote

718. By Shane Fagan

shortened the long line since it would probably break pep8

717. By Shane Fagan

added some code for detecting the connection in 11.10, it works but needs testing on 11.04 or below. Its a few little changes but all are needed to work I think.

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 2011-03-02 10:55:12 +0000
3+++ ubuntu_sso/networkstate/linux.py 2011-06-16 14:09:34 +0000
4@@ -33,13 +33,6 @@
5 UNKNOWN: "unknown",
6 }
7
8-# Internal NetworkManager State constants
9-NM_STATE_UNKNOWN = 0
10-NM_STATE_ASLEEP = 1
11-NM_STATE_CONNECTING = 2
12-NM_STATE_CONNECTED = 3
13-NM_STATE_DISCONNECTED = 4
14-
15 NM_DBUS_INTERFACE = "org.freedesktop.NetworkManager"
16 NM_DBUS_OBJECTPATH = "/org/freedesktop/NetworkManager"
17 DBUS_UNKNOWN_SERVICE = "org.freedesktop.DBus.Error.ServiceUnknown"
18@@ -62,9 +55,9 @@
19
20 def got_state(self, state):
21 """Called by DBus when the state is retrieved from NM."""
22- if state == NM_STATE_CONNECTED:
23+ if state == self.nm_state_connected:
24 self.call_result_cb(ONLINE)
25- elif state == NM_STATE_CONNECTING:
26+ elif state == self.nm_state_connecting:
27 logger.debug("Currently connecting, waiting for signal")
28 else:
29 self.call_result_cb(OFFLINE)
30@@ -74,23 +67,60 @@
31 if isinstance(error, self.dbus.exceptions.DBusException) and \
32 error.get_dbus_name() == DBUS_UNKNOWN_SERVICE:
33 logger.debug("Network Manager not present")
34- self.call_result_cb(UNKNOWN)
35+ self.call_result_cb(self.nm_state_unknown)
36 else:
37 logger.error("Error contacting NetworkManager: %s" % \
38 str(error))
39- self.call_result_cb(UNKNOWN)
40+ self.call_result_cb(self.nm_state_unknown)
41
42 def state_changed(self, state):
43 """Called when a signal is emmited by Network Manager."""
44- if int(state) == NM_STATE_CONNECTED:
45+ if int(state) == self.nm_state_connected:
46 self.call_result_cb(ONLINE)
47- elif int(state) == NM_STATE_DISCONNECTED:
48+ elif int(state) == self.nm_state_disconnected:
49 self.call_result_cb(OFFLINE)
50 else:
51 logger.debug("Not yet connected: continuing to wait")
52
53 def find_online_state(self):
54 """Get the network state and return it thru the set callback."""
55+ sysbus = self.dbus.SystemBus()
56+ nm_proxy = sysbus.get_object(NM_DBUS_INTERFACE,
57+ NM_DBUS_OBJECTPATH,
58+ follow_name_owner_changes=True)
59+ nm_proxy.Get(NM_DBUS_INTERFACE,
60+ "Version", reply_handler=self.got_version,
61+ error_handler=self.got_error)
62+
63+ def got_version(self, version):
64+ """Parse the version and pass what state version to use in set_state_values."""
65+ x = str(version).split(".")
66+ new_state_version = False
67+ if int(x[0]) == 0 and int(x[1]) <= 8:
68+ if int(x[1]) == 8 and x[2].startswith("99"):
69+ new_state_version = True
70+ else:
71+ new_state_version = True
72+ self.set_state_values(new_state_version)
73+
74+ def set_state_values(self, new_state_version):
75+ """Set the state values."""
76+ if new_state_version == True:
77+ self.nm_state_asleep = 10
78+ self.nm_state_disconnecting = 20
79+ self.nm_state_disconnected = 30
80+ self.nm_state_connecting = 40
81+ self.nm_state_connected = 70
82+ else:
83+ self.nm_state_asleep = 1
84+ self.nm_state_connecting = 2
85+ self.nm_state_connected = 3
86+ self.nm_state_disconnected = 4
87+ self.nm_state_unknown = 0
88+ self.detect_online_state()
89+
90+ def detect_online_state(self):
91+ """Detect the online state."""
92 try:
93 sysbus = self.dbus.SystemBus()
94 nm_proxy = sysbus.get_object(NM_DBUS_INTERFACE,
95
96=== modified file 'ubuntu_sso/networkstate/tests/test_linux.py'
97--- ubuntu_sso/networkstate/tests/test_linux.py 2011-06-15 19:19:19 +0000
98+++ ubuntu_sso/networkstate/tests/test_linux.py 2011-06-16 14:09:34 +0000
99@@ -19,12 +19,11 @@
100 # with this program. If not, see <http://www.gnu.org/licenses/>.
101 """Tests for the network state detection code."""
102
103+from twisted.trial.unittest import TestCase
104+
105 from ubuntu_sso.networkstate import (NetworkManagerState,
106 ONLINE, OFFLINE, UNKNOWN)
107-from ubuntu_sso.networkstate.linux import (DBUS_UNKNOWN_SERVICE,
108- NM_STATE_DISCONNECTED,
109- NM_STATE_CONNECTING,
110- NM_STATE_CONNECTED)
111+from ubuntu_sso.networkstate.linux import (DBUS_UNKNOWN_SERVICE)
112
113 from mocker import ARGS, KWARGS, ANY, MockerTestCase
114
115@@ -47,6 +46,7 @@
116 """Base test case for NM state tests."""
117
118 def setUp(self):
119+<<<<<<< TREE
120 """Setup the mocker dbus object tree."""
121 super(NetworkManagerBaseTestCase, self).setUp()
122
123@@ -76,6 +76,30 @@
124 self.mocker.result(exc)
125 signalmock.remove()
126 self.mocker.replay()
127+=======
128+ """Setup the mocker dbus object tree."""
129+ self.dbusmock = self.mocker.mock()
130+ self.dbusmock.SystemBus()
131+ sysbusmock = self.mocker.mock()
132+ self.mocker.result(sysbusmock)
133+
134+ sysbusmock.get_object(ARGS, KWARGS)
135+ proxymock = self.mocker.mock()
136+ self.mocker.result(proxymock)
137+
138+ self.dbusmock.Interface(proxymock, ANY)
139+ ifmock = self.mocker.mock()
140+ self.mocker.result(ifmock)
141+
142+ ifmock.connect_to_signal(ARGS, KWARGS)
143+ signalmock = self.mocker.mock()
144+ self.mocker.result(signalmock)
145+
146+ proxymock.Get(ARGS, KWARGS)
147+ signalmock.remove()
148+
149+ self.mocker.replay()
150+>>>>>>> MERGE-SOURCE
151
152 # Invalid name "assert[A-Z].*"
153 # pylint: disable=C0103
154@@ -125,24 +149,130 @@
155
156 def test_nm_online(self):
157 """Check the connected case."""
158+<<<<<<< TREE
159 self.check_nm_state(self.assertOnline, NM_STATE_CONNECTED)
160+=======
161+
162+ def got_state_cb(state):
163+ """State was given."""
164+ self.assertEquals(state, ONLINE)
165+
166+ nms = NetworkManagerState(got_state_cb, self.dbusmock)
167+ nms.set_state_values(True)
168+ nms.got_state(nms.nm_state_connected)
169+>>>>>>> MERGE-SOURCE
170
171 def test_nm_offline(self):
172 """Check the disconnected case."""
173+<<<<<<< TREE
174 self.check_nm_state(self.assertOffline, NM_STATE_DISCONNECTED)
175+=======
176+
177+ def got_state_cb(state):
178+ """State was given."""
179+ self.assertEquals(state, OFFLINE)
180+
181+ nms = NetworkManagerState(got_state_cb, self.dbusmock)
182+ nms.set_state_values(True)
183+ nms.got_state(nms.nm_state_disconnected)
184+>>>>>>> MERGE-SOURCE
185
186 def test_nm_connecting_then_online(self):
187 """Check the waiting for connection case."""
188+<<<<<<< TREE
189 self.check_nm_state_change(self.assertOnline,
190 NM_STATE_CONNECTING, NM_STATE_CONNECTED)
191+=======
192+
193+ def got_state_cb(state):
194+ """State was given."""
195+ self.assertEquals(state, ONLINE)
196+
197+ nms = NetworkManagerState(got_state_cb, self.dbusmock)
198+ nms.set_state_values(True)
199+ nms.got_state(nms.nm_state_connecting)
200+ nms.state_changed(nms.nm_state_connected)
201+>>>>>>> MERGE-SOURCE
202
203 def test_nm_connecting_then_offline(self):
204 """Check the waiting but fail case."""
205+<<<<<<< TREE
206 self.check_nm_state_change(self.assertOffline,
207 NM_STATE_CONNECTING, NM_STATE_DISCONNECTED)
208
209
210 class NetworkManagerStateErrorsTestCase(NetworkManagerBaseTestCase):
211+=======
212+
213+ def got_state_cb(state):
214+ """State was given."""
215+ self.assertEquals(state, OFFLINE)
216+
217+ nms = NetworkManagerState(got_state_cb, self.dbusmock)
218+ nms.set_state_values(True)
219+ nms.got_state(nms.nm_state_connecting)
220+ nms.state_changed(nms.nm_state_disconnected)
221+
222+
223+class CheckVersionTestCase(TestCase):
224+ """Check the version tests."""
225+
226+ def test_got_version(self):
227+ """Check that got version checks for the right version."""
228+ self.new_state_version = None
229+
230+ def check_return_value(state_version):
231+ """Test true return."""
232+ self.new_state_version = state_version
233+
234+ def check_got_version(version, expected):
235+ nms.got_version(version)
236+ self.assertEquals(self.new_state_version, expected)
237+
238+ nms = NetworkManagerState(None, None)
239+ self.patch(nms, "set_state_values", check_return_value)
240+ versions = ["0.8.9997", "0.9", "1.0", "1.12.4215"]
241+ for x in versions:
242+ check_got_version(x, True)
243+ versions = ["0.8.8713", "0.3.2141", "0.1.3124", "0.5.5125"]
244+ for x in versions:
245+ check_got_version(x, False)
246+
247+class CheckSetValuesTestCase(TestCase):
248+ """Check that the values are set."""
249+
250+ def test_set_state(self):
251+ """Test that the states are actually set."""
252+ def check_set_values(nms):
253+ if nms.nm_state_asleep == 10 and \
254+ nms.nm_state_disconnected == 30 and \
255+ nms.nm_state_connecting == 40 and \
256+ nms.nm_state_connected == 70 and \
257+ nms.nm_state_unknown == 0:
258+ return True
259+ else:
260+ return False
261+ nms = NetworkManagerState(None, None)
262+ nms.set_state_values(True)
263+ self.assertEquals(check_set_values(nms), True)
264+ nms.set_state_values(False)
265+ self.assertEquals(check_set_values(nms), False)
266+
267+
268+class CheckDetectOnlineStateMethodCall(TestCase):
269+ """Check detect online state method call."""
270+
271+ def test_detect_online_state_call(self):
272+ self.is_called = False
273+ def check_method_call():
274+ self.is_called = True
275+ nms = NetworkManagerState(None, None)
276+ self.patch(nms, "detect_online_state", check_method_call)
277+ nms.detect_online_state()
278+ self.assertEquals(self.is_called, True)
279+
280+class NetworkManagerStateErrorsTestCase(MockerTestCase):
281+>>>>>>> MERGE-SOURCE
282 """Test NetworkManager state retrieval code."""
283
284 def mock_except_while_getting_proxy(self, exc):
285@@ -154,8 +284,20 @@
286
287 def test_nm_not_running(self):
288 """Check the case when NM is not running."""
289+<<<<<<< TREE
290 self.connect_proxy(TestNmNotAvailableException)
291 self.check_nm_error(self.assertUnknown, TestNmNotAvailableException())
292+=======
293+
294+ def got_state_cb(state):
295+ """State was given."""
296+ self.assertEquals(state, nms.nm_state_unknown)
297+
298+ self.mock_dbus_error_while_getting_state(TestNmNotAvailableException)
299+ nms = NetworkManagerState(got_state_cb, self.dbusmock)
300+ nms.set_state_values(True)
301+ nms.got_error(TestNmNotAvailableException())
302+>>>>>>> MERGE-SOURCE
303
304 def test_dbus_problem(self):
305 """Check the case when DBus throws some other exception."""

Subscribers

People subscribed via source and target branches