~ubuntu-branches/ubuntu/precise/ubuntuone-client/precise-201112142106

« back to all changes in this revision

Viewing changes to tests/syncdaemon/test_fsm.py

  • Committer: Bazaar Package Importer
  • Author(s): Rodney Dawes
  • Date: 2011-08-25 16:11:47 UTC
  • mfrom: (1.1.54 upstream)
  • Revision ID: james.westby@ubuntu.com-20110825161147-v6zedpznh2evnurj
Tags: 1.7.2-0ubuntu1
* New upstream release.
  - Work correctly with static and GI bindings of gobject (LP: #829186)

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
 
24
24
import errno
25
25
import os
26
 
import shutil
27
26
import time
28
27
 
29
28
from mocker import MockerTestCase, ANY
37
36
)
38
37
 
39
38
from ubuntuone.devtools.handlers import MementoHandler
40
 
from ubuntuone.platform import make_dir, make_link, open_file, path_exists
 
39
from ubuntuone.devtools.testcase import skipIfOS
 
40
from ubuntuone.platform import (
 
41
    listdir,
 
42
    make_dir,
 
43
    make_link,
 
44
    open_file,
 
45
    path_exists,
 
46
    remove_dir,
 
47
    remove_file,
 
48
    set_dir_readonly,
 
49
    set_dir_readwrite,
 
50
    stat_path,
 
51
)
41
52
from ubuntuone.syncdaemon.filesystem_manager import (
42
53
    DirectoryNotRemovable,
43
54
    EnableShareWrite,
57
68
from ubuntuone.syncdaemon.volume_manager import Share, allow_writes
58
69
 
59
70
BROKEN_PICKLE = '\axb80\x02}q\x01(U\x01aU\x04testq\x02U\x01bU\x06brokenq\x03u.'
 
71
skipOnWinIfMetadataPreV4 = \
 
72
    skipIfOS('win32', 'In windows there is no need to migrate metadata older than v5, so we skip these tests.')
60
73
 
61
74
 
62
75
@defer.inlineCallbacks
63
 
def _create_share(share_id, share_name, fsm, shares_dir, access_level='Modify'):
 
76
def _create_share(share_id, share_name, fsm, shares_dir,
 
77
                  access_level='Modify'):
64
78
    """Create a share."""
65
 
    share_path = os.path.join(shares_dir, share_name)
66
 
    os.makedirs(share_path)
 
79
    assert isinstance(share_name, unicode)
 
80
    share_path = os.path.join(shares_dir, share_name.encode('utf8'))
 
81
    make_dir(share_path, recursive=True)
67
82
    share = Share(path=share_path, volume_id=share_id,
68
83
                  access_level=access_level)
69
84
    yield fsm.vm.add_share(share)
84
99
        self.tritcask_path = self.mktemp("tritcask")
85
100
 
86
101
        self.db = Tritcask(self.tritcask_path)
 
102
        self.addCleanup(self.db.shutdown)
87
103
        self.fsm = FileSystemManager(self.fsmdir, self.partials_dir,
88
104
                                     FakeVolumeManager(self.root_dir), self.db)
89
105
        self.eq = EventQueue(self.fsm)
 
106
        self.addCleanup(self.eq.shutdown)
90
107
        self.fsm.register_eq(self.eq)
91
 
        self.share = yield self.create_share('share', 'share_name')
 
108
        self.share = yield self.create_share('share', u'share_name')
92
109
        self.share_path = self.share.path
93
110
        config.get_user_config().set_use_trash(True)
94
111
 
96
113
        self.handler = MementoHandler()
97
114
        self.handler.setLevel(0)
98
115
        logger.root_logger.addHandler(self.handler)
99
 
 
100
 
    @defer.inlineCallbacks
101
 
    def tearDown(self):
102
 
        """Clean up the tests."""
103
 
        self.eq.shutdown()
104
 
        self.db.shutdown()
105
 
        # remove the handler
106
 
        logger.root_logger.removeHandler(self.handler)
107
 
        yield super(FSMTestCase, self).tearDown()
 
116
        self.addCleanup(logger.root_logger.removeHandler, self.handler)
108
117
 
109
118
    @defer.inlineCallbacks
110
119
    def create_share(self, share_id, share_name, fsm=None, shares_dir=None,
111
120
                     access_level='Modify'):
112
121
        """Create a share."""
 
122
        assert isinstance(share_name, unicode)
113
123
        if fsm is None:
114
124
            fsm = self.fsm
115
125
        if shares_dir is None:
142
152
        fsmdir = self.mktemp("a_fsmdir")
143
153
        partials_dir = self.mktemp("a_partials_dir")
144
154
        db = Tritcask(fsmdir)
 
155
        self.addCleanup(db.shutdown)
145
156
        FileSystemManager(fsmdir, partials_dir,
146
157
                          FakeVolumeManager(fsmdir), db)
147
 
        self.assertTrue(os.path.exists(fsmdir))
148
 
        db.shutdown()
 
158
        self.assertTrue(path_exists(fsmdir))
149
159
 
150
160
    @defer.inlineCallbacks
151
161
    def test_complex_startup(self):
156
166
        partials_dir = self.mktemp("a_partials_dir")
157
167
 
158
168
        db = Tritcask(fsmdir)
 
169
        self.addCleanup(db.shutdown)
159
170
        fsm = FileSystemManager(fsmdir, partials_dir,
160
171
                                FakeVolumeManager(fsmdir), db)
161
 
        share = yield _create_share('share', 'share_name',
 
172
        share = yield _create_share('share', u'share_name',
162
173
                                    fsm=fsm, shares_dir=fsmdir)
163
174
        self.assertEqual(fsm._idx_path, {})
164
175
        self.assertEqual(fsm._idx_node_id, {})
186
197
        self.assertEqual(fsm._idx_node_id, {("share","uuid1"):created_mdid1})
187
198
        self.assertTrue(fsm.get_by_mdid(created_mdid1))
188
199
        self.assertTrue(fsm.get_by_mdid(created_mdid2))
189
 
        db.shutdown()
190
200
 
191
201
 
192
202
class CreationTests(FSMTestCase):
282
292
 
283
293
    def test_fresh_metadata(self):
284
294
        """Initing with nothing in the metadata, it should leave it right."""
285
 
        md_version = open(os.path.join(self.fsmdir, "metadata_version")).read()
 
295
        with open_file(os.path.join(self.fsmdir, "metadata_version")) as f:
 
296
            md_version = f.read()
286
297
        self.assertEqual(md_version, METADATA_VERSION)
287
298
 
 
299
    @skipOnWinIfMetadataPreV4
288
300
    @defer.inlineCallbacks
289
301
    def test_old_metadata_None(self):
290
302
        """Test old metadata situation, in None."""
291
303
        # create some stuff
292
304
        path = os.path.join(self.share.path, 'path')
293
 
        open(path, "w").close()
 
305
        open_file(path, "w").close()
294
306
        mdid = self.fsm.create(path, "share")
295
307
        self.fsm.set_node_id(path, "uuid")
296
308
        # create a path with the old layout
297
 
        other_share = yield self.create_share('share1', 'share1_name')
 
309
        other_share = yield self.create_share('share1', u'share1_name')
298
310
        share_mdid = self.fsm.create(other_share.path, "share1")
299
311
        self.fsm.set_node_id(other_share.path, "uuid1")
300
 
        os.makedirs(os.path.join(self.root_dir, 'Ubuntu One'))
 
312
        make_dir(os.path.join(self.root_dir, 'Ubuntu One'), recursive=True)
301
313
        old_shares_path = os.path.join(self.root_dir, 'Ubuntu One',
302
314
                            'Shared With Me')
303
315
        old_path = os.path.join(old_shares_path, 'share1_name')
319
331
 
320
332
        # delete the version that should have left the previous fsm
321
333
        version_file = os.path.join(self.fsmdir, "metadata_version")
322
 
        os.remove(version_file)
 
334
        remove_file(version_file)
323
335
 
324
336
        # create a old-style fs with the data:
325
337
        old_fs = FileShelf(self.fsm.old_fs._path)
328
340
 
329
341
        # start up again, and check
330
342
        db = Tritcask(self.tritcask_path+'.new')
 
343
        self.addCleanup(db.shutdown)
331
344
        newfsm = FileSystemManager(self.fsmdir, self.partials_dir,
332
345
                                   self.fsm.vm, db)
333
 
        md_version = open(version_file).read()
 
346
        md_version = open_file(version_file).read()
334
347
        self.assertEqual(md_version, METADATA_VERSION)
335
348
        newmdobj = newfsm.get_by_path(path)
336
349
        self.assertEqual(newmdobj.mdid, mdid)
337
 
        self.assertEqual(newmdobj.stat, os.stat(path))
 
350
        self.assertEqual(newmdobj.stat, stat_path(path))
338
351
        self.assertEqual(newmdobj.generation, None)
339
352
        self.assertEqual(newmdobj.local_hash, "")
340
353
        self.assertEqual(newmdobj.server_hash, "")
342
355
        self.assertTrue(other_share.path in newfsm._idx_path)
343
356
        self.assertFalse(old_path in self.fsm._idx_path)
344
357
        self.assertFalse(old_path in newfsm._idx_path)
345
 
        db.shutdown()
346
358
 
 
359
    @skipOnWinIfMetadataPreV4
347
360
    @defer.inlineCallbacks
348
361
    def test_old_metadata_1(self):
349
362
        """Test old metadata situation, in v1."""
356
369
        self.fsm.set_node_id(path2, "uuid2")
357
370
 
358
371
        # create a path with the old layout
359
 
        other_share = yield self.create_share('share1', 'share1_name')
 
372
        other_share = yield self.create_share('share1', u'share1_name')
360
373
        share_mdid = self.fsm.create(other_share.path, "share1")
361
374
        self.fsm.set_node_id(other_share.path, "uuid1")
362
 
        os.makedirs(os.path.join(self.root_dir, 'Ubuntu One'))
 
375
        make_dir(os.path.join(self.root_dir, 'Ubuntu One'), recursive=True)
363
376
        old_shares_path = os.path.join(self.root_dir, 'Ubuntu One',
364
377
                            'Shared With Me')
365
378
        old_path = os.path.join(old_shares_path, 'share1_name')
383
396
 
384
397
        # put the old version in file
385
398
        version_file = os.path.join(self.fsmdir, "metadata_version")
386
 
        with open(version_file, "w") as fh:
 
399
        with open_file(version_file, "w") as fh:
387
400
            fh.write("1")
388
401
 
389
402
        # create a old-style fs with the data:
393
406
 
394
407
        # start up again, and check
395
408
        db = Tritcask(self.tritcask_path+'.new')
 
409
        self.addCleanup(db.shutdown)
396
410
        newfsm = FileSystemManager(self.fsmdir, self.partials_dir,
397
411
                                   self.fsm.vm, db)
398
 
        md_version = open(version_file).read()
 
412
        md_version = open_file(version_file).read()
399
413
        self.assertEqual(md_version, METADATA_VERSION)
400
414
        newmdobj = newfsm.get_by_path(path1)
401
415
        self.assertEqual(newmdobj.mdid, mdid1)
407
421
        self.assertEqual(2, len(newfsm._idx_node_id))
408
422
        self.assertTrue(other_share.path in newfsm._idx_path)
409
423
        self.assertFalse(old_path in newfsm._idx_path)
410
 
        db.shutdown()
411
424
 
 
425
    @skipOnWinIfMetadataPreV4
412
426
    @defer.inlineCallbacks
413
427
    def test_old_metadata_2(self):
414
428
        """Test old metadata situation, in v2."""
417
431
        mdid = self.fsm.create(path, "share")
418
432
        self.fsm.set_node_id(path, "uuid")
419
433
        # create a path with the old layout
420
 
        other_share = yield self.create_share('share1', 'share1_name')
 
434
        other_share = yield self.create_share('share1', u'share1_name')
421
435
        share_mdid = self.fsm.create(other_share.path, "share1")
422
436
        self.fsm.set_node_id(other_share.path, "uuid1")
423
 
        os.makedirs(os.path.join(self.root_dir, 'Ubuntu One'))
 
437
        make_dir(os.path.join(self.root_dir, 'Ubuntu One'), recursive=True)
424
438
        old_shares_path = os.path.join(self.root_dir, 'Ubuntu One',
425
439
                            'Shared With Me')
426
440
        old_path = os.path.join(old_shares_path, 'share1_name')
440
454
 
441
455
        # put the old version in file
442
456
        version_file = os.path.join(self.fsmdir, "metadata_version")
443
 
        with open(version_file, "w") as fh:
 
457
        with open_file(version_file, "w") as fh:
444
458
            fh.write("2")
445
459
 
446
460
        # create a old-style fs with the data:
450
464
 
451
465
        # start up again, and check
452
466
        db = Tritcask(self.tritcask_path+'.new')
 
467
        self.addCleanup(db.shutdown)
453
468
        newfsm = FileSystemManager(self.fsmdir, self.partials_dir,
454
469
                                   self.fsm.vm, db)
455
 
        md_version = open(version_file).read()
 
470
        md_version = open_file(version_file).read()
456
471
        self.assertEqual(md_version, METADATA_VERSION)
457
472
        newmdobj = newfsm.get_by_path(path)
458
473
        self.assertEqual(newmdobj.mdid, mdid)
464
479
        self.assertEqual(2, len(newfsm._idx_node_id))
465
480
        self.assertTrue(other_share.path in newfsm._idx_path)
466
481
        self.assertFalse(old_path in newfsm._idx_path)
467
 
        db.shutdown()
468
482
 
 
483
    @skipOnWinIfMetadataPreV4
469
484
    @defer.inlineCallbacks
470
485
    def test_old_metadata_3(self):
471
486
        """Test old metadata situation, in v3."""
474
489
        root_mdid = self.fsm.create(self.root_dir, "")
475
490
        self.fsm.set_node_id(self.root_dir, "uuid")
476
491
        # a share
477
 
        other_share = yield self.create_share('share1', 'share1_name')
 
492
        other_share = yield self.create_share('share1', u'share1_name')
478
493
        share_mdid = self.fsm.create(other_share.path, "share1")
479
494
        self.fsm.set_node_id(other_share.path, "uuid1")
480
 
        os.makedirs(os.path.join(self.root_dir, 'Ubuntu One'))
 
495
        make_dir(os.path.join(self.root_dir, 'Ubuntu One'), recursive=True)
481
496
        old_shares_path = os.path.join(self.root_dir, 'Ubuntu One',
482
497
                            'Shared With Me')
483
498
        old_path = os.path.join(old_shares_path, 'share1_name')
496
511
 
497
512
        # put the old version in file
498
513
        version_file = os.path.join(self.fsmdir, "metadata_version")
499
 
        with open(version_file, "w") as fh:
 
514
        with open_file(version_file, "w") as fh:
500
515
            fh.write("3")
501
516
 
502
517
        # create a old-style fs with the data:
506
521
 
507
522
        # start up again, and check
508
523
        db = Tritcask(self.tritcask_path+'.new')
 
524
        self.addCleanup(db.shutdown)
509
525
        newfsm = FileSystemManager(self.fsmdir, self.partials_dir,
510
526
                                   self.fsm.vm, db)
511
 
        md_version = open(version_file).read()
 
527
        md_version = open_file(version_file).read()
512
528
        self.assertEqual(md_version, METADATA_VERSION)
513
529
        newmdobj = newfsm.get_by_path(other_share.path)
514
530
        self.assertEquals(newmdobj.mdid, share_md['mdid'])
525
541
        self.assertFalse(old_path in newfsm._idx_path)
526
542
        self.assertTrue(root_dir in newfsm._idx_path)
527
543
        self.assertFalse(old_root_path in newfsm._idx_path)
528
 
        db.shutdown()
529
544
 
 
545
    @skipOnWinIfMetadataPreV4
530
546
    def test_old_metadata_4(self):
531
547
        """Test old metadata situation, in v4."""
532
548
        # create some stuff
554
570
 
555
571
        # put the old version in file
556
572
        version_file = os.path.join(self.fsmdir, "metadata_version")
557
 
        with open(version_file, "w") as fh:
 
573
        with open_file(version_file, "w") as fh:
558
574
            fh.write("4")
559
575
 
560
576
        # create a old-style fs with the data:
572
588
 
573
589
        # start up again, and check
574
590
        db = Tritcask(self.tritcask_path+'.new')
 
591
        self.addCleanup(db.shutdown)
575
592
        newfsm = FileSystemManager(self.fsmdir, self.partials_dir,
576
593
                                   self.fsm.vm, db)
577
 
        md_version = open(version_file).read()
 
594
        md_version = open_file(version_file).read()
578
595
        self.assertEqual(md_version, METADATA_VERSION)
579
596
        newmdobj = newfsm.get_by_path(path)
580
597
        self.assertEqual(newmdobj.mdid, mdid)
592
609
        r = [("share", "uuid_1", "old_parent", "new_parent",
593
610
              "new_name", "pfrom", "pto")]
594
611
        self.assertEqual(list(self.fsm.get_iter_move_limbo()), r)
595
 
        db.shutdown()
596
612
 
 
613
    @skipOnWinIfMetadataPreV4
597
614
    def test_old_metadata_5(self):
598
615
        """Test old metadata situation, in v5."""
599
616
        # create some stuff
613
630
 
614
631
        # put the old version in file
615
632
        version_file = os.path.join(self.fsmdir, "metadata_version")
616
 
        with open(version_file, "w") as fh:
 
633
        with open_file(version_file, "w") as fh:
617
634
            fh.write("4")
618
635
 
619
636
        # create a old-style fs with the data:
631
648
 
632
649
        # start up again, and check
633
650
        db = Tritcask(self.tritcask_path+'.new')
 
651
        self.addCleanup(db.shutdown)
634
652
        newfsm = FileSystemManager(self.fsmdir, self.partials_dir,
635
653
                                   self.fsm.vm, db)
636
 
        md_version = open(version_file).read()
 
654
        md_version = open_file(version_file).read()
637
655
        self.assertEqual(md_version, METADATA_VERSION)
638
656
        newmdobj = newfsm.get_by_path(path)
639
657
        self.assertEqual(newmdobj.share_id, 'share')
652
670
        r = [("share", "uuid_1", "old_parent", "new_parent",
653
671
              "new_name", "pfrom", "pto")]
654
672
        self.assertEqual(list(self.fsm.get_iter_move_limbo()), r)
655
 
        db.shutdown()
656
673
 
 
674
    @skipOnWinIfMetadataPreV4
657
675
    def test_old_metadata_None_broken_pickle_wihtout_backup(self):
658
676
        """Test old metadata situation, in None with broken metadata values."""
659
677
        # create some stuff
661
679
        path1 = os.path.join(self.share.path, 'path1')
662
680
        path2 = os.path.join(self.share.path, 'path2')
663
681
        for p in [path, path1, path2]:
664
 
            open(p, "w").close()
 
682
            open_file(p, "w").close()
665
683
        mdid = self.fsm.create(path, "share", node_id='uuid')
666
684
        mdid1 = self.fsm.create(path1, "share", node_id='uuid1')
667
685
        mdid2 = self.fsm.create(path2, "share", node_id='uuid2')
672
690
            old_fs[k] = v
673
691
 
674
692
        # break the node on purpose
675
 
        with open(old_fs.key_file(mdid1), 'w') as f:
 
693
        with open_file(old_fs.key_file(mdid1), 'w') as f:
676
694
            f.write(BROKEN_PICKLE)
677
695
            os.fsync(f.fileno())
678
696
 
679
697
        #break the node by creating a 0 byte pickle
680
 
        with open(old_fs.key_file(mdid2), 'w') as f:
 
698
        with open_file(old_fs.key_file(mdid2), 'w') as f:
681
699
            os.fsync(f.fileno())
682
700
 
683
701
        # delete the version that should have left the previous fsm
684
702
        version_file = os.path.join(self.fsmdir, "metadata_version")
685
 
        os.remove(version_file)
 
703
        remove_file(version_file)
686
704
 
687
705
        # start up again, and check
688
706
        db = Tritcask(self.tritcask_path+'.new')
 
707
        self.addCleanup(db.shutdown)
689
708
        newfsm = FileSystemManager(self.fsmdir, self.partials_dir,
690
709
                                   self.fsm.vm, db)
691
 
        md_version = open(version_file).read()
 
710
        md_version = open_file(version_file).read()
692
711
        self.assertEqual(md_version, METADATA_VERSION)
693
712
        self.assertTrue(newfsm.get_by_mdid(mdid) is not None)
694
713
        self.assertRaises(KeyError, newfsm.get_by_mdid, mdid1)
695
714
        self.assertRaises(KeyError, newfsm.get_by_mdid, mdid2)
696
 
        db.shutdown()
697
715
 
 
716
    @skipOnWinIfMetadataPreV4
698
717
    def test_old_metadata_1_broken_pickle_without_backup(self):
699
718
        """Test old metadata situation, in v1 with broken metadata values."""
700
719
        # create some stuff
718
737
            old_fs[k] = v
719
738
 
720
739
        # break the second node on purpose but with an invalid pickle
721
 
        with open(old_fs.key_file(mdid1), 'w') as f:
 
740
        with open_file(old_fs.key_file(mdid1), 'w') as f:
722
741
            f.write(BROKEN_PICKLE)
723
742
            os.fsync(f.fileno())
724
743
        #break the third node by creating a 0 byte pickle
725
 
        with open(old_fs.key_file(mdid2), 'w') as f:
 
744
        with open_file(old_fs.key_file(mdid2), 'w') as f:
726
745
            os.fsync(f.fileno())
727
746
 
728
747
        # put the version file in 1
729
748
        version_file = os.path.join(self.fsmdir, "metadata_version")
730
 
        with open(version_file, "w") as fh:
 
749
        with open_file(version_file, "w") as fh:
731
750
            fh.write("1")
732
751
 
733
752
        # start up again, and check
734
753
        db = Tritcask(self.tritcask_path+'.new')
 
754
        self.addCleanup(db.shutdown)
735
755
        newfsm = FileSystemManager(self.fsmdir, self.partials_dir,
736
756
                                   self.fsm.vm, db)
737
 
        md_version = open(version_file).read()
 
757
        md_version = open_file(version_file).read()
738
758
        self.assertEqual(md_version, METADATA_VERSION)
739
759
        newmdobj = newfsm.get_by_path(path)
740
760
        self.assertEqual(newmdobj.mdid, mdid)
745
765
        self.assertRaises(KeyError, newfsm.get_by_mdid, mdid2)
746
766
        # pylint: disable-msg=W0212
747
767
        self.assertEqual(1, len(newfsm._idx_node_id))
748
 
        db.shutdown()
749
768
 
 
769
    @skipOnWinIfMetadataPreV4
750
770
    def test_old_metadata_2_broken_pickle_without_backup(self):
751
771
        """Test old metadata situation, in v2 with broken metadata values."""
752
772
        # create some stuff
754
774
        path1 = os.path.join(self.share.path, 'path1')
755
775
        path2 = os.path.join(self.share.path, 'path2')
756
776
        for p in [path, path1, path2]:
757
 
            open(p, "w").close()
 
777
            open_file(p, "w").close()
758
778
        mdid = self.fsm.create(path, "share", node_id='uuid')
759
779
        mdid1 = self.fsm.create(path1, "share", node_id='uuid1')
760
780
        mdid2 = self.fsm.create(path2, "share", node_id='uuid2')
772
792
 
773
793
        # put the version file in 1
774
794
        version_file = os.path.join(self.fsmdir, "metadata_version")
775
 
        with open(version_file, "w") as fh:
 
795
        with open_file(version_file, "w") as fh:
776
796
            fh.write("2")
777
797
        # break the second node on purpose but with an invalid pickle
778
 
        with open(old_fs.key_file(mdid1), 'w') as f:
 
798
        with open_file(old_fs.key_file(mdid1), 'w') as f:
779
799
            f.write(BROKEN_PICKLE)
780
800
            os.fsync(f.fileno())
781
801
        #break the third node by creating a 0 byte pickle
782
 
        with open(old_fs.key_file(mdid2), 'w') as f:
 
802
        with open_file(old_fs.key_file(mdid2), 'w') as f:
783
803
            os.fsync(f.fileno())
784
804
 
785
805
        # start up again, and check
786
806
        db = Tritcask(self.tritcask_path+'.new')
 
807
        self.addCleanup(db.shutdown)
787
808
        newfsm = FileSystemManager(self.fsmdir, self.partials_dir,
788
809
                                   self.fsm.vm, db)
789
 
        md_version = open(version_file).read()
 
810
        md_version = open_file(version_file).read()
790
811
        self.assertEqual(md_version, METADATA_VERSION)
791
812
        newmdobj = newfsm.get_by_path(path)
792
813
        self.assertEqual(newmdobj.mdid, mdid)
797
818
        self.assertRaises(KeyError, newfsm.get_by_mdid, mdid2)
798
819
        # pylint: disable-msg=W0212
799
820
        self.assertEqual(1, len(newfsm._idx_node_id))
800
 
        db.shutdown()
801
821
 
 
822
    @skipOnWinIfMetadataPreV4
802
823
    def test_old_metadata_None_broken_pickle_with_backup(self):
803
824
        """Test old metadata situation, in None with broken metadata values."""
804
825
        # create some stuff
806
827
        path1 = os.path.join(self.share.path, 'path1')
807
828
        path2 = os.path.join(self.share.path, 'path2')
808
829
        for p in [path, path1, path2]:
809
 
            open(p, "w").close()
 
830
            open_file(p, "w").close()
810
831
        mdid = self.fsm.create(path, "share")
811
832
        self.fsm.set_node_id(path, "uuid")
812
833
        mdid1 = self.fsm.create(path1, "share")
828
849
        old_fs[mdid2] = mdobj
829
850
        old_fs[mdid2] = mdobj
830
851
        # break the node on purpose
831
 
        with open(old_fs.key_file(mdid1), 'w') as f:
 
852
        with open_file(old_fs.key_file(mdid1), 'w') as f:
832
853
            f.write(BROKEN_PICKLE)
833
854
            os.fsync(f.fileno())
834
855
 
835
856
        #break the node by creating a 0 byte pickle
836
 
        with open(old_fs.key_file(mdid2), 'w') as f:
 
857
        with open_file(old_fs.key_file(mdid2), 'w') as f:
837
858
            os.fsync(f.fileno())
838
859
 
839
860
        # delete the version that should have left the previous fsm
840
861
        version_file = os.path.join(self.fsmdir, "metadata_version")
841
 
        os.remove(version_file)
 
862
        remove_file(version_file)
842
863
 
843
864
        # start up again, and check
844
865
        db = Tritcask(self.tritcask_path+'.new')
 
866
        self.addCleanup(db.shutdown)
845
867
        newfsm = FileSystemManager(self.fsmdir, self.partials_dir,
846
868
                                   self.fsm.vm, db)
847
 
        md_version = open(version_file).read()
 
869
        md_version = open_file(version_file).read()
848
870
        self.assertEqual(md_version, METADATA_VERSION)
849
871
        self.assertTrue(newfsm.get_by_mdid(mdid) is not None)
850
872
        # pylint: disable-msg=W0212
853
875
        # check that the broken mdid's load the old metadata
854
876
        self.assertEquals(None, newfsm.get_by_mdid(mdid1).node_id)
855
877
        self.assertEquals(None, newfsm.get_by_mdid(mdid2).node_id)
856
 
        db.shutdown()
857
878
 
 
879
    @skipOnWinIfMetadataPreV4
858
880
    def test_old_metadata_1_broken_pickle_with_backup(self):
859
881
        """Test old metadata situation, in v1 with broken metadata values."""
860
882
        # create some stuff
889
911
        old_fs[mdid2] = mdobj
890
912
        old_fs[mdid2] = mdobj
891
913
        # break the second node on purpose but with an invalid pickle
892
 
        with open(old_fs.key_file(mdid1), 'w') as f:
 
914
        with open_file(old_fs.key_file(mdid1), 'w') as f:
893
915
            f.write(BROKEN_PICKLE)
894
916
            os.fsync(f.fileno())
895
917
        #break the third node by creating a 0 byte pickle
896
 
        with open(old_fs.key_file(mdid2), 'w') as f:
 
918
        with open_file(old_fs.key_file(mdid2), 'w') as f:
897
919
            os.fsync(f.fileno())
898
920
 
899
921
        # put the version file in 1
900
922
        version_file = os.path.join(self.fsmdir, "metadata_version")
901
 
        with open(version_file, "w") as fh:
 
923
        with open_file(version_file, "w") as fh:
902
924
            fh.write("1")
903
925
 
904
926
        # start up again, and check
905
927
        db = Tritcask(self.tritcask_path+'.new')
 
928
        self.addCleanup(db.shutdown)
906
929
        newfsm = FileSystemManager(self.fsmdir, self.partials_dir,
907
930
                                   self.fsm.vm, db)
908
 
        md_version = open(version_file).read()
 
931
        md_version = open_file(version_file).read()
909
932
        self.assertEqual(md_version, METADATA_VERSION)
910
933
        newmdobj = newfsm.get_by_path(path)
911
934
        self.assertEqual(newmdobj.mdid, mdid)
918
941
        # check that the broken mdid's load the old metadata
919
942
        self.assertEquals(None, newfsm.get_by_mdid(mdid1).node_id)
920
943
        self.assertEquals(None, newfsm.get_by_mdid(mdid2).node_id)
921
 
        db.shutdown()
922
944
 
 
945
    @skipOnWinIfMetadataPreV4
923
946
    def test_old_metadata_2_broken_pickle_with_backup(self):
924
947
        """Test old metadata situation, in v2 with broken metadata values."""
925
948
        # create some stuff
927
950
        path1 = os.path.join(self.share.path, 'path1')
928
951
        path2 = os.path.join(self.share.path, 'path2')
929
952
        for p in [path, path1, path2]:
930
 
            open(p, "w").close()
 
953
            open_file(p, "w").close()
931
954
        mdid = self.fsm.create(path, "share")
932
955
        self.fsm.set_node_id(path, "uuid")
933
956
        mdid1 = self.fsm.create(path1, "share")
943
966
 
944
967
        # put the version file in 1
945
968
        version_file = os.path.join(self.fsmdir, "metadata_version")
946
 
        with open(version_file, "w") as fh:
 
969
        with open_file(version_file, "w") as fh:
947
970
            fh.write("2")
948
971
 
949
972
        # create a old-style fs with the data
962
985
        old_fs[mdid2] = mdobj
963
986
 
964
987
        # break the second node on purpose but with an invalid pickle
965
 
        with open(old_fs.key_file(mdid1), 'w') as f:
 
988
        with open_file(old_fs.key_file(mdid1), 'w') as f:
966
989
            f.write(BROKEN_PICKLE)
967
990
            os.fsync(f.fileno())
968
991
        #break the third node by creating a 0 byte pickle
969
 
        with open(old_fs.key_file(mdid2), 'w') as f:
 
992
        with open_file(old_fs.key_file(mdid2), 'w') as f:
970
993
            os.fsync(f.fileno())
971
994
 
972
995
        # start up again, and check
973
996
        db = Tritcask(self.tritcask_path+'.new')
 
997
        self.addCleanup(db.shutdown)
974
998
        newfsm = FileSystemManager(self.fsmdir, self.partials_dir,
975
999
                                   self.fsm.vm, db)
976
 
        md_version = open(version_file).read()
 
1000
        md_version = open_file(version_file).read()
977
1001
        self.assertEqual(md_version, METADATA_VERSION)
978
1002
        newmdobj = newfsm.get_by_path(path)
979
1003
        self.assertEqual(newmdobj.mdid, mdid)
986
1010
        # check that the broken mdid's load the old metadata
987
1011
        self.assertEquals(None, newfsm.get_by_mdid(mdid1).node_id)
988
1012
        self.assertEquals(None, newfsm.get_by_mdid(mdid2).node_id)
989
 
        db.shutdown()
990
1013
 
991
1014
    def test_current_metadata_phantom_node_older(self):
992
1015
        """Test current metadata with a phantom node."""
1006
1029
 
1007
1030
        # start up again, and check
1008
1031
        db = Tritcask(self.tritcask_path)
 
1032
        self.addCleanup(db.shutdown)
1009
1033
        # patch this tritcask instance to return the keys ordered by tstamp
1010
1034
        # (reversed), in order to make this test deterministic.
1011
1035
        def rsorted_keys():
1023
1047
        self.handler.check_warning("Path already in the index: %s" % (path))
1024
1048
        self.handler.check_debug("Replacing and deleting node %s witth newer "
1025
1049
                                 "node: %s" % (mdid, mdid_1))
1026
 
        db.shutdown()
1027
1050
 
1028
1051
    def test_current_metadata_phantom_node_newer(self):
1029
1052
        """Test current metadata with a phantom node."""
1043
1066
 
1044
1067
        # start up again, and check
1045
1068
        db = Tritcask(self.tritcask_path)
 
1069
        self.addCleanup(db.shutdown)
1046
1070
        # patch this tritcask instance to return the keys ordered by tstamp
1047
1071
        # (reversed), in order to make this test deterministic.
1048
1072
        def sorted_keys():
1061
1085
        self.handler.check_debug("The node: %s is newer than: %s, "
1062
1086
                                 "leaving it alone and deleting the old one.",
1063
1087
                                 mdid, mdid_1)
1064
 
        db.shutdown()
 
1088
 
1065
1089
 
1066
1090
class GetSetTests(FSMTestCase):
1067
1091
    """Test the get/set interface."""
1331
1355
    def test_get_all_by_share(self):
1332
1356
        """ Test that it returns all the mdids in a share. """
1333
1357
        # create the shares
1334
 
        share1 = yield self.create_share('share_id1', 'share_name1',
 
1358
        share1 = yield self.create_share('share_id1', u'share_name1',
1335
1359
                                         access_level='View')
1336
 
        share2 = yield self.create_share('share_id2', 'share_name2',
 
1360
        share2 = yield self.create_share('share_id2', u'share_name2',
1337
1361
                                         access_level='View')
1338
1362
        self.fsm.create(share1.path, "share_id1", is_dir=True)
1339
1363
        self.fsm.set_node_id(share1.path, "uuid1")
1364
1388
        mdid8 = self.fsm.create(path8, "share_id2")
1365
1389
 
1366
1390
        # get them
1367
 
        all = set()
 
1391
        all_data = set()
1368
1392
        for mdobj in self.fsm.get_mdobjs_by_share_id("share_id1"):
1369
 
            all.add(mdobj.mdid)
1370
 
        self.assertTrue(mdid3 in all)
1371
 
        self.assertTrue(mdid4 in all)
1372
 
        self.assertTrue(mdid5 in all)
1373
 
        self.assertTrue(mdid6 not in all)
1374
 
        self.assertTrue(mdid7 not in all)
1375
 
        self.assertTrue(mdid8 not in all)
 
1393
            all_data.add(mdobj.mdid)
 
1394
        self.assertTrue(mdid3 in all_data)
 
1395
        self.assertTrue(mdid4 in all_data)
 
1396
        self.assertTrue(mdid5 in all_data)
 
1397
        self.assertTrue(mdid6 not in all_data)
 
1398
        self.assertTrue(mdid7 not in all_data)
 
1399
        self.assertTrue(mdid8 not in all_data)
1376
1400
 
1377
 
        all = set()
 
1401
        all_data = set()
1378
1402
        for mdobj in self.fsm.get_mdobjs_by_share_id("share_id2"):
1379
 
            all.add(mdobj.mdid)
1380
 
        self.assertTrue(mdid3 not in all)
1381
 
        self.assertTrue(mdid4 not in all)
1382
 
        self.assertTrue(mdid5 not in all)
1383
 
        self.assertTrue(mdid6 in all)
1384
 
        self.assertTrue(mdid7 in all)
1385
 
        self.assertTrue(mdid8 in all)
 
1403
            all_data.add(mdobj.mdid)
 
1404
        self.assertTrue(mdid3 not in all_data)
 
1405
        self.assertTrue(mdid4 not in all_data)
 
1406
        self.assertTrue(mdid5 not in all_data)
 
1407
        self.assertTrue(mdid6 in all_data)
 
1408
        self.assertTrue(mdid7 in all_data)
 
1409
        self.assertTrue(mdid8 in all_data)
1386
1410
 
1387
 
        all = set()
1388
 
        for mdobj in self.fsm.get_mdobjs_by_share_id("share_id1", os.
1389
 
                                                 path.join(share1.path, 'a')):
1390
 
            all.add(mdobj.mdid)
1391
 
        self.assertTrue(mdid3 in all)
1392
 
        self.assertTrue(mdid4 in all)
1393
 
        self.assertTrue(mdid5 not in all)
1394
 
        self.assertTrue(mdid6 not in all)
1395
 
        self.assertTrue(mdid7 not in all)
1396
 
        self.assertTrue(mdid8 not in all)
 
1411
        all_data = set()
 
1412
        patha = os.path.join(share1.path, 'a')
 
1413
        for mdobj in self.fsm.get_mdobjs_by_share_id("share_id1", patha):
 
1414
            all_data.add(mdobj.mdid)
 
1415
        self.assertTrue(mdid3 in all_data)
 
1416
        self.assertTrue(mdid4 in all_data)
 
1417
        self.assertTrue(mdid5 not in all_data)
 
1418
        self.assertTrue(mdid6 not in all_data)
 
1419
        self.assertTrue(mdid7 not in all_data)
 
1420
        self.assertTrue(mdid8 not in all_data)
1397
1421
 
1398
1422
    @defer.inlineCallbacks
1399
1423
    def test_get_all_by_share_mixed(self):
1400
1424
        """Test that it returns all the mdids in a share with mixed nodes."""
1401
1425
        # create the shares
1402
 
        share = yield self.create_share('share_id', 'sharetest',
 
1426
        share = yield self.create_share('share_id', u'sharetest',
1403
1427
                                        access_level='View')
1404
1428
        self.fsm.create(share.path, "share_id", is_dir=True)
1405
1429
        self.fsm.set_node_id(share.path, "uuid")
1416
1440
        mdid3 = self.fsm.create(path3, "share_id")
1417
1441
 
1418
1442
        # get them
1419
 
        all = set()
 
1443
        all_data = set()
1420
1444
        for mdobj in self.fsm.get_mdobjs_by_share_id("share_id"):
1421
 
            all.add(mdobj.mdid)
1422
 
        self.assertTrue(mdid1 in all)
1423
 
        self.assertTrue(mdid2 in all)
1424
 
        self.assertTrue(mdid3 in all)
 
1445
            all_data.add(mdobj.mdid)
 
1446
        self.assertTrue(mdid1 in all_data)
 
1447
        self.assertTrue(mdid2 in all_data)
 
1448
        self.assertTrue(mdid3 in all_data)
1425
1449
 
1426
1450
    def test_internal_set_node_id(self):
1427
1451
        """Test _set_node_id"""
1491
1515
 
1492
1516
    def test_no_tree(self):
1493
1517
        """Test just receiving the dir and not the tree."""
1494
 
        expected = ['a/b', 'a/b1', 'a/b2', 'a/c', 'a/x.txt']
 
1518
        expected = [os.path.join('a', 'b'),
 
1519
                    os.path.join('a', 'b1'),
 
1520
                    os.path.join( 'a', 'b2'),
 
1521
                    os.path.join( 'a', 'c'),
 
1522
                    os.path.join( 'a', 'x.txt')]
1495
1523
        actual = sorted([d.path for d in self.fsm.get_mdobjs_in_dir(
1496
1524
                                    os.path.join(self.share.path, 'a'))])
1497
1525
        self.assertEqual(expected, actual)
1498
1526
 
1499
1527
    def test_similar_paths(self):
1500
1528
        """Test having similar paths (a/b, a/b1, a/b2)."""
1501
 
        expected = ['a/b/y.txt']
 
1529
        expected = [os.path.join('a', 'b', 'y.txt')]
1502
1530
        actual = sorted([d.path for d in self.fsm.get_mdobjs_in_dir(
1503
1531
                                    os.path.join(self.share.path, 'a', 'b'))])
1504
1532
        self.assertEqual(expected, actual)
1506
1534
    @defer.inlineCallbacks
1507
1535
    def test_with_two_shares(self):
1508
1536
        """Test having 2 shares."""
1509
 
        second_share = yield self.create_share('second_share', 'the_second')
 
1537
        second_share = yield self.create_share('second_share', u'the_second')
1510
1538
        self.create_some_contents(second_share)
1511
1539
 
1512
1540
        expected = ['a']
1517
1545
    @defer.inlineCallbacks
1518
1546
    def test_both_shares(self):
1519
1547
        """Test having 2 shares and asking for mdobjs in shares_dir."""
1520
 
        second_share = yield self.create_share('second_share', 'the_second')
 
1548
        second_share = yield self.create_share('second_share', u'the_second')
1521
1549
        self.create_some_contents(second_share)
1522
1550
 
1523
1551
        expected = []
1538
1566
        """Test creation when there's a file."""
1539
1567
        # file
1540
1568
        path = os.path.join(self.share.path, "thisfile")
1541
 
        open(path, "w").close()
 
1569
        open_file(path, "w").close()
1542
1570
        mdobj = self.create_node("thisfile")
1543
 
        self.assertEqual(mdobj.stat, os.stat(path))
 
1571
        self.assertEqual(mdobj.stat, stat_path(path))
1544
1572
 
1545
1573
        # dir
1546
1574
        path = os.path.join(self.share.path, "thisdir")
1547
 
        os.mkdir(path)
 
1575
        make_dir(path)
1548
1576
        mdobj = self.create_node("thisdir")
1549
 
        self.assertEqual(mdobj.stat, os.stat(path))
 
1577
        self.assertEqual(mdobj.stat, stat_path(path))
1550
1578
 
1551
1579
    def test_commit_partial(self):
1552
1580
        """Test that it's updated in the commit."""
1553
1581
        path = os.path.join(self.share.path, "thisfile")
1554
 
        open(path, "w").close()
 
1582
        open_file(path, "w").close()
1555
1583
        mdobj = self.create_node("thisfile")
1556
1584
        mdid = mdobj.mdid
1557
 
        oldstat = os.stat(path)
 
1585
        oldstat = stat_path(path)
1558
1586
        self.assertEqual(mdobj.stat, oldstat)
1559
1587
 
1560
1588
        # create a partial
1568
1596
        # commit the partial
1569
1597
        self.fsm.commit_partial(mdobj.node_id, mdobj.share_id, "localhash")
1570
1598
        mdobj = self.fsm.get_by_mdid(mdid)
1571
 
        self.assertEqual(mdobj.stat, os.stat(path))
 
1599
        self.assertEqual(mdobj.stat, stat_path(path))
1572
1600
 
1573
1601
    def test_move(self):
1574
1602
        """Test that move refreshes stat."""
1575
1603
        path1 = os.path.join(self.share.path, "thisfile1")
1576
1604
        path2 = os.path.join(self.share.path, "thisfile2")
1577
 
        open(path1, "w").close()
 
1605
        open_file(path1, "w").close()
1578
1606
        mdobj = self.create_node(path1)
1579
 
        self.assertEqual(mdobj.stat, os.stat(path1))
 
1607
        self.assertEqual(mdobj.stat, stat_path(path1))
1580
1608
 
1581
1609
        # move
1582
1610
        self.fsm.move_file("share", path1, path2)
1583
1611
 
1584
1612
        # check
1585
1613
        mdobj = self.fsm.get_by_path(path2)
1586
 
        self.assertEqual(mdobj.stat, os.stat(path2))
 
1614
        self.assertEqual(mdobj.stat, stat_path(path2))
1587
1615
 
1588
1616
    def test_move_overwriting(self):
1589
1617
        """Test that move refreshes stat when overwrites other file."""
1590
1618
        self.fsm.create(self.share_path, self.share.id, is_dir=True)
1591
1619
        path1 = os.path.join(self.share.path, "thisfile1")
1592
1620
        path2 = os.path.join(self.share.path, "thisfile2")
1593
 
        open(path1, "w").close()
1594
 
        open(path2, "w").close()
 
1621
        open_file(path1, "w").close()
 
1622
        open_file(path2, "w").close()
1595
1623
        mdobj1 = self.create_node(path1)
1596
1624
        mdobj2 = self.create_node(path2)
1597
 
        self.assertEqual(mdobj1.stat, os.stat(path1))
1598
 
        self.assertEqual(mdobj2.stat, os.stat(path2))
 
1625
        self.assertEqual(mdobj1.stat, stat_path(path1))
 
1626
        self.assertEqual(mdobj2.stat, stat_path(path2))
1599
1627
 
1600
1628
        # move
1601
1629
        self.fsm.move_file("share", path1, path2)
1603
1631
        # check
1604
1632
        self.assertRaises(KeyError, self.fsm.get_by_path, path1)
1605
1633
        mdobj2 = self.fsm.get_by_path(path2)
1606
 
        self.assertEqual(mdobj2.stat, os.stat(path2))
 
1634
        self.assertEqual(mdobj2.stat, stat_path(path2))
1607
1635
 
1608
1636
    def test_set_stat_by_mdid(self):
1609
1637
        """Test that update_stat works."""
1610
1638
        path = os.path.join(self.share.path, "thisfile")
1611
 
        open(path, "w").close()
 
1639
        open_file(path, "w").close()
1612
1640
        mdobj = self.create_node("thisfile")
1613
1641
        mdid = mdobj.mdid
1614
 
        oldstat = os.stat(path)
 
1642
        oldstat = stat_path(path)
1615
1643
        self.assertEqual(mdobj.stat, oldstat)
1616
1644
 
1617
1645
        # touch the file, it's not automagically updated
1618
 
        open(path, "w").close()
 
1646
        open_file(path, "w").close()
1619
1647
        mdobj = self.fsm.get_by_mdid(mdid)
1620
1648
        self.assertEqual(mdobj.stat, oldstat)
1621
1649
 
1639
1667
        # create partial ok
1640
1668
        self.fsm.create_partial("uuid", "share")
1641
1669
        self.assertTrue(self.fsm.get_by_mdid(mdid).info.is_partial)
1642
 
        self.assertTrue(os.path.exists(partial_path))
 
1670
        self.assertTrue(path_exists(partial_path))
1643
1671
        mdobj = self.fsm.get_by_mdid(mdid)
1644
1672
        when = mdobj.info.last_partial_created
1645
1673
        now = time.time()
1663
1691
        # create partial
1664
1692
        self.fsm.create_partial("uuid", "share")
1665
1693
        self.assertTrue(self.fsm.get_by_mdid(mdid).info.is_partial)
1666
 
        with open(partial_path, "w") as fh:
 
1694
        with open_file(partial_path, "w") as fh:
1667
1695
            fh.write("test info!")
1668
1696
 
1669
1697
        # commit partial, and check that the file is moved, and metadata is ok
1670
1698
        self.fsm.commit_partial("uuid", "share", local_hash=9876)
1671
 
        self.assertFalse(os.path.exists(partial_path))
1672
 
        with open(testfile) as fh:
 
1699
        self.assertFalse(path_exists(partial_path))
 
1700
        with open_file(testfile) as fh:
1673
1701
            in_file = fh.read()
1674
1702
        self.assertEqual(in_file, "test info!")
1675
1703
        mdobj = self.fsm.get_by_mdid(mdid)
1699
1727
        # create partial
1700
1728
        self.fsm.create_partial("uuid", "share")
1701
1729
        self.assertTrue(self.fsm.get_by_mdid(mdid).info.is_partial)
1702
 
        with open(partial_path, "w") as fh:
 
1730
        with open_file(partial_path, "w") as fh:
1703
1731
            fh.write("test info!")
1704
 
        with open(testfile, "w") as fh:
 
1732
        with open_file(testfile, "w") as fh:
1705
1733
            fh.write("previous stuff!")
1706
1734
 
1707
1735
        # remove partial, and check that the file is gone, and metadata is ok
1708
1736
        self.fsm.remove_partial("uuid", "share")
1709
 
        self.assertFalse(os.path.exists(partial_path))
1710
 
        with open(testfile) as fh:
 
1737
        self.assertFalse(path_exists(partial_path))
 
1738
        with open_file(testfile) as fh:
1711
1739
            in_file = fh.read()
1712
1740
        self.assertEqual(in_file, "previous stuff!")
1713
1741
        mdobj = self.fsm.get_by_mdid(mdid)
1728
1756
        testdir = os.path.join(self.share_path, "path")
1729
1757
        mdid = self.fsm.create(testdir, "share", is_dir=True)
1730
1758
        self.fsm.set_node_id(testdir, "uuid")
1731
 
        os.mkdir(testdir)
 
1759
        make_dir(testdir)
1732
1760
 
1733
1761
        # create partial ok
1734
1762
        self.fsm.create_partial("uuid", "share")
1735
1763
        self.assertTrue(self.fsm.get_by_mdid(mdid).info.is_partial)
1736
1764
        partial_path = os.path.join(self.fsm.partials_dir,
1737
1765
                            mdid + ".u1partial." + os.path.basename(testdir))
1738
 
        self.assertTrue(os.path.exists(partial_path))
 
1766
        self.assertTrue(path_exists(partial_path))
1739
1767
        mdobj = self.fsm.get_by_mdid(mdid)
1740
1768
        when = mdobj.info.last_partial_created
1741
1769
        now = time.time()
1756
1784
        # create partial ok
1757
1785
        self.fsm.create_partial("uuid", "share")
1758
1786
        self.assertTrue(self.fsm.get_by_mdid(mdid).info.is_partial)
1759
 
        self.assertTrue(os.path.exists(testdir))
 
1787
        self.assertTrue(path_exists(testdir))
1760
1788
        partial_path = os.path.join(self.fsm.partials_dir,
1761
1789
                            mdid + ".u1partial." + os.path.basename(testdir))
1762
 
        self.assertTrue(os.path.exists(partial_path))
 
1790
        self.assertTrue(path_exists(partial_path))
1763
1791
        mdobj = self.fsm.get_by_mdid(mdid)
1764
1792
        when = mdobj.info.last_partial_created
1765
1793
        now = time.time()
1799
1827
        self.fsm.remove_partial("uuid", "share")
1800
1828
        partial_path = os.path.join(self.fsm.partials_dir,
1801
1829
                            mdid + ".u1partial." + os.path.basename(testdir))
1802
 
        self.assertFalse(os.path.exists(partial_path))
 
1830
        self.assertFalse(path_exists(partial_path))
1803
1831
        mdobj = self.fsm.get_by_mdid(mdid)
1804
1832
        self.assertFalse(mdobj.info.is_partial)
1805
1833
        when = mdobj.info.last_partial_removed
1818
1846
 
1819
1847
        It should leave the partials dir permissions intact.
1820
1848
        """
1821
 
        share = yield self.create_share('ro_share', 'ro_share_name',
 
1849
        share = yield self.create_share('ro_share', u'ro_share_name',
1822
1850
                                        access_level='View')
1823
1851
        testdir = os.path.join(share.path, "path")
1824
1852
        mdid = self.fsm.create(testdir, share.volume_id, is_dir=False)
1842
1870
        self.fsm.set_node_id(testfile, "uuid")
1843
1871
 
1844
1872
        # ugly problem not handled
1845
 
        os.rmdir(self.partials_dir)
 
1873
        remove_dir(self.partials_dir)
1846
1874
        try:
1847
1875
            self.fsm.create_partial("uuid", "share")
1848
1876
        except IOError, e:
1871
1899
                    repeat -= 10
1872
1900
            else:
1873
1901
                fh.close()
1874
 
                os.unlink(testfile)
 
1902
                remove_file(testfile)
1875
1903
                break
1876
1904
        mdid = self.fsm.create(testfile, "share")
1877
1905
        self.fsm.set_node_id(testfile, "uuid")
1973
2001
        testfile = os.path.join(self.share_path, "path")
1974
2002
        mdid = self.fsm.create(testfile, "share")
1975
2003
        self.fsm.set_node_id(testfile, "uuid")
1976
 
        with open(testfile, "w") as fh:
 
2004
        with open_file(testfile, "w") as fh:
1977
2005
            fh.write("test!")
1978
2006
 
1979
2007
        # move first time
1980
2008
        self.fsm.move_to_conflict(mdid)
1981
 
        self.assertFalse(os.path.exists(testfile))
1982
 
        with open(testfile + self.fsm.CONFLICT_SUFFIX) as fh:
 
2009
        self.assertFalse(path_exists(testfile))
 
2010
        with open_file(testfile + self.fsm.CONFLICT_SUFFIX) as fh:
1983
2011
            in_file = fh.read()
1984
2012
        self.assertEqual(in_file, "test!")
1985
2013
        mdobj = self.fsm.get_by_mdid(mdid)
1988
2016
        self.assertTrue(now-3 <= when <= now) # 3 seconds test range
1989
2017
 
1990
2018
        # move second time, start the .N serie
1991
 
        with open(testfile, "w") as fh:
 
2019
        with open_file(testfile, "w") as fh:
1992
2020
            fh.write("test 1!")
1993
2021
        self.fsm.move_to_conflict(mdid)
1994
 
        self.assertFalse(os.path.exists(testfile))
1995
 
        with open(testfile + ".u1conflict.1") as fh:
 
2022
        self.assertFalse(path_exists(testfile))
 
2023
        with open_file(testfile + ".u1conflict.1") as fh:
1996
2024
            in_file = fh.read()
1997
2025
        self.assertEqual(in_file, "test 1!")
1998
2026
 
1999
2027
        # create a few more, test a higher one
2000
 
        open(testfile + ".u1conflict.2", "w").close()
2001
 
        open(testfile + ".u1conflict.3", "w").close()
2002
 
        open(testfile + ".u1conflict.4", "w").close()
2003
 
        open(testfile + ".u1conflict.5", "w").close()
2004
 
        with open(testfile, "w") as fh:
 
2028
        open_file(testfile + ".u1conflict.2", "w").close()
 
2029
        open_file(testfile + ".u1conflict.3", "w").close()
 
2030
        open_file(testfile + ".u1conflict.4", "w").close()
 
2031
        open_file(testfile + ".u1conflict.5", "w").close()
 
2032
        with open_file(testfile, "w") as fh:
2005
2033
            fh.write("test 6!")
2006
2034
        self.fsm.move_to_conflict(mdid)
2007
 
        self.assertFalse(os.path.exists(testfile))
2008
 
        with open(testfile + ".u1conflict.6") as fh:
 
2035
        self.assertFalse(path_exists(testfile))
 
2036
        with open_file(testfile + ".u1conflict.6") as fh:
2009
2037
            in_file = fh.read()
2010
2038
        self.assertEqual(in_file, "test 6!")
2011
2039
 
2020
2048
        testfile = os.path.join(self.share_path, "path")
2021
2049
        mdid = self.fsm.create(testfile, "share")
2022
2050
        self.fsm.set_node_id(testfile, "uuid")
2023
 
        with open(testfile, "w") as fh:
 
2051
        with open_file(testfile, "w") as fh:
2024
2052
            fh.write("test!")
2025
2053
 
2026
2054
        self.fsm.move_to_conflict(mdid)
2038
2066
        testdir = os.path.join(self.share_path, "path")
2039
2067
        mdid = self.fsm.create(testdir, "share", is_dir=True)
2040
2068
        self.fsm.set_node_id(testdir, "uuid")
2041
 
        os.mkdir(testdir)
 
2069
        make_dir(testdir)
2042
2070
 
2043
2071
        self.fsm.move_to_conflict(mdid)
2044
2072
 
2073
2101
        testfile = os.path.join(self.share_path, "path")
2074
2102
        mdid = self.fsm.create(testfile, "share")
2075
2103
        self.fsm.set_node_id(testfile, "uuid")
2076
 
        with open(testfile, "w") as fh:
 
2104
        with open_file(testfile, "w") as fh:
2077
2105
            fh.write("test!")
2078
2106
 
2079
2107
        # move the file
2080
2108
        to_path = os.path.join(self.share_path, "path2")
2081
2109
        self.fsm.move_file("share", testfile, to_path)
2082
 
        self.assertFalse(os.path.exists(testfile))
2083
 
        with open(to_path) as fh:
 
2110
        self.assertFalse(path_exists(testfile))
 
2111
        with open_file(to_path) as fh:
2084
2112
            in_file = fh.read()
2085
2113
        self.assertEqual(in_file, "test!")
2086
2114
        mdobj = self.fsm.get_by_mdid(mdid)
2091
2119
 
2092
2120
        # move again, to a directory
2093
2121
        from_path = to_path
2094
 
        os.mkdir(os.path.join(self.share_path, "testdir"))
 
2122
        make_dir(os.path.join(self.share_path, "testdir"))
2095
2123
        to_path = os.path.join(self.share_path, "testdir", "path3")
2096
2124
        self.fsm.move_file("share", from_path, to_path)
2097
 
        self.assertFalse(os.path.exists(from_path))
2098
 
        with open(to_path) as fh:
 
2125
        self.assertFalse(path_exists(from_path))
 
2126
        with open_file(to_path) as fh:
2099
2127
            in_file = fh.read()
2100
2128
        self.assertEqual(in_file, "test!")
2101
2129
 
2117
2145
        testfile1 = os.path.join(self.share_path, "path1")
2118
2146
        mdid1 = self.fsm.create(testfile1, "share")
2119
2147
        self.fsm.set_node_id(testfile1, "uuid1")
2120
 
        with open(testfile1, "w") as fh:
 
2148
        with open_file(testfile1, "w") as fh:
2121
2149
            fh.write("test 1")
2122
2150
 
2123
2151
        testfile2 = os.path.join(self.share_path, "path2")
2124
2152
        mdid2 = self.fsm.create(testfile2, "share")
2125
2153
        self.fsm.set_node_id(testfile2, "uuid2")
2126
 
        with open(testfile2, "w") as fh:
 
2154
        with open_file(testfile2, "w") as fh:
2127
2155
            fh.write("test 2")
2128
2156
 
2129
2157
        # move the file
2130
2158
        self.fsm.move_file("share", testfile1, testfile2)
2131
 
        self.assertFalse(os.path.exists(testfile1))
2132
 
        with open(testfile2) as fh:
 
2159
        self.assertFalse(path_exists(testfile1))
 
2160
        with open_file(testfile2) as fh:
2133
2161
            in_file = fh.read()
2134
2162
        self.assertEqual(in_file, "test 1")
2135
2163
        mdobj = self.fsm.get_by_mdid(mdid1)
2149
2177
        self.fsm.set_node_id(from_path, "uuid")
2150
2178
 
2151
2179
        # move the file
2152
 
        os.mkdir(from_path)
 
2180
        make_dir(from_path)
2153
2181
        to_path = os.path.join(self.share_path, "path2")
2154
2182
        self.fsm.move_file("share", from_path, to_path)
2155
 
        self.assertFalse(os.path.exists(from_path))
2156
 
        self.assertTrue(os.path.exists(to_path))
 
2183
        self.assertFalse(path_exists(from_path))
 
2184
        self.assertTrue(path_exists(to_path))
2157
2185
        mdobj = self.fsm.get_by_mdid(mdid)
2158
2186
        self.assertEqual(mdobj.info.last_moved_from, from_path)
2159
2187
        when = mdobj.info.last_moved_time
2162
2190
 
2163
2191
        # move again, to a directory
2164
2192
        from_path = to_path
2165
 
        os.mkdir(os.path.join(self.share_path, "testdir"))
 
2193
        make_dir(os.path.join(self.share_path, "testdir"))
2166
2194
        to_path = os.path.join(self.share_path, "testdir", "path3")
2167
2195
        self.fsm.move_file("share", from_path, to_path)
2168
 
        self.assertFalse(os.path.exists(from_path))
2169
 
        self.assertTrue(os.path.exists(to_path))
 
2196
        self.assertFalse(path_exists(from_path))
 
2197
        self.assertTrue(path_exists(to_path))
2170
2198
        mdobj = self.fsm.get_by_mdid(mdid)
2171
2199
        self.assertEqual(mdobj.info.last_moved_from, from_path)
2172
2200
        when = mdobj.info.last_moved_time
2179
2207
        from_path = os.path.join(self.share_path, "path")
2180
2208
        mdid = self.fsm.create(from_path, "share", is_dir=True)
2181
2209
        self.fsm.set_node_id(from_path, "uuid")
2182
 
        os.mkdir(from_path)
 
2210
        make_dir(from_path)
2183
2211
 
2184
2212
        # the file outside, with a similar name just to confuse
2185
2213
        otherfile = os.path.join(self.share_path, "pa")
2186
2214
        self.fsm.create(otherfile, "share", is_dir=False)
2187
2215
        self.fsm.set_node_id(otherfile, "otheruuid")
2188
 
        open(otherfile, "w").close()
 
2216
        open_file(otherfile, "w").close()
2189
2217
 
2190
2218
        # the file inside
2191
2219
        filepath = os.path.join(from_path, "file.txt")
2192
2220
        fileid = self.fsm.create(filepath, "share", is_dir=False)
2193
2221
        self.fsm.set_node_id(filepath, "fileuuid")
2194
 
        open(filepath, "w").close()
 
2222
        open_file(filepath, "w").close()
2195
2223
 
2196
2224
        # move the dir
2197
2225
        to_path = os.path.join(self.share_path, "path2")
2198
2226
        self.fsm.move_file("share", from_path, to_path)
2199
 
        self.assertFalse(os.path.exists(from_path))
2200
 
        self.assertTrue(os.path.exists(to_path))
 
2227
        self.assertFalse(path_exists(from_path))
 
2228
        self.assertTrue(path_exists(to_path))
2201
2229
        mdobj = self.fsm.get_by_mdid(mdid)
2202
2230
        self.assertEqual(mdobj.info.last_moved_from, from_path)
2203
2231
        when = mdobj.info.last_moved_time
2206
2234
 
2207
2235
        # check that file inside is ok
2208
2236
        newfilepath = os.path.join(to_path, "file.txt")
2209
 
        self.assertFalse(os.path.exists(filepath))
2210
 
        self.assertTrue(os.path.exists(newfilepath))
 
2237
        self.assertFalse(path_exists(filepath))
 
2238
        self.assertTrue(path_exists(newfilepath))
2211
2239
        mdobj = self.fsm.get_by_path(newfilepath)
2212
2240
        self.assertEqual(mdobj.mdid, fileid)
2213
 
        self.assertEqual(mdobj.path, "path2/file.txt")
 
2241
        self.assertEqual(mdobj.path, os.path.join('path2', 'file.txt'))
2214
2242
 
2215
2243
        # check the outer file
2216
 
        self.assertTrue(os.path.exists(otherfile))
 
2244
        self.assertTrue(path_exists(otherfile))
2217
2245
        mdobj = self.fsm.get_by_path(otherfile)
2218
2246
        self.assertEqual(mdobj.path, "pa")
2219
2247
 
2220
2248
    def _delete_file(self):
2221
2249
        """Helper to test that a file is deleted."""
2222
2250
        testfile = os.path.join(self.share_path, "path")
2223
 
        open(testfile, "w").close()
 
2251
        open_file(testfile, "w").close()
2224
2252
        mdid = self.fsm.create(testfile, "share")
2225
2253
        self.fsm.set_node_id(testfile, "uuid")
2226
2254
 
2227
2255
        # delete the file
2228
2256
        self.fsm.delete_file(testfile)
2229
 
        self.assertFalse(os.path.exists(testfile))
 
2257
        self.assertFalse(path_exists(testfile))
2230
2258
        self.assert_no_metadata(mdid, testfile, "share", "uuid")
2231
2259
 
2232
2260
    def test_delete_file_directly(self):
2258
2286
    def _delete_dir(self):
2259
2287
        """Helper to test that an empty dir is deleted."""
2260
2288
        testdir = os.path.join(self.share.path, "path")
2261
 
        os.mkdir(testdir)
 
2289
        make_dir(testdir)
2262
2290
        mdid = self.fsm.create(testdir, "share", is_dir=True)
2263
2291
        self.fsm.set_node_id(testdir, "uuid")
2264
2292
 
2265
2293
        # try to delete the dir, but has files on it
2266
 
        open(os.path.join(testdir, "foo"), "w").close()
 
2294
        open_file(os.path.join(testdir, "foo"), "w").close()
2267
2295
        self.assertEqual(self.fsm.get_by_mdid(mdid).path, "path")
2268
2296
        self.assertEqual(self.fsm.get_by_path(testdir).path, "path")
2269
2297
        self.assertEqual(self.fsm.get_by_node_id("share", "uuid").path, "path")
2270
 
        os.remove(os.path.join(testdir, "foo"))
 
2298
        remove_file(os.path.join(testdir, "foo"))
2271
2299
 
2272
2300
        # really delete the dir
2273
2301
        self.fsm.delete_file(testdir)
2274
2302
 
2275
 
        self.assertFalse(os.path.exists(testdir))
 
2303
        self.assertFalse(path_exists(testdir))
2276
2304
        self.assert_no_metadata(mdid, testdir, "share", "uuid")
2277
2305
 
2278
2306
    def test_delete_dir_directly(self):
2304
2332
    def _delete_dir_when_non_empty_and_no_modifications(self):
2305
2333
        """Helper to test that a dir is deleted, non empty but ok to clean."""
2306
2334
        local_dir = os.path.join(self.root_dir, "foo")
2307
 
        os.mkdir(local_dir)
 
2335
        make_dir(local_dir)
2308
2336
        mdid = self.fsm.create(local_dir, "", is_dir=True)
2309
2337
        self.fsm.set_node_id(local_dir, "uuid")
2310
2338
 
2311
2339
        local_file = os.path.join(local_dir, "bar.txt")
2312
 
        open(local_file, 'w').close() # touch bar.txt so it exists
 
2340
        open_file(local_file, 'w').close() # touch bar.txt so it exists
2313
2341
        mdid_file = self.fsm.create(local_file, "")
2314
2342
        self.fsm.set_node_id(local_file, "uuid_file")
2315
2343
 
2316
 
        assert len(os.listdir(local_dir)) > 0 # local_dir is not empty
 
2344
        assert len(listdir(local_dir)) > 0 # local_dir is not empty
2317
2345
        assert not self.fsm.local_changed(path=local_dir)
2318
2346
 
2319
2347
        self.fsm.delete_file(local_dir)
2320
2348
 
2321
 
        self.assertFalse(os.path.exists(local_file))
 
2349
        self.assertFalse(path_exists(local_file))
2322
2350
        self.assert_no_metadata(mdid_file, local_file, "", "uuid_file")
2323
2351
 
2324
 
        self.assertFalse(os.path.exists(local_dir))
 
2352
        self.assertFalse(path_exists(local_dir))
2325
2353
        self.assert_no_metadata(mdid, local_dir, "", "uuid")
2326
2354
 
2327
2355
    def test_delete_nonempty_cleanable_dir_directly(self):
2330
2358
 
2331
2359
        # check it was sent to trash, not just deleted
2332
2360
        called = []
2333
 
        orig_call = shutil.rmtree
2334
 
        self.patch(shutil, 'rmtree',
 
2361
        orig_call = filesystem_manager.remove_tree
 
2362
        self.patch(filesystem_manager, 'remove_tree',
2335
2363
                   lambda path: called.append(True) or orig_call(path))
2336
2364
 
2337
2365
        self._delete_dir_when_non_empty_and_no_modifications()
2353
2381
    def test_delete_dir_when_non_empty_and_modifications_prior_delete(self):
2354
2382
        """Test that a dir is deleted, when is not empty and modified."""
2355
2383
        local_dir = os.path.join(self.root_dir, "foo")
2356
 
        os.mkdir(local_dir)
 
2384
        make_dir(local_dir)
2357
2385
        self.fsm.create(local_dir, "", is_dir=True)
2358
2386
        self.fsm.set_node_id(local_dir, "uuid")
2359
2387
 
2360
2388
        local_file = os.path.join(local_dir, "bar.txt")
2361
 
        open(local_file, 'w').close() # touch bar.txt so it exists
 
2389
        open_file(local_file, 'w').close() # touch bar.txt so it exists
2362
2390
        mdid_file = self.fsm.create(local_file, "")
2363
2391
        self.fsm.set_node_id(local_file, "uuid_file")
2364
2392
        self.fsm.set_by_mdid(mdid_file, local_hash=98765)
2365
2393
 
2366
 
        assert len(os.listdir(local_dir)) > 0 # local_dir is not empty
 
2394
        assert len(listdir(local_dir)) > 0 # local_dir is not empty
2367
2395
        assert self.fsm.changed(path=local_file) == self.fsm.CHANGED_LOCAL
2368
2396
        self.assertRaises(DirectoryNotRemovable,
2369
2397
                          self.fsm.delete_file, local_dir)
2372
2400
        """Test that a dir is not deleted, when there is a conflicted file."""
2373
2401
        # local directory
2374
2402
        local_dir = os.path.join(self.root_dir, "foo")
2375
 
        os.mkdir(local_dir)
 
2403
        make_dir(local_dir)
2376
2404
        self.fsm.create(local_dir, "", is_dir=True)
2377
2405
        self.fsm.set_node_id(local_dir, "uuid")
2378
2406
 
2379
2407
        local_file = os.path.join(local_dir,
2380
2408
                                  "bar.txt" + self.fsm.CONFLICT_SUFFIX)
2381
 
        open(local_file, 'w').close() # touch bar.txt.u1conflict
 
2409
        open_file(local_file, 'w').close() # touch bar.txt.u1conflict
2382
2410
 
2383
2411
        assert not local_file in self.fsm._idx_path
2384
2412
        self.assertRaises(DirectoryNotRemovable,
2393
2421
        """Test that a dir is not deleted, when there is a conflicted dir."""
2394
2422
        # local directory
2395
2423
        local_dir = os.path.join(self.root_dir, "foo")
2396
 
        os.mkdir(local_dir)
 
2424
        make_dir(local_dir)
2397
2425
        self.fsm.create(local_dir, "", is_dir=True)
2398
2426
        self.fsm.set_node_id(local_dir, "uuid")
2399
2427
 
2400
2428
        local_subdir = os.path.join(local_dir,
2401
2429
                                    "subdir_bar" + self.fsm.CONFLICT_SUFFIX)
2402
 
        os.mkdir(local_subdir)
 
2430
        make_dir(local_subdir)
2403
2431
 
2404
2432
        assert not local_subdir in self.fsm._idx_path
2405
2433
        self.assertRaises(DirectoryNotRemovable,
2413
2441
    def test_no_warning_on_log_file_when_recursive_delete(self):
2414
2442
        """Test that sucessfully deleted dir does not log OSError."""
2415
2443
        local_dir = os.path.join(self.root_dir, "foo")
2416
 
        os.mkdir(local_dir)
 
2444
        make_dir(local_dir)
2417
2445
        self.fsm.create(local_dir, "", is_dir=True)
2418
2446
        self.fsm.set_node_id(local_dir, "uuid")
2419
2447
 
2420
2448
        local_file = os.path.join(local_dir, "bar.txt")
2421
 
        open(local_file, 'w').close() # touch bar.txt so it exists
 
2449
        open_file(local_file, 'w').close() # touch bar.txt so it exists
2422
2450
        self.fsm.create(local_file, "")
2423
2451
        self.fsm.set_node_id(local_file, "uuid_file")
2424
2452
 
2449
2477
        tdir = os.path.join(self.share_path, "adir")
2450
2478
        mdid1 = self.fsm.create(tdir, "share", is_dir=True)
2451
2479
        self.fsm.set_node_id(tdir, "uuid1")
2452
 
        os.mkdir(tdir)
 
2480
        make_dir(tdir)
2453
2481
 
2454
2482
        testfile = os.path.join(tdir, "path")
2455
2483
        mdid2 = self.fsm.create(testfile, "share")
2456
2484
        self.fsm.set_node_id(testfile, "uuid2")
2457
 
        with open(testfile, "w") as fh:
 
2485
        with open_file(testfile, "w") as fh:
2458
2486
            fh.write("test!")
2459
2487
 
2460
2488
        # move the dir to conflict, the file is still there, but with no MD
2461
2489
        self.fsm.move_to_conflict(mdid1)
2462
 
        self.assertFalse(os.path.exists(tdir))
2463
 
        self.assertTrue(os.path.exists(tdir + self.fsm.CONFLICT_SUFFIX))
 
2490
        self.assertFalse(path_exists(tdir))
 
2491
        self.assertTrue(path_exists(tdir + self.fsm.CONFLICT_SUFFIX))
2464
2492
        testfile = os.path.join(self.share_path,
2465
2493
                                tdir + self.fsm.CONFLICT_SUFFIX, "path")
2466
 
        self.assertTrue(os.path.exists(testfile))
 
2494
        self.assertTrue(path_exists(testfile))
2467
2495
        self.assertTrue(self.fsm.get_by_mdid(mdid1))
2468
2496
        self.assert_no_metadata(mdid2, testfile, "share", "uuid2")
2469
2497
 
2472
2500
        tdir1 = os.path.join(self.share_path, "adirectory")
2473
2501
        mdid1 = self.fsm.create(tdir1, "share", is_dir=True)
2474
2502
        self.fsm.set_node_id(tdir1, "uuid1")
2475
 
        os.mkdir(tdir1)
 
2503
        make_dir(tdir1)
2476
2504
 
2477
2505
        tdir2 = os.path.join(self.share_path, "adir")
2478
2506
        mdid2 = self.fsm.create(tdir2, "share", is_dir=True)
2479
2507
        self.fsm.set_node_id(tdir2, "uuid2")
2480
 
        os.mkdir(tdir2)
 
2508
        make_dir(tdir2)
2481
2509
 
2482
2510
        testfile = os.path.join(tdir2, "path")
2483
2511
        mdid3 = self.fsm.create(testfile, "share")
2484
2512
        self.fsm.set_node_id(testfile, "uuid3")
2485
 
        with open(testfile, "w") as fh:
 
2513
        with open_file(testfile, "w") as fh:
2486
2514
            fh.write("test!")
2487
2515
 
2488
2516
        # move the dir2 to conflict, see dir2 and file inside it went ok
2489
2517
        self.fsm.move_to_conflict(mdid2)
2490
 
        self.assertFalse(os.path.exists(tdir2))
2491
 
        self.assertTrue(os.path.exists(tdir2 + self.fsm.CONFLICT_SUFFIX))
 
2518
        self.assertFalse(path_exists(tdir2))
 
2519
        self.assertTrue(path_exists(tdir2 + self.fsm.CONFLICT_SUFFIX))
2492
2520
        testfile = os.path.join(self.share_path,
2493
2521
                                tdir2 + self.fsm.CONFLICT_SUFFIX, "path")
2494
 
        self.assertTrue(os.path.exists(testfile))
 
2522
        self.assertTrue(path_exists(testfile))
2495
2523
        self.assertTrue(self.fsm.get_by_mdid(mdid2))
2496
2524
        self.assertRaises(KeyError, self.fsm.get_by_mdid, mdid3)
2497
2525
 
2498
2526
        # and check that the one with similar path is untouched
2499
 
        self.assertTrue(os.path.exists(tdir1))
 
2527
        self.assertTrue(path_exists(tdir1))
2500
2528
        self.assertTrue(self.fsm.get_by_mdid(mdid1))
2501
2529
 
2502
2530
 
2533
2561
    def test_trash_normal(self):
2534
2562
        """Test that a node is sent to and removed from trash."""
2535
2563
        testfile = os.path.join(self.share_path, "path")
2536
 
        open(testfile, "w").close()
 
2564
        open_file(testfile, "w").close()
2537
2565
        mdid = self.fsm.create(testfile, "share")
2538
2566
        self.fsm.set_node_id(testfile, "uuid")
2539
2567
        self.assertTrue(self.fsm.has_metadata(mdid=mdid))
2574
2602
    def test_trash_with_node_in_none(self):
2575
2603
        """Test that in trash is saved the marker if node_id is None."""
2576
2604
        testfile = os.path.join(self.share_path, "path")
2577
 
        open(testfile, "w").close()
 
2605
        open_file(testfile, "w").close()
2578
2606
        mdid = self.fsm.create(testfile, "share")
2579
2607
 
2580
2608
        # delete to trash and check the marker
2813
2841
        """If the dir already exist, don't raise an error."""
2814
2842
        local_dir = os.path.join(self.root_dir, "foo")
2815
2843
        mdid = self.fsm.create(local_dir, "", is_dir=True)
2816
 
        os.mkdir(local_dir)
 
2844
        make_dir(local_dir)
2817
2845
        self.fsm.make_dir(mdid)
2818
 
        self.assertTrue(os.path.exists(local_dir))
 
2846
        self.assertTrue(path_exists(local_dir))
2819
2847
 
2820
2848
    @defer.inlineCallbacks
2821
2849
    def test_make_dir_in_ro_share(self):
2822
2850
        """Also works in a read only share."""
2823
 
        share = yield self.create_share('ro_share_id', 'ro',
 
2851
        share = yield self.create_share('ro_share_id', u'ro',
2824
2852
                                        access_level='View')
2825
2853
        testdir = os.path.join(share.path, "foo")
2826
2854
        mdid = self.fsm.create(testdir, 'ro_share_id', is_dir=True)
2827
2855
        self.fsm.make_dir(mdid)
2828
 
        self.assertTrue(os.path.exists(testdir))
 
2856
        self.assertTrue(path_exists(testdir))
2829
2857
 
2830
2858
    @defer.inlineCallbacks
2831
2859
    def test_make_dir_ro_watch(self):
2839
2867
 
2840
2868
        self.eq.add_watch = add_watch
2841
2869
        self.eq.add_to_mute_filter = lambda *a: called.append(a)
2842
 
        share = yield self.create_share('ro_share_id', 'ro',
 
2870
        share = yield self.create_share('ro_share_id', u'ro',
2843
2871
                                        access_level='View')
2844
2872
        testdir = os.path.join(share.path, "foo")
2845
2873
        mdid = self.fsm.create(testdir, 'ro_share_id', is_dir=True)
2915
2943
                         self.fsm.CHANGED_SERVER)
2916
2944
 
2917
2945
        # remove the .partial by hand, to see it crash
2918
 
        os.remove(partial_path)
 
2946
        remove_file(partial_path)
2919
2947
        # pylint: disable-msg=W0212
2920
2948
        self.assertRaises(InconsistencyError,
2921
2949
                          self.fsm._check_partial, mdid=mdid)
2937
2965
        self.assertEqual(self.fsm.changed(path=testfile), self.fsm.CHANGED_NONE)
2938
2966
 
2939
2967
        # put a .partial by hand, to see it crash
2940
 
        open(partial_path, "w").close()
 
2968
        open_file(partial_path, "w").close()
2941
2969
        # pylint: disable-msg=W0212
2942
2970
        self.assertRaises(InconsistencyError,
2943
2971
                          self.fsm._check_partial, mdid=mdid)
2961
2989
                         self.fsm.CHANGED_LOCAL)
2962
2990
 
2963
2991
        # put a .partial by hand, to see it crash
2964
 
        open(partial_path, "w").close()
 
2992
        open_file(partial_path, "w").close()
2965
2993
        # pylint: disable-msg=W0212
2966
2994
        self.assertRaises(InconsistencyError,
2967
2995
                          self.fsm._check_partial, mdid=mdid)
3043
3071
    @defer.inlineCallbacks
3044
3072
    def test_file_ro_share_fail(self):
3045
3073
        """ Test that manual creation of a file, fails on a ro-share. """
3046
 
        share = yield self.create_share('ro_share', 'ro_share_name',
 
3074
        share = yield self.create_share('ro_share', u'ro_share_name',
3047
3075
                                        access_level='View')
3048
3076
        testfile = os.path.join(share.path, "a_file")
3049
 
        self.assertRaises(IOError, open, testfile, 'w')
 
3077
        self.assertRaises(IOError, open_file, testfile, 'w')
3050
3078
 
3051
3079
    @defer.inlineCallbacks
3052
3080
    def test_dir_ro_share(self):
3053
3081
        """ Test that the creation of a file using fsm, works on a ro-share."""
3054
 
        share = yield self.create_share('ro_share', 'ro_share_name',
 
3082
        share = yield self.create_share('ro_share', u'ro_share_name',
3055
3083
                                        access_level='View')
3056
3084
        testdir = os.path.join(share.path, "path2")
3057
3085
        self.fsm.create(testdir, share.volume_id, is_dir=True)
3060
3088
        fd = self.fsm.get_partial_for_writing('uuid2', share.volume_id)
3061
3089
        fd.flush()
3062
3090
        fd.close()
3063
 
        self.assertTrue(os.path.exists(testdir))
 
3091
        self.assertTrue(path_exists(testdir))
3064
3092
 
3065
3093
    @defer.inlineCallbacks
3066
3094
    def test_file_ro_share(self):
3067
3095
        """ Test that the creation of a file using fsm, works on a ro-share."""
3068
 
        self.share = yield self.create_share('ro_share', 'ro_share_name',
 
3096
        self.share = yield self.create_share('ro_share', u'ro_share_name',
3069
3097
                                             access_level='View')
3070
3098
        testfile = os.path.join(self.share.path, "a_file")
3071
3099
        self.fsm.create(testfile, self.share.volume_id, is_dir=False)
3075
3103
        fd.flush()
3076
3104
        fd.close()
3077
3105
        self.fsm.commit_partial('uuid3', self.share.volume_id, None)
3078
 
        self.assertTrue(os.path.exists(testfile))
 
3106
        self.assertTrue(path_exists(testfile))
3079
3107
 
3080
3108
    @defer.inlineCallbacks
3081
3109
    def test_delete_dir_ro_share(self):
3082
3110
        """ Test that fsm is able to delete a dir in a ro.share. """
3083
 
        share = yield self.create_share('ro_share', 'ro_share_name',
 
3111
        share = yield self.create_share('ro_share', u'ro_share_name',
3084
3112
                                        access_level='View')
3085
3113
        testdir = os.path.join(share.path, "path2")
3086
3114
        self.fsm.create(testdir, share.volume_id, is_dir=True)
3090
3118
        fd.flush()
3091
3119
        fd.close()
3092
3120
        self.fsm.remove_partial('uuid2', share.volume_id)
3093
 
        self.assertTrue(os.path.exists(testdir))
 
3121
        self.assertTrue(path_exists(testdir))
3094
3122
        self.fsm.delete_file(testdir)
3095
 
        self.assertFalse(os.path.exists(testdir))
 
3123
        self.assertFalse(path_exists(testdir))
3096
3124
 
3097
3125
    @defer.inlineCallbacks
3098
3126
    def test_delete_non_empty_dir_ro_share(self):
3099
3127
        """Test that fsm is able to delete a non-empty dir in a ro.share."""
3100
 
        share = yield self.create_share('ro_share', 'ro_share_name',
 
3128
        share = yield self.create_share('ro_share', u'ro_share_name',
3101
3129
                                        access_level='View')
3102
3130
        testdir = os.path.join(share.path, "path2")
3103
3131
        mdid = self.fsm.create(testdir, share.volume_id, is_dir=True)
3118
3146
        fd.close()
3119
3147
        self.fsm.commit_partial('uuid3', share.volume_id, None)
3120
3148
        self.fsm.upload_finished(mdid, self.fsm.get_by_mdid(mdid).local_hash)
3121
 
        self.assertTrue(os.path.exists(testdir))
3122
 
        self.assertTrue(os.path.exists(testfile))
 
3149
        self.assertTrue(path_exists(testdir))
 
3150
        self.assertTrue(path_exists(testfile))
3123
3151
        self.fsm.delete_file(testdir)
3124
 
        self.assertFalse(os.path.exists(testdir))
3125
 
        self.assertFalse(os.path.exists(testfile))
 
3152
        self.assertFalse(path_exists(testdir))
 
3153
        self.assertFalse(path_exists(testfile))
3126
3154
 
3127
3155
    @defer.inlineCallbacks
3128
3156
    def test_delete_non_empty_dir_rw_share(self):
3129
3157
        """Test that fsm is able to delete a non-empty dir in a rw.share."""
3130
 
        share = yield self.create_share('rw_share', 'rw_share_name',
 
3158
        share = yield self.create_share('rw_share', u'rw_share_name',
3131
3159
                                        access_level='Modify')
3132
3160
        testdir = os.path.join(share.path, "path2")
3133
3161
        mdid = self.fsm.create(testdir, share.volume_id, is_dir=True)
3148
3176
        fd.close()
3149
3177
        self.fsm.commit_partial('uuid3', share.volume_id, None)
3150
3178
        self.fsm.upload_finished(mdid, self.fsm.get_by_mdid(mdid).local_hash)
3151
 
        self.assertTrue(os.path.exists(testdir))
3152
 
        self.assertTrue(os.path.exists(testfile))
 
3179
        self.assertTrue(path_exists(testdir))
 
3180
        self.assertTrue(path_exists(testfile))
3153
3181
        self.fsm.delete_file(testdir)
3154
 
        self.assertFalse(os.path.exists(testdir))
3155
 
        self.assertFalse(os.path.exists(testfile))
 
3182
        self.assertFalse(path_exists(testdir))
 
3183
        self.assertFalse(path_exists(testfile))
3156
3184
 
3157
3185
    @defer.inlineCallbacks
3158
3186
    def test_delete_non_empty_dir_bad_perms_rw_share(self):
3159
3187
        """Test that fsm is able to delete a non-empty dir in a rw.share."""
3160
 
        share = yield self.create_share('rw_share', 'rw_share_name',
 
3188
        share = yield self.create_share('rw_share', u'rw_share_name',
3161
3189
                                        access_level='Modify')
3162
3190
        testdir = os.path.join(share.path, "path2")
3163
3191
        mdid = self.fsm.create(testdir, share.volume_id, is_dir=True)
3178
3206
        fd.close()
3179
3207
        self.fsm.commit_partial('uuid3', share.volume_id, None)
3180
3208
        self.fsm.upload_finished(mdid, self.fsm.get_by_mdid(mdid).local_hash)
3181
 
        self.assertTrue(os.path.exists(testdir))
3182
 
        self.assertTrue(os.path.exists(testfile))
 
3209
        self.assertTrue(path_exists(testdir))
 
3210
        self.assertTrue(path_exists(testfile))
3183
3211
 
3184
3212
        # make the dir read-only, the error should be logged
3185
 
        os.chmod(testdir, 0555)
 
3213
        set_dir_readonly(testdir)
 
3214
        self.addCleanup(set_dir_readwrite, testdir)
 
3215
 
3186
3216
        self.fsm.delete_file(testdir)
3187
3217
        self.assertTrue(self.handler.check_warning("OSError", testdir,
3188
3218
                                                   "when trying to remove"))
3189
 
        self.assertTrue(os.path.exists(testdir))
3190
 
        self.assertTrue(os.path.exists(testfile))
 
3219
        self.assertTrue(path_exists(testdir))
 
3220
        self.assertTrue(path_exists(testfile))
3191
3221
 
3192
3222
    @defer.inlineCallbacks
3193
3223
    def test_delete_file_ro_share(self):
3194
3224
        """ Test that fsm is able to delete a file in a ro-share. """
3195
 
        self.share = yield self.create_share('ro_share', 'ro_share_name',
 
3225
        self.share = yield self.create_share('ro_share', u'ro_share_name',
3196
3226
                                        access_level='View')
3197
3227
        testfile = os.path.join(self.share.path, "a_file")
3198
3228
        self.fsm.create(testfile, self.share.volume_id, is_dir=False)
3202
3232
        fd.flush()
3203
3233
        fd.close()
3204
3234
        self.fsm.commit_partial('uuid3', self.share.volume_id, None)
3205
 
        self.assertTrue(os.path.exists(testfile))
 
3235
        self.assertTrue(path_exists(testfile))
3206
3236
        self.fsm.delete_file(testfile)
3207
 
        self.assertFalse(os.path.exists(testfile))
 
3237
        self.assertFalse(path_exists(testfile))
3208
3238
 
3209
3239
    @defer.inlineCallbacks
3210
3240
    def test_move_to_conflict_ro_share(self):
3211
3241
        """ Test that fsm is able to handle move_to_conflict in a ro-share. """
3212
 
        self.share = yield self.create_share('ro_share', 'ro_share_name',
 
3242
        self.share = yield self.create_share('ro_share', u'ro_share_name',
3213
3243
                                             access_level='View')
3214
3244
        testfile = os.path.join(self.share.path, "a_file")
3215
3245
        file_mdid = self.fsm.create(testfile, self.share.volume_id,
3220
3250
        fd.flush()
3221
3251
        fd.close()
3222
3252
        self.fsm.commit_partial('uuid3', self.share.volume_id, None)
3223
 
        self.assertTrue(os.path.exists(testfile))
 
3253
        self.assertTrue(path_exists(testfile))
3224
3254
        self.fsm.move_to_conflict(file_mdid)
3225
 
        self.assertTrue(os.path.exists(testfile + self.fsm.CONFLICT_SUFFIX))
 
3255
        self.assertTrue(path_exists(testfile + self.fsm.CONFLICT_SUFFIX))
3226
3256
 
3227
3257
    @defer.inlineCallbacks
3228
3258
    def test_file_rw_share_no_fail(self):
3229
3259
        """ Test that manual creation of a file, ona  rw-share. """
3230
 
        share = yield self.create_share('ro_share', 'ro_share_name')
 
3260
        share = yield self.create_share('ro_share', u'ro_share_name')
3231
3261
        testfile = os.path.join(share.path, "a_file")
3232
 
        open(testfile, 'w').close()
3233
 
        self.assertTrue(os.path.exists(testfile))
 
3262
        open_file(testfile, 'w').close()
 
3263
        self.assertTrue(path_exists(testfile))
3234
3264
 
3235
3265
    @defer.inlineCallbacks
3236
3266
    def test_dir_rw_share(self):
3237
3267
        """ Test that the creation of a file using fsm, works on a rw-share."""
3238
 
        share = yield self.create_share('ro_share', 'ro_share_name')
 
3268
        share = yield self.create_share('ro_share', u'ro_share_name')
3239
3269
        testdir = os.path.join(share.path, "path2")
3240
3270
        self.fsm.create(testdir, share.volume_id, is_dir=True)
3241
3271
        self.fsm.set_node_id(testdir, "uuid2")
3243
3273
        fd = self.fsm.get_partial_for_writing('uuid2', share.volume_id)
3244
3274
        fd.flush()
3245
3275
        fd.close()
3246
 
        self.assertTrue(os.path.exists(testdir))
 
3276
        self.assertTrue(path_exists(testdir))
3247
3277
 
3248
3278
    @defer.inlineCallbacks
3249
3279
    def test_file_rw_share(self):
3250
3280
        """Test that the creation of a file using fsm, works on a rw-share."""
3251
 
        self.share = yield self.create_share('ro_share', 'ro_share_name')
 
3281
        self.share = yield self.create_share('ro_share', u'ro_share_name')
3252
3282
        testfile = os.path.join(self.share.path, "a_file")
3253
3283
        self.fsm.create(testfile, self.share.volume_id, is_dir=False)
3254
3284
        self.fsm.set_node_id(testfile, "uuid3")
3257
3287
        fd.flush()
3258
3288
        fd.close()
3259
3289
        self.fsm.commit_partial('uuid3', self.share.volume_id, None)
3260
 
        self.assertTrue(os.path.exists(testfile))
 
3290
        self.assertTrue(path_exists(testfile))
3261
3291
 
3262
3292
    def test_share_and_root(self):
3263
3293
        """ Test the creation of a file with the same relative path in a share
3277
3307
        fd = self.fsm.get_partial_for_writing('uuid2', self.share.volume_id)
3278
3308
        fd.flush()
3279
3309
        fd.close()
3280
 
        self.assertTrue(os.path.exists(self.fsm.get_abspath("", a_dir_root)))
3281
 
        self.assertTrue(os.path.exists(a_dir_share))
 
3310
        self.assertTrue(path_exists(self.fsm.get_abspath("", a_dir_root)))
 
3311
        self.assertTrue(path_exists(a_dir_share))
3282
3312
 
3283
3313
 
3284
3314
class TestEnableShareWrite(FSMTestCase):
3289
3319
        """Test setup"""
3290
3320
        yield super(TestEnableShareWrite, self).setUp()
3291
3321
        # create a ro share
3292
 
        self.share_ro = yield self.create_share('share_ro', 'share_ro_name',
 
3322
        self.share_ro = yield self.create_share('share_ro', u'share_ro_name',
3293
3323
                                                access_level='View')
3294
3324
        self.share_ro_path = self.share_ro.path
3295
3325
 
3301
3331
                                     os.W_OK)
3302
3332
        with EnableShareWrite(self.share_ro, path) as enabled:
3303
3333
            self.assertTrue(enabled.ro)
3304
 
            with open(path, 'w') as f:
 
3334
            with open_file(path, 'w') as f:
3305
3335
                f.write(data)
3306
 
        self.assertEqual(data, open(path, 'r').read())
 
3336
        self.assertEqual(data, open_file(path, 'r').read())
3307
3337
        self.assertFalse(os.access(self.share_ro_path, os.W_OK))
3308
3338
        # check that the parent permissions are ok
3309
3339
        self.assertEqual(can_write_parent,
3319
3349
        can_write_parent = os.access(os.path.dirname(self.share_path), os.W_OK)
3320
3350
        with EnableShareWrite(self.share, path) as enabled:
3321
3351
            self.assertFalse(enabled.ro)
3322
 
            with open(path, 'w') as f:
 
3352
            with open_file(path, 'w') as f:
3323
3353
                f.write(data)
3324
 
        self.assertEquals(data, open(path, 'r').read())
 
3354
        self.assertEquals(data, open_file(path, 'r').read())
3325
3355
        self.assertTrue(os.access(self.share_path, os.W_OK))
3326
3356
        # check that the parent permissions are ok
3327
3357
        self.assertEquals(can_write_parent, os.access(self.share_path, os.W_OK))
3341
3371
                             self.data_dir, self.partials_dir)
3342
3372
        self.addCleanup(self.main.shutdown)
3343
3373
        self.fsm = self.main.fs
3344
 
        self.share = yield self.create_share('share', 'share_name')
 
3374
        self.share = yield self.create_share('share', u'share_name')
3345
3375
        self.share_path = self.share.path
3346
3376
 
3347
3377
    @defer.inlineCallbacks
3353
3383
 
3354
3384
        defer.returnValue(share)
3355
3385
 
 
3386
    @skipOnWinIfMetadataPreV4
3356
3387
    @defer.inlineCallbacks
3357
3388
    def test_old_metadata_None_missing_share(self):
3358
3389
        """test loading metadata v0. that points to a share that
3360
3391
        """
3361
3392
        # create some stuff
3362
3393
        path = os.path.join(self.share.path, 'path')
3363
 
        open(path, "w").close()
 
3394
        open_file(path, "w").close()
3364
3395
        mdid = self.fsm.create(path, "share")
3365
3396
        self.fsm.set_node_id(path, "uuid")
3366
3397
        # create a path with the old layout
3367
 
        other_share = yield self.create_share('share1', 'share1_name')
 
3398
        other_share = yield self.create_share('share1', u'share1_name')
3368
3399
        share_mdid = self.fsm.create(other_share.path, "share1")
3369
3400
        self.fsm.set_node_id(other_share.path, "uuid1")
3370
 
        os.makedirs(os.path.join(self.root_dir, 'Ubuntu One'))
 
3401
        make_dir(os.path.join(self.root_dir, 'Ubuntu One'), recursive=True)
3371
3402
        old_shares_path = os.path.join(self.root_dir, 'Ubuntu One',
3372
3403
                            'Shared With Me')
3373
 
        old_path = os.path.join(old_shares_path, 'share1_name')
 
3404
        old_path = os.path.join(old_shares_path, u'share1_name')
3374
3405
        make_link(self.shares_dir, old_shares_path)
3375
3406
 
3376
3407
        # put the old path in the mdobj
3388
3419
 
3389
3420
        # delete the version that should have left the previous fsm
3390
3421
        version_file = os.path.join(self.data_dir, "metadata_version")
3391
 
        os.remove(version_file)
 
3422
        remove_file(version_file)
3392
3423
 
3393
3424
        # create a old-style fs with the data:
3394
3425
        old_fs = FileShelf(self.fsm.old_fs._path)
3400
3431
 
3401
3432
        # start up again, and check
3402
3433
        db = Tritcask(os.path.join(self.main.data_dir, 'tritcask.new'))
 
3434
        self.addCleanup(db.shutdown)
3403
3435
        newfsm = FileSystemManager(self.data_dir, self.partials_dir,
3404
3436
                                   self.fsm.vm, db)
3405
 
        md_version = open(version_file).read()
 
3437
        md_version = open_file(version_file).read()
3406
3438
        self.assertEqual(md_version, METADATA_VERSION)
3407
3439
        newmdobj = newfsm.get_by_path(path)
3408
3440
        self.assertEqual(newmdobj.mdid, mdid)
3409
 
        self.assertEqual(newmdobj.stat, os.stat(path))
 
3441
        self.assertEqual(newmdobj.stat, stat_path(path))
3410
3442
        self.assertEqual(newmdobj.local_hash, "")
3411
3443
        self.assertEqual(newmdobj.server_hash, "")
3412
3444
        self.assertTrue(isinstance(newmdobj.path, str))
3414
3446
        self.assertFalse(old_path in self.fsm._idx_path)
3415
3447
        self.assertFalse(old_path in newfsm._idx_path)
3416
3448
        self.assertRaises(KeyError, newfsm.get_by_mdid, share_mdid)
3417
 
        db.shutdown()
3418
3449
 
 
3450
    @skipOnWinIfMetadataPreV4
3419
3451
    @defer.inlineCallbacks
3420
3452
    def test_old_metadata_1_missing_share(self):
3421
3453
        """test loading metadata v1. that points to a share that
3430
3462
        self.fsm.set_node_id(path2, "uuid2")
3431
3463
 
3432
3464
        # create a path with the old layout
3433
 
        other_share = yield self.create_share('share1', 'share1_name')
 
3465
        other_share = yield self.create_share('share1', u'share1_name')
3434
3466
        share_mdid = self.fsm.create(other_share.path, "share1")
3435
3467
        self.fsm.set_node_id(other_share.path, "uuid3")
3436
 
        os.makedirs(os.path.join(self.root_dir, 'Ubuntu One'))
 
3468
        make_dir(os.path.join(self.root_dir, 'Ubuntu One'), recursive=True)
3437
3469
        old_shares_path = os.path.join(self.root_dir, 'Ubuntu One',
3438
3470
                            'Shared With Me')
3439
 
        old_path = os.path.join(old_shares_path, 'share1_name')
 
3471
        old_path = os.path.join(old_shares_path, u'share1_name')
3440
3472
        make_link(self.shares_dir, old_shares_path)
3441
3473
 
3442
3474
        # put the old path in the mdobj
3456
3488
 
3457
3489
        # put the version file in 1
3458
3490
        version_file = os.path.join(self.data_dir, "metadata_version")
3459
 
        with open(version_file, "w") as fh:
 
3491
        with open_file(version_file, "w") as fh:
3460
3492
            fh.write("1")
3461
3493
 
3462
3494
        # create a old-style fs with the data:
3469
3501
 
3470
3502
        # start up again, and check
3471
3503
        db = Tritcask(os.path.join(self.main.data_dir, 'tritcask.new'))
 
3504
        self.addCleanup(db.shutdown)
3472
3505
        newfsm = FileSystemManager(self.data_dir, self.partials_dir,
3473
3506
                                   self.fsm.vm, db)
3474
3507
        version_file = os.path.join(self.data_dir, "metadata_version")
3475
 
        md_version = open(version_file).read()
 
3508
        md_version = open_file(version_file).read()
3476
3509
        self.assertEqual(md_version, METADATA_VERSION)
3477
3510
        # pylint: disable-msg=W0212
3478
3511
        self.assertEqual(1, len(newfsm._idx_node_id))
3479
3512
        self.assertEqual(2, len(newfsm._idx_path))
3480
3513
        self.assertEquals('uuid1', newfsm.get_by_mdid(mdid1).node_id)
3481
3514
        self.assertRaises(KeyError, newfsm.get_by_mdid, share_mdid)
3482
 
        db.shutdown()
3483
3515
 
 
3516
    @skipOnWinIfMetadataPreV4
3484
3517
    @defer.inlineCallbacks
3485
3518
    def test_old_metadata_2_missing_share(self):
3486
3519
        """test loading metadata v2. that points to a share that
3491
3524
        mdid = self.fsm.create(path, "share")
3492
3525
        self.fsm.set_node_id(path, "uuid")
3493
3526
        # create a path with the old layout
3494
 
        other_share = yield self.create_share('share1', 'share1_name')
 
3527
        other_share = yield self.create_share('share1', u'share1_name')
3495
3528
        share_mdid = self.fsm.create(other_share.path, "share1")
3496
3529
        self.fsm.set_node_id(other_share.path, "uuid3")
3497
 
        os.makedirs(os.path.join(self.root_dir, 'Ubuntu One'))
 
3530
        make_dir(os.path.join(self.root_dir, 'Ubuntu One'), recursive=True)
3498
3531
        old_shares_path = os.path.join(self.root_dir, 'Ubuntu One',
3499
3532
                            'Shared With Me')
3500
 
        old_path = os.path.join(old_shares_path, 'share1_name')
 
3533
        old_path = os.path.join(old_shares_path, u'share1_name')
3501
3534
        make_link(self.shares_dir, old_shares_path)
3502
3535
 
3503
3536
        # put the old path in the mdobj
3513
3546
 
3514
3547
        # put the version file in 1
3515
3548
        version_file = os.path.join(self.data_dir, "metadata_version")
3516
 
        with open(version_file, "w") as fh:
 
3549
        with open_file(version_file, "w") as fh:
3517
3550
            fh.write("2")
3518
3551
 
3519
3552
        # create a old-style fs with the data:
3526
3559
 
3527
3560
        # start up again, and check
3528
3561
        db = Tritcask(os.path.join(self.main.data_dir, 'tritcask.new'))
 
3562
        self.addCleanup(db.shutdown)
3529
3563
        newfsm = FileSystemManager(self.data_dir, self.partials_dir,
3530
3564
                                   self.fsm.vm, db)
3531
3565
        version_file = os.path.join(self.data_dir, "metadata_version")
3532
 
        md_version = open(version_file).read()
 
3566
        md_version = open_file(version_file).read()
3533
3567
        self.assertEqual(md_version, METADATA_VERSION)
3534
3568
        self.assertTrue(newfsm.get_by_mdid(mdid) is not None)
3535
3569
        # pylint: disable-msg=W0212
3536
3570
        self.assertEqual(1, len(newfsm._idx_node_id))
3537
3571
        self.assertEqual(2, len(newfsm._idx_path))
3538
3572
        self.assertRaises(KeyError, newfsm.get_by_mdid, share_mdid)
3539
 
        db.shutdown()
3540
3573
 
 
3574
    @skipOnWinIfMetadataPreV4
3541
3575
    @defer.inlineCallbacks
3542
3576
    def test_old_metadata_3_missing_share(self):
3543
3577
        """test loading metadata v3. that points to a share that
3548
3582
        root_mdid = self.fsm.get_by_path(self.root_dir).mdid
3549
3583
        self.fsm.set_node_id(self.root_dir, "uuid")
3550
3584
        # a share
3551
 
        other_share = yield self.create_share('share1', 'share1_name')
 
3585
        other_share = yield self.create_share('share1', u'share1_name')
3552
3586
        share_mdid = self.fsm.create(other_share.path, "share1")
3553
3587
        self.fsm.set_node_id(other_share.path, "uuid1")
3554
 
        os.makedirs(os.path.join(self.root_dir, 'Ubuntu One'))
 
3588
        make_dir(os.path.join(self.root_dir, 'Ubuntu One'), recursive=True)
3555
3589
        old_shares_path = os.path.join(self.root_dir, 'Ubuntu One',
3556
3590
                            'Shared With Me')
3557
 
        old_path = os.path.join(old_shares_path, 'share1_name')
 
3591
        old_path = os.path.join(old_shares_path, u'share1_name')
3558
3592
        make_link(self.shares_dir, old_shares_path)
3559
3593
        old_root_path = os.path.join(os.path.dirname(self.root_dir),
3560
3594
                                     'Ubuntu One', 'My Files')
3569
3603
 
3570
3604
        # put the version file in 1
3571
3605
        version_file = os.path.join(self.data_dir, "metadata_version")
3572
 
        with open(version_file, "w") as fh:
 
3606
        with open_file(version_file, "w") as fh:
3573
3607
            fh.write("3")
3574
3608
 
3575
3609
        # create a old-style fs with the data:
3582
3616
 
3583
3617
        # start up again, and check
3584
3618
        db = Tritcask(os.path.join(self.main.data_dir, 'tritcask.new'))
 
3619
        self.addCleanup(db.shutdown)
3585
3620
        newfsm = FileSystemManager(self.data_dir, self.partials_dir,
3586
3621
                                   self.fsm.vm, db)
3587
3622
        version_file = os.path.join(self.data_dir, "metadata_version")
3588
 
        md_version = open(version_file).read()
 
3623
        md_version = open_file(version_file).read()
3589
3624
        self.assertEqual(md_version, METADATA_VERSION)
3590
3625
        self.assertTrue(newfsm.get_by_mdid(root_mdid) is not None)
3591
3626
        # pylint: disable-msg=W0212
3592
3627
        self.assertEqual(1, len(newfsm._idx_node_id))
3593
3628
        self.assertEqual(1, len(newfsm._idx_path))
3594
3629
        self.assertRaises(KeyError, newfsm.get_by_mdid, share_mdid)
3595
 
        db.shutdown()
3596
3630
 
3597
3631
    @defer.inlineCallbacks
3598
3632
    def test_metadata_missing_share(self):
3599
3633
        """test loading current metadata that points to a share
3600
3634
        that we don't have
3601
3635
        """
3602
 
        md_version = open(os.path.join(self.data_dir,
 
3636
        md_version = open_file(os.path.join(self.data_dir,
3603
3637
                                       "metadata_version")).read()
3604
3638
        self.assertEqual(md_version, METADATA_VERSION)
3605
3639
        path = os.path.join(self.share.path, 'path')
3606
3640
        path1 = os.path.join(self.share.path, 'path1')
3607
 
        other_share = yield self.create_share('share1', 'share1_name')
 
3641
        other_share = yield self.create_share('share1', u'share1_name')
3608
3642
 
3609
3643
        path2 = os.path.join(other_share.path, 'broken_path2')
3610
3644
        for p in [path, path1, path2]:
3611
 
            open(p, "w").close()
 
3645
            open_file(p, "w").close()
3612
3646
        mdid = self.fsm.create(path, "share")
3613
3647
        self.fsm.set_node_id(path, "uuid")
3614
3648
        mdid1 = self.fsm.create(path1, "share")
3623
3657
        newfsm = FileSystemManager(self.data_dir, self.partials_dir,
3624
3658
                                   self.fsm.vm, self.main.db)
3625
3659
        version_file = os.path.join(self.data_dir, "metadata_version")
3626
 
        md_version = open(version_file).read()
 
3660
        md_version = open_file(version_file).read()
3627
3661
        self.assertEqual(md_version, METADATA_VERSION)
3628
3662
        self.assertTrue(newfsm.get_by_mdid(mdid) is not None)
3629
3663
        # pylint: disable-msg=W0212
3653
3687
            self.fsm.create(d, '', is_dir=True)
3654
3688
            self.fsm.set_node_id(d, 'uuid')
3655
3689
 
3656
 
        open(self.some_file, 'w').close()
 
3690
        open_file(self.some_file, 'w').close()
3657
3691
        self.fsm.create(self.some_file, "")
3658
3692
        self.fsm.set_node_id(self.some_file, "uuid_file")
3659
3693
 
3667
3701
    def test_dir_names_only(self):
3668
3702
        """Check paths starting with excluding directories with same prefix."""
3669
3703
        similar_dir = os.path.join(self.root_dir, 'fooo')
3670
 
        os.mkdir(similar_dir)
 
3704
        make_dir(similar_dir)
3671
3705
        self.fsm.create(similar_dir, '', is_dir=True)
3672
3706
        self.fsm.set_node_id(similar_dir, 'uuid')
3673
3707
 
3747
3781
        """Move file adds a mute filter."""
3748
3782
        path1 = os.path.join(self.share.path, "thisfile1")
3749
3783
        path2 = os.path.join(self.share.path, "thisfile2")
3750
 
        open(path1, "w").close()
 
3784
        open_file(path1, "w").close()
3751
3785
        self.create_node(path1)
3752
3786
 
3753
3787
        # move and check
3769
3803
        """Move dir adds a mute filter."""
3770
3804
        path1 = os.path.join(self.share.path, "thisfile1")
3771
3805
        path2 = os.path.join(self.share.path, "thisfile2")
3772
 
        os.mkdir(path1)
 
3806
        make_dir(path1)
3773
3807
        self.create_node(path1, is_dir=True)
3774
3808
 
3775
3809
        # move and check
3790
3824
    def test_deletefile_ok(self):
3791
3825
        """Delete file adds a mute filter."""
3792
3826
        testfile = os.path.join(self.share_path, "path")
3793
 
        open(testfile, "w").close()
 
3827
        open_file(testfile, "w").close()
3794
3828
        self.fsm.create(testfile, "share")
3795
3829
        self.fsm.set_node_id(testfile, "uuid")
3796
3830
 
3811
3845
    def test_deletedir_ok(self):
3812
3846
        """Delete dir adds a mute filter."""
3813
3847
        testfile = os.path.join(self.share_path, "path")
3814
 
        os.mkdir(testfile)
 
3848
        make_dir(testfile)
3815
3849
        self.fsm.create(testfile, "share", is_dir=True)
3816
3850
        self.fsm.set_node_id(testfile, "uuid")
3817
3851
 
3836
3870
        ignored (and that transforms the MOVE into DELETE).
3837
3871
        """
3838
3872
        path1 = os.path.join(self.share.path, "thisfile1")
3839
 
        open(path1, "w").close()
 
3873
        open_file(path1, "w").close()
3840
3874
        mdobj = self.create_node(path1)
3841
3875
 
3842
3876
        # move and check
3859
3893
        ignored (and that transforms the MOVE into DELETE).
3860
3894
        """
3861
3895
        path1 = os.path.join(self.share.path, "thisfile1")
3862
 
        os.mkdir(path1)
 
3896
        make_dir(path1)
3863
3897
        mdobj = self.create_node(path1, is_dir=True)
3864
3898
 
3865
3899
        # move and check
3994
4028
    def setUp(self):
3995
4029
        """Set up."""
3996
4030
        yield super(OsIntegrationTests, self).setUp()
3997
 
        self.open = self.mocker.replace('ubuntuone.platform.open_file')
 
4031
        self.open_file = self.mocker.replace('ubuntuone.platform.open_file')
3998
4032
        self.normpath = self.mocker.replace('ubuntuone.platform.normpath')
3999
4033
        self.listdir = self.mocker.replace('ubuntuone.platform.listdir')
4000
4034
 
4010
4044
        self.fsm._get_partial_path = self.mocker.mock()
4011
4045
        self.fsm._get_partial_path(path)
4012
4046
        self.mocker.result(path)
4013
 
        self.open(path, 'wb')
 
4047
        self.open_file(path, 'wb')
4014
4048
        self.mocker.result(fd)
4015
4049
        self.mocker.replay()
4016
4050
        self.assertEqual(fd, self.fsm.get_partial_for_writing(node_id, share_id))
4031
4065
        self.mocker.result(True)
4032
4066
        self.fsm._get_partial_path(path)
4033
4067
        self.mocker.result(path)
4034
 
        self.open(path, 'rb')
 
4068
        self.open_file(path, 'rb')
4035
4069
        self.mocker.result(fd)
4036
4070
        self.mocker.replay()
4037
4071
        self.assertEqual(fd, self.fsm.get_partial(node_id, share_id))
4045
4079
        self.fsm.fs = dict(id=mdobj)
4046
4080
        self.fsm.get_abspath(mdobj['share_id'], mdobj['path'])
4047
4081
        self.mocker.result(mdobj['path'])
4048
 
        self.open(mdobj['path'], 'rb')
 
4082
        self.open_file(mdobj['path'], 'rb')
4049
4083
        self.mocker.result(fd)
4050
4084
        self.mocker.replay()
4051
4085
        self.assertEqual(fd, self.fsm.open_file(mdid))