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