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 .mount_test_base import MountTestBase
25 logger = logging.getLogger('arvados.arv-mount')
28 class AssertWithTimeout(object):
29 """Allow some time for an assertion to pass."""
31 def __init__(self, timeout=0):
32 self.timeout = timeout
35 self.deadline = time.time() + self.timeout
44 def attempt(self, fn, *args, **kwargs):
47 except AssertionError:
48 if time.time() > self.deadline:
55 class FuseMountTest(MountTestBase):
57 super(FuseMountTest, self).setUp()
59 cw = arvados.CollectionWriter()
61 cw.start_new_file('thing1.txt')
63 cw.start_new_file('thing2.txt')
66 cw.start_new_stream('dir1')
67 cw.start_new_file('thing3.txt')
69 cw.start_new_file('thing4.txt')
72 cw.start_new_stream('dir2')
73 cw.start_new_file('thing5.txt')
75 cw.start_new_file('thing6.txt')
78 cw.start_new_stream('dir2/dir3')
79 cw.start_new_file('thing7.txt')
82 cw.start_new_file('thing8.txt')
85 cw.start_new_stream('edgecases')
86 for f in ":/.../-/*/ ".split("/"):
90 for f in ":/.../-/*/ ".split("/"):
91 cw.start_new_stream('edgecases/dirs/' + f)
92 cw.start_new_file('x/x')
95 self.testcollection = cw.finish()
96 self.api.collections().create(body={"manifest_text":cw.manifest_text()}).execute()
99 self.make_mount(fuse.CollectionDirectory, collection_record=self.testcollection)
101 self.assertDirContents(None, ['thing1.txt', 'thing2.txt',
102 'edgecases', 'dir1', 'dir2'])
103 self.assertDirContents('dir1', ['thing3.txt', 'thing4.txt'])
104 self.assertDirContents('dir2', ['thing5.txt', 'thing6.txt', 'dir3'])
105 self.assertDirContents('dir2/dir3', ['thing7.txt', 'thing8.txt'])
106 self.assertDirContents('edgecases',
107 "dirs/:/.../-/*/ ".split("/"))
108 self.assertDirContents('edgecases/dirs',
109 ":/.../-/*/ ".split("/"))
111 files = {'thing1.txt': 'data 1',
112 'thing2.txt': 'data 2',
113 'dir1/thing3.txt': 'data 3',
114 'dir1/thing4.txt': 'data 4',
115 'dir2/thing5.txt': 'data 5',
116 'dir2/thing6.txt': 'data 6',
117 'dir2/dir3/thing7.txt': 'data 7',
118 'dir2/dir3/thing8.txt': 'data 8'}
120 for k, v in viewitems(files):
121 with open(os.path.join(self.mounttmp, k), 'rb') as f:
122 self.assertEqual(v, f.read().decode())
125 class FuseMagicTest(MountTestBase):
126 def setUp(self, api=None):
127 super(FuseMagicTest, self).setUp(api=api)
129 self.test_project = run_test_server.fixture('groups')['aproject']['uuid']
130 self.non_project_group = run_test_server.fixture('groups')['public']['uuid']
131 self.collection_in_test_project = run_test_server.fixture('collections')['foo_collection_in_aproject']['name']
133 cw = arvados.CollectionWriter()
135 cw.start_new_file('thing1.txt')
138 self.testcollection = cw.finish()
139 self.test_manifest = cw.manifest_text()
140 coll = self.api.collections().create(body={"manifest_text":self.test_manifest}).execute()
141 self.test_manifest_pdh = coll['portable_data_hash']
144 self.make_mount(fuse.MagicDirectory)
146 mount_ls = llfuse.listdir(self.mounttmp)
147 self.assertIn('README', mount_ls)
148 self.assertFalse(any(arvados.util.keep_locator_pattern.match(fn) or
149 arvados.util.uuid_pattern.match(fn)
151 "new FUSE MagicDirectory has no collections or projects")
152 self.assertDirContents(self.testcollection, ['thing1.txt'])
153 self.assertDirContents(os.path.join('by_id', self.testcollection),
155 self.assertIn(self.collection_in_test_project,
156 llfuse.listdir(os.path.join(self.mounttmp, self.test_project)))
157 self.assertIn(self.collection_in_test_project,
158 llfuse.listdir(os.path.join(self.mounttmp, 'by_id', self.test_project)))
160 mount_ls = llfuse.listdir(self.mounttmp)
161 self.assertIn('README', mount_ls)
162 self.assertIn(self.testcollection, mount_ls)
163 self.assertIn(self.testcollection,
164 llfuse.listdir(os.path.join(self.mounttmp, 'by_id')))
165 self.assertIn(self.test_project, mount_ls)
166 self.assertIn(self.test_project,
167 llfuse.listdir(os.path.join(self.mounttmp, 'by_id')))
169 with self.assertRaises(OSError):
170 llfuse.listdir(os.path.join(self.mounttmp, 'by_id', self.non_project_group))
173 files[os.path.join(self.mounttmp, self.testcollection, 'thing1.txt')] = 'data 1'
175 for k, v in viewitems(files):
176 with open(os.path.join(self.mounttmp, k), 'rb') as f:
177 self.assertEqual(v, f.read().decode())
180 class FuseTagsTest(MountTestBase):
182 self.make_mount(fuse.TagsDirectory)
184 d1 = llfuse.listdir(self.mounttmp)
186 self.assertEqual(['foo_tag'], d1)
188 d2 = llfuse.listdir(os.path.join(self.mounttmp, 'foo_tag'))
190 self.assertEqual(['zzzzz-4zz18-fy296fx3hot09f7'], d2)
192 d3 = llfuse.listdir(os.path.join(self.mounttmp, 'foo_tag', 'zzzzz-4zz18-fy296fx3hot09f7'))
194 self.assertEqual(['foo'], d3)
197 class FuseTagsUpdateTest(MountTestBase):
198 def tag_collection(self, coll_uuid, tag_name):
199 return self.api.links().create(
200 body={'link': {'head_uuid': coll_uuid,
206 self.make_mount(fuse.TagsDirectory, poll_time=1)
208 self.assertIn('foo_tag', llfuse.listdir(self.mounttmp))
210 bar_uuid = run_test_server.fixture('collections')['bar_file']['uuid']
211 self.tag_collection(bar_uuid, 'fuse_test_tag')
212 for attempt in AssertWithTimeout(10):
213 attempt(self.assertIn, 'fuse_test_tag', llfuse.listdir(self.mounttmp))
214 self.assertDirContents('fuse_test_tag', [bar_uuid])
216 baz_uuid = run_test_server.fixture('collections')['baz_file']['uuid']
217 l = self.tag_collection(baz_uuid, 'fuse_test_tag')
218 for attempt in AssertWithTimeout(10):
219 attempt(self.assertDirContents, 'fuse_test_tag', [bar_uuid, baz_uuid])
221 self.api.links().delete(uuid=l['uuid']).execute()
222 for attempt in AssertWithTimeout(10):
223 attempt(self.assertDirContents, 'fuse_test_tag', [bar_uuid])
226 def fuseSharedTestHelper(mounttmp):
227 class Test(unittest.TestCase):
229 # Double check that we can open and read objects in this folder as a file,
230 # and that its contents are what we expect.
231 baz_path = os.path.join(
235 'collection in FUSE project',
237 with open(baz_path) as f:
238 self.assertEqual("baz", f.read())
240 # check mtime on collection
241 st = os.stat(baz_path)
243 mtime = st.st_mtime_ns // 1000000000
244 except AttributeError:
246 self.assertEqual(mtime, 1391448174)
248 # shared_dirs is a list of the directories exposed
249 # by fuse.SharedDirectory (i.e. any object visible
250 # to the current user)
251 shared_dirs = llfuse.listdir(mounttmp)
253 self.assertIn('FUSE User', shared_dirs)
255 # fuse_user_objs is a list of the objects owned by the FUSE
256 # test user (which present as files in the 'FUSE User'
258 fuse_user_objs = llfuse.listdir(os.path.join(mounttmp, 'FUSE User'))
259 fuse_user_objs.sort()
260 self.assertEqual(['FUSE Test Project', # project owned by user
261 'collection #1 owned by FUSE', # collection owned by user
262 'collection #2 owned by FUSE' # collection owned by user
265 # test_proj_files is a list of the files in the FUSE Test Project.
266 test_proj_files = llfuse.listdir(os.path.join(mounttmp, 'FUSE User', 'FUSE Test Project'))
267 test_proj_files.sort()
268 self.assertEqual(['collection in FUSE project'
274 class FuseSharedTest(MountTestBase):
276 self.make_mount(fuse.SharedDirectory,
277 exclude=self.api.users().current().execute()['uuid'])
278 keep = arvados.keep.KeepClient()
279 keep.put("baz".encode())
281 self.pool.apply(fuseSharedTestHelper, (self.mounttmp,))
284 class FuseHomeTest(MountTestBase):
286 self.make_mount(fuse.ProjectDirectory,
287 project_object=self.api.users().current().execute())
289 d1 = llfuse.listdir(self.mounttmp)
290 self.assertIn('Unrestricted public data', d1)
292 d2 = llfuse.listdir(os.path.join(self.mounttmp, 'Unrestricted public data'))
293 public_project = run_test_server.fixture('groups')[
294 'anonymously_accessible_project']
297 for name, item in viewitems(run_test_server.fixture('collections')):
298 if 'name' not in item:
300 elif item['owner_uuid'] == public_project['uuid']:
301 self.assertIn(item['name'], d2)
304 # Artificial assumption here: there is no public
305 # collection fixture with the same name as a
306 # non-public collection.
307 self.assertNotIn(item['name'], d2)
309 self.assertNotEqual(0, found_in)
310 self.assertNotEqual(0, found_not_in)
312 d3 = llfuse.listdir(os.path.join(self.mounttmp, 'Unrestricted public data', 'GNU General Public License, version 3'))
313 self.assertEqual(["GNU_General_Public_License,_version_3.pdf"], d3)
316 def fuseModifyFileTestHelperReadStartContents(mounttmp):
317 class Test(unittest.TestCase):
319 d1 = llfuse.listdir(mounttmp)
320 self.assertEqual(["file1.txt"], d1)
321 with open(os.path.join(mounttmp, "file1.txt")) as f:
322 self.assertEqual("blub", f.read())
325 def fuseModifyFileTestHelperReadEndContents(mounttmp):
326 class Test(unittest.TestCase):
328 d1 = llfuse.listdir(mounttmp)
329 self.assertEqual(["file1.txt"], d1)
330 with open(os.path.join(mounttmp, "file1.txt")) as f:
331 self.assertEqual("plnp", f.read())
334 class FuseModifyFileTest(MountTestBase):
336 collection = arvados.collection.Collection(api_client=self.api)
337 with collection.open("file1.txt", "w") as f:
340 collection.save_new()
342 m = self.make_mount(fuse.CollectionDirectory)
344 m.new_collection(collection.api_response(), collection)
346 self.pool.apply(fuseModifyFileTestHelperReadStartContents, (self.mounttmp,))
348 with collection.open("file1.txt", "w") as f:
351 self.pool.apply(fuseModifyFileTestHelperReadEndContents, (self.mounttmp,))
354 class FuseAddFileToCollectionTest(MountTestBase):
356 collection = arvados.collection.Collection(api_client=self.api)
357 with collection.open("file1.txt", "w") as f:
360 collection.save_new()
362 m = self.make_mount(fuse.CollectionDirectory)
364 m.new_collection(collection.api_response(), collection)
366 d1 = llfuse.listdir(self.mounttmp)
367 self.assertEqual(["file1.txt"], d1)
369 with collection.open("file2.txt", "w") as f:
372 d1 = llfuse.listdir(self.mounttmp)
373 self.assertEqual(["file1.txt", "file2.txt"], sorted(d1))
376 class FuseRemoveFileFromCollectionTest(MountTestBase):
378 collection = arvados.collection.Collection(api_client=self.api)
379 with collection.open("file1.txt", "w") as f:
382 with collection.open("file2.txt", "w") as f:
385 collection.save_new()
387 m = self.make_mount(fuse.CollectionDirectory)
389 m.new_collection(collection.api_response(), collection)
391 d1 = llfuse.listdir(self.mounttmp)
392 self.assertEqual(["file1.txt", "file2.txt"], sorted(d1))
394 collection.remove("file2.txt")
396 d1 = llfuse.listdir(self.mounttmp)
397 self.assertEqual(["file1.txt"], d1)
400 def fuseCreateFileTestHelper(mounttmp):
401 class Test(unittest.TestCase):
403 with open(os.path.join(mounttmp, "file1.txt"), "w") as f:
407 class FuseCreateFileTest(MountTestBase):
409 collection = arvados.collection.Collection(api_client=self.api)
410 collection.save_new()
412 collection2 = self.api.collections().get(uuid=collection.manifest_locator()).execute()
413 self.assertEqual(collection2["manifest_text"], "")
415 collection.save_new()
417 m = self.make_mount(fuse.CollectionDirectory)
419 m.new_collection(collection.api_response(), collection)
420 self.assertTrue(m.writable())
422 self.assertNotIn("file1.txt", collection)
424 self.pool.apply(fuseCreateFileTestHelper, (self.mounttmp,))
426 self.assertIn("file1.txt", collection)
428 d1 = llfuse.listdir(self.mounttmp)
429 self.assertEqual(["file1.txt"], d1)
431 collection2 = self.api.collections().get(uuid=collection.manifest_locator()).execute()
432 assertRegex(self, collection2["manifest_text"],
433 r'\. d41d8cd98f00b204e9800998ecf8427e\+0\+A\S+ 0:0:file1\.txt$')
436 def fuseWriteFileTestHelperWriteFile(mounttmp):
437 class Test(unittest.TestCase):
439 with open(os.path.join(mounttmp, "file1.txt"), "w") as f:
440 f.write("Hello world!")
443 def fuseWriteFileTestHelperReadFile(mounttmp):
444 class Test(unittest.TestCase):
446 with open(os.path.join(mounttmp, "file1.txt"), "r") as f:
447 self.assertEqual(f.read(), "Hello world!")
450 class FuseWriteFileTest(MountTestBase):
452 collection = arvados.collection.Collection(api_client=self.api)
453 collection.save_new()
455 m = self.make_mount(fuse.CollectionDirectory)
457 m.new_collection(collection.api_response(), collection)
458 self.assertTrue(m.writable())
460 self.assertNotIn("file1.txt", collection)
462 self.assertEqual(0, self.operations.write_counter.get())
463 self.pool.apply(fuseWriteFileTestHelperWriteFile, (self.mounttmp,))
464 self.assertEqual(12, self.operations.write_counter.get())
466 with collection.open("file1.txt") as f:
467 self.assertEqual(f.read(), "Hello world!")
469 self.assertEqual(0, self.operations.read_counter.get())
470 self.pool.apply(fuseWriteFileTestHelperReadFile, (self.mounttmp,))
471 self.assertEqual(12, self.operations.read_counter.get())
473 collection2 = self.api.collections().get(uuid=collection.manifest_locator()).execute()
474 assertRegex(self, collection2["manifest_text"],
475 r'\. 86fb269d190d2c85f6e0468ceca42a20\+12\+A\S+ 0:12:file1\.txt$')
478 def fuseUpdateFileTestHelper(mounttmp):
479 class Test(unittest.TestCase):
481 with open(os.path.join(mounttmp, "file1.txt"), "w") as f:
482 f.write("Hello world!")
484 with open(os.path.join(mounttmp, "file1.txt"), "r+") as f:
486 self.assertEqual(fr, "Hello world!")
488 f.write("Hola mundo!")
491 self.assertEqual(fr, "Hola mundo!!")
493 with open(os.path.join(mounttmp, "file1.txt"), "r") as f:
494 self.assertEqual(f.read(), "Hola mundo!!")
498 class FuseUpdateFileTest(MountTestBase):
500 collection = arvados.collection.Collection(api_client=self.api)
501 collection.save_new()
503 m = self.make_mount(fuse.CollectionDirectory)
505 m.new_collection(collection.api_response(), collection)
506 self.assertTrue(m.writable())
508 # See note in MountTestBase.setUp
509 self.pool.apply(fuseUpdateFileTestHelper, (self.mounttmp,))
511 collection2 = self.api.collections().get(uuid=collection.manifest_locator()).execute()
512 assertRegex(self, collection2["manifest_text"],
513 r'\. daaef200ebb921e011e3ae922dd3266b\+11\+A\S+ 86fb269d190d2c85f6e0468ceca42a20\+12\+A\S+ 0:11:file1\.txt 22:1:file1\.txt$')
516 def fuseMkdirTestHelper(mounttmp):
517 class Test(unittest.TestCase):
519 with self.assertRaises(IOError):
520 with open(os.path.join(mounttmp, "testdir", "file1.txt"), "w") as f:
521 f.write("Hello world!")
523 os.mkdir(os.path.join(mounttmp, "testdir"))
525 with self.assertRaises(OSError):
526 os.mkdir(os.path.join(mounttmp, "testdir"))
528 d1 = llfuse.listdir(mounttmp)
529 self.assertEqual(["testdir"], d1)
531 with open(os.path.join(mounttmp, "testdir", "file1.txt"), "w") as f:
532 f.write("Hello world!")
534 d1 = llfuse.listdir(os.path.join(mounttmp, "testdir"))
535 self.assertEqual(["file1.txt"], d1)
539 class FuseMkdirTest(MountTestBase):
541 collection = arvados.collection.Collection(api_client=self.api)
542 collection.save_new()
544 m = self.make_mount(fuse.CollectionDirectory)
546 m.new_collection(collection.api_response(), collection)
547 self.assertTrue(m.writable())
549 self.pool.apply(fuseMkdirTestHelper, (self.mounttmp,))
551 collection2 = self.api.collections().get(uuid=collection.manifest_locator()).execute()
552 assertRegex(self, collection2["manifest_text"],
553 r'\./testdir 86fb269d190d2c85f6e0468ceca42a20\+12\+A\S+ 0:12:file1\.txt$')
556 def fuseRmTestHelperWriteFile(mounttmp):
557 class Test(unittest.TestCase):
559 os.mkdir(os.path.join(mounttmp, "testdir"))
561 with open(os.path.join(mounttmp, "testdir", "file1.txt"), "w") as f:
562 f.write("Hello world!")
566 def fuseRmTestHelperDeleteFile(mounttmp):
567 class Test(unittest.TestCase):
569 # Can't delete because it's not empty
570 with self.assertRaises(OSError):
571 os.rmdir(os.path.join(mounttmp, "testdir"))
573 d1 = llfuse.listdir(os.path.join(mounttmp, "testdir"))
574 self.assertEqual(["file1.txt"], d1)
577 os.remove(os.path.join(mounttmp, "testdir", "file1.txt"))
579 # Make sure it's empty
580 d1 = llfuse.listdir(os.path.join(mounttmp, "testdir"))
581 self.assertEqual([], d1)
583 # Try to delete it again
584 with self.assertRaises(OSError):
585 os.remove(os.path.join(mounttmp, "testdir", "file1.txt"))
589 def fuseRmTestHelperRmdir(mounttmp):
590 class Test(unittest.TestCase):
592 # Should be able to delete now that it is empty
593 os.rmdir(os.path.join(mounttmp, "testdir"))
595 # Make sure it's empty
596 d1 = llfuse.listdir(os.path.join(mounttmp))
597 self.assertEqual([], d1)
599 # Try to delete it again
600 with self.assertRaises(OSError):
601 os.rmdir(os.path.join(mounttmp, "testdir"))
605 class FuseRmTest(MountTestBase):
607 collection = arvados.collection.Collection(api_client=self.api)
608 collection.save_new()
610 m = self.make_mount(fuse.CollectionDirectory)
612 m.new_collection(collection.api_response(), collection)
613 self.assertTrue(m.writable())
615 self.pool.apply(fuseRmTestHelperWriteFile, (self.mounttmp,))
618 collection2 = self.api.collections().get(uuid=collection.manifest_locator()).execute()
619 assertRegex(self, collection2["manifest_text"],
620 r'\./testdir 86fb269d190d2c85f6e0468ceca42a20\+12\+A\S+ 0:12:file1\.txt$')
621 self.pool.apply(fuseRmTestHelperDeleteFile, (self.mounttmp,))
623 # Empty directories are represented by an empty file named "."
624 collection2 = self.api.collections().get(uuid=collection.manifest_locator()).execute()
625 assertRegex(self, collection2["manifest_text"],
626 r'./testdir d41d8cd98f00b204e9800998ecf8427e\+0\+A\S+ 0:0:\\056\n')
628 self.pool.apply(fuseRmTestHelperRmdir, (self.mounttmp,))
630 # manifest should be empty now.
631 collection2 = self.api.collections().get(uuid=collection.manifest_locator()).execute()
632 self.assertEqual(collection2["manifest_text"], "")
635 def fuseMvFileTestHelperWriteFile(mounttmp):
636 class Test(unittest.TestCase):
638 os.mkdir(os.path.join(mounttmp, "testdir"))
640 with open(os.path.join(mounttmp, "testdir", "file1.txt"), "w") as f:
641 f.write("Hello world!")
645 def fuseMvFileTestHelperMoveFile(mounttmp):
646 class Test(unittest.TestCase):
648 d1 = llfuse.listdir(os.path.join(mounttmp))
649 self.assertEqual(["testdir"], d1)
650 d1 = llfuse.listdir(os.path.join(mounttmp, "testdir"))
651 self.assertEqual(["file1.txt"], d1)
653 os.rename(os.path.join(mounttmp, "testdir", "file1.txt"), os.path.join(mounttmp, "file1.txt"))
655 d1 = llfuse.listdir(os.path.join(mounttmp))
656 self.assertEqual(["file1.txt", "testdir"], sorted(d1))
657 d1 = llfuse.listdir(os.path.join(mounttmp, "testdir"))
658 self.assertEqual([], d1)
662 class FuseMvFileTest(MountTestBase):
664 collection = arvados.collection.Collection(api_client=self.api)
665 collection.save_new()
667 m = self.make_mount(fuse.CollectionDirectory)
669 m.new_collection(collection.api_response(), collection)
670 self.assertTrue(m.writable())
672 self.pool.apply(fuseMvFileTestHelperWriteFile, (self.mounttmp,))
675 collection2 = self.api.collections().get(uuid=collection.manifest_locator()).execute()
676 assertRegex(self, collection2["manifest_text"],
677 r'\./testdir 86fb269d190d2c85f6e0468ceca42a20\+12\+A\S+ 0:12:file1\.txt$')
679 self.pool.apply(fuseMvFileTestHelperMoveFile, (self.mounttmp,))
681 collection2 = self.api.collections().get(uuid=collection.manifest_locator()).execute()
682 assertRegex(self, collection2["manifest_text"],
683 r'\. 86fb269d190d2c85f6e0468ceca42a20\+12\+A\S+ 0:12:file1\.txt\n\./testdir d41d8cd98f00b204e9800998ecf8427e\+0\+A\S+ 0:0:\\056\n')
686 def fuseRenameTestHelper(mounttmp):
687 class Test(unittest.TestCase):
689 os.mkdir(os.path.join(mounttmp, "testdir"))
691 with open(os.path.join(mounttmp, "testdir", "file1.txt"), "w") as f:
692 f.write("Hello world!")
696 class FuseRenameTest(MountTestBase):
698 collection = arvados.collection.Collection(api_client=self.api)
699 collection.save_new()
701 m = self.make_mount(fuse.CollectionDirectory)
703 m.new_collection(collection.api_response(), collection)
704 self.assertTrue(m.writable())
706 self.pool.apply(fuseRenameTestHelper, (self.mounttmp,))
709 collection2 = self.api.collections().get(uuid=collection.manifest_locator()).execute()
710 assertRegex(self, collection2["manifest_text"],
711 r'\./testdir 86fb269d190d2c85f6e0468ceca42a20\+12\+A\S+ 0:12:file1\.txt$')
713 d1 = llfuse.listdir(os.path.join(self.mounttmp))
714 self.assertEqual(["testdir"], d1)
715 d1 = llfuse.listdir(os.path.join(self.mounttmp, "testdir"))
716 self.assertEqual(["file1.txt"], d1)
718 os.rename(os.path.join(self.mounttmp, "testdir"), os.path.join(self.mounttmp, "testdir2"))
720 d1 = llfuse.listdir(os.path.join(self.mounttmp))
721 self.assertEqual(["testdir2"], sorted(d1))
722 d1 = llfuse.listdir(os.path.join(self.mounttmp, "testdir2"))
723 self.assertEqual(["file1.txt"], d1)
725 collection2 = self.api.collections().get(uuid=collection.manifest_locator()).execute()
726 assertRegex(self, collection2["manifest_text"],
727 r'\./testdir2 86fb269d190d2c85f6e0468ceca42a20\+12\+A\S+ 0:12:file1\.txt$')
730 class FuseUpdateFromEventTest(MountTestBase):
732 collection = arvados.collection.Collection(api_client=self.api)
733 collection.save_new()
735 m = self.make_mount(fuse.CollectionDirectory)
737 m.new_collection(collection.api_response(), collection)
739 self.operations.listen_for_events()
741 d1 = llfuse.listdir(os.path.join(self.mounttmp))
742 self.assertEqual([], sorted(d1))
744 with arvados.collection.Collection(collection.manifest_locator(), api_client=self.api) as collection2:
745 with collection2.open("file1.txt", "w") as f:
748 for attempt in AssertWithTimeout(10):
749 attempt(self.assertEqual, ["file1.txt"], llfuse.listdir(os.path.join(self.mounttmp)))
752 class FuseDeleteProjectEventTest(MountTestBase):
755 aproject = self.api.groups().create(body={
757 "group_class": "project"
760 bproject = self.api.groups().create(body={
762 "group_class": "project",
763 "owner_uuid": aproject["uuid"]
766 self.make_mount(fuse.ProjectDirectory,
767 project_object=self.api.users().current().execute())
769 self.operations.listen_for_events()
771 d1 = llfuse.listdir(os.path.join(self.mounttmp, "aproject"))
772 self.assertEqual(["bproject"], sorted(d1))
774 self.api.groups().delete(uuid=bproject["uuid"]).execute()
776 for attempt in AssertWithTimeout(10):
777 attempt(self.assertEqual, [], llfuse.listdir(os.path.join(self.mounttmp, "aproject")))
780 def fuseFileConflictTestHelper(mounttmp):
781 class Test(unittest.TestCase):
783 with open(os.path.join(mounttmp, "file1.txt"), "w") as f:
786 d1 = sorted(llfuse.listdir(os.path.join(mounttmp)))
787 self.assertEqual(len(d1), 2)
789 with open(os.path.join(mounttmp, "file1.txt"), "r") as f:
790 self.assertEqual(f.read(), "bar")
792 assertRegex(self, d1[1],
793 r'file1\.txt~\d\d\d\d\d\d\d\d-\d\d\d\d\d\d~conflict~')
795 with open(os.path.join(mounttmp, d1[1]), "r") as f:
796 self.assertEqual(f.read(), "foo")
800 class FuseFileConflictTest(MountTestBase):
802 collection = arvados.collection.Collection(api_client=self.api)
803 collection.save_new()
805 m = self.make_mount(fuse.CollectionDirectory)
807 m.new_collection(collection.api_response(), collection)
809 d1 = llfuse.listdir(os.path.join(self.mounttmp))
810 self.assertEqual([], sorted(d1))
812 with arvados.collection.Collection(collection.manifest_locator(), api_client=self.api) as collection2:
813 with collection2.open("file1.txt", "w") as f:
816 # See note in MountTestBase.setUp
817 self.pool.apply(fuseFileConflictTestHelper, (self.mounttmp,))
820 def fuseUnlinkOpenFileTest(mounttmp):
821 class Test(unittest.TestCase):
823 with open(os.path.join(mounttmp, "file1.txt"), "w+") as f:
826 d1 = llfuse.listdir(os.path.join(mounttmp))
827 self.assertEqual(["file1.txt"], sorted(d1))
829 os.remove(os.path.join(mounttmp, "file1.txt"))
831 d1 = llfuse.listdir(os.path.join(mounttmp))
832 self.assertEqual([], sorted(d1))
835 self.assertEqual(f.read(), "foo")
839 self.assertEqual(f.read(), "foobar")
843 class FuseUnlinkOpenFileTest(MountTestBase):
845 collection = arvados.collection.Collection(api_client=self.api)
846 collection.save_new()
848 m = self.make_mount(fuse.CollectionDirectory)
850 m.new_collection(collection.api_response(), collection)
852 # See note in MountTestBase.setUp
853 self.pool.apply(fuseUnlinkOpenFileTest, (self.mounttmp,))
855 self.assertEqual(collection.manifest_text(), "")
858 def fuseMvFileBetweenCollectionsTest1(mounttmp, uuid1, uuid2):
859 class Test(unittest.TestCase):
861 with open(os.path.join(mounttmp, uuid1, "file1.txt"), "w") as f:
862 f.write("Hello world!")
864 d1 = os.listdir(os.path.join(mounttmp, uuid1))
865 self.assertEqual(["file1.txt"], sorted(d1))
866 d1 = os.listdir(os.path.join(mounttmp, uuid2))
867 self.assertEqual([], sorted(d1))
871 def fuseMvFileBetweenCollectionsTest2(mounttmp, uuid1, uuid2):
872 class Test(unittest.TestCase):
874 os.rename(os.path.join(mounttmp, uuid1, "file1.txt"), os.path.join(mounttmp, uuid2, "file2.txt"))
876 d1 = os.listdir(os.path.join(mounttmp, uuid1))
877 self.assertEqual([], sorted(d1))
878 d1 = os.listdir(os.path.join(mounttmp, uuid2))
879 self.assertEqual(["file2.txt"], sorted(d1))
883 class FuseMvFileBetweenCollectionsTest(MountTestBase):
885 collection1 = arvados.collection.Collection(api_client=self.api)
886 collection1.save_new()
888 collection2 = arvados.collection.Collection(api_client=self.api)
889 collection2.save_new()
891 m = self.make_mount(fuse.MagicDirectory)
893 # See note in MountTestBase.setUp
894 self.pool.apply(fuseMvFileBetweenCollectionsTest1, (self.mounttmp,
895 collection1.manifest_locator(),
896 collection2.manifest_locator()))
901 assertRegex(self, collection1.manifest_text(), r"\. 86fb269d190d2c85f6e0468ceca42a20\+12\+A\S+ 0:12:file1\.txt$")
902 self.assertEqual(collection2.manifest_text(), "")
904 self.pool.apply(fuseMvFileBetweenCollectionsTest2, (self.mounttmp,
905 collection1.manifest_locator(),
906 collection2.manifest_locator()))
911 self.assertEqual(collection1.manifest_text(), "")
912 assertRegex(self, collection2.manifest_text(), r"\. 86fb269d190d2c85f6e0468ceca42a20\+12\+A\S+ 0:12:file2\.txt$")
914 collection1.stop_threads()
915 collection2.stop_threads()
918 def fuseMvDirBetweenCollectionsTest1(mounttmp, uuid1, uuid2):
919 class Test(unittest.TestCase):
921 os.mkdir(os.path.join(mounttmp, uuid1, "testdir"))
922 with open(os.path.join(mounttmp, uuid1, "testdir", "file1.txt"), "w") as f:
923 f.write("Hello world!")
925 d1 = os.listdir(os.path.join(mounttmp, uuid1))
926 self.assertEqual(["testdir"], sorted(d1))
927 d1 = os.listdir(os.path.join(mounttmp, uuid1, "testdir"))
928 self.assertEqual(["file1.txt"], sorted(d1))
930 d1 = os.listdir(os.path.join(mounttmp, uuid2))
931 self.assertEqual([], sorted(d1))
936 def fuseMvDirBetweenCollectionsTest2(mounttmp, uuid1, uuid2):
937 class Test(unittest.TestCase):
939 os.rename(os.path.join(mounttmp, uuid1, "testdir"), os.path.join(mounttmp, uuid2, "testdir2"))
941 d1 = os.listdir(os.path.join(mounttmp, uuid1))
942 self.assertEqual([], sorted(d1))
944 d1 = os.listdir(os.path.join(mounttmp, uuid2))
945 self.assertEqual(["testdir2"], sorted(d1))
946 d1 = os.listdir(os.path.join(mounttmp, uuid2, "testdir2"))
947 self.assertEqual(["file1.txt"], sorted(d1))
949 with open(os.path.join(mounttmp, uuid2, "testdir2", "file1.txt"), "r") as f:
950 self.assertEqual(f.read(), "Hello world!")
954 class FuseMvDirBetweenCollectionsTest(MountTestBase):
956 collection1 = arvados.collection.Collection(api_client=self.api)
957 collection1.save_new()
959 collection2 = arvados.collection.Collection(api_client=self.api)
960 collection2.save_new()
962 m = self.make_mount(fuse.MagicDirectory)
964 # See note in MountTestBase.setUp
965 self.pool.apply(fuseMvDirBetweenCollectionsTest1, (self.mounttmp,
966 collection1.manifest_locator(),
967 collection2.manifest_locator()))
972 assertRegex(self, collection1.manifest_text(), r"\./testdir 86fb269d190d2c85f6e0468ceca42a20\+12\+A\S+ 0:12:file1\.txt$")
973 self.assertEqual(collection2.manifest_text(), "")
975 self.pool.apply(fuseMvDirBetweenCollectionsTest2, (self.mounttmp,
976 collection1.manifest_locator(),
977 collection2.manifest_locator()))
982 self.assertEqual(collection1.manifest_text(), "")
983 assertRegex(self, collection2.manifest_text(), r"\./testdir2 86fb269d190d2c85f6e0468ceca42a20\+12\+A\S+ 0:12:file1\.txt$")
985 collection1.stop_threads()
986 collection2.stop_threads()
988 def fuseProjectMkdirTestHelper1(mounttmp):
989 class Test(unittest.TestCase):
991 os.mkdir(os.path.join(mounttmp, "testcollection"))
992 with self.assertRaises(OSError):
993 os.mkdir(os.path.join(mounttmp, "testcollection"))
996 def fuseProjectMkdirTestHelper2(mounttmp):
997 class Test(unittest.TestCase):
999 with open(os.path.join(mounttmp, "testcollection", "file1.txt"), "w") as f:
1000 f.write("Hello world!")
1001 with self.assertRaises(OSError):
1002 os.rmdir(os.path.join(mounttmp, "testcollection"))
1003 os.remove(os.path.join(mounttmp, "testcollection", "file1.txt"))
1004 with self.assertRaises(OSError):
1005 os.remove(os.path.join(mounttmp, "testcollection"))
1006 os.rmdir(os.path.join(mounttmp, "testcollection"))
1009 class FuseProjectMkdirRmdirTest(MountTestBase):
1011 self.make_mount(fuse.ProjectDirectory,
1012 project_object=self.api.users().current().execute())
1014 d1 = llfuse.listdir(self.mounttmp)
1015 self.assertNotIn('testcollection', d1)
1017 self.pool.apply(fuseProjectMkdirTestHelper1, (self.mounttmp,))
1019 d1 = llfuse.listdir(self.mounttmp)
1020 self.assertIn('testcollection', d1)
1022 self.pool.apply(fuseProjectMkdirTestHelper2, (self.mounttmp,))
1024 d1 = llfuse.listdir(self.mounttmp)
1025 self.assertNotIn('testcollection', d1)
1028 def fuseProjectMvTestHelper1(mounttmp):
1029 class Test(unittest.TestCase):
1031 d1 = llfuse.listdir(mounttmp)
1032 self.assertNotIn('testcollection', d1)
1034 os.mkdir(os.path.join(mounttmp, "testcollection"))
1036 d1 = llfuse.listdir(mounttmp)
1037 self.assertIn('testcollection', d1)
1039 with self.assertRaises(OSError):
1040 os.rename(os.path.join(mounttmp, "testcollection"), os.path.join(mounttmp, 'Unrestricted public data'))
1042 os.rename(os.path.join(mounttmp, "testcollection"), os.path.join(mounttmp, 'Unrestricted public data', 'testcollection'))
1044 d1 = llfuse.listdir(mounttmp)
1045 self.assertNotIn('testcollection', d1)
1047 d1 = llfuse.listdir(os.path.join(mounttmp, 'Unrestricted public data'))
1048 self.assertIn('testcollection', d1)
1052 class FuseProjectMvTest(MountTestBase):
1054 self.make_mount(fuse.ProjectDirectory,
1055 project_object=self.api.users().current().execute())
1057 self.pool.apply(fuseProjectMvTestHelper1, (self.mounttmp,))
1060 def fuseFsyncTestHelper(mounttmp, k):
1061 class Test(unittest.TestCase):
1063 fd = os.open(os.path.join(mounttmp, k), os.O_RDONLY)
1069 class FuseFsyncTest(FuseMagicTest):
1071 self.make_mount(fuse.MagicDirectory)
1072 self.pool.apply(fuseFsyncTestHelper, (self.mounttmp, self.testcollection))
1075 class MagicDirApiError(FuseMagicTest):
1077 api = mock.MagicMock()
1078 super(MagicDirApiError, self).setUp(api=api)
1079 api.collections().get().execute.side_effect = iter([
1080 Exception('API fail'),
1082 "manifest_text": self.test_manifest,
1083 "portable_data_hash": self.test_manifest_pdh,
1086 api.keep.get.side_effect = Exception('Keep fail')
1089 with mock.patch('arvados_fuse.fresh.FreshBase._poll_time', new_callable=mock.PropertyMock, return_value=60) as mock_poll_time:
1090 self.make_mount(fuse.MagicDirectory)
1092 self.operations.inodes.inode_cache.cap = 1
1093 self.operations.inodes.inode_cache.min_entries = 2
1095 with self.assertRaises(OSError):
1096 llfuse.listdir(os.path.join(self.mounttmp, self.testcollection))
1098 llfuse.listdir(os.path.join(self.mounttmp, self.testcollection))
1101 class FuseUnitTest(unittest.TestCase):
1102 def test_sanitize_filename(self):
1121 for f in acceptable:
1122 self.assertEqual(f, fuse.sanitize_filename(f))
1123 for f in unacceptable:
1124 self.assertNotEqual(f, fuse.sanitize_filename(f))
1125 # The sanitized filename should be the same length, though.
1126 self.assertEqual(len(f), len(fuse.sanitize_filename(f)))
1128 self.assertEqual("_", fuse.sanitize_filename(""))
1129 self.assertEqual("_", fuse.sanitize_filename("."))
1130 self.assertEqual("__", fuse.sanitize_filename(".."))
1133 class FuseMagicTestPDHOnly(MountTestBase):
1134 def setUp(self, api=None):
1135 super(FuseMagicTestPDHOnly, self).setUp(api=api)
1137 cw = arvados.CollectionWriter()
1139 cw.start_new_file('thing1.txt')
1142 self.testcollection = cw.finish()
1143 self.test_manifest = cw.manifest_text()
1144 created = self.api.collections().create(body={"manifest_text":self.test_manifest}).execute()
1145 self.testcollectionuuid = str(created['uuid'])
1147 def verify_pdh_only(self, pdh_only=False, skip_pdh_only=False):
1148 if skip_pdh_only is True:
1149 self.make_mount(fuse.MagicDirectory) # in this case, the default by_id applies
1151 self.make_mount(fuse.MagicDirectory, pdh_only=pdh_only)
1153 mount_ls = llfuse.listdir(self.mounttmp)
1154 self.assertIn('README', mount_ls)
1155 self.assertFalse(any(arvados.util.keep_locator_pattern.match(fn) or
1156 arvados.util.uuid_pattern.match(fn)
1157 for fn in mount_ls),
1158 "new FUSE MagicDirectory lists Collection")
1160 # look up using pdh should succeed in all cases
1161 self.assertDirContents(self.testcollection, ['thing1.txt'])
1162 self.assertDirContents(os.path.join('by_id', self.testcollection),
1164 mount_ls = llfuse.listdir(self.mounttmp)
1165 self.assertIn('README', mount_ls)
1166 self.assertIn(self.testcollection, mount_ls)
1167 self.assertIn(self.testcollection,
1168 llfuse.listdir(os.path.join(self.mounttmp, 'by_id')))
1171 files[os.path.join(self.mounttmp, self.testcollection, 'thing1.txt')] = 'data 1'
1173 for k, v in viewitems(files):
1174 with open(os.path.join(self.mounttmp, k), 'rb') as f:
1175 self.assertEqual(v, f.read().decode())
1177 # look up using uuid should fail when pdh_only is set
1178 if pdh_only is True:
1179 with self.assertRaises(OSError):
1180 self.assertDirContents(os.path.join('by_id', self.testcollectionuuid),
1183 self.assertDirContents(os.path.join('by_id', self.testcollectionuuid),
1186 def test_with_pdh_only_true(self):
1187 self.verify_pdh_only(pdh_only=True)
1189 def test_with_pdh_only_false(self):
1190 self.verify_pdh_only(pdh_only=False)
1192 def test_with_default_by_id(self):
1193 self.verify_pdh_only(skip_pdh_only=True)