57
68
from ubuntuone.syncdaemon.volume_manager import Share, allow_writes
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.')
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")
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)
96
113
self.handler = MementoHandler()
97
114
self.handler.setLevel(0)
98
115
logger.root_logger.addHandler(self.handler)
100
@defer.inlineCallbacks
102
"""Clean up the tests."""
106
logger.root_logger.removeHandler(self.handler)
107
yield super(FSMTestCase, self).tearDown()
116
self.addCleanup(logger.root_logger.removeHandler, self.handler)
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)
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))
158
self.assertTrue(path_exists(fsmdir))
150
160
@defer.inlineCallbacks
151
161
def test_complex_startup(self):
156
166
partials_dir = self.mktemp("a_partials_dir")
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, {})
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)
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')
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,
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, "")
356
369
self.fsm.set_node_id(path2, "uuid2")
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')
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,
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)
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')
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,
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)
474
489
root_mdid = self.fsm.create(self.root_dir, "")
475
490
self.fsm.set_node_id(self.root_dir, "uuid")
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')
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,
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'])
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,
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)
613
@skipOnWinIfMetadataPreV4
597
614
def test_old_metadata_5(self):
598
615
"""Test old metadata situation, in v5."""
599
616
# create some stuff
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,
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)
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]:
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')
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())
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())
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)
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,
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)
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
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())
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:
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,
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))
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]:
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')
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:
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())
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,
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))
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]:
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())
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())
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)
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,
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)
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())
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:
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,
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)
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]:
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")
962
985
old_fs[mdid2] = mdobj
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())
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,
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)
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")
1368
1392
for mdobj in self.fsm.get_mdobjs_by_share_id("share_id1"):
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)
1378
1402
for mdobj in self.fsm.get_mdobjs_by_share_id("share_id2"):
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)
1388
for mdobj in self.fsm.get_mdobjs_by_share_id("share_id1", os.
1389
path.join(share1.path, 'a')):
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)
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)
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")
1420
1444
for mdobj in self.fsm.get_mdobjs_by_share_id("share_id"):
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)
1426
1450
def test_internal_set_node_id(self):
1427
1451
"""Test _set_node_id"""
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)
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)
1538
1566
"""Test creation when there's a 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))
1546
1574
path = os.path.join(self.share.path, "thisdir")
1548
1576
mdobj = self.create_node("thisdir")
1549
self.assertEqual(mdobj.stat, os.stat(path))
1577
self.assertEqual(mdobj.stat, stat_path(path))
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)
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))
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))
1582
1610
self.fsm.move_file("share", path1, path2)
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))
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))
1601
1629
self.fsm.move_file("share", path1, path2)
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))
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)
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)
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!")
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!")
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")
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
1819
1847
It should leave the partials dir permissions intact.
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)
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!")
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
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!")
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!")
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!")
2026
2054
self.fsm.move_to_conflict(mdid)
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!")
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)
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!")
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")
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")
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")
2151
2179
# move the file
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
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")
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()
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()
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
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'))
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")
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")
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")
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")
2262
2290
mdid = self.fsm.create(testdir, "share", is_dir=True)
2263
2291
self.fsm.set_node_id(testdir, "uuid")
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"))
2272
2300
# really delete the dir
2273
2301
self.fsm.delete_file(testdir)
2275
self.assertFalse(os.path.exists(testdir))
2303
self.assertFalse(path_exists(testdir))
2276
2304
self.assert_no_metadata(mdid, testdir, "share", "uuid")
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")
2308
2336
mdid = self.fsm.create(local_dir, "", is_dir=True)
2309
2337
self.fsm.set_node_id(local_dir, "uuid")
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")
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)
2319
2347
self.fsm.delete_file(local_dir)
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")
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")
2327
2355
def test_delete_nonempty_cleanable_dir_directly(self):
2331
2359
# check it was sent to trash, not just deleted
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))
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")
2357
2385
self.fsm.create(local_dir, "", is_dir=True)
2358
2386
self.fsm.set_node_id(local_dir, "uuid")
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)
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")
2376
2404
self.fsm.create(local_dir, "", is_dir=True)
2377
2405
self.fsm.set_node_id(local_dir, "uuid")
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
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")
2397
2425
self.fsm.create(local_dir, "", is_dir=True)
2398
2426
self.fsm.set_node_id(local_dir, "uuid")
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)
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")
2417
2445
self.fsm.create(local_dir, "", is_dir=True)
2418
2446
self.fsm.set_node_id(local_dir, "uuid")
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")
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")
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!")
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")
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")
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")
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!")
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)
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))
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")
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)
2817
2845
self.fsm.make_dir(mdid)
2818
self.assertTrue(os.path.exists(local_dir))
2846
self.assertTrue(path_exists(local_dir))
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))
2830
2858
@defer.inlineCallbacks
2831
2859
def test_make_dir_ro_watch(self):
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)
2937
2965
self.assertEqual(self.fsm.changed(path=testfile), self.fsm.CHANGED_NONE)
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)
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')
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)
3063
self.assertTrue(os.path.exists(testdir))
3091
self.assertTrue(path_exists(testdir))
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)
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))
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)
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))
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)
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))
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)
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))
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)
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))
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)
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))
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)
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))
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,
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))
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))
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)
3246
self.assertTrue(os.path.exists(testdir))
3276
self.assertTrue(path_exists(testdir))
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")
3277
3307
fd = self.fsm.get_partial_for_writing('uuid2', self.share.volume_id)
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))
3284
3314
class TestEnableShareWrite(FSMTestCase):
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:
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:
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))
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)
3376
3407
# put the old path in the mdobj
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))
3430
3462
self.fsm.set_node_id(path2, "uuid2")
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)
3442
3474
# put the old path in the mdobj
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)
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)
3503
3536
# put the old path in the mdobj
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)
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")
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')
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)
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
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')
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')
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")
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")
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))