11644: Use filesystem UUID and path as DeviceID for local disk volumes.
[arvados.git] / services / fuse / tests / test_tmp_collection.py
1 import arvados
2 import arvados_fuse
3 import arvados_fuse.command
4 import json
5 import logging
6 import os
7 import tempfile
8 import unittest
9
10 from .integration_test import IntegrationTest
11 from .mount_test_base import MountTestBase
12
13 logger = logging.getLogger('arvados.arv-mount')
14
15
16 class TmpCollectionArgsTest(unittest.TestCase):
17     def setUp(self):
18         self.tmpdir = tempfile.mkdtemp()
19
20     def tearDown(self):
21         os.rmdir(self.tmpdir)
22
23     def test_tmp_only(self):
24         args = arvados_fuse.command.ArgumentParser().parse_args([
25             '--mount-tmp', 'tmp1',
26             '--mount-tmp', 'tmp2',
27             self.tmpdir,
28         ])
29         self.assertIn(args.mode, [None, 'custom'])
30         self.assertEqual(['tmp1', 'tmp2'], args.mount_tmp)
31         for mtype in ['home', 'shared', 'by_id', 'by_pdh', 'by_tag']:
32             self.assertEqual([], getattr(args, 'mount_'+mtype))
33
34     def test_tmp_and_home(self):
35         args = arvados_fuse.command.ArgumentParser().parse_args([
36             '--mount-tmp', 'test_tmp',
37             '--mount-home', 'test_home',
38             self.tmpdir,
39         ])
40         self.assertIn(args.mode, [None, 'custom'])
41         self.assertEqual(['test_tmp'], args.mount_tmp)
42         self.assertEqual(['test_home'], args.mount_home)
43
44     def test_no_tmp(self):
45         args = arvados_fuse.command.ArgumentParser().parse_args([
46             self.tmpdir,
47         ])
48         self.assertEqual([], args.mount_tmp)
49
50
51 def current_manifest(tmpdir):
52     return json.load(open(
53         os.path.join(tmpdir, '.arvados#collection')
54     ))['manifest_text']
55
56
57 class TmpCollectionTest(IntegrationTest):
58     mnt_args = [
59         '--read-write',
60         '--mount-tmp', 'zzz',
61     ]
62
63     @IntegrationTest.mount(argv=mnt_args+['--mount-tmp', 'yyy'])
64     def test_two_tmp(self):
65         self.pool_test(os.path.join(self.mnt, 'zzz'),
66                        os.path.join(self.mnt, 'yyy'))
67     @staticmethod
68     def _test_two_tmp(self, zzz, yyy):
69         self.assertEqual(current_manifest(zzz), "")
70         self.assertEqual(current_manifest(yyy), "")
71         with open(os.path.join(zzz, 'foo'), 'w') as f:
72             f.write('foo')
73         self.assertNotEqual(current_manifest(zzz), "")
74         self.assertEqual(current_manifest(yyy), "")
75         os.unlink(os.path.join(zzz, 'foo'))
76         with open(os.path.join(yyy, 'bar'), 'w') as f:
77             f.write('bar')
78         self.assertEqual(current_manifest(zzz), "")
79         self.assertNotEqual(current_manifest(yyy), "")
80
81     @IntegrationTest.mount(argv=mnt_args)
82     def test_tmp_empty(self):
83         self.pool_test(os.path.join(self.mnt, 'zzz'))
84     @staticmethod
85     def _test_tmp_empty(self, tmpdir):
86         self.assertEqual(current_manifest(tmpdir), "")
87
88     @IntegrationTest.mount(argv=mnt_args)
89     def test_tmp_onefile(self):
90         self.pool_test(os.path.join(self.mnt, 'zzz'))
91     @staticmethod
92     def _test_tmp_onefile(self, tmpdir):
93         with open(os.path.join(tmpdir, 'foo'), 'w') as f:
94             f.write('foo')
95         self.assertRegexpMatches(
96             current_manifest(tmpdir),
97             r'^\. acbd18db4cc2f85cedef654fccc4a4d8\+3(\+\S+)? 0:3:foo\n$')
98
99     @IntegrationTest.mount(argv=mnt_args)
100     def test_tmp_snapshots(self):
101         self.pool_test(os.path.join(self.mnt, 'zzz'))
102     @staticmethod
103     def _test_tmp_snapshots(self, tmpdir):
104         ops = [
105             ('foo', 'bar',
106              r'^\. 37b51d194a7513e45b56f6524f2d51f2\+3(\+\S+)? 0:3:foo\n$'),
107             ('foo', 'foo',
108              r'^\. acbd18db4cc2f85cedef654fccc4a4d8\+3(\+\S+)? 0:3:foo\n$'),
109             ('bar', 'bar',
110              r'^\. 37b51d194a7513e45b56f6524f2d51f2\+3(\+\S+)? acbd18db4cc2f85cedef654fccc4a4d8\+3(\+\S+)? 0:3:bar 3:3:foo\n$'),
111             ('foo', None,
112              r'^\. 37b51d194a7513e45b56f6524f2d51f2\+3(\+\S+)? 0:3:bar\n$'),
113             ('bar', None,
114              r'^$'),
115         ]
116         for _ in range(10):
117             for fn, content, expect in ops:
118                 path = os.path.join(tmpdir, fn)
119                 if content is None:
120                     os.unlink(path)
121                 else:
122                     with open(path, 'w') as f:
123                         f.write(content)
124                 self.assertRegexpMatches(current_manifest(tmpdir), expect)
125
126     @IntegrationTest.mount(argv=mnt_args)
127     def test_tmp_rewrite(self):
128         self.pool_test(os.path.join(self.mnt, 'zzz'))
129     @staticmethod
130     def _test_tmp_rewrite(self, tmpdir):
131         with open(os.path.join(tmpdir, "b1"), 'w') as f:
132             f.write("b1")
133         with open(os.path.join(tmpdir, "b2"), 'w') as f:
134             f.write("b2")
135         with open(os.path.join(tmpdir, "b1"), 'w') as f:
136             f.write("1b")
137         self.assertRegexpMatches(current_manifest(tmpdir), "^\. ed4f3f67c70b02b29c50ce1ea26666bd\+4(\+\S+)? 0:2:b1 2:2:b2\n$")