58
58
from ubuntuone.syncdaemon import states, interfaces
59
from ubuntuone.syncdaemon import action_queue
59
60
from ubuntuone.syncdaemon.action_queue import (
60
61
ActionQueue, ActionQueueCommand, ChangePublicAccess, CreateUDF,
61
DeleteVolume, Download, ListVolumes, ActionQueueProtocol,
62
DeleteVolume, Download, ListVolumes, ActionQueueProtocol, ListShares,
62
63
RequestQueue, UploadProgressWrapper, Upload, SIMULT_TRANSFERS,
63
64
CreateShare, DeleteShare, GetPublicFiles, GetDelta, GetDeltaFromScratch,
64
65
TRANSFER_PROGRESS_THRESHOLD, Unlink, Move, MakeFile, MakeDir, DeltaList,
1237
1238
yield self._disconnect_factory()
1240
def test_connection_started_logging(self):
1241
"""Test that the connection started logs connector info, not AQ's."""
1242
assert self.action_queue.host == '127.0.0.1'
1243
assert self.action_queue.port == 0
1245
class FakeConnector(object):
1246
"""Fake connector."""
1250
self.action_queue.startedConnecting(FakeConnector())
1251
self.assertTrue(self.handler.check_info("Connection started",
1252
"host 1.2.3.4", "port 4321"))
1240
1255
class NetworkmanagerTestCase(FactoryBaseTestCase):
1241
1256
"""Base test case generating a connected factory."""
1719
1734
def test_go_run_bad_release_pathlock(self):
1720
1735
"""If run went bad, release the pathlock."""
1722
self.cmd.run = lambda: defer.fail(ValueError())
1737
self.cmd.run = lambda: defer.fail(ValueError("error message"))
1723
1738
self.cmd._acquire_pathlock = lambda: defer.succeed(
1724
1739
lambda: called.append(True))
1731
self.fail("It didn't raise an exception!")
1732
1742
self.assertTrue(called)
1744
# check exception to assure a traceback was logged, and check the
1745
# messages in ERROR (the real logging level); finally, clean the
1746
# records as if we leave them with the exception the test will fail
1747
self.assertTrue(self.handler.check_exception(ValueError))
1748
self.assertTrue(self.handler.check_error("Error running the command",
1750
self.handler.records = []
1734
1752
def test_run_initial(self):
1735
1753
"""Call ._start, log, and set running."""
1737
self.cmd._start = lambda: called.append(True)# or defer.succeed(True)
1755
d = defer.Deferred()
1756
self.cmd._start = lambda: called.append(True) or d
1758
# run, and will lock in the _start
1740
self.assertFalse(self.cmd.running)
1741
1760
self.assertTrue(called)
1742
1761
self.assertTrue(self.handler.check_debug('starting'))
1763
# release the _start, check log and that it still not running
1765
self.assertTrue(self.handler.check_debug('started'))
1766
self.assertFalse(self.cmd.running)
1744
1768
def test_run_stop_if_cancelled_while_start(self):
1745
1769
"""Cancelled while _start."""
1746
1770
self.rq.queue(self.cmd)
2072
2096
self.assertFalse(called)
2073
2097
self.assertTrue(self.cmd.cancelled)
2099
def test_slots(self):
2100
"""Inherited commands must have __slot__ (that is not inherited)."""
2101
for obj_name in dir(action_queue):
2102
obj = getattr(action_queue, obj_name)
2103
if isinstance(obj, type) and issubclass(obj, ActionQueueCommand) \
2104
and obj is not ActionQueueCommand:
2105
self.assertNotIdentical(obj.__slots__,
2106
ActionQueueCommand.__slots__,
2107
"class %s has no __slots__" % obj)
2076
2110
class CreateUDFTestCase(ConnectedBaseTestCase):
2077
2111
"""Test for CreateUDF ActionQueueCommand."""
2269
2303
return self.deferred
2306
class ListSharesTestCase(ConnectedBaseTestCase):
2307
"""Test for ListShares ActionQueueCommand."""
2311
res = super(ListSharesTestCase, self).setUp()
2312
self.rq = RequestQueue(action_queue=self.action_queue)
2315
def test_queued_mixed_types(self):
2316
"""Command gets queued if other command is waiting."""
2317
cmd1 = FakeCommand()
2319
cmd2 = ListShares(self.rq)
2320
self.assertTrue(cmd2._should_be_queued())
2322
def test_queued_two(self):
2323
"""Two queued commands is not ok."""
2324
cmd1 = ListShares(self.rq)
2326
cmd2 = ListShares(self.rq)
2327
self.assertFalse(cmd2._should_be_queued())
2329
def test_uniqueness(self):
2330
"""Info used for uniqueness."""
2331
cmd = ListShares(self.rq)
2332
self.assertEqual(cmd.uniqueness, 'ListShares')
2272
2335
class ListVolumesTestCase(ConnectedBaseTestCase):
2273
2336
"""Test for ListVolumes ActionQueueCommand."""
2277
2340
res = super(ListVolumesTestCase, self).setUp()
2279
request_queue = RequestQueue(action_queue=self.action_queue)
2280
self.command = ListVolumes(request_queue)
2342
self.rq = RequestQueue(action_queue=self.action_queue)
2343
self.command = ListVolumes(self.rq)
2324
2387
event = ('AQ_LIST_VOLUMES_ERROR', {'error': msg})
2325
2388
self.assertIn(event, self.command.action_queue.event_queue.events)
2390
def test_queued_mixed_types(self):
2391
"""Command gets queued if other command is waiting."""
2392
cmd1 = FakeCommand()
2394
cmd2 = ListVolumes(self.rq)
2395
self.assertTrue(cmd2._should_be_queued())
2397
def test_queued_two(self):
2398
"""Two queued commands is not ok."""
2399
cmd1 = ListVolumes(self.rq)
2401
cmd2 = ListVolumes(self.rq)
2402
self.assertFalse(cmd2._should_be_queued())
2404
def test_uniqueness(self):
2405
"""Info used for uniqueness."""
2406
cmd = ListVolumes(self.rq)
2407
self.assertEqual(cmd.uniqueness, 'ListVolumes')
2328
2410
class DeleteVolumeTestCase(ConnectedBaseTestCase):
2329
2411
"""Test for DeleteVolume ActionQueueCommand."""
2516
2598
def setUp(self):
2517
2599
super(GetPublicFilesTestCase, self).setUp()
2518
2600
self.user_connect()
2519
request_queue = RequestQueue(action_queue=self.action_queue)
2520
self.command = GetPublicFiles(request_queue)
2601
self.rq = RequestQueue(action_queue=self.action_queue)
2602
self.command = GetPublicFiles(self.rq)
2522
2604
def test_init(self):
2523
2605
"""Test __init__ method."""
2606
2688
event = ('AQ_PUBLIC_FILES_LIST_ERROR', {'error': msg})
2607
2689
self.assertIn(event, self.command.action_queue.event_queue.events)
2691
def test_queued_mixed_types(self):
2692
"""Command gets queued if other command is waiting."""
2693
cmd1 = FakeCommand()
2695
cmd2 = GetPublicFiles(self.rq)
2696
self.assertTrue(cmd2._should_be_queued())
2698
def test_queued_two(self):
2699
"""Two queued commands is not ok."""
2700
cmd1 = GetPublicFiles(self.rq)
2702
cmd2 = GetPublicFiles(self.rq)
2703
self.assertFalse(cmd2._should_be_queued())
2705
def test_uniqueness(self):
2706
"""Info used for uniqueness."""
2707
cmd = GetPublicFiles(self.rq)
2708
self.assertEqual(cmd.uniqueness, 'GetPublicFiles')
2610
2711
class DownloadUnconnectedTestCase(FactoryBaseTestCase):
2611
2712
"""Test for Download ActionQueueCommand, no connection"""
3836
3937
event, str(exc)))
3838
3939
@defer.inlineCallbacks
3940
def assert_send_request_and_handle_errors_on_connection_end(self, exc):
3941
"""_send_request_and_handle_errors is ok when connection lost/done."""
3942
request = self.fail_please(exc)
3943
kwargs = dict(request=request, request_error=SpecificException,
3944
event_error='BAR', event_ok='FOO')
3945
d = self.action_queue._send_request_and_handle_errors(**kwargs)
3948
# check that SYS_UNKNOWN_ERROR wasn't sent, and that logged ok
3949
events = self.action_queue.event_queue.events
3950
self.assertNotIn('SYS_UNKNOWN_ERROR', [x[0] for x in events])
3951
self.assertTrue(self.handler.check_info(request.__name__, str(exc)))
3953
def test_send_request_and_handle_errors_on_connection_lost(self):
3954
"""_send_request_and_handle_errors is correct when connection lost."""
3955
e = twisted_error.ConnectionLost()
3956
return self.assert_send_request_and_handle_errors_on_connection_end(e)
3958
def test_send_request_and_handle_errors_on_connection_done(self):
3959
"""_send_request_and_handle_errors is correct when connection lost."""
3960
e = twisted_error.ConnectionDone()
3961
return self.assert_send_request_and_handle_errors_on_connection_end(e)
3963
@defer.inlineCallbacks
3839
3964
def assert_send_request_and_handle_errors_on_server_error(self, serr):
3840
3965
"""_send_request_and_handle_errors is correct when server error."""
3841
3966
# XXX: we need to replace this list with and exception list