1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: AGPL-3.0
5 from __future__ import absolute_import
6 from future.utils import viewitems
7 from builtins import str
8 from builtins import object
9 from six import assertRegex
20 import arvados_fuse as fuse
21 from . import run_test_server
23 from .integration_test import IntegrationTest
24 from .mount_test_base import MountTestBase
26 logger = logging.getLogger('arvados.arv-mount')
29 class AssertWithTimeout(object):
30 """Allow some time for an assertion to pass."""
32 def __init__(self, timeout=0):
33 self.timeout = timeout
36 self.deadline = time.time() + self.timeout
45 def attempt(self, fn, *args, **kwargs):
48 except AssertionError:
49 if time.time() > self.deadline:
56 class FuseMountTest(MountTestBase):
58 super(FuseMountTest, self).setUp()
60 cw = arvados.CollectionWriter()
62 cw.start_new_file('thing1.txt')
64 cw.start_new_file('thing2.txt')
67 cw.start_new_stream('dir1')
68 cw.start_new_file('thing3.txt')
70 cw.start_new_file('thing4.txt')
73 cw.start_new_stream('dir2')
74 cw.start_new_file('thing5.txt')
76 cw.start_new_file('thing6.txt')
79 cw.start_new_stream('dir2/dir3')
80 cw.start_new_file('thing7.txt')
83 cw.start_new_file('thing8.txt')
86 cw.start_new_stream('edgecases')
87 for f in ":/.../-/*/ ".split("/"):
91 for f in ":/.../-/*/ ".split("/"):
92 cw.start_new_stream('edgecases/dirs/' + f)
93 cw.start_new_file('x/x')
96 self.testcollection = cw.finish()
97 self.api.collections().create(body={"manifest_text":cw.manifest_text()}).execute()
100 self.make_mount(fuse.CollectionDirectory, collection_record=self.testcollection)
102 self.assertDirContents(None, ['thing1.txt', 'thing2.txt',
103 'edgecases', 'dir1', 'dir2'])
104 self.assertDirContents('dir1', ['thing3.txt', 'thing4.txt'])
105 self.assertDirContents('dir2', ['thing5.txt', 'thing6.txt', 'dir3'])
106 self.assertDirContents('dir2/dir3', ['thing7.txt', 'thing8.txt'])
107 self.assertDirContents('edgecases',
108 "dirs/:/.../-/*/ ".split("/"))
109 self.assertDirContents('edgecases/dirs',
110 ":/.../-/*/ ".split("/"))
112 files = {'thing1.txt': 'data 1',
113 'thing2.txt': 'data 2',
114 'dir1/thing3.txt': 'data 3',
115 'dir1/thing4.txt': 'data 4',
116 'dir2/thing5.txt': 'data 5',
117 'dir2/thing6.txt': 'data 6',
118 'dir2/dir3/thing7.txt': 'data 7',
119 'dir2/dir3/thing8.txt': 'data 8'}
121 for k, v in viewitems(files):
122 with open(os.path.join(self.mounttmp, k), 'rb') as f:
123 self.assertEqual(v, f.read().decode())
126 class FuseMagicTest(MountTestBase):
127 def setUp(self, api=None):
128 super(FuseMagicTest, self).setUp(api=api)
130 self.test_project = run_test_server.fixture('groups')['aproject']['uuid']
131 self.non_project_group = run_test_server.fixture('groups')['public_role']['uuid']
132 self.filter_group = run_test_server.fixture('groups')['afiltergroup']['uuid']
133 self.collection_in_test_project = run_test_server.fixture('collections')['foo_collection_in_aproject']['name']
134 self.collection_in_filter_group = run_test_server.fixture('collections')['baz_file']['name']
136 cw = arvados.CollectionWriter()
138 cw.start_new_file('thing1.txt')
141 self.testcollection = cw.finish()
142 self.test_manifest = cw.manifest_text()
143 coll = self.api.collections().create(body={"manifest_text":self.test_manifest}).execute()
144 self.test_manifest_pdh = coll['portable_data_hash']
147 self.make_mount(fuse.MagicDirectory)
149 mount_ls = llfuse.listdir(self.mounttmp)
150 self.assertIn('README', mount_ls)
151 self.assertFalse(any(arvados.util.keep_locator_pattern.match(fn) or
152 arvados.util.uuid_pattern.match(fn)
154 "new FUSE MagicDirectory has no collections or projects")
155 self.assertDirContents(self.testcollection, ['thing1.txt'])
156 self.assertDirContents(os.path.join('by_id', self.testcollection),
158 self.assertIn(self.collection_in_test_project,
159 llfuse.listdir(os.path.join(self.mounttmp, self.test_project)))
160 self.assertIn(self.collection_in_test_project,
161 llfuse.listdir(os.path.join(self.mounttmp, 'by_id', self.test_project)))
162 self.assertIn(self.collection_in_filter_group,
163 llfuse.listdir(os.path.join(self.mounttmp, self.filter_group)))
164 self.assertIn(self.collection_in_filter_group,
165 llfuse.listdir(os.path.join(self.mounttmp, 'by_id', self.filter_group)))
168 mount_ls = llfuse.listdir(self.mounttmp)
169 self.assertIn('README', mount_ls)
170 self.assertIn(self.testcollection, mount_ls)
171 self.assertIn(self.testcollection,
172 llfuse.listdir(os.path.join(self.mounttmp, 'by_id')))
173 self.assertIn(self.test_project, mount_ls)
174 self.assertIn(self.test_project,
175 llfuse.listdir(os.path.join(self.mounttmp, 'by_id')))
176 self.assertIn(self.filter_group,
177 llfuse.listdir(os.path.join(self.mounttmp, 'by_id')))
179 with self.assertRaises(OSError):
180 llfuse.listdir(os.path.join(self.mounttmp, 'by_id', self.non_project_group))
183 files[os.path.join(self.mounttmp, self.testcollection, 'thing1.txt')] = 'data 1'
185 for k, v in viewitems(files):
186 with open(os.path.join(self.mounttmp, k), 'rb') as f:
187 self.assertEqual(v, f.read().decode())
190 class FuseTagsTest(MountTestBase):
192 self.make_mount(fuse.TagsDirectory)
194 d1 = llfuse.listdir(self.mounttmp)
196 self.assertEqual(['foo_tag'], d1)
198 d2 = llfuse.listdir(os.path.join(self.mounttmp, 'foo_tag'))
200 self.assertEqual(['zzzzz-4zz18-fy296fx3hot09f7'], d2)
202 d3 = llfuse.listdir(os.path.join(self.mounttmp, 'foo_tag', 'zzzzz-4zz18-fy296fx3hot09f7'))
204 self.assertEqual(['foo'], d3)
207 class FuseTagsUpdateTest(MountTestBase):
208 def tag_collection(self, coll_uuid, tag_name):
209 return self.api.links().create(
210 body={'link': {'head_uuid': coll_uuid,
216 self.make_mount(fuse.TagsDirectory, poll_time=1)
218 self.assertIn('foo_tag', llfuse.listdir(self.mounttmp))
220 bar_uuid = run_test_server.fixture('collections')['bar_file']['uuid']
221 self.tag_collection(bar_uuid, 'fuse_test_tag')
222 for attempt in AssertWithTimeout(10):
223 attempt(self.assertIn, 'fuse_test_tag', llfuse.listdir(self.mounttmp))
224 self.assertDirContents('fuse_test_tag', [bar_uuid])
226 baz_uuid = run_test_server.fixture('collections')['baz_file']['uuid']
227 l = self.tag_collection(baz_uuid, 'fuse_test_tag')
228 for attempt in AssertWithTimeout(10):
229 attempt(self.assertDirContents, 'fuse_test_tag', [bar_uuid, baz_uuid])
231 self.api.links().delete(uuid=l['uuid']).execute()
232 for attempt in AssertWithTimeout(10):
233 attempt(self.assertDirContents, 'fuse_test_tag', [bar_uuid])
236 def fuseSharedTestHelper(mounttmp):
237 class Test(unittest.TestCase):
239 # Double check that we can open and read objects in this folder as a file,
240 # and that its contents are what we expect.
241 baz_path = os.path.join(
245 'collection in FUSE project',
247 with open(baz_path) as f:
248 self.assertEqual("baz", f.read())
250 # check mtime on collection
251 st = os.stat(baz_path)
253 mtime = st.st_mtime_ns // 1000000000
254 except AttributeError:
256 self.assertEqual(mtime, 1391448174)
258 # shared_dirs is a list of the directories exposed
259 # by fuse.SharedDirectory (i.e. any object visible
260 # to the current user)
261 shared_dirs = llfuse.listdir(mounttmp)
263 self.assertIn('FUSE User', shared_dirs)
265 # fuse_user_objs is a list of the objects owned by the FUSE
266 # test user (which present as files in the 'FUSE User'
268 fuse_user_objs = llfuse.listdir(os.path.join(mounttmp, 'FUSE User'))
269 fuse_user_objs.sort()
270 self.assertEqual(['FUSE Test Project', # project owned by user
271 'collection #1 owned by FUSE', # collection owned by user
272 'collection #2 owned by FUSE' # collection owned by user
275 # test_proj_files is a list of the files in the FUSE Test Project.
276 test_proj_files = llfuse.listdir(os.path.join(mounttmp, 'FUSE User', 'FUSE Test Project'))
277 test_proj_files.sort()
278 self.assertEqual(['collection in FUSE project'
284 class FuseSharedTest(MountTestBase):
286 self.make_mount(fuse.SharedDirectory,
287 exclude=self.api.users().current().execute()['uuid'])
288 keep = arvados.keep.KeepClient()
289 keep.put("baz".encode())
291 self.pool.apply(fuseSharedTestHelper, (self.mounttmp,))
294 class FuseHomeTest(MountTestBase):
296 self.make_mount(fuse.ProjectDirectory,
297 project_object=self.api.users().current().execute())
299 d1 = llfuse.listdir(self.mounttmp)
300 self.assertIn('Unrestricted public data', d1)
302 d2 = llfuse.listdir(os.path.join(self.mounttmp, 'Unrestricted public data'))
303 public_project = run_test_server.fixture('groups')[
304 'anonymously_accessible_project']
307 for name, item in viewitems(run_test_server.fixture('collections')):
308 if 'name' not in item:
310 elif item['owner_uuid'] == public_project['uuid']:
311 self.assertIn(item['name'], d2)
314 # Artificial assumption here: there is no public
315 # collection fixture with the same name as a
316 # non-public collection.
317 self.assertNotIn(item['name'], d2)
319 self.assertNotEqual(0, found_in)
320 self.assertNotEqual(0, found_not_in)
322 d3 = llfuse.listdir(os.path.join(self.mounttmp, 'Unrestricted public data', 'GNU General Public License, version 3'))
323 self.assertEqual(["GNU_General_Public_License,_version_3.pdf"], d3)
326 def fuseModifyFileTestHelperReadStartContents(mounttmp):
327 class Test(unittest.TestCase):
329 d1 = llfuse.listdir(mounttmp)
330 self.assertEqual(["file1.txt"], d1)
331 with open(os.path.join(mounttmp, "file1.txt")) as f:
332 self.assertEqual("blub", f.read())
335 def fuseModifyFileTestHelperReadEndContents(mounttmp):
336 class Test(unittest.TestCase):
338 d1 = llfuse.listdir(mounttmp)
339 self.assertEqual(["file1.txt"], d1)
340 with open(os.path.join(mounttmp, "file1.txt")) as f:
341 self.assertEqual("plnp", f.read())
344 class FuseModifyFileTest(MountTestBase):
346 collection = arvados.collection.Collection(api_client=self.api)
347 with collection.open("file1.txt", "w") as f:
350 collection.save_new()
352 m = self.make_mount(fuse.CollectionDirectory)
354 m.new_collection(collection.api_response(), collection)
356 self.pool.apply(fuseModifyFileTestHelperReadStartContents, (self.mounttmp,))
358 with collection.open("file1.txt", "w") as f:
361 self.pool.apply(fuseModifyFileTestHelperReadEndContents, (self.mounttmp,))
364 class FuseAddFileToCollectionTest(MountTestBase):
366 collection = arvados.collection.Collection(api_client=self.api)
367 with collection.open("file1.txt", "w") as f:
370 collection.save_new()
372 m = self.make_mount(fuse.CollectionDirectory)
374 m.new_collection(collection.api_response(), collection)
376 d1 = llfuse.listdir(self.mounttmp)
377 self.assertEqual(["file1.txt"], d1)
379 with collection.open("file2.txt", "w") as f:
382 d1 = llfuse.listdir(self.mounttmp)
383 self.assertEqual(["file1.txt", "file2.txt"], sorted(d1))
386 class FuseRemoveFileFromCollectionTest(MountTestBase):
388 collection = arvados.collection.Collection(api_client=self.api)
389 with collection.open("file1.txt", "w") as f:
392 with collection.open("file2.txt", "w") as f:
395 collection.save_new()
397 m = self.make_mount(fuse.CollectionDirectory)
399 m.new_collection(collection.api_response(), collection)
401 d1 = llfuse.listdir(self.mounttmp)
402 self.assertEqual(["file1.txt", "file2.txt"], sorted(d1))
404 collection.remove("file2.txt")
406 d1 = llfuse.listdir(self.mounttmp)
407 self.assertEqual(["file1.txt"], d1)
410 def fuseCreateFileTestHelper(mounttmp):
411 class Test(unittest.TestCase):
413 with open(os.path.join(mounttmp, "file1.txt"), "w") as f:
417 class FuseCreateFileTest(MountTestBase):
419 collection = arvados.collection.Collection(api_client=self.api)
420 collection.save_new()
422 collection2 = self.api.collections().get(uuid=collection.manifest_locator()).execute()
423 self.assertEqual(collection2["manifest_text"], "")
425 collection.save_new()
427 m = self.make_mount(fuse.CollectionDirectory)
429 m.new_collection(collection.api_response(), collection)
430 self.assertTrue(m.writable())
432 self.assertNotIn("file1.txt", collection)
434 self.pool.apply(fuseCreateFileTestHelper, (self.mounttmp,))
436 self.assertIn("file1.txt", collection)
438 d1 = llfuse.listdir(self.mounttmp)
439 self.assertEqual(["file1.txt"], d1)
441 collection2 = self.api.collections().get(uuid=collection.manifest_locator()).execute()
442 assertRegex(self, collection2["manifest_text"],
443 r'\. d41d8cd98f00b204e9800998ecf8427e\+0\+A\S+ 0:0:file1\.txt$')
446 def fuseWriteFileTestHelperWriteFile(mounttmp):
447 class Test(unittest.TestCase):
449 with open(os.path.join(mounttmp, "file1.txt"), "w") as f:
450 f.write("Hello world!")
453 def fuseWriteFileTestHelperReadFile(mounttmp):
454 class Test(unittest.TestCase):
456 with open(os.path.join(mounttmp, "file1.txt"), "r") as f:
457 self.assertEqual(f.read(), "Hello world!")
460 class FuseWriteFileTest(MountTestBase):
462 collection = arvados.collection.Collection(api_client=self.api)
463 collection.save_new()
465 m = self.make_mount(fuse.CollectionDirectory)
467 m.new_collection(collection.api_response(), collection)
468 self.assertTrue(m.writable())
470 self.assertNotIn("file1.txt", collection)
472 self.assertEqual(0, self.operations.write_counter.get())
473 self.pool.apply(fuseWriteFileTestHelperWriteFile, (self.mounttmp,))
474 self.assertEqual(12, self.operations.write_counter.get())
476 with collection.open("file1.txt") as f:
477 self.assertEqual(f.read(), "Hello world!")
479 self.assertEqual(0, self.operations.read_counter.get())
480 self.pool.apply(fuseWriteFileTestHelperReadFile, (self.mounttmp,))
481 self.assertEqual(12, self.operations.read_counter.get())
483 collection2 = self.api.collections().get(uuid=collection.manifest_locator()).execute()
484 assertRegex(self, collection2["manifest_text"],
485 r'\. 86fb269d190d2c85f6e0468ceca42a20\+12\+A\S+ 0:12:file1\.txt$')
488 def fuseUpdateFileTestHelper(mounttmp):
489 class Test(unittest.TestCase):
491 with open(os.path.join(mounttmp, "file1.txt"), "w") as f:
492 f.write("Hello world!")
494 with open(os.path.join(mounttmp, "file1.txt"), "r+") as f:
496 self.assertEqual(fr, "Hello world!")
498 f.write("Hola mundo!")
501 self.assertEqual(fr, "Hola mundo!!")
503 with open(os.path.join(mounttmp, "file1.txt"), "r") as f:
504 self.assertEqual(f.read(), "Hola mundo!!")
508 class FuseUpdateFileTest(MountTestBase):
510 collection = arvados.collection.Collection(api_client=self.api)
511 collection.save_new()
513 m = self.make_mount(fuse.CollectionDirectory)
515 m.new_collection(collection.api_response(), collection)
516 self.assertTrue(m.writable())
518 # See note in MountTestBase.setUp
519 self.pool.apply(fuseUpdateFileTestHelper, (self.mounttmp,))
521 collection2 = self.api.collections().get(uuid=collection.manifest_locator()).execute()
522 assertRegex(self, collection2["manifest_text"],
523 r'\. daaef200ebb921e011e3ae922dd3266b\+11\+A\S+ 86fb269d190d2c85f6e0468ceca42a20\+12\+A\S+ 0:11:file1\.txt 22:1:file1\.txt$')
526 def fuseMkdirTestHelper(mounttmp):
527 class Test(unittest.TestCase):
529 with self.assertRaises(IOError):
530 with open(os.path.join(mounttmp, "testdir", "file1.txt"), "w") as f:
531 f.write("Hello world!")
533 os.mkdir(os.path.join(mounttmp, "testdir"))
535 with self.assertRaises(OSError):
536 os.mkdir(os.path.join(mounttmp, "testdir"))
538 d1 = llfuse.listdir(mounttmp)
539 self.assertEqual(["testdir"], d1)
541 with open(os.path.join(mounttmp, "testdir", "file1.txt"), "w") as f:
542 f.write("Hello world!")
544 d1 = llfuse.listdir(os.path.join(mounttmp, "testdir"))
545 self.assertEqual(["file1.txt"], d1)
549 class FuseMkdirTest(MountTestBase):
551 collection = arvados.collection.Collection(api_client=self.api)
552 collection.save_new()
554 m = self.make_mount(fuse.CollectionDirectory)
556 m.new_collection(collection.api_response(), collection)
557 self.assertTrue(m.writable())
559 self.pool.apply(fuseMkdirTestHelper, (self.mounttmp,))
561 collection2 = self.api.collections().get(uuid=collection.manifest_locator()).execute()
562 assertRegex(self, collection2["manifest_text"],
563 r'\./testdir 86fb269d190d2c85f6e0468ceca42a20\+12\+A\S+ 0:12:file1\.txt$')
566 def fuseRmTestHelperWriteFile(mounttmp):
567 class Test(unittest.TestCase):
569 os.mkdir(os.path.join(mounttmp, "testdir"))
571 with open(os.path.join(mounttmp, "testdir", "file1.txt"), "w") as f:
572 f.write("Hello world!")
576 def fuseRmTestHelperDeleteFile(mounttmp):
577 class Test(unittest.TestCase):
579 # Can't delete because it's not empty
580 with self.assertRaises(OSError):
581 os.rmdir(os.path.join(mounttmp, "testdir"))
583 d1 = llfuse.listdir(os.path.join(mounttmp, "testdir"))
584 self.assertEqual(["file1.txt"], d1)
587 os.remove(os.path.join(mounttmp, "testdir", "file1.txt"))
589 # Make sure it's empty
590 d1 = llfuse.listdir(os.path.join(mounttmp, "testdir"))
591 self.assertEqual([], d1)
593 # Try to delete it again
594 with self.assertRaises(OSError):
595 os.remove(os.path.join(mounttmp, "testdir", "file1.txt"))
599 def fuseRmTestHelperRmdir(mounttmp):
600 class Test(unittest.TestCase):
602 # Should be able to delete now that it is empty
603 os.rmdir(os.path.join(mounttmp, "testdir"))
605 # Make sure it's empty
606 d1 = llfuse.listdir(os.path.join(mounttmp))
607 self.assertEqual([], d1)
609 # Try to delete it again
610 with self.assertRaises(OSError):
611 os.rmdir(os.path.join(mounttmp, "testdir"))
615 class FuseRmTest(MountTestBase):
617 collection = arvados.collection.Collection(api_client=self.api)
618 collection.save_new()
620 m = self.make_mount(fuse.CollectionDirectory)
622 m.new_collection(collection.api_response(), collection)
623 self.assertTrue(m.writable())
625 self.pool.apply(fuseRmTestHelperWriteFile, (self.mounttmp,))
628 collection2 = self.api.collections().get(uuid=collection.manifest_locator()).execute()
629 assertRegex(self, collection2["manifest_text"],
630 r'\./testdir 86fb269d190d2c85f6e0468ceca42a20\+12\+A\S+ 0:12:file1\.txt$')
631 self.pool.apply(fuseRmTestHelperDeleteFile, (self.mounttmp,))
633 # Empty directories are represented by an empty file named "."
634 collection2 = self.api.collections().get(uuid=collection.manifest_locator()).execute()
635 assertRegex(self, collection2["manifest_text"],
636 r'./testdir d41d8cd98f00b204e9800998ecf8427e\+0\+A\S+ 0:0:\\056\n')
638 self.pool.apply(fuseRmTestHelperRmdir, (self.mounttmp,))
640 # manifest should be empty now.
641 collection2 = self.api.collections().get(uuid=collection.manifest_locator()).execute()
642 self.assertEqual(collection2["manifest_text"], "")
645 def fuseMvFileTestHelperWriteFile(mounttmp):
646 class Test(unittest.TestCase):
648 os.mkdir(os.path.join(mounttmp, "testdir"))
650 with open(os.path.join(mounttmp, "testdir", "file1.txt"), "w") as f:
651 f.write("Hello world!")
655 def fuseMvFileTestHelperMoveFile(mounttmp):
656 class Test(unittest.TestCase):
658 d1 = llfuse.listdir(os.path.join(mounttmp))
659 self.assertEqual(["testdir"], d1)
660 d1 = llfuse.listdir(os.path.join(mounttmp, "testdir"))
661 self.assertEqual(["file1.txt"], d1)
663 os.rename(os.path.join(mounttmp, "testdir", "file1.txt"), os.path.join(mounttmp, "file1.txt"))
665 d1 = llfuse.listdir(os.path.join(mounttmp))
666 self.assertEqual(["file1.txt", "testdir"], sorted(d1))
667 d1 = llfuse.listdir(os.path.join(mounttmp, "testdir"))
668 self.assertEqual([], d1)
672 class FuseMvFileTest(MountTestBase):
674 collection = arvados.collection.Collection(api_client=self.api)
675 collection.save_new()
677 m = self.make_mount(fuse.CollectionDirectory)
679 m.new_collection(collection.api_response(), collection)
680 self.assertTrue(m.writable())
682 self.pool.apply(fuseMvFileTestHelperWriteFile, (self.mounttmp,))
685 collection2 = self.api.collections().get(uuid=collection.manifest_locator()).execute()
686 assertRegex(self, collection2["manifest_text"],
687 r'\./testdir 86fb269d190d2c85f6e0468ceca42a20\+12\+A\S+ 0:12:file1\.txt$')
689 self.pool.apply(fuseMvFileTestHelperMoveFile, (self.mounttmp,))
691 collection2 = self.api.collections().get(uuid=collection.manifest_locator()).execute()
692 assertRegex(self, collection2["manifest_text"],
693 r'\. 86fb269d190d2c85f6e0468ceca42a20\+12\+A\S+ 0:12:file1\.txt\n\./testdir d41d8cd98f00b204e9800998ecf8427e\+0\+A\S+ 0:0:\\056\n')
696 def fuseRenameTestHelper(mounttmp):
697 class Test(unittest.TestCase):
699 os.mkdir(os.path.join(mounttmp, "testdir"))
701 with open(os.path.join(mounttmp, "testdir", "file1.txt"), "w") as f:
702 f.write("Hello world!")
706 class FuseRenameTest(MountTestBase):
708 collection = arvados.collection.Collection(api_client=self.api)
709 collection.save_new()
711 m = self.make_mount(fuse.CollectionDirectory)
713 m.new_collection(collection.api_response(), collection)
714 self.assertTrue(m.writable())
716 self.pool.apply(fuseRenameTestHelper, (self.mounttmp,))
719 collection2 = self.api.collections().get(uuid=collection.manifest_locator()).execute()
720 assertRegex(self, collection2["manifest_text"],
721 r'\./testdir 86fb269d190d2c85f6e0468ceca42a20\+12\+A\S+ 0:12:file1\.txt$')
723 d1 = llfuse.listdir(os.path.join(self.mounttmp))
724 self.assertEqual(["testdir"], d1)
725 d1 = llfuse.listdir(os.path.join(self.mounttmp, "testdir"))
726 self.assertEqual(["file1.txt"], d1)
728 os.rename(os.path.join(self.mounttmp, "testdir"), os.path.join(self.mounttmp, "testdir2"))
730 d1 = llfuse.listdir(os.path.join(self.mounttmp))
731 self.assertEqual(["testdir2"], sorted(d1))
732 d1 = llfuse.listdir(os.path.join(self.mounttmp, "testdir2"))
733 self.assertEqual(["file1.txt"], d1)
735 collection2 = self.api.collections().get(uuid=collection.manifest_locator()).execute()
736 assertRegex(self, collection2["manifest_text"],
737 r'\./testdir2 86fb269d190d2c85f6e0468ceca42a20\+12\+A\S+ 0:12:file1\.txt$')
740 class FuseUpdateFromEventTest(MountTestBase):
742 collection = arvados.collection.Collection(api_client=self.api)
743 collection.save_new()
745 m = self.make_mount(fuse.CollectionDirectory)
747 m.new_collection(collection.api_response(), collection)
749 self.operations.listen_for_events()
751 d1 = llfuse.listdir(os.path.join(self.mounttmp))
752 self.assertEqual([], sorted(d1))
754 with arvados.collection.Collection(collection.manifest_locator(), api_client=self.api) as collection2:
755 with collection2.open("file1.txt", "w") as f:
758 for attempt in AssertWithTimeout(10):
759 attempt(self.assertEqual, ["file1.txt"], llfuse.listdir(os.path.join(self.mounttmp)))
762 class FuseDeleteProjectEventTest(MountTestBase):
765 aproject = self.api.groups().create(body={
767 "group_class": "project"
770 bproject = self.api.groups().create(body={
772 "group_class": "project",
773 "owner_uuid": aproject["uuid"]
776 self.make_mount(fuse.ProjectDirectory,
777 project_object=self.api.users().current().execute())
779 self.operations.listen_for_events()
781 d1 = llfuse.listdir(os.path.join(self.mounttmp, "aproject"))
782 self.assertEqual(["bproject"], sorted(d1))
784 self.api.groups().delete(uuid=bproject["uuid"]).execute()
786 for attempt in AssertWithTimeout(10):
787 attempt(self.assertEqual, [], llfuse.listdir(os.path.join(self.mounttmp, "aproject")))
790 def fuseFileConflictTestHelper(mounttmp):
791 class Test(unittest.TestCase):
793 with open(os.path.join(mounttmp, "file1.txt"), "w") as f:
796 d1 = sorted(llfuse.listdir(os.path.join(mounttmp)))
797 self.assertEqual(len(d1), 2)
799 with open(os.path.join(mounttmp, "file1.txt"), "r") as f:
800 self.assertEqual(f.read(), "bar")
802 assertRegex(self, d1[1],
803 r'file1\.txt~\d\d\d\d\d\d\d\d-\d\d\d\d\d\d~conflict~')
805 with open(os.path.join(mounttmp, d1[1]), "r") as f:
806 self.assertEqual(f.read(), "foo")
810 class FuseFileConflictTest(MountTestBase):
812 collection = arvados.collection.Collection(api_client=self.api)
813 collection.save_new()
815 m = self.make_mount(fuse.CollectionDirectory)
817 m.new_collection(collection.api_response(), collection)
819 d1 = llfuse.listdir(os.path.join(self.mounttmp))
820 self.assertEqual([], sorted(d1))
822 with arvados.collection.Collection(collection.manifest_locator(), api_client=self.api) as collection2:
823 with collection2.open("file1.txt", "w") as f:
826 # See note in MountTestBase.setUp
827 self.pool.apply(fuseFileConflictTestHelper, (self.mounttmp,))
830 def fuseUnlinkOpenFileTest(mounttmp):
831 class Test(unittest.TestCase):
833 with open(os.path.join(mounttmp, "file1.txt"), "w+") as f:
836 d1 = llfuse.listdir(os.path.join(mounttmp))
837 self.assertEqual(["file1.txt"], sorted(d1))
839 os.remove(os.path.join(mounttmp, "file1.txt"))
841 d1 = llfuse.listdir(os.path.join(mounttmp))
842 self.assertEqual([], sorted(d1))
845 self.assertEqual(f.read(), "foo")
849 self.assertEqual(f.read(), "foobar")
853 class FuseUnlinkOpenFileTest(MountTestBase):
855 collection = arvados.collection.Collection(api_client=self.api)
856 collection.save_new()
858 m = self.make_mount(fuse.CollectionDirectory)
860 m.new_collection(collection.api_response(), collection)
862 # See note in MountTestBase.setUp
863 self.pool.apply(fuseUnlinkOpenFileTest, (self.mounttmp,))
865 self.assertEqual(collection.manifest_text(), "")
868 def fuseMvFileBetweenCollectionsTest1(mounttmp, uuid1, uuid2):
869 class Test(unittest.TestCase):
871 with open(os.path.join(mounttmp, uuid1, "file1.txt"), "w") as f:
872 f.write("Hello world!")
874 d1 = os.listdir(os.path.join(mounttmp, uuid1))
875 self.assertEqual(["file1.txt"], sorted(d1))
876 d1 = os.listdir(os.path.join(mounttmp, uuid2))
877 self.assertEqual([], sorted(d1))
881 def fuseMvFileBetweenCollectionsTest2(mounttmp, uuid1, uuid2):
882 class Test(unittest.TestCase):
884 os.rename(os.path.join(mounttmp, uuid1, "file1.txt"), os.path.join(mounttmp, uuid2, "file2.txt"))
886 d1 = os.listdir(os.path.join(mounttmp, uuid1))
887 self.assertEqual([], sorted(d1))
888 d1 = os.listdir(os.path.join(mounttmp, uuid2))
889 self.assertEqual(["file2.txt"], sorted(d1))
893 class FuseMvFileBetweenCollectionsTest(MountTestBase):
895 collection1 = arvados.collection.Collection(api_client=self.api)
896 collection1.save_new()
898 collection2 = arvados.collection.Collection(api_client=self.api)
899 collection2.save_new()
901 m = self.make_mount(fuse.MagicDirectory)
903 # See note in MountTestBase.setUp
904 self.pool.apply(fuseMvFileBetweenCollectionsTest1, (self.mounttmp,
905 collection1.manifest_locator(),
906 collection2.manifest_locator()))
911 assertRegex(self, collection1.manifest_text(), r"\. 86fb269d190d2c85f6e0468ceca42a20\+12\+A\S+ 0:12:file1\.txt$")
912 self.assertEqual(collection2.manifest_text(), "")
914 self.pool.apply(fuseMvFileBetweenCollectionsTest2, (self.mounttmp,
915 collection1.manifest_locator(),
916 collection2.manifest_locator()))
921 self.assertEqual(collection1.manifest_text(), "")
922 assertRegex(self, collection2.manifest_text(), r"\. 86fb269d190d2c85f6e0468ceca42a20\+12\+A\S+ 0:12:file2\.txt$")
924 collection1.stop_threads()
925 collection2.stop_threads()
928 def fuseMvDirBetweenCollectionsTest1(mounttmp, uuid1, uuid2):
929 class Test(unittest.TestCase):
931 os.mkdir(os.path.join(mounttmp, uuid1, "testdir"))
932 with open(os.path.join(mounttmp, uuid1, "testdir", "file1.txt"), "w") as f:
933 f.write("Hello world!")
935 d1 = os.listdir(os.path.join(mounttmp, uuid1))
936 self.assertEqual(["testdir"], sorted(d1))
937 d1 = os.listdir(os.path.join(mounttmp, uuid1, "testdir"))
938 self.assertEqual(["file1.txt"], sorted(d1))
940 d1 = os.listdir(os.path.join(mounttmp, uuid2))
941 self.assertEqual([], sorted(d1))
946 def fuseMvDirBetweenCollectionsTest2(mounttmp, uuid1, uuid2):
947 class Test(unittest.TestCase):
949 os.rename(os.path.join(mounttmp, uuid1, "testdir"), os.path.join(mounttmp, uuid2, "testdir2"))
951 d1 = os.listdir(os.path.join(mounttmp, uuid1))
952 self.assertEqual([], sorted(d1))
954 d1 = os.listdir(os.path.join(mounttmp, uuid2))
955 self.assertEqual(["testdir2"], sorted(d1))
956 d1 = os.listdir(os.path.join(mounttmp, uuid2, "testdir2"))
957 self.assertEqual(["file1.txt"], sorted(d1))
959 with open(os.path.join(mounttmp, uuid2, "testdir2", "file1.txt"), "r") as f:
960 self.assertEqual(f.read(), "Hello world!")
964 class FuseMvDirBetweenCollectionsTest(MountTestBase):
966 collection1 = arvados.collection.Collection(api_client=self.api)
967 collection1.save_new()
969 collection2 = arvados.collection.Collection(api_client=self.api)
970 collection2.save_new()
972 m = self.make_mount(fuse.MagicDirectory)
974 # See note in MountTestBase.setUp
975 self.pool.apply(fuseMvDirBetweenCollectionsTest1, (self.mounttmp,
976 collection1.manifest_locator(),
977 collection2.manifest_locator()))
982 assertRegex(self, collection1.manifest_text(), r"\./testdir 86fb269d190d2c85f6e0468ceca42a20\+12\+A\S+ 0:12:file1\.txt$")
983 self.assertEqual(collection2.manifest_text(), "")
985 self.pool.apply(fuseMvDirBetweenCollectionsTest2, (self.mounttmp,
986 collection1.manifest_locator(),
987 collection2.manifest_locator()))
992 self.assertEqual(collection1.manifest_text(), "")
993 assertRegex(self, collection2.manifest_text(), r"\./testdir2 86fb269d190d2c85f6e0468ceca42a20\+12\+A\S+ 0:12:file1\.txt$")
995 collection1.stop_threads()
996 collection2.stop_threads()
998 def fuseProjectMkdirTestHelper1(mounttmp):
999 class Test(unittest.TestCase):
1001 os.mkdir(os.path.join(mounttmp, "testcollection"))
1002 with self.assertRaises(OSError):
1003 os.mkdir(os.path.join(mounttmp, "testcollection"))
1006 def fuseProjectMkdirTestHelper2(mounttmp):
1007 class Test(unittest.TestCase):
1009 with open(os.path.join(mounttmp, "testcollection", "file1.txt"), "w") as f:
1010 f.write("Hello world!")
1011 with self.assertRaises(OSError):
1012 os.rmdir(os.path.join(mounttmp, "testcollection"))
1013 os.remove(os.path.join(mounttmp, "testcollection", "file1.txt"))
1014 with self.assertRaises(OSError):
1015 os.remove(os.path.join(mounttmp, "testcollection"))
1016 os.rmdir(os.path.join(mounttmp, "testcollection"))
1019 class FuseProjectMkdirRmdirTest(MountTestBase):
1021 self.make_mount(fuse.ProjectDirectory,
1022 project_object=self.api.users().current().execute())
1024 d1 = llfuse.listdir(self.mounttmp)
1025 self.assertNotIn('testcollection', d1)
1027 self.pool.apply(fuseProjectMkdirTestHelper1, (self.mounttmp,))
1029 d1 = llfuse.listdir(self.mounttmp)
1030 self.assertIn('testcollection', d1)
1032 self.pool.apply(fuseProjectMkdirTestHelper2, (self.mounttmp,))
1034 d1 = llfuse.listdir(self.mounttmp)
1035 self.assertNotIn('testcollection', d1)
1038 def fuseProjectMvTestHelper1(mounttmp):
1039 class Test(unittest.TestCase):
1041 d1 = llfuse.listdir(mounttmp)
1042 self.assertNotIn('testcollection', d1)
1044 os.mkdir(os.path.join(mounttmp, "testcollection"))
1046 d1 = llfuse.listdir(mounttmp)
1047 self.assertIn('testcollection', d1)
1049 with self.assertRaises(OSError):
1050 os.rename(os.path.join(mounttmp, "testcollection"), os.path.join(mounttmp, 'Unrestricted public data'))
1052 os.rename(os.path.join(mounttmp, "testcollection"), os.path.join(mounttmp, 'Unrestricted public data', 'testcollection'))
1054 d1 = llfuse.listdir(mounttmp)
1055 self.assertNotIn('testcollection', d1)
1057 d1 = llfuse.listdir(os.path.join(mounttmp, 'Unrestricted public data'))
1058 self.assertIn('testcollection', d1)
1062 class FuseProjectMvTest(MountTestBase):
1064 self.make_mount(fuse.ProjectDirectory,
1065 project_object=self.api.users().current().execute())
1067 self.pool.apply(fuseProjectMvTestHelper1, (self.mounttmp,))
1070 def fuseFsyncTestHelper(mounttmp, k):
1071 class Test(unittest.TestCase):
1073 fd = os.open(os.path.join(mounttmp, k), os.O_RDONLY)
1079 class FuseFsyncTest(FuseMagicTest):
1081 self.make_mount(fuse.MagicDirectory)
1082 self.pool.apply(fuseFsyncTestHelper, (self.mounttmp, self.testcollection))
1085 class MagicDirApiError(FuseMagicTest):
1087 api = mock.MagicMock()
1088 super(MagicDirApiError, self).setUp(api=api)
1089 api.collections().get().execute.side_effect = iter([
1090 Exception('API fail'),
1092 "manifest_text": self.test_manifest,
1093 "portable_data_hash": self.test_manifest_pdh,
1096 api.keep.get.side_effect = Exception('Keep fail')
1099 with mock.patch('arvados_fuse.fresh.FreshBase._poll_time', new_callable=mock.PropertyMock, return_value=60) as mock_poll_time:
1100 self.make_mount(fuse.MagicDirectory)
1102 self.operations.inodes.inode_cache.cap = 1
1103 self.operations.inodes.inode_cache.min_entries = 2
1105 with self.assertRaises(OSError):
1106 llfuse.listdir(os.path.join(self.mounttmp, self.testcollection))
1108 llfuse.listdir(os.path.join(self.mounttmp, self.testcollection))
1111 class SanitizeFilenameTest(MountTestBase):
1112 def test_sanitize_filename(self):
1113 pdir = fuse.ProjectDirectory(1, {}, self.api, 0, project_object=self.api.users().current().execute())
1132 for f in acceptable:
1133 self.assertEqual(f, pdir.sanitize_filename(f))
1134 for f in unacceptable:
1135 self.assertNotEqual(f, pdir.sanitize_filename(f))
1136 # The sanitized filename should be the same length, though.
1137 self.assertEqual(len(f), len(pdir.sanitize_filename(f)))
1139 self.assertEqual("_", pdir.sanitize_filename(""))
1140 self.assertEqual("_", pdir.sanitize_filename("."))
1141 self.assertEqual("__", pdir.sanitize_filename(".."))
1144 class FuseMagicTestPDHOnly(MountTestBase):
1145 def setUp(self, api=None):
1146 super(FuseMagicTestPDHOnly, self).setUp(api=api)
1148 cw = arvados.CollectionWriter()
1150 cw.start_new_file('thing1.txt')
1153 self.testcollection = cw.finish()
1154 self.test_manifest = cw.manifest_text()
1155 created = self.api.collections().create(body={"manifest_text":self.test_manifest}).execute()
1156 self.testcollectionuuid = str(created['uuid'])
1158 def verify_pdh_only(self, pdh_only=False, skip_pdh_only=False):
1159 if skip_pdh_only is True:
1160 self.make_mount(fuse.MagicDirectory) # in this case, the default by_id applies
1162 self.make_mount(fuse.MagicDirectory, pdh_only=pdh_only)
1164 mount_ls = llfuse.listdir(self.mounttmp)
1165 self.assertIn('README', mount_ls)
1166 self.assertFalse(any(arvados.util.keep_locator_pattern.match(fn) or
1167 arvados.util.uuid_pattern.match(fn)
1168 for fn in mount_ls),
1169 "new FUSE MagicDirectory lists Collection")
1171 # look up using pdh should succeed in all cases
1172 self.assertDirContents(self.testcollection, ['thing1.txt'])
1173 self.assertDirContents(os.path.join('by_id', self.testcollection),
1175 mount_ls = llfuse.listdir(self.mounttmp)
1176 self.assertIn('README', mount_ls)
1177 self.assertIn(self.testcollection, mount_ls)
1178 self.assertIn(self.testcollection,
1179 llfuse.listdir(os.path.join(self.mounttmp, 'by_id')))
1182 files[os.path.join(self.mounttmp, self.testcollection, 'thing1.txt')] = 'data 1'
1184 for k, v in viewitems(files):
1185 with open(os.path.join(self.mounttmp, k), 'rb') as f:
1186 self.assertEqual(v, f.read().decode())
1188 # look up using uuid should fail when pdh_only is set
1189 if pdh_only is True:
1190 with self.assertRaises(OSError):
1191 self.assertDirContents(os.path.join('by_id', self.testcollectionuuid),
1194 self.assertDirContents(os.path.join('by_id', self.testcollectionuuid),
1197 def test_with_pdh_only_true(self):
1198 self.verify_pdh_only(pdh_only=True)
1200 def test_with_pdh_only_false(self):
1201 self.verify_pdh_only(pdh_only=False)
1203 def test_with_default_by_id(self):
1204 self.verify_pdh_only(skip_pdh_only=True)
1207 class SlashSubstitutionTest(IntegrationTest):
1210 '--mount-home', 'zzz',
1214 super(SlashSubstitutionTest, self).setUp()
1215 self.api = arvados.safeapi.ThreadSafeApiCache(arvados.config.settings())
1216 self.api.config = lambda: {"Collections": {"ForwardSlashNameSubstitution": "[SLASH]"}}
1217 self.testcoll = self.api.collections().create(body={"name": "foo/bar/baz"}).execute()
1218 self.testcolleasy = self.api.collections().create(body={"name": "foo-bar-baz"}).execute()
1219 self.fusename = 'foo[SLASH]bar[SLASH]baz'
1221 @IntegrationTest.mount(argv=mnt_args)
1222 @mock.patch('arvados.util.get_config_once')
1223 def test_slash_substitution_before_listing(self, get_config_once):
1224 get_config_once.return_value = {"Collections": {"ForwardSlashNameSubstitution": "[SLASH]"}}
1225 self.pool_test(os.path.join(self.mnt, 'zzz'), self.fusename)
1226 self.checkContents()
1228 def _test_slash_substitution_before_listing(self, tmpdir, fusename):
1229 with open(os.path.join(tmpdir, 'foo-bar-baz', 'waz'), 'w') as f:
1231 with open(os.path.join(tmpdir, fusename, 'waz'), 'w') as f:
1234 @IntegrationTest.mount(argv=mnt_args)
1235 @mock.patch('arvados.util.get_config_once')
1236 def test_slash_substitution_after_listing(self, get_config_once):
1237 get_config_once.return_value = {"Collections": {"ForwardSlashNameSubstitution": "[SLASH]"}}
1238 self.pool_test(os.path.join(self.mnt, 'zzz'), self.fusename)
1239 self.checkContents()
1241 def _test_slash_substitution_after_listing(self, tmpdir, fusename):
1242 with open(os.path.join(tmpdir, 'foo-bar-baz', 'waz'), 'w') as f:
1245 with open(os.path.join(tmpdir, fusename, 'waz'), 'w') as f:
1248 def checkContents(self):
1249 self.assertRegexpMatches(self.api.collections().get(uuid=self.testcoll['uuid']).execute()['manifest_text'], ' acbd18db') # md5(foo)
1250 self.assertRegexpMatches(self.api.collections().get(uuid=self.testcolleasy['uuid']).execute()['manifest_text'], ' f561aaf6') # md5(xxx)
1252 @IntegrationTest.mount(argv=mnt_args)
1253 @mock.patch('arvados.util.get_config_once')
1254 def test_slash_substitution_conflict(self, get_config_once):
1255 self.testcollconflict = self.api.collections().create(body={"name": self.fusename}).execute()
1256 get_config_once.return_value = {"Collections": {"ForwardSlashNameSubstitution": "[SLASH]"}}
1257 self.pool_test(os.path.join(self.mnt, 'zzz'), self.fusename)
1258 self.assertRegexpMatches(self.api.collections().get(uuid=self.testcollconflict['uuid']).execute()['manifest_text'], ' acbd18db') # md5(foo)
1259 # foo/bar/baz collection unchanged, because it is masked by foo[SLASH]bar[SLASH]baz
1260 self.assertEqual(self.api.collections().get(uuid=self.testcoll['uuid']).execute()['manifest_text'], '')
1262 def _test_slash_substitution_conflict(self, tmpdir, fusename):
1263 with open(os.path.join(tmpdir, fusename, 'waz'), 'w') as f: