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