Merge branch 'master' into 2272-setup-user-in-workbench
[arvados.git] / sdk / python / test_mount.py
1 import unittest
2 import arvados
3 import arvados.fuse as fuse
4 import threading
5 import time
6 import os
7 import llfuse
8 import tempfile
9 import shutil
10 import subprocess
11 import glob
12
13 class FuseMountTest(unittest.TestCase):
14     def setUp(self):
15         self.keeptmp = tempfile.mkdtemp()
16         os.environ['KEEP_LOCAL_STORE'] = self.keeptmp
17
18         cw = arvados.CollectionWriter()
19
20         cw.start_new_file('thing1.txt')
21         cw.write("data 1")
22         cw.start_new_file('thing2.txt')
23         cw.write("data 2")
24         cw.start_new_stream('dir1')
25
26         cw.start_new_file('thing3.txt')
27         cw.write("data 3")
28         cw.start_new_file('thing4.txt')
29         cw.write("data 4")
30
31         cw.start_new_stream('dir2')
32         cw.start_new_file('thing5.txt')
33         cw.write("data 5")
34         cw.start_new_file('thing6.txt')
35         cw.write("data 6")
36
37         cw.start_new_stream('dir2/dir3')
38         cw.start_new_file('thing7.txt')
39         cw.write("data 7")
40
41         cw.start_new_file('thing8.txt')
42         cw.write("data 8")
43
44         self.testcollection = cw.finish()
45
46     def runTest(self):
47         # Create the request handler
48         operations = fuse.Operations(os.getuid(), os.getgid())
49         e = operations.inodes.add_entry(fuse.Directory(llfuse.ROOT_INODE))
50         operations.inodes.load_collection(e, arvados.CollectionReader(arvados.Keep.get(self.testcollection)))
51
52         self.mounttmp = tempfile.mkdtemp()
53
54         llfuse.init(operations, self.mounttmp, [])
55         t = threading.Thread(None, lambda: llfuse.main())
56         t.start()
57
58         # wait until the driver is finished initializing
59         operations.initlock.wait()
60
61         # now check some stuff
62         d1 = os.listdir(self.mounttmp)
63         d1.sort()
64         self.assertEqual(d1, ['dir1', 'dir2', 'thing1.txt', 'thing2.txt'])
65
66         d2 = os.listdir(os.path.join(self.mounttmp, 'dir1'))
67         d2.sort()
68         self.assertEqual(d2, ['thing3.txt', 'thing4.txt'])
69
70         d3 = os.listdir(os.path.join(self.mounttmp, 'dir2'))
71         d3.sort()
72         self.assertEqual(d3, ['dir3', 'thing5.txt', 'thing6.txt'])
73
74         d4 = os.listdir(os.path.join(self.mounttmp, 'dir2/dir3'))
75         d4.sort()
76         self.assertEqual(d4, ['thing7.txt', 'thing8.txt'])
77         
78         files = {'thing1.txt': 'data 1',
79                  'thing2.txt': 'data 2',
80                  'dir1/thing3.txt': 'data 3',
81                  'dir1/thing4.txt': 'data 4',
82                  'dir2/thing5.txt': 'data 5',
83                  'dir2/thing6.txt': 'data 6',         
84                  'dir2/dir3/thing7.txt': 'data 7',
85                  'dir2/dir3/thing8.txt': 'data 8'}
86
87         for k, v in files.items():
88             with open(os.path.join(self.mounttmp, k)) as f:
89                 self.assertEqual(f.read(), v)
90         
91
92     def tearDown(self):
93         # llfuse.close is buggy, so use fusermount instead.
94         #llfuse.close(unmount=True)
95         subprocess.call(["fusermount", "-u", self.mounttmp])
96
97         os.rmdir(self.mounttmp)
98         shutil.rmtree(self.keeptmp)
99
100 class FuseMagicTest(unittest.TestCase):
101     def setUp(self):
102         self.keeptmp = tempfile.mkdtemp()
103         os.environ['KEEP_LOCAL_STORE'] = self.keeptmp
104
105         cw = arvados.CollectionWriter()
106
107         cw.start_new_file('thing1.txt')
108         cw.write("data 1")
109
110         self.testcollection = cw.finish()
111
112     def runTest(self):
113         # Create the request handler
114         operations = fuse.Operations(os.getuid(), os.getgid())
115         e = operations.inodes.add_entry(fuse.MagicDirectory(llfuse.ROOT_INODE, operations.inodes))
116
117         self.mounttmp = tempfile.mkdtemp()
118
119         llfuse.init(operations, self.mounttmp, [])
120         t = threading.Thread(None, lambda: llfuse.main())
121         t.start()
122
123         # wait until the driver is finished initializing
124         operations.initlock.wait()
125
126         # now check some stuff
127         d1 = os.listdir(self.mounttmp)
128         d1.sort()
129         self.assertEqual(d1, [])
130
131         d2 = os.listdir(os.path.join(self.mounttmp, self.testcollection))
132         d2.sort()
133         self.assertEqual(d2, ['thing1.txt'])
134
135         d3 = os.listdir(self.mounttmp)
136         d3.sort()
137         self.assertEqual(d3, [self.testcollection])
138         
139         files = {}
140         files[os.path.join(self.mounttmp, self.testcollection, 'thing1.txt')] = 'data 1'
141
142         for k, v in files.items():
143             with open(os.path.join(self.mounttmp, k)) as f:
144                 self.assertEqual(f.read(), v)
145         
146
147     def tearDown(self):
148         # llfuse.close is buggy, so use fusermount instead.
149         #llfuse.close(unmount=True)
150         subprocess.call(["fusermount", "-u", self.mounttmp])
151
152         os.rmdir(self.mounttmp)
153         shutil.rmtree(self.keeptmp)