~ubuntu-branches/ubuntu/oneiric/ubuntuone-client/oneiric

« back to all changes in this revision

Viewing changes to tests/platform/linux/test_filesystem_notifications.py

  • Committer: Bazaar Package Importer
  • Author(s): Rodney Dawes
  • Date: 2011-02-23 18:34:09 UTC
  • mfrom: (1.1.45 upstream)
  • Revision ID: james.westby@ubuntu.com-20110223183409-535o7yo165wbjmca
Tags: 1.5.5-0ubuntu1
* New upstream release.
  - Subscribing to a RO share will not download content (LP: #712528)
  - Can't synchronize "~/Ubuntu One Music" (LP: #714976)
  - Syncdaemon needs to show progress in Unity launcher (LP: #702116)
  - Notifications say "your cloud" (LP: #715887)
  - No longer requires python-libproxy
  - Recommend unity and indicator libs by default

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
import os
24
24
import unittest
25
25
 
26
 
from twisted.internet import defer, reactor
 
26
from twisted.internet import defer
27
27
 
28
28
from ubuntuone.syncdaemon import (
29
29
    event_queue,
31
31
)
32
32
from contrib.testing import testcase
33
33
from ubuntuone.devtools.handlers import MementoHandler
34
 
from ubuntuone.platform.linux.filesystem_notifications import (
35
 
    _GeneralINotifyProcessor
36
 
)
37
34
from ubuntuone.syncdaemon import volume_manager
38
35
from ubuntuone.syncdaemon.tritcask import Tritcask
39
36
 
253
250
    assertEqual = assertEquals = failUnlessEquals = failUnlessEqual
254
251
 
255
252
 
256
 
class IgnoreFileTests(unittest.TestCase):
257
 
    """Tests the ignore files behaviour."""
258
 
 
259
 
    def test_filter_none(self):
260
 
        """Still works ok even if not receiving a regex to ignore."""
261
 
        p = _GeneralINotifyProcessor(None)
262
 
        self.assertFalse(p.is_ignored("froo.pyc"))
263
 
 
264
 
    def test_filter_one(self):
265
 
        """Filters stuff that matches (or not) this one regex."""
266
 
        p = _GeneralINotifyProcessor(None, ['\A.*\\.pyc\Z'])
267
 
        self.assertTrue(p.is_ignored("froo.pyc"))
268
 
        self.assertFalse(p.is_ignored("froo.pyc.real"))
269
 
        self.assertFalse(p.is_ignored("otherstuff"))
270
 
 
271
 
    def test_filter_two_simple(self):
272
 
        """Filters stuff that matches (or not) these simple regexes."""
273
 
        p = _GeneralINotifyProcessor(None, ['\A.*foo\Z', '\A.*bar\Z'])
274
 
        self.assertTrue(p.is_ignored("blah_foo"))
275
 
        self.assertTrue(p.is_ignored("blah_bar"))
276
 
        self.assertFalse(p.is_ignored("bar_xxx"))
277
 
        self.assertFalse(p.is_ignored("--foo--"))
278
 
        self.assertFalse(p.is_ignored("otherstuff"))
279
 
 
280
 
    def test_filter_two_complex(self):
281
 
        """Filters stuff that matches (or not) these complex regexes."""
282
 
        p = _GeneralINotifyProcessor(None,
283
 
                                     ['\A.*foo\Z|\Afoo.*\Z', '\A.*bar\Z'])
284
 
        self.assertTrue(p.is_ignored("blah_foo"))
285
 
        self.assertTrue(p.is_ignored("blah_bar"))
286
 
        self.assertTrue(p.is_ignored("foo_xxx"))
287
 
        self.assertFalse(p.is_ignored("--foo--"))
288
 
        self.assertFalse(p.is_ignored("otherstuff"))
289
 
 
290
 
 
291
253
class WatchTests(BaseFSMonitorTestCase):
292
254
    """Test the EQ API to add and remove watchs."""
293
255
 
439
401
        self.assertFalse(self.monitor.has_watch(path_ancestor))
440
402
 
441
403
 
442
 
class MutedSignalsTests(BaseTwisted):
443
 
    """Test that EQ filter some signals on demand."""
444
 
 
445
 
    def check_filter(self, _=None):
446
 
        self.assertFalse(self.monitor._processor._to_mute._cnt)
447
 
        self.finished_ok()
448
 
 
449
 
    def test_mute_and_remove(self):
450
 
        """Test add and remove the mute."""
451
 
        # add
452
 
        self.monitor.add_to_mute_filter('FS_FILE_OPEN', path='somepath')
453
 
        self.assertEqual(self.monitor._processor._to_mute._cnt,
454
 
                         {'FS_FILE_OPEN': [{'path': 'somepath'}]})
455
 
        self.monitor.add_to_mute_filter('FS_FILE_OPEN', path='somepath')
456
 
        self.assertEqual(self.monitor._processor._to_mute._cnt,
457
 
                         {'FS_FILE_OPEN': [{'path': 'somepath'},
458
 
                                           {'path': 'somepath'}]})
459
 
        self.monitor.add_to_mute_filter('FS_FILE_OPEN', path='otherpath')
460
 
        self.assertEqual(self.monitor._processor._to_mute._cnt,
461
 
                         {'FS_FILE_OPEN': [{'path': 'somepath'},
462
 
                                           {'path': 'somepath'},
463
 
                                           {'path': 'otherpath'}]})
464
 
 
465
 
        # remove
466
 
        self.monitor.rm_from_mute_filter('FS_FILE_OPEN', path='somepath')
467
 
        self.assertEqual(self.monitor._processor._to_mute._cnt,
468
 
                         {'FS_FILE_OPEN': [{'path': 'somepath'},
469
 
                                           {'path': 'otherpath'}]})
470
 
        self.monitor.rm_from_mute_filter('FS_FILE_OPEN', path='otherpath')
471
 
        self.assertEqual(self.monitor._processor._to_mute._cnt,
472
 
                         {'FS_FILE_OPEN': [{'path': 'somepath'}]})
473
 
        self.monitor.rm_from_mute_filter('FS_FILE_OPEN', path='somepath')
474
 
        self.assertEqual(self.monitor._processor._to_mute._cnt, {})
475
 
 
476
 
    def _how_many_muted(self):
477
 
        """Return how many events are muted."""
478
 
        mute_filter = self.monitor._processor._to_mute
479
 
        return sum(len(x) for x in mute_filter._cnt.values())
480
 
 
481
 
    def test_file_open(self):
482
 
        """Test receiving the open signal on files."""
483
 
        testfile = os.path.join(self.root_dir, "foo")
484
 
        open(testfile, "w").close()
485
 
        self.monitor.add_to_mute_filter("FS_FILE_OPEN", path=testfile)
486
 
        self.monitor.add_to_mute_filter("FS_FILE_CLOSE_NOWRITE", path=testfile)
487
 
        self.assertEqual(self._how_many_muted(), 2)
488
 
        self.monitor.add_watch(self.root_dir)
489
 
 
490
 
        # generate the event
491
 
        open(testfile)
492
 
        reactor.callLater(.1, self.check_filter)
493
 
        return self._deferred
494
 
 
495
 
    def test_file_close_nowrite(self):
496
 
        """Test receiving the close_nowrite signal on files."""
497
 
        testfile = os.path.join(self.root_dir, "foo")
498
 
        open(testfile, "w").close()
499
 
        fh = open(testfile)
500
 
        self.monitor.add_to_mute_filter("FS_FILE_CLOSE_NOWRITE", path=testfile)
501
 
        self.assertEqual(self._how_many_muted(), 1)
502
 
        self.monitor.add_watch(self.root_dir)
503
 
 
504
 
        # generate the event
505
 
        fh.close()
506
 
        reactor.callLater(.1, self.check_filter)
507
 
        return self._deferred
508
 
 
509
 
    def test_file_create_close_write(self):
510
 
        """Test receiving the create and close_write signals on files."""
511
 
        testfile = os.path.join(self.root_dir, "foo")
512
 
        self.monitor.add_to_mute_filter("FS_FILE_CREATE", path=testfile)
513
 
        self.monitor.add_to_mute_filter("FS_FILE_OPEN", path=testfile)
514
 
        self.monitor.add_to_mute_filter("FS_FILE_CLOSE_WRITE", path=testfile)
515
 
        self.assertEqual(self._how_many_muted(), 3)
516
 
        self.monitor.add_watch(self.root_dir)
517
 
 
518
 
        # generate the event
519
 
        open(testfile, "w").close()
520
 
        reactor.callLater(.1, self.check_filter)
521
 
        return self._deferred
522
 
 
523
 
    def test_dir_create(self):
524
 
        """Test receiving the create signal on dirs."""
525
 
        testdir = os.path.join(self.root_dir, "foo")
526
 
        self.monitor.add_to_mute_filter("FS_DIR_CREATE", path=testdir)
527
 
        self.assertEqual(self._how_many_muted(), 1)
528
 
        self.monitor.add_watch(self.root_dir)
529
 
 
530
 
        # generate the event
531
 
        os.mkdir(testdir)
532
 
        reactor.callLater(.1, self.check_filter)
533
 
        return self._deferred
534
 
 
535
 
    def test_file_delete(self):
536
 
        """Test the delete signal on a file."""
537
 
        testfile = os.path.join(self.root_dir, "foo")
538
 
        open(testfile, "w").close()
539
 
        self.monitor.add_to_mute_filter("FS_FILE_DELETE", path=testfile)
540
 
        self.assertEqual(self._how_many_muted(), 1)
541
 
        self.monitor.add_watch(self.root_dir)
542
 
 
543
 
        # generate the event
544
 
        os.remove(testfile)
545
 
        reactor.callLater(.1, self.check_filter)
546
 
        return self._deferred
547
 
 
548
 
    def test_dir_delete(self):
549
 
        """Test the delete signal on a dir."""
550
 
        testdir = os.path.join(self.root_dir, "foo")
551
 
        os.mkdir(testdir)
552
 
        self.monitor.add_to_mute_filter("FS_DIR_DELETE", path=testdir)
553
 
        self.assertEqual(self._how_many_muted(), 1)
554
 
        self.monitor.add_watch(self.root_dir)
555
 
 
556
 
        # generate the event
557
 
        os.rmdir(testdir)
558
 
        reactor.callLater(.1, self.check_filter)
559
 
        return self._deferred
560
 
 
561
 
    def test_file_moved_inside(self):
562
 
        """Test the synthesis of the FILE_MOVE event."""
563
 
        fromfile = os.path.join(self.root_dir, "foo")
564
 
        self.fs.create(fromfile, "")
565
 
        self.fs.set_node_id(fromfile, "from_node_id")
566
 
        tofile = os.path.join(self.root_dir, "bar")
567
 
        self.fs.create(tofile, "")
568
 
        self.fs.set_node_id(tofile, "to_node_id")
569
 
        open(fromfile, "w").close()
570
 
        self.monitor.add_to_mute_filter("FS_FILE_MOVE",
571
 
                                        path_from=fromfile, path_to=tofile)
572
 
        self.assertEqual(self._how_many_muted(), 1)
573
 
        self.monitor.add_watch(self.root_dir)
574
 
 
575
 
        # generate the event
576
 
        os.rename(fromfile, tofile)
577
 
        reactor.callLater(.1, self.check_filter)
578
 
        return self._deferred
579
 
 
580
 
    def test_dir_moved_inside(self):
581
 
        """Test the synthesis of the DIR_MOVE event."""
582
 
        fromdir = os.path.join(self.root_dir, "foo")
583
 
        self.fs.create(fromdir, "")
584
 
        self.fs.set_node_id(fromdir, "from_node_id")
585
 
        todir = os.path.join(self.root_dir, "bar")
586
 
        self.fs.create(todir, "")
587
 
        self.fs.set_node_id(todir, "to_node_id")
588
 
        os.mkdir(fromdir)
589
 
        self.monitor.add_to_mute_filter("FS_DIR_MOVE",
590
 
                                        path_from=fromdir, path_to=todir)
591
 
        self.assertEqual(self._how_many_muted(), 1)
592
 
        self.monitor.add_watch(self.root_dir)
593
 
 
594
 
        # generate the event
595
 
        os.rename(fromdir, todir)
596
 
        reactor.callLater(.1, self.check_filter)
597
 
        return self._deferred
598
 
 
599
 
    def test_file_moved_from_conflict(self):
600
 
        """Test the handling of the FILE_MOVE event when source is conflict."""
601
 
        fromfile = os.path.join(self.root_dir, "foo.u1conflict")
602
 
        self.fs.create(fromfile, "")
603
 
        self.fs.set_node_id(fromfile, "from_node_id")
604
 
        tofile = os.path.join(self.root_dir, "foo")
605
 
        self.fs.create(tofile, "")
606
 
        self.fs.set_node_id(tofile, "to_node_id")
607
 
        open(fromfile, "w").close()
608
 
        self.monitor.add_to_mute_filter("FS_FILE_MOVE",
609
 
                                        path_from=fromfile, path_to=tofile)
610
 
        self.assertEqual(self._how_many_muted(), 2)
611
 
        self.monitor.add_watch(self.root_dir)
612
 
 
613
 
        # generate the event
614
 
        os.rename(fromfile, tofile)
615
 
        reactor.callLater(.1, self.check_filter)
616
 
        return self._deferred
617
 
 
618
 
    def test_file_moved_from_partial(self):
619
 
        """Test the handling of the FILE_MOVE event when source is partial."""
620
 
        fromfile = os.path.join(self.root_dir, "mdid.u1partial.foo")
621
 
        root_dir = os.path.join(self.root_dir, "my_files")
622
 
        tofile = os.path.join(root_dir, "foo")
623
 
        os.mkdir(root_dir)
624
 
        open(fromfile, "w").close()
625
 
        self.monitor.add_to_mute_filter("FS_FILE_CREATE", path=tofile)
626
 
        self.monitor.add_to_mute_filter("FS_FILE_CLOSE_WRITE", path=tofile)
627
 
        self.assertEqual(self._how_many_muted(), 2)
628
 
        self.monitor.add_watch(root_dir)
629
 
 
630
 
        # generate the event
631
 
        os.rename(fromfile, tofile)
632
 
        reactor.callLater(.1, self.check_filter)
633
 
        return self._deferred
634
 
 
635
 
 
636
404
def test_suite():
637
405
    """Collect the tests."""
638
406
    import sys