Merge lp:~nataliabidart/ubuntuone-client/clean-env-aq into lp:ubuntuone-client

Proposed by Natalia Bidart
Status: Merged
Approved by: Alejandro J. Cura
Approved revision: 1101
Merged at revision: 1101
Proposed branch: lp:~nataliabidart/ubuntuone-client/clean-env-aq
Merge into: lp:ubuntuone-client
Diff against target: 339 lines (+42/-75)
3 files modified
tests/syncdaemon/test_action_queue.py (+11/-23)
tests/syncdaemon/test_main.py (+29/-48)
ubuntuone/syncdaemon/main.py (+2/-4)
To merge this branch: bzr merge lp:~nataliabidart/ubuntuone-client/clean-env-aq
Reviewer Review Type Date Requested Status
Alejandro J. Cura (community) Approve
Roberto Alsina (community) Approve
Review via email: mp+71052@code.launchpad.net

Commit message

- Fake ExternalInterface when testing the main module (LP: #823982).
- Do a proper clean on test_action_queue tests (LP: #823895).

To post a comment you must log in.
Revision history for this message
Roberto Alsina (ralsina) wrote :

tests run, looks ok!

review: Approve
Revision history for this message
Alejandro J. Cura (alecu) wrote :

+1

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'tests/syncdaemon/test_action_queue.py'
2--- tests/syncdaemon/test_action_queue.py 2011-08-09 12:56:06 +0000
3+++ tests/syncdaemon/test_action_queue.py 2011-08-10 14:39:36 +0000
4@@ -214,6 +214,7 @@
5 if hasattr(self, 'testing_deferred'):
6 self.testing_deferred.callback(True)
7
8+
9 class TestActionQueue(ActionQueue):
10 """AQ class that uses the testing protocol."""
11 protocol = TestingProtocol
12@@ -235,15 +236,13 @@
13 self.shares = self.mktemp('shares')
14 self.partials = self.mktemp('partials')
15
16- # set up Fake Main to use the testing AQ, the port will
17- # be decided later
18- self._orig_fakemain_aq = (FakeMain._fake_AQ_class,
19- FakeMain._fake_AQ_params)
20- FakeMain._fake_AQ_class = TestActionQueue
21- FakeMain._fake_AQ_params = ('127.0.0.1', 0, False)
22+ # set up FakeMain to use the testing AQ, the port will be decided later
23+ self.patch(FakeMain, '_fake_AQ_class', TestActionQueue)
24+ self.patch(FakeMain, '_fake_AQ_params', ('127.0.0.1', 0, False))
25
26 self.main = FakeMain(root_dir=self.root, shares_dir=self.shares,
27 data_dir=self.data, partials_dir=self.partials)
28+ self.addCleanup(self.main.shutdown)
29
30 self.action_queue = self.main.action_q
31 self.action_queue.connection_timeout = 3
32@@ -267,24 +266,14 @@
33 self.handler.setLevel(logging.DEBUG)
34 self._logger = logging.getLogger('ubuntuone.SyncDaemon')
35 self._logger.addHandler(self.handler)
36+ self.addCleanup(self._logger.removeHandler, self.handler)
37+
38 # setup done according to the platform
39 setup_action_queue_test(self)
40
41- def _linux_setup(self):
42- import dbus
43- dbus.service.BusName.__del__ = lambda _: None
44-
45 @defer.inlineCallbacks
46 def tearDown(self):
47 """Cleanup."""
48- self._logger.removeHandler(self.handler)
49- self.main.shutdown()
50-
51- # restore Fake Main settings
52- cls, params = self._orig_fakemain_aq
53- FakeMain._fake_AQ_class = cls
54- FakeMain._fake_AQ_params = params
55-
56 for record in self.handler.records:
57 exc_info = getattr(record, 'exc_info', None)
58 if exc_info is not None:
59@@ -452,15 +441,14 @@
60 event_queue = self.eq = FakedEventQueue()
61
62 self.rq = RequestQueue(action_queue=FakeAQ())
63+ self.addCleanup(self.eq.shutdown)
64
65 # add a Memento handler to the logger
66 self.log_handler = MementoHandler()
67 self.log_handler.setLevel(logging.DEBUG)
68- logging.getLogger('ubuntuone.SyncDaemon').addHandler(self.log_handler)
69-
70- def tearDown(self):
71- """Tear down."""
72- self.eq.shutdown()
73+ logger = logging.getLogger('ubuntuone.SyncDaemon')
74+ logger.addHandler(self.log_handler)
75+ self.addCleanup(logger.removeHandler, self.log_handler)
76
77 def _add_to_rq(self, *cmds):
78 """Add the commands to rq.waiting and hashed_waiting."""
79
80=== modified file 'tests/syncdaemon/test_main.py'
81--- tests/syncdaemon/test_main.py 2011-08-09 12:56:06 +0000
82+++ tests/syncdaemon/test_main.py 2011-08-10 14:39:36 +0000
83@@ -57,6 +57,15 @@
84 """Something! :)"""
85
86
87+class FakedExternalInterface(object):
88+ """Do nothing."""
89+
90+ def __init__(self, *a, **kw):
91+ self._called = []
92+ self.connect = lambda *a, **kw: self._called.append(('connect', a, kw))
93+ self.shutdown = lambda *a, **kw: None
94+
95+
96 class MainTests(BaseTwistedTestCase):
97 """ Basic tests to check main.Main """
98
99@@ -71,6 +80,8 @@
100 # perform the extra setup steps according to the platform
101 setup_main_test(self)
102
103+ self.patch(main_mod, 'ExternalInterface', FakedExternalInterface)
104+
105 self.handler = MementoHandler()
106 self.handler.setLevel(logging.DEBUG)
107 self._logger = logging.getLogger('ubuntuone.SyncDaemon')
108@@ -78,27 +89,29 @@
109 self.addCleanup(self._logger.removeHandler, self.handler)
110
111 def build_main(self, **kwargs):
112- """
113- Build and return a Main object, using reasonable defaults for
114- the tests, plus whatever extra kwargs are passed in.
115+ """Build and return a Main object.
116+
117+ Use reasonable defaults for the tests, plus whatever extra kwargs are
118+ passed in.
119+
120 """
121 # get the params using the platform code to ensure they are correct
122- params = get_main_params(self, _get_main_common_params(self))
123+ params = get_main_params(self, _get_main_common_params(self))
124 params.update(kwargs)
125 m = main_mod.Main(**params)
126+ self.addCleanup(m.shutdown)
127 m.local_rescan = lambda *_: m.event_q.push('SYS_LOCAL_RESCAN_DONE')
128 return m
129
130 def test_main_initialization(self):
131 """test that creating a Main instance works as expected."""
132 main = self.build_main()
133- main.shutdown()
134+ self.assertIsInstance(main, main_mod.Main)
135
136 def test_main_start(self):
137 """Test that Main.start works."""
138 main = self.build_main()
139 main.start()
140- main.shutdown()
141
142 def test_main_restarts_on_critical_error(self):
143 """Test that Main restarts when syncdaemon gets into UNKNOWN_ERROR."""
144@@ -108,11 +121,11 @@
145 main.start()
146 main.event_q.push('SYS_UNKNOWN_ERROR')
147 self.assertTrue(self.restarted)
148- main.shutdown()
149
150 def test_shutdown_pushes_sys_quit(self):
151 """When shutting down, the SYS_QUIT event is pushed."""
152- main = self.build_main()
153+ params = get_main_params(self, _get_main_common_params(self))
154+ main = main_mod.Main(**params)
155 events = []
156 self.patch(main.event_q, 'push',
157 lambda *a, **kw: events.append((a, kw)))
158@@ -147,34 +160,29 @@
159 main.start()
160 main.event_q.push('SYS_NET_CONNECTED')
161 main.event_q.push('SYS_USER_CONNECT', access_token='')
162- d0.addCallback(lambda _: main.shutdown())
163 return d0
164
165 def test_create_dirs_already_exists_dirs(self):
166 """test that creating a Main instance works as expected."""
167 link = os.path.join(self.root, 'Shared With Me')
168- self.assertFalse(path_exists(link))
169+ self.assertFalse(is_link(link))
170 self.assertTrue(path_exists(self.shares))
171 self.assertTrue(path_exists(self.root))
172 main = self.build_main()
173- self.assertTrue(path_exists(main.shares_dir_link))
174 # check that the shares link is actually a link
175 self.assertTrue(is_link(main.shares_dir_link))
176 self.assertEquals(link, main.shares_dir_link)
177- main.shutdown()
178
179 def test_create_dirs_already_exists_symlink_too(self):
180 """test that creating a Main instance works as expected."""
181 link = os.path.join(self.root, 'Shared With Me')
182 make_link(self.shares, link)
183- self.assertTrue(path_exists(link))
184 self.assertTrue(is_link(link))
185 self.assertTrue(path_exists(self.shares))
186 self.assertTrue(path_exists(self.root))
187 main = self.build_main()
188 # check that the shares link is actually a link
189 self.assertTrue(is_link(main.shares_dir_link))
190- main.shutdown()
191
192 def test_create_dirs_already_exists_but_not_symlink(self):
193 """test that creating a Main instance works as expected."""
194@@ -188,21 +196,17 @@
195 # check that the shares link is actually a link
196 self.assertEquals(main.shares_dir_link, link)
197 self.assertFalse(is_link(main.shares_dir_link))
198- main.shutdown()
199
200 def test_create_dirs_none_exists(self):
201 """test that creating a Main instance works as expected."""
202- link = os.path.join(self.root, 'Shared With Me')
203 # remove the existing dirs
204 remove_dir(self.root)
205 remove_dir(self.shares)
206 main = self.build_main()
207 # check that the shares link is actually a link
208- self.assertTrue(path_exists(link))
209 self.assertTrue(is_link(main.shares_dir_link))
210 self.assertTrue(path_exists(self.shares))
211 self.assertTrue(path_exists(self.root))
212- main.shutdown()
213
214 def test_connect_if_autoconnect_is_enabled(self):
215 """If autoconnect option is enabled, connect the syncdaemon."""
216@@ -211,15 +215,9 @@
217 user_config.set_autoconnect(True)
218 self.addCleanup(user_config.set_autoconnect, orig)
219
220- self.connect_called = False
221- self.patch(main_mod.ubuntuone.platform.ExternalInterface, 'connect',
222- lambda *a, **kw: setattr(self, 'connect_called', (a, kw)))
223-
224 main = self.build_main()
225- self.addCleanup(main.shutdown)
226-
227- self.assertEqual(self.connect_called, ((main.external,),
228- {'autoconnecting': True}))
229+ expected = [('connect', (), {'autoconnecting': True})]
230+ self.assertEqual(main.external._called, expected)
231
232 def test_dont_connect_if_autoconnect_is_disabled(self):
233 """If autoconnect option is disabled, do not connect the syncdaemon."""
234@@ -228,14 +226,8 @@
235 user_config.set_autoconnect(False)
236 self.addCleanup(user_config.set_autoconnect, orig)
237
238- self.connect_called = False
239- self.patch(main_mod.ubuntuone.platform.ExternalInterface, 'connect',
240- lambda *a, **kw: setattr(self, 'connect_called', True))
241-
242 main = self.build_main()
243- self.addCleanup(main.shutdown)
244-
245- self.assertFalse(self.connect_called)
246+ self.assertEqual(main.external._called, [])
247
248 def _get_listeners(self, main):
249 """Return the subscribed objects."""
250@@ -247,17 +239,15 @@
251
252 def test_event_logger_starts_if_available(self):
253 """The event logger is started if available."""
254- self.patch(main_mod.status_listener,
255+ self.patch(main_mod.event_logging,
256 "get_listener", lambda *a: FakeListener())
257 main = self.build_main()
258- self.addCleanup(main.shutdown)
259 self.assertIn(main.eventlog_listener, self._get_listeners(main))
260
261 def test_event_logger_not_started_if_not_available(self):
262 """The event logger is not started if it's not available."""
263 self.patch(main_mod.event_logging, "get_listener", lambda *a: None)
264 main = self.build_main()
265- self.addCleanup(main.shutdown)
266 self.assertNotIn(main.eventlog_listener, self._get_listeners(main))
267
268 def test_status_listener_is_installed(self):
269@@ -265,43 +255,34 @@
270 self.patch(main_mod.status_listener,
271 "get_listener", lambda *a: FakeListener())
272 main = self.build_main()
273- self.addCleanup(main.shutdown)
274 self.assertIn(main.status_listener, self._get_listeners(main))
275
276 def test_status_listener_not_installed_when_disabled(self):
277 """The status listener is not started if it's not available."""
278 self.patch(main_mod.status_listener, "get_listener", lambda *a: None)
279 main = self.build_main()
280- self.addCleanup(main.shutdown)
281 self.assertNotIn(main.status_listener, self._get_listeners(main))
282
283 def test_get_rootdir(self):
284 """The get_rootdir returns the root dir."""
285- expected = '~/Ubuntu Test One'
286+ expected = os.path.expanduser(os.path.join('~', 'Ubuntu Test One'))
287 main = self.build_main(root_dir=expected)
288- self.addCleanup(main.shutdown)
289-
290 self.assertEqual(main.get_rootdir(), expected)
291
292 def test_get_sharesdir(self):
293 """The get_sharesdir returns the shares dir."""
294- expected = '~/Share it to Me'
295+ expected = os.path.expanduser(os.path.join('~', 'Share it to Me'))
296 main = self.build_main(shares_dir=expected)
297- self.addCleanup(main.shutdown)
298-
299 self.assertEqual(main.get_sharesdir(), expected)
300
301 def test_get_sharesdirlink(self):
302 """The get_sharesdirlink returns the shares dir link."""
303 expected = 'Share it to Me'
304 main = self.build_main(shares_symlink_name=expected)
305- self.addCleanup(main.shutdown)
306-
307 self.assertEqual(main.get_sharesdir_link(),
308 os.path.join(main.get_rootdir(), expected))
309
310 def test_version_is_logged(self):
311 """Test that the client version is logged."""
312- main = self.build_main()
313- self.addCleanup(main.shutdown)
314+ self.build_main()
315 self.assertTrue(self.handler.check_info("client version", VERSION))
316
317=== modified file 'ubuntuone/syncdaemon/main.py'
318--- ubuntuone/syncdaemon/main.py 2011-07-29 12:51:04 +0000
319+++ ubuntuone/syncdaemon/main.py 2011-08-10 14:39:36 +0000
320@@ -37,9 +37,7 @@
321 volume_manager,
322 )
323 from ubuntuone import syncdaemon, clientdefs
324-import ubuntuone.platform
325-
326-from ubuntuone.platform import event_logging
327+from ubuntuone.platform import event_logging, ExternalInterface
328 from ubuntuone.syncdaemon import status_listener
329 from ubuntuone.syncdaemon.states import StateManager, QueueManager
330
331@@ -129,7 +127,7 @@
332 self.lr = local_rescan.LocalRescan(self.vm, self.fs,
333 self.event_q, self.action_q)
334
335- self.external = ubuntuone.platform.ExternalInterface(
336+ self.external = ExternalInterface(
337 self, glib_loop, broadcast_events)
338 self.external.oauth_credentials = oauth_credentials
339 if user_config.get_autoconnect():

Subscribers

People subscribed via source and target branches