/
tests.py
148 lines (125 loc) · 4.66 KB
/
tests.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# -*- coding: utf-8 -*-
"""
:author: samu
:created: 2/18/13 3:27 PM
"""
import hashlib
import os
import shutil
import subprocess
import unittest
class StorageTestCase(unittest.TestCase):
"""We test the scripts, using subprocess"""
def setUp(self):
self.tmp_dir = os.tmpnam()
print "-- TMP DIR {}".format(self.tmp_dir)
os.makedirs(self.tmp_dir)
self.server_process = subprocess.Popen([
'blobstore_server',
'--storage={}'.format(self.tmp_dir),
'--address=127.0.0.1',
'--port=16135',
])
def tearDown(self):
self.server_process.terminate()
shutil.rmtree(self.tmp_dir)
def test_store_blobs(self):
"""Test storage/retrieval of some blobs"""
BLOBS = [
"First string of example text",
"Second string of example text",
# Blobs cannot be unicode! We must encode 'em first..
u"Ju§t áñóth€r lĩnẽ øf ünïcödë ƭẻxt".encode('utf-8'),
"Some non-printables: \x01\x02\x03\x04\x80\x81\x82\x83",
]
BLOB_ASSIGNED_IDS = {}
## Store the blobs
for i, blob in enumerate(BLOBS):
client_process = subprocess.Popen([
'blobstore_client',
'--address=127.0.0.1',
'--port=16135',
'store',
'-',
],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
)
stdoutdata, stderrdata = client_process.communicate(input=blob)
blob_id = stdoutdata.rstrip()
self.assertNotIn("\n", blob_id,
'output must be a single-line blob_id')
self.assertRegexpMatches(blob_id, r'^[0-9a-z]{40}$',
'blob_id must be a valid SHA-1 sum')
self.assertEqual(blob_id, hashlib.sha1(blob).hexdigest(),
'blob_id must be the blob SHA-1 sum')
BLOB_ASSIGNED_IDS[i] = blob_id
## Check that the list contains all and only the actual ids
client_process = subprocess.Popen([
'blobstore_client',
'--address=127.0.0.1',
'--port=16135',
'list',
], stdout=subprocess.PIPE)
stdoutdata, stderrdata = client_process.communicate(None)
ids_in_list = []
for line in stdoutdata.splitlines():
ids_in_list.append(line.rstrip())
self.assertListEqual(
sorted(ids_in_list),
sorted(BLOB_ASSIGNED_IDS.values()),
'The blobs list must match the list of ids of inserted blobs.')
## Check that stored data is correct
for i, blob in enumerate(BLOBS):
client_process = subprocess.Popen([
'blobstore_client',
'--address=127.0.0.1',
'--port=16135',
'retrieve',
BLOB_ASSIGNED_IDS[i],
],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
)
stdoutdata, stderrdata = client_process.communicate(input=blob)
self.assertEqual(stdoutdata, blob,
"The retrieved blob must match the stored one")
def test_no_duplicates(self):
"""Test that stored data contains no duplicates"""
BLOBS = [
'This is the BLOB-0',
'This is the BLOB-1',
'This is the BLOB-1',
'This is the BLOB-0',
'This is the BLOB-2',
'This is the BLOB-0',
'This is the BLOB-1',
]
STORED_IDS = set()
for i, blob in enumerate(BLOBS):
client_process = subprocess.Popen([
'blobstore_client', '--address=127.0.0.1', '--port=16135',
'store', '-',
],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
)
stdoutdata, stderrdata = client_process.communicate(input=blob)
blob_id = stdoutdata.rstrip()
STORED_IDS.add(blob_id)
client_process = subprocess.Popen([
'blobstore_client',
'--address=127.0.0.1',
'--port=16135',
'list',
], stdout=subprocess.PIPE)
stdoutdata, stderrdata = client_process.communicate(None)
ids_in_list = []
for line in stdoutdata.splitlines():
ids_in_list.append(line.rstrip())
self.assertListEqual(
sorted(ids_in_list),
sorted(list(STORED_IDS)),
'The blobs list must match the list of ids of inserted blobs.')
if __name__ == '__main__':
unittest.main()