/
basic.py
executable file
·130 lines (97 loc) · 3.72 KB
/
basic.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env python
from __future__ import with_statement
import unittest
import os
import subprocess
import time
import glob
import stat
class BasicGridfsFUSETestCase(unittest.TestCase):
def setUp(self):
self.mount = os.path.join(os.path.dirname(os.path.abspath(__file__)),
'mount')
os.mkdir('tests/mount')
subprocess.check_call(['./mount_gridfs', '--db=gridfstest',
self.mount])
# wait for mount to complete
time.sleep(1)
def tearDown(self):
for filename in glob.iglob(os.path.join(self.mount, '*')):
os.remove(filename)
subprocess.check_call(['umount', self.mount])
os.rmdir(self.mount)
def test_read_write(self):
with open(os.path.join(self.mount, 'testfile.txt'), 'w') as w:
w.write("This is a test of GridFS FUSE.")
with open(os.path.join(self.mount, 'testfile.txt'), 'r') as r:
self.assertEqual("This is a test of GridFS FUSE.",
r.read())
def test_stat(self):
path = os.path.join(self.mount, 'testfile.txt')
with open(path, 'w') as w:
w.write('test')
stat_result = os.stat(path)
mode = stat_result.st_mode
self.assert_(stat.S_ISREG(mode))
self.assertEquals(0555, stat.S_IMODE(mode))
self.assertEquals(4, stat_result.st_size)
def test_ls(self):
self.assertEquals(0, len(os.listdir(self.mount)))
with open(os.path.join(self.mount, 'file1'), 'w') as f1:
f1.write("file1")
with open(os.path.join(self.mount, 'file2'), 'w') as f2:
f2.write("file2")
files = os.listdir(self.mount)
self.assertEquals(2, len(files))
self.assert_('file1' in files)
self.assert_('file2' in files)
def test_unlink(self):
path = os.path.join(self.mount, 'file')
with open(path, 'w') as w:
w.write("a file")
self.assert_('file' in os.listdir(self.mount))
os.unlink(path)
self.assert_('file' not in os.listdir(self.mount))
def test_rename(self):
path1 = os.path.join(self.mount, 'file1')
path2 = os.path.join(self.mount, 'file2')
with open(path1, 'w') as w:
w.write('file1')
self.assert_('file1' in os.listdir(self.mount))
self.assert_('file2' not in os.listdir(self.mount))
os.rename(path1, path2)
self.assert_('file1' not in os.listdir(self.mount))
self.assert_('file2' in os.listdir(self.mount))
with open(path2, 'r') as r:
self.assertEquals('file1', r.read())
def test_big_file(self):
# Test creation/reading of a file that's bigger than
# the chunk size
path = os.path.join(self.mount, 'big')
size = 256 * 1024 * 3 + 100
data = 'A' * size
with open(path, 'w') as w:
w.write(data)
with open(path, 'r') as r:
self.assertEquals(data, r.read())
self.assertEquals(size, os.stat(path).st_size)
def test_seek(self):
path = os.path.join(self.mount, 'file')
size1 = 256 * 1024 * 3
data1 = 'A' * size1
data2 = 'B' * size1
expected = ('A' * (size1 / 2)) + data2
size2 = len(expected)
with open(path, 'w') as w:
w.write(data1)
w.seek(size1 / 2)
w.write(data2)
with open(path, 'r') as r:
self.assertEquals(expected, r.read())
self.assertEquals(size2, os.stat(path).st_size)
def suite():
suite = unittest.TestSuite()
suite.addTest(BasicGridfsFUSETestCase())
return suite
if __name__ == '__main__':
unittest.main()