##// END OF EJS Templates
py3: convert strings to bytes in tests/test-remotefilelog-histpack.py...
Pulkit Goyal -
r40764:9446d5aa default
parent child Browse files
Show More
@@ -1,274 +1,276
1 #!/usr/bin/env python
1 #!/usr/bin/env python
2 from __future__ import absolute_import
2 from __future__ import absolute_import
3
3
4 import hashlib
4 import hashlib
5 import os
5 import os
6 import random
6 import random
7 import shutil
7 import shutil
8 import stat
8 import stat
9 import struct
9 import struct
10 import sys
10 import sys
11 import tempfile
11 import tempfile
12 import unittest
12 import unittest
13
13
14 import silenttestrunner
14 import silenttestrunner
15
15
16 from mercurial.node import nullid
16 from mercurial.node import nullid
17 from mercurial import (
17 from mercurial import (
18 pycompat,
18 ui as uimod,
19 ui as uimod,
19 )
20 )
20 # Load the local remotefilelog, not the system one
21 # Load the local remotefilelog, not the system one
21 sys.path[0:0] = [os.path.join(os.path.dirname(__file__), '..')]
22 sys.path[0:0] = [os.path.join(os.path.dirname(__file__), '..')]
22 from hgext.remotefilelog import (
23 from hgext.remotefilelog import (
23 basepack,
24 basepack,
24 historypack,
25 historypack,
25 )
26 )
26
27
27 class histpacktests(unittest.TestCase):
28 class histpacktests(unittest.TestCase):
28 def setUp(self):
29 def setUp(self):
29 self.tempdirs = []
30 self.tempdirs = []
30
31
31 def tearDown(self):
32 def tearDown(self):
32 for d in self.tempdirs:
33 for d in self.tempdirs:
33 shutil.rmtree(d)
34 shutil.rmtree(d)
34
35
35 def makeTempDir(self):
36 def makeTempDir(self):
36 tempdir = tempfile.mkdtemp()
37 tempdir = tempfile.mkdtemp()
37 self.tempdirs.append(tempdir)
38 self.tempdirs.append(tempdir)
38 return tempdir
39 return pycompat.fsencode(tempdir)
39
40
40 def getHash(self, content):
41 def getHash(self, content):
41 return hashlib.sha1(content).digest()
42 return hashlib.sha1(content).digest()
42
43
43 def getFakeHash(self):
44 def getFakeHash(self):
44 return ''.join(chr(random.randint(0, 255)) for _ in range(20))
45 return b''.join(pycompat.bytechr(random.randint(0, 255))
46 for _ in range(20))
45
47
46 def createPack(self, revisions=None):
48 def createPack(self, revisions=None):
47 """Creates and returns a historypack containing the specified revisions.
49 """Creates and returns a historypack containing the specified revisions.
48
50
49 `revisions` is a list of tuples, where each tuple contains a filanem,
51 `revisions` is a list of tuples, where each tuple contains a filanem,
50 node, p1node, p2node, and linknode.
52 node, p1node, p2node, and linknode.
51 """
53 """
52 if revisions is None:
54 if revisions is None:
53 revisions = [("filename", self.getFakeHash(), nullid, nullid,
55 revisions = [("filename", self.getFakeHash(), nullid, nullid,
54 self.getFakeHash(), None)]
56 self.getFakeHash(), None)]
55
57
56 packdir = self.makeTempDir()
58 packdir = pycompat.fsencode(self.makeTempDir())
57 packer = historypack.mutablehistorypack(uimod.ui(), packdir,
59 packer = historypack.mutablehistorypack(uimod.ui(), packdir,
58 version=2)
60 version=2)
59
61
60 for filename, node, p1, p2, linknode, copyfrom in revisions:
62 for filename, node, p1, p2, linknode, copyfrom in revisions:
61 packer.add(filename, node, p1, p2, linknode, copyfrom)
63 packer.add(filename, node, p1, p2, linknode, copyfrom)
62
64
63 path = packer.close()
65 path = packer.close()
64 return historypack.historypack(path)
66 return historypack.historypack(path)
65
67
66 def testAddSingle(self):
68 def testAddSingle(self):
67 """Test putting a single entry into a pack and reading it out.
69 """Test putting a single entry into a pack and reading it out.
68 """
70 """
69 filename = "foo"
71 filename = "foo"
70 node = self.getFakeHash()
72 node = self.getFakeHash()
71 p1 = self.getFakeHash()
73 p1 = self.getFakeHash()
72 p2 = self.getFakeHash()
74 p2 = self.getFakeHash()
73 linknode = self.getFakeHash()
75 linknode = self.getFakeHash()
74
76
75 revisions = [(filename, node, p1, p2, linknode, None)]
77 revisions = [(filename, node, p1, p2, linknode, None)]
76 pack = self.createPack(revisions)
78 pack = self.createPack(revisions)
77
79
78 actual = pack.getancestors(filename, node)[node]
80 actual = pack.getancestors(filename, node)[node]
79 self.assertEquals(p1, actual[0])
81 self.assertEquals(p1, actual[0])
80 self.assertEquals(p2, actual[1])
82 self.assertEquals(p2, actual[1])
81 self.assertEquals(linknode, actual[2])
83 self.assertEquals(linknode, actual[2])
82
84
83 def testAddMultiple(self):
85 def testAddMultiple(self):
84 """Test putting multiple unrelated revisions into a pack and reading
86 """Test putting multiple unrelated revisions into a pack and reading
85 them out.
87 them out.
86 """
88 """
87 revisions = []
89 revisions = []
88 for i in range(10):
90 for i in range(10):
89 filename = "foo-%s" % i
91 filename = "foo-%s" % i
90 node = self.getFakeHash()
92 node = self.getFakeHash()
91 p1 = self.getFakeHash()
93 p1 = self.getFakeHash()
92 p2 = self.getFakeHash()
94 p2 = self.getFakeHash()
93 linknode = self.getFakeHash()
95 linknode = self.getFakeHash()
94 revisions.append((filename, node, p1, p2, linknode, None))
96 revisions.append((filename, node, p1, p2, linknode, None))
95
97
96 pack = self.createPack(revisions)
98 pack = self.createPack(revisions)
97
99
98 for filename, node, p1, p2, linknode, copyfrom in revisions:
100 for filename, node, p1, p2, linknode, copyfrom in revisions:
99 actual = pack.getancestors(filename, node)[node]
101 actual = pack.getancestors(filename, node)[node]
100 self.assertEquals(p1, actual[0])
102 self.assertEquals(p1, actual[0])
101 self.assertEquals(p2, actual[1])
103 self.assertEquals(p2, actual[1])
102 self.assertEquals(linknode, actual[2])
104 self.assertEquals(linknode, actual[2])
103 self.assertEquals(copyfrom, actual[3])
105 self.assertEquals(copyfrom, actual[3])
104
106
105 def testAddAncestorChain(self):
107 def testAddAncestorChain(self):
106 """Test putting multiple revisions in into a pack and read the ancestor
108 """Test putting multiple revisions in into a pack and read the ancestor
107 chain.
109 chain.
108 """
110 """
109 revisions = []
111 revisions = []
110 filename = "foo"
112 filename = b"foo"
111 lastnode = nullid
113 lastnode = nullid
112 for i in range(10):
114 for i in range(10):
113 node = self.getFakeHash()
115 node = self.getFakeHash()
114 revisions.append((filename, node, lastnode, nullid, nullid, None))
116 revisions.append((filename, node, lastnode, nullid, nullid, None))
115 lastnode = node
117 lastnode = node
116
118
117 # revisions must be added in topological order, newest first
119 # revisions must be added in topological order, newest first
118 revisions = list(reversed(revisions))
120 revisions = list(reversed(revisions))
119 pack = self.createPack(revisions)
121 pack = self.createPack(revisions)
120
122
121 # Test that the chain has all the entries
123 # Test that the chain has all the entries
122 ancestors = pack.getancestors(revisions[0][0], revisions[0][1])
124 ancestors = pack.getancestors(revisions[0][0], revisions[0][1])
123 for filename, node, p1, p2, linknode, copyfrom in revisions:
125 for filename, node, p1, p2, linknode, copyfrom in revisions:
124 ap1, ap2, alinknode, acopyfrom = ancestors[node]
126 ap1, ap2, alinknode, acopyfrom = ancestors[node]
125 self.assertEquals(ap1, p1)
127 self.assertEquals(ap1, p1)
126 self.assertEquals(ap2, p2)
128 self.assertEquals(ap2, p2)
127 self.assertEquals(alinknode, linknode)
129 self.assertEquals(alinknode, linknode)
128 self.assertEquals(acopyfrom, copyfrom)
130 self.assertEquals(acopyfrom, copyfrom)
129
131
130 def testPackMany(self):
132 def testPackMany(self):
131 """Pack many related and unrelated ancestors.
133 """Pack many related and unrelated ancestors.
132 """
134 """
133 # Build a random pack file
135 # Build a random pack file
134 allentries = {}
136 allentries = {}
135 ancestorcounts = {}
137 ancestorcounts = {}
136 revisions = []
138 revisions = []
137 random.seed(0)
139 random.seed(0)
138 for i in range(100):
140 for i in range(100):
139 filename = "filename-%s" % i
141 filename = b"filename-%d" % i
140 entries = []
142 entries = []
141 p2 = nullid
143 p2 = nullid
142 linknode = nullid
144 linknode = nullid
143 for j in range(random.randint(1, 100)):
145 for j in range(random.randint(1, 100)):
144 node = self.getFakeHash()
146 node = self.getFakeHash()
145 p1 = nullid
147 p1 = nullid
146 if len(entries) > 0:
148 if len(entries) > 0:
147 p1 = entries[random.randint(0, len(entries) - 1)]
149 p1 = entries[random.randint(0, len(entries) - 1)]
148 entries.append(node)
150 entries.append(node)
149 revisions.append((filename, node, p1, p2, linknode, None))
151 revisions.append((filename, node, p1, p2, linknode, None))
150 allentries[(filename, node)] = (p1, p2, linknode)
152 allentries[(filename, node)] = (p1, p2, linknode)
151 if p1 == nullid:
153 if p1 == nullid:
152 ancestorcounts[(filename, node)] = 1
154 ancestorcounts[(filename, node)] = 1
153 else:
155 else:
154 newcount = ancestorcounts[(filename, p1)] + 1
156 newcount = ancestorcounts[(filename, p1)] + 1
155 ancestorcounts[(filename, node)] = newcount
157 ancestorcounts[(filename, node)] = newcount
156
158
157 # Must add file entries in reverse topological order
159 # Must add file entries in reverse topological order
158 revisions = list(reversed(revisions))
160 revisions = list(reversed(revisions))
159 pack = self.createPack(revisions)
161 pack = self.createPack(revisions)
160
162
161 # Verify the pack contents
163 # Verify the pack contents
162 for (filename, node), (p1, p2, lastnode) in allentries.iteritems():
164 for (filename, node), (p1, p2, lastnode) in allentries.iteritems():
163 ancestors = pack.getancestors(filename, node)
165 ancestors = pack.getancestors(filename, node)
164 self.assertEquals(ancestorcounts[(filename, node)],
166 self.assertEquals(ancestorcounts[(filename, node)],
165 len(ancestors))
167 len(ancestors))
166 for anode, (ap1, ap2, alinknode, copyfrom) in ancestors.iteritems():
168 for anode, (ap1, ap2, alinknode, copyfrom) in ancestors.iteritems():
167 ep1, ep2, elinknode = allentries[(filename, anode)]
169 ep1, ep2, elinknode = allentries[(filename, anode)]
168 self.assertEquals(ap1, ep1)
170 self.assertEquals(ap1, ep1)
169 self.assertEquals(ap2, ep2)
171 self.assertEquals(ap2, ep2)
170 self.assertEquals(alinknode, elinknode)
172 self.assertEquals(alinknode, elinknode)
171 self.assertEquals(copyfrom, None)
173 self.assertEquals(copyfrom, None)
172
174
173 def testGetNodeInfo(self):
175 def testGetNodeInfo(self):
174 revisions = []
176 revisions = []
175 filename = "foo"
177 filename = b"foo"
176 lastnode = nullid
178 lastnode = nullid
177 for i in range(10):
179 for i in range(10):
178 node = self.getFakeHash()
180 node = self.getFakeHash()
179 revisions.append((filename, node, lastnode, nullid, nullid, None))
181 revisions.append((filename, node, lastnode, nullid, nullid, None))
180 lastnode = node
182 lastnode = node
181
183
182 pack = self.createPack(revisions)
184 pack = self.createPack(revisions)
183
185
184 # Test that getnodeinfo returns the expected results
186 # Test that getnodeinfo returns the expected results
185 for filename, node, p1, p2, linknode, copyfrom in revisions:
187 for filename, node, p1, p2, linknode, copyfrom in revisions:
186 ap1, ap2, alinknode, acopyfrom = pack.getnodeinfo(filename, node)
188 ap1, ap2, alinknode, acopyfrom = pack.getnodeinfo(filename, node)
187 self.assertEquals(ap1, p1)
189 self.assertEquals(ap1, p1)
188 self.assertEquals(ap2, p2)
190 self.assertEquals(ap2, p2)
189 self.assertEquals(alinknode, linknode)
191 self.assertEquals(alinknode, linknode)
190 self.assertEquals(acopyfrom, copyfrom)
192 self.assertEquals(acopyfrom, copyfrom)
191
193
192 def testGetMissing(self):
194 def testGetMissing(self):
193 """Test the getmissing() api.
195 """Test the getmissing() api.
194 """
196 """
195 revisions = []
197 revisions = []
196 filename = "foo"
198 filename = b"foo"
197 for i in range(10):
199 for i in range(10):
198 node = self.getFakeHash()
200 node = self.getFakeHash()
199 p1 = self.getFakeHash()
201 p1 = self.getFakeHash()
200 p2 = self.getFakeHash()
202 p2 = self.getFakeHash()
201 linknode = self.getFakeHash()
203 linknode = self.getFakeHash()
202 revisions.append((filename, node, p1, p2, linknode, None))
204 revisions.append((filename, node, p1, p2, linknode, None))
203
205
204 pack = self.createPack(revisions)
206 pack = self.createPack(revisions)
205
207
206 missing = pack.getmissing([(filename, revisions[0][1])])
208 missing = pack.getmissing([(filename, revisions[0][1])])
207 self.assertFalse(missing)
209 self.assertFalse(missing)
208
210
209 missing = pack.getmissing([(filename, revisions[0][1]),
211 missing = pack.getmissing([(filename, revisions[0][1]),
210 (filename, revisions[1][1])])
212 (filename, revisions[1][1])])
211 self.assertFalse(missing)
213 self.assertFalse(missing)
212
214
213 fakenode = self.getFakeHash()
215 fakenode = self.getFakeHash()
214 missing = pack.getmissing([(filename, revisions[0][1]),
216 missing = pack.getmissing([(filename, revisions[0][1]),
215 (filename, fakenode)])
217 (filename, fakenode)])
216 self.assertEquals(missing, [(filename, fakenode)])
218 self.assertEquals(missing, [(filename, fakenode)])
217
219
218 # Test getmissing on a non-existant filename
220 # Test getmissing on a non-existant filename
219 missing = pack.getmissing([("bar", fakenode)])
221 missing = pack.getmissing([("bar", fakenode)])
220 self.assertEquals(missing, [("bar", fakenode)])
222 self.assertEquals(missing, [("bar", fakenode)])
221
223
222 def testAddThrows(self):
224 def testAddThrows(self):
223 pack = self.createPack()
225 pack = self.createPack()
224
226
225 try:
227 try:
226 pack.add('filename', nullid, nullid, nullid, nullid, None)
228 pack.add(b'filename', nullid, nullid, nullid, nullid, None)
227 self.assertTrue(False, "historypack.add should throw")
229 self.assertTrue(False, "historypack.add should throw")
228 except RuntimeError:
230 except RuntimeError:
229 pass
231 pass
230
232
231 def testBadVersionThrows(self):
233 def testBadVersionThrows(self):
232 pack = self.createPack()
234 pack = self.createPack()
233 path = pack.path + '.histpack'
235 path = pack.path + '.histpack'
234 with open(path) as f:
236 with open(path) as f:
235 raw = f.read()
237 raw = f.read()
236 raw = struct.pack('!B', 255) + raw[1:]
238 raw = struct.pack('!B', 255) + raw[1:]
237 os.chmod(path, os.stat(path).st_mode | stat.S_IWRITE)
239 os.chmod(path, os.stat(path).st_mode | stat.S_IWRITE)
238 with open(path, 'w+') as f:
240 with open(path, 'w+') as f:
239 f.write(raw)
241 f.write(raw)
240
242
241 try:
243 try:
242 pack = historypack.historypack(pack.path)
244 pack = historypack.historypack(pack.path)
243 self.assertTrue(False, "bad version number should have thrown")
245 self.assertTrue(False, "bad version number should have thrown")
244 except RuntimeError:
246 except RuntimeError:
245 pass
247 pass
246
248
247 def testLargePack(self):
249 def testLargePack(self):
248 """Test creating and reading from a large pack with over X entries.
250 """Test creating and reading from a large pack with over X entries.
249 This causes it to use a 2^16 fanout table instead."""
251 This causes it to use a 2^16 fanout table instead."""
250 total = basepack.SMALLFANOUTCUTOFF + 1
252 total = basepack.SMALLFANOUTCUTOFF + 1
251 revisions = []
253 revisions = []
252 for i in xrange(total):
254 for i in xrange(total):
253 filename = "foo-%s" % i
255 filename = b"foo-%d" % i
254 node = self.getFakeHash()
256 node = self.getFakeHash()
255 p1 = self.getFakeHash()
257 p1 = self.getFakeHash()
256 p2 = self.getFakeHash()
258 p2 = self.getFakeHash()
257 linknode = self.getFakeHash()
259 linknode = self.getFakeHash()
258 revisions.append((filename, node, p1, p2, linknode, None))
260 revisions.append((filename, node, p1, p2, linknode, None))
259
261
260 pack = self.createPack(revisions)
262 pack = self.createPack(revisions)
261 self.assertEquals(pack.params.fanoutprefix, basepack.LARGEFANOUTPREFIX)
263 self.assertEquals(pack.params.fanoutprefix, basepack.LARGEFANOUTPREFIX)
262
264
263 for filename, node, p1, p2, linknode, copyfrom in revisions:
265 for filename, node, p1, p2, linknode, copyfrom in revisions:
264 actual = pack.getancestors(filename, node)[node]
266 actual = pack.getancestors(filename, node)[node]
265 self.assertEquals(p1, actual[0])
267 self.assertEquals(p1, actual[0])
266 self.assertEquals(p2, actual[1])
268 self.assertEquals(p2, actual[1])
267 self.assertEquals(linknode, actual[2])
269 self.assertEquals(linknode, actual[2])
268 self.assertEquals(copyfrom, actual[3])
270 self.assertEquals(copyfrom, actual[3])
269 # TODO:
271 # TODO:
270 # histpack store:
272 # histpack store:
271 # - repack two packs into one
273 # - repack two packs into one
272
274
273 if __name__ == '__main__':
275 if __name__ == '__main__':
274 silenttestrunner.main(__name__)
276 silenttestrunner.main(__name__)
General Comments 0
You need to be logged in to leave comments. Login now