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