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

« back to all changes in this revision

Viewing changes to tests/syncdaemon/test_eq_inotify.py

  • Committer: Bazaar Package Importer
  • Author(s): Rodney Dawes
  • Date: 2010-06-08 17:31:18 UTC
  • mto: This revision was merged to the branch mainline in revision 31.
  • Revision ID: james.westby@ubuntu.com-20100608173118-o8s897ll11rtne99
Tags: upstream-1.3.0
ImportĀ upstreamĀ versionĀ 1.3.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
49
49
    def _create_udf(self, path):
50
50
        """Create an UDF and returns it and the volume"""
51
51
        os.makedirs(path)
52
 
        udf = volume_manager.UDF("vol_id", "node_id", path, path, True)
 
52
        udf = volume_manager.UDF("vol_id", "node_id", path.decode('utf-8'),
 
53
                                 path, True)
53
54
        self.vm.add_udf(udf)
54
55
 
55
56
    def test_add_general_watch(self):
105
106
        self.assertTrue(path_ancestor in self.eq._general_watchs)
106
107
        self.assertEqual(self.eq._ancestors_watchs, {})
107
108
 
 
109
    def test_rm_watch_not_dir_anymore(self):
 
110
        """Test that a watch can be removed even not having the dir anymore.
 
111
 
 
112
        This is the case where the directory is deleted from the filesystem,
 
113
        the watch is automatically removed in pyinotify but we need to take
 
114
        care of it from our own data structures.
 
115
        """
 
116
        not_existing_dir = "not-added-dir"
 
117
        self.eq.inotify_add_watch(not_existing_dir)
 
118
        self.assertTrue(self.eq.inotify_has_watch(not_existing_dir))
 
119
        self.eq.inotify_rm_watch(not_existing_dir)
 
120
        self.assertFalse(self.eq.inotify_has_watch(not_existing_dir))
 
121
 
108
122
    def test_rm_watch_wrong(self):
109
123
        """Test that general watchs can be removed."""
110
124
        # add two types of watchs
115
129
        self.eq.inotify_add_watch(path_ancestor)
116
130
 
117
131
        # remove different stuff
118
 
        self.assertRaises(ValueError,
119
 
                          self.eq.inotify_rm_watch, "not-added-dir")
 
132
        self.eq.inotify_rm_watch("not-added-dir")
 
133
        self.assertTrue(self.log_handler.check_warning('remove', 'watch',
 
134
                                                       'not-added-dir'))
120
135
 
121
136
    def test_rm_watch_general(self):
122
137
        """Test that general watchs can be removed."""
172
187
        self.assertFalse(self.eq.inotify_has_watch(path_ancestor))
173
188
 
174
189
 
 
190
class WatchManagerTests(BaseEQTestCase):
 
191
    """Test the structures where we have the path/watch."""
 
192
 
 
193
    timeout = 3
 
194
 
 
195
    def setUp(self):
 
196
        """Set up."""
 
197
        BaseEQTestCase.setUp(self)
 
198
        self.deferred = deferred = defer.Deferred()
 
199
 
 
200
        class HitMe(object):
 
201
            # class-closure, cannot use self, pylint: disable-msg=E0213
 
202
            def handle_default(*a):
 
203
                    deferred.callback(True)
 
204
 
 
205
        self.eq.subscribe(HitMe())
 
206
 
 
207
    def test_watch_updated_when_deleting_dir(self):
 
208
        """Internal data structures are fixed when deleting the dir."""
 
209
        path = os.path.join(self.root_dir, "path")
 
210
        os.mkdir(path)
 
211
        self.eq.inotify_add_watch(self.root_dir)
 
212
        self.eq.inotify_add_watch(path)
 
213
 
 
214
        # we have the watch, remove the dir, the watch should be gone
 
215
        self.assertTrue(self.eq.inotify_has_watch(path))
 
216
        os.rmdir(path)
 
217
 
 
218
        def check(_):
 
219
            """Check state after the event."""
 
220
            self.assertFalse(self.eq.inotify_has_watch(path))
 
221
 
 
222
        self.deferred.addCallback(check)
 
223
        return self.deferred
 
224
 
 
225
    def test_watch_updated_when_renaming_dir(self):
 
226
        """Internal data structures are fixed when renaming the dir."""
 
227
        path1 = os.path.join(self.root_dir, "path1")
 
228
        path2 = os.path.join(self.root_dir, "path2")
 
229
        os.mkdir(path1)
 
230
        self.eq.inotify_add_watch(self.root_dir)
 
231
        self.eq.inotify_add_watch(path1)
 
232
 
 
233
        # we have the watch, rename the dir, new name should have the watch,
 
234
        # old name should not
 
235
        self.assertTrue(self.eq.inotify_has_watch(path1))
 
236
        os.rename(path1, path2)
 
237
 
 
238
        def check(_):
 
239
            """Check state after the event."""
 
240
            self.assertTrue(self.eq.inotify_has_watch(path2))
 
241
            self.assertFalse(self.eq.inotify_has_watch(path1))
 
242
 
 
243
        self.deferred.addCallback(check)
 
244
        return self.deferred
 
245
 
 
246
    def test_watch_updated_when_movingout_dir(self):
 
247
        """Internal data structures are fixed when moving out the dir."""
 
248
        notu1 = os.path.join(self.root_dir, "notu1")
 
249
        path1 = os.path.join(self.root_dir, "path1")
 
250
        path2 = os.path.join(notu1, "path2")
 
251
        os.mkdir(notu1)
 
252
        os.mkdir(path1)
 
253
        self.eq.inotify_add_watch(self.root_dir)
 
254
        self.eq.inotify_add_watch(path1)
 
255
 
 
256
        # we have the watch, move it outside watched structure, no more watches
 
257
        self.assertTrue(self.eq.inotify_has_watch(path1))
 
258
        os.rename(path1, path2)
 
259
 
 
260
        def check(_):
 
261
            """Check state after the event."""
 
262
            self.assertFalse(self.eq.inotify_has_watch(path1))
 
263
            self.assertFalse(self.eq.inotify_has_watch(path2))
 
264
 
 
265
        self.deferred.addCallback(check)
 
266
        return self.deferred
 
267
 
 
268
    def test_fix_path_not_there(self):
 
269
        """Try to fix path but it's not there."""
 
270
        self.eq._general_watchs = {}
 
271
        self.eq._ancestors_watchs = {}
 
272
        self.eq.inotify_watch_fix("not-there", "new-one")
 
273
        self.assertTrue(self.log_handler.check_warning("Tried to fix",
 
274
                                                       "not-there"))
 
275
 
 
276
    def test_fix_path_general(self):
 
277
        """Fixing path in general watches."""
 
278
        self.eq._general_watchs = {'/path1/foo': 1, '/other': 2}
 
279
        self.eq._ancestors_watchs = {'/foo': 3}
 
280
        self.eq.inotify_watch_fix('/path1/foo', '/path1/new')
 
281
        self.assertEqual(self.eq._general_watchs, {'/path1/new': 1, '/other': 2})
 
282
        self.assertEqual(self.eq._ancestors_watchs, {'/foo': 3})
 
283
 
 
284
    def test_fix_path_ancestors(self):
 
285
        """Fixing path in ancestors watches."""
 
286
        self.eq._general_watchs = {'/bar': 3}
 
287
        self.eq._ancestors_watchs = {'/oth': 1, '/other': 2}
 
288
        self.eq.inotify_watch_fix('/oth', '/baz')
 
289
        self.assertEqual(self.eq._general_watchs, {'/bar': 3})
 
290
        self.assertEqual(self.eq._ancestors_watchs, {'/baz': 1, '/other': 2})
 
291
 
 
292
 
175
293
class DynamicHitMe(object):
176
294
    """Helper class to test a sequence of signals."""
177
295
 
221
339
    def setUp(self):
222
340
        """Setup the test."""
223
341
        BaseEQTestCase.setUp(self)
 
342
 
224
343
        # create the deferred for the tests
225
344
        self._deferred = defer.Deferred()
226
345
 
366
485
            def handle_FS_FILE_DELETE(innerself, path):
367
486
                if path != testfile:
368
487
                    self.finished_error("received a wrong path")
369
 
                else:
370
 
                    self.finished_ok()
 
488
                    return
 
489
 
 
490
                if not self.log_handler.check_info('FS_FILE_DELETE', testfile):
 
491
                    self.finished_error("FS_FILE_DELETE must appear in INFO.")
 
492
                    return
 
493
 
 
494
                self.finished_ok()
371
495
 
372
496
        self.eq.inotify_add_watch(self.root_dir)
373
497
        self.eq.subscribe(HitMe())
387
511
            def handle_FS_DIR_DELETE(innerself, path):
388
512
                if path != testdir:
389
513
                    self.finished_error("received a wrong path")
390
 
                else:
391
 
                    self.finished_ok()
 
514
                    return
 
515
 
 
516
                if not self.log_handler.check_info('FS_DIR_DELETE', testdir):
 
517
                    self.finished_error("FS_DIR_DELETE must appear in INFO.")
 
518
                    return
 
519
 
 
520
                # file deletion should remove its watch
 
521
                self.assertFalse(self.eq.inotify_has_watch(testdir))
 
522
 
 
523
                self.finished_ok()
392
524
 
393
525
        self.eq.inotify_add_watch(self.root_dir)
394
526
        self.eq.subscribe(HitMe())