test_object_store.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. # test_object_store.py -- tests for object_store.py
  2. # Copyright (C) 2008 Jelmer Vernooij <jelmer@jelmer.uk>
  3. #
  4. # Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
  5. # General Public License as public by the Free Software Foundation; version 2.0
  6. # or (at your option) any later version. You can redistribute it and/or
  7. # modify it under the terms of either of these two licenses.
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. #
  15. # You should have received a copy of the licenses; if not, see
  16. # <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
  17. # and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
  18. # License, Version 2.0.
  19. #
  20. """Tests for the object store interface."""
  21. import os
  22. import shutil
  23. import stat
  24. import sys
  25. import tempfile
  26. from contextlib import closing
  27. from io import BytesIO
  28. from dulwich.errors import NotTreeError
  29. from dulwich.index import commit_tree
  30. from dulwich.object_store import (
  31. DiskObjectStore,
  32. MemoryObjectStore,
  33. ObjectStoreGraphWalker,
  34. OverlayObjectStore,
  35. commit_tree_changes,
  36. read_packs_file,
  37. tree_lookup_path,
  38. )
  39. from dulwich.objects import (
  40. S_IFGITLINK,
  41. Blob,
  42. EmptyFileException,
  43. SubmoduleEncountered,
  44. Tree,
  45. TreeEntry,
  46. sha_to_hex,
  47. )
  48. from dulwich.pack import REF_DELTA, write_pack_objects
  49. from dulwich.tests.test_object_store import ObjectStoreTests, PackBasedObjectStoreTests
  50. from dulwich.tests.utils import build_pack, make_object
  51. from . import TestCase
  52. testobject = make_object(Blob, data=b"yummy data")
  53. class OverlayObjectStoreTests(ObjectStoreTests, TestCase):
  54. def setUp(self):
  55. TestCase.setUp(self)
  56. self.bases = [MemoryObjectStore(), MemoryObjectStore()]
  57. self.store = OverlayObjectStore(self.bases, self.bases[0])
  58. class MemoryObjectStoreTests(ObjectStoreTests, TestCase):
  59. def setUp(self):
  60. TestCase.setUp(self)
  61. self.store = MemoryObjectStore()
  62. def test_add_pack(self):
  63. o = MemoryObjectStore()
  64. f, commit, abort = o.add_pack()
  65. try:
  66. b = make_object(Blob, data=b"more yummy data")
  67. write_pack_objects(f.write, [(b, None)])
  68. except BaseException:
  69. abort()
  70. raise
  71. else:
  72. commit()
  73. def test_add_pack_emtpy(self):
  74. o = MemoryObjectStore()
  75. f, commit, abort = o.add_pack()
  76. commit()
  77. def test_add_thin_pack(self):
  78. o = MemoryObjectStore()
  79. blob = make_object(Blob, data=b"yummy data")
  80. o.add_object(blob)
  81. f = BytesIO()
  82. entries = build_pack(
  83. f,
  84. [
  85. (REF_DELTA, (blob.id, b"more yummy data")),
  86. ],
  87. store=o,
  88. )
  89. o.add_thin_pack(f.read, None)
  90. packed_blob_sha = sha_to_hex(entries[0][3])
  91. self.assertEqual(
  92. (Blob.type_num, b"more yummy data"), o.get_raw(packed_blob_sha)
  93. )
  94. def test_add_thin_pack_empty(self):
  95. o = MemoryObjectStore()
  96. f = BytesIO()
  97. entries = build_pack(f, [], store=o)
  98. self.assertEqual([], entries)
  99. o.add_thin_pack(f.read, None)
  100. class DiskObjectStoreTests(PackBasedObjectStoreTests, TestCase):
  101. def setUp(self):
  102. TestCase.setUp(self)
  103. self.store_dir = tempfile.mkdtemp()
  104. self.addCleanup(shutil.rmtree, self.store_dir)
  105. self.store = DiskObjectStore.init(self.store_dir)
  106. def tearDown(self):
  107. TestCase.tearDown(self)
  108. PackBasedObjectStoreTests.tearDown(self)
  109. def test_loose_compression_level(self):
  110. alternate_dir = tempfile.mkdtemp()
  111. self.addCleanup(shutil.rmtree, alternate_dir)
  112. alternate_store = DiskObjectStore(alternate_dir, loose_compression_level=6)
  113. b2 = make_object(Blob, data=b"yummy data")
  114. alternate_store.add_object(b2)
  115. def test_alternates(self):
  116. alternate_dir = tempfile.mkdtemp()
  117. self.addCleanup(shutil.rmtree, alternate_dir)
  118. alternate_store = DiskObjectStore(alternate_dir)
  119. b2 = make_object(Blob, data=b"yummy data")
  120. alternate_store.add_object(b2)
  121. store = DiskObjectStore(self.store_dir)
  122. self.assertRaises(KeyError, store.__getitem__, b2.id)
  123. store.add_alternate_path(alternate_dir)
  124. self.assertIn(b2.id, store)
  125. self.assertEqual(b2, store[b2.id])
  126. def test_read_alternate_paths(self):
  127. store = DiskObjectStore(self.store_dir)
  128. abs_path = os.path.abspath(os.path.normpath("/abspath"))
  129. # ensures in particular existence of the alternates file
  130. store.add_alternate_path(abs_path)
  131. self.assertEqual(set(store._read_alternate_paths()), {abs_path})
  132. store.add_alternate_path("relative-path")
  133. self.assertIn(
  134. os.path.join(store.path, "relative-path"),
  135. set(store._read_alternate_paths()),
  136. )
  137. # arguably, add_alternate_path() could strip comments.
  138. # Meanwhile it's more convenient to use it than to import INFODIR
  139. store.add_alternate_path("# comment")
  140. for alt_path in store._read_alternate_paths():
  141. self.assertNotIn("#", alt_path)
  142. def test_file_modes(self):
  143. self.store.add_object(testobject)
  144. path = self.store._get_shafile_path(testobject.id)
  145. mode = os.stat(path).st_mode
  146. packmode = "0o100444" if sys.platform != "win32" else "0o100666"
  147. self.assertEqual(oct(mode), packmode)
  148. def test_corrupted_object_raise_exception(self):
  149. """Corrupted sha1 disk file should raise specific exception."""
  150. self.store.add_object(testobject)
  151. self.assertEqual(
  152. (Blob.type_num, b"yummy data"), self.store.get_raw(testobject.id)
  153. )
  154. self.assertTrue(self.store.contains_loose(testobject.id))
  155. self.assertIsNotNone(self.store._get_loose_object(testobject.id))
  156. path = self.store._get_shafile_path(testobject.id)
  157. old_mode = os.stat(path).st_mode
  158. os.chmod(path, 0o600)
  159. with open(path, "wb") as f: # corrupt the file
  160. f.write(b"")
  161. os.chmod(path, old_mode)
  162. expected_error_msg = "Corrupted empty file detected"
  163. try:
  164. self.store.contains_loose(testobject.id)
  165. except EmptyFileException as e:
  166. self.assertEqual(str(e), expected_error_msg)
  167. try:
  168. self.store._get_loose_object(testobject.id)
  169. except EmptyFileException as e:
  170. self.assertEqual(str(e), expected_error_msg)
  171. # this does not change iteration on loose objects though
  172. self.assertEqual([testobject.id], list(self.store._iter_loose_objects()))
  173. def test_tempfile_in_loose_store(self):
  174. self.store.add_object(testobject)
  175. self.assertEqual([testobject.id], list(self.store._iter_loose_objects()))
  176. # add temporary files to the loose store
  177. for i in range(256):
  178. dirname = os.path.join(self.store_dir, f"{i:02x}")
  179. if not os.path.isdir(dirname):
  180. os.makedirs(dirname)
  181. fd, n = tempfile.mkstemp(prefix="tmp_obj_", dir=dirname)
  182. os.close(fd)
  183. self.assertEqual([testobject.id], list(self.store._iter_loose_objects()))
  184. def test_add_alternate_path(self):
  185. store = DiskObjectStore(self.store_dir)
  186. self.assertEqual([], list(store._read_alternate_paths()))
  187. store.add_alternate_path("/foo/path")
  188. self.assertEqual(["/foo/path"], list(store._read_alternate_paths()))
  189. store.add_alternate_path("/bar/path")
  190. self.assertEqual(
  191. ["/foo/path", "/bar/path"], list(store._read_alternate_paths())
  192. )
  193. def test_rel_alternative_path(self):
  194. alternate_dir = tempfile.mkdtemp()
  195. self.addCleanup(shutil.rmtree, alternate_dir)
  196. alternate_store = DiskObjectStore(alternate_dir)
  197. b2 = make_object(Blob, data=b"yummy data")
  198. alternate_store.add_object(b2)
  199. store = DiskObjectStore(self.store_dir)
  200. self.assertRaises(KeyError, store.__getitem__, b2.id)
  201. store.add_alternate_path(os.path.relpath(alternate_dir, self.store_dir))
  202. self.assertEqual(list(alternate_store), list(store.alternates[0]))
  203. self.assertIn(b2.id, store)
  204. self.assertEqual(b2, store[b2.id])
  205. def test_pack_dir(self):
  206. o = DiskObjectStore(self.store_dir)
  207. self.assertEqual(os.path.join(self.store_dir, "pack"), o.pack_dir)
  208. def test_add_pack(self):
  209. o = DiskObjectStore(self.store_dir)
  210. self.addCleanup(o.close)
  211. f, commit, abort = o.add_pack()
  212. try:
  213. b = make_object(Blob, data=b"more yummy data")
  214. write_pack_objects(f.write, [(b, None)])
  215. except BaseException:
  216. abort()
  217. raise
  218. else:
  219. commit()
  220. def test_add_thin_pack(self):
  221. o = DiskObjectStore(self.store_dir)
  222. try:
  223. blob = make_object(Blob, data=b"yummy data")
  224. o.add_object(blob)
  225. f = BytesIO()
  226. entries = build_pack(
  227. f,
  228. [
  229. (REF_DELTA, (blob.id, b"more yummy data")),
  230. ],
  231. store=o,
  232. )
  233. with o.add_thin_pack(f.read, None) as pack:
  234. packed_blob_sha = sha_to_hex(entries[0][3])
  235. pack.check_length_and_checksum()
  236. self.assertEqual(sorted([blob.id, packed_blob_sha]), list(pack))
  237. self.assertTrue(o.contains_packed(packed_blob_sha))
  238. self.assertTrue(o.contains_packed(blob.id))
  239. self.assertEqual(
  240. (Blob.type_num, b"more yummy data"),
  241. o.get_raw(packed_blob_sha),
  242. )
  243. finally:
  244. o.close()
  245. def test_add_thin_pack_empty(self):
  246. with closing(DiskObjectStore(self.store_dir)) as o:
  247. f = BytesIO()
  248. entries = build_pack(f, [], store=o)
  249. self.assertEqual([], entries)
  250. o.add_thin_pack(f.read, None)
  251. class TreeLookupPathTests(TestCase):
  252. def setUp(self):
  253. TestCase.setUp(self)
  254. self.store = MemoryObjectStore()
  255. blob_a = make_object(Blob, data=b"a")
  256. blob_b = make_object(Blob, data=b"b")
  257. blob_c = make_object(Blob, data=b"c")
  258. for blob in [blob_a, blob_b, blob_c]:
  259. self.store.add_object(blob)
  260. blobs = [
  261. (b"a", blob_a.id, 0o100644),
  262. (b"ad/b", blob_b.id, 0o100644),
  263. (b"ad/bd/c", blob_c.id, 0o100755),
  264. (b"ad/c", blob_c.id, 0o100644),
  265. (b"c", blob_c.id, 0o100644),
  266. (b"d", blob_c.id, S_IFGITLINK),
  267. ]
  268. self.tree_id = commit_tree(self.store, blobs)
  269. def get_object(self, sha):
  270. return self.store[sha]
  271. def test_lookup_blob(self):
  272. o_id = tree_lookup_path(self.get_object, self.tree_id, b"a")[1]
  273. self.assertIsInstance(self.store[o_id], Blob)
  274. def test_lookup_tree(self):
  275. o_id = tree_lookup_path(self.get_object, self.tree_id, b"ad")[1]
  276. self.assertIsInstance(self.store[o_id], Tree)
  277. o_id = tree_lookup_path(self.get_object, self.tree_id, b"ad/bd")[1]
  278. self.assertIsInstance(self.store[o_id], Tree)
  279. o_id = tree_lookup_path(self.get_object, self.tree_id, b"ad/bd/")[1]
  280. self.assertIsInstance(self.store[o_id], Tree)
  281. def test_lookup_submodule(self):
  282. tree_lookup_path(self.get_object, self.tree_id, b"d")[1]
  283. self.assertRaises(
  284. SubmoduleEncountered,
  285. tree_lookup_path,
  286. self.get_object,
  287. self.tree_id,
  288. b"d/a",
  289. )
  290. def test_lookup_nonexistent(self):
  291. self.assertRaises(
  292. KeyError, tree_lookup_path, self.get_object, self.tree_id, b"j"
  293. )
  294. def test_lookup_not_tree(self):
  295. self.assertRaises(
  296. NotTreeError,
  297. tree_lookup_path,
  298. self.get_object,
  299. self.tree_id,
  300. b"ad/b/j",
  301. )
  302. class ObjectStoreGraphWalkerTests(TestCase):
  303. def get_walker(self, heads, parent_map):
  304. new_parent_map = {
  305. k * 40: [(p * 40) for p in ps] for (k, ps) in parent_map.items()
  306. }
  307. return ObjectStoreGraphWalker(
  308. [x * 40 for x in heads], new_parent_map.__getitem__
  309. )
  310. def test_ack_invalid_value(self):
  311. gw = self.get_walker([], {})
  312. self.assertRaises(ValueError, gw.ack, "tooshort")
  313. def test_empty(self):
  314. gw = self.get_walker([], {})
  315. self.assertIs(None, next(gw))
  316. gw.ack(b"a" * 40)
  317. self.assertIs(None, next(gw))
  318. def test_descends(self):
  319. gw = self.get_walker([b"a"], {b"a": [b"b"], b"b": []})
  320. self.assertEqual(b"a" * 40, next(gw))
  321. self.assertEqual(b"b" * 40, next(gw))
  322. def test_present(self):
  323. gw = self.get_walker([b"a"], {b"a": [b"b"], b"b": []})
  324. gw.ack(b"a" * 40)
  325. self.assertIs(None, next(gw))
  326. def test_parent_present(self):
  327. gw = self.get_walker([b"a"], {b"a": [b"b"], b"b": []})
  328. self.assertEqual(b"a" * 40, next(gw))
  329. gw.ack(b"a" * 40)
  330. self.assertIs(None, next(gw))
  331. def test_child_ack_later(self):
  332. gw = self.get_walker([b"a"], {b"a": [b"b"], b"b": [b"c"], b"c": []})
  333. self.assertEqual(b"a" * 40, next(gw))
  334. self.assertEqual(b"b" * 40, next(gw))
  335. gw.ack(b"a" * 40)
  336. self.assertIs(None, next(gw))
  337. def test_only_once(self):
  338. # a b
  339. # | |
  340. # c d
  341. # \ /
  342. # e
  343. gw = self.get_walker(
  344. [b"a", b"b"],
  345. {
  346. b"a": [b"c"],
  347. b"b": [b"d"],
  348. b"c": [b"e"],
  349. b"d": [b"e"],
  350. b"e": [],
  351. },
  352. )
  353. walk = []
  354. acked = False
  355. walk.append(next(gw))
  356. walk.append(next(gw))
  357. # A branch (a, c) or (b, d) may be done after 2 steps or 3 depending on
  358. # the order walked: 3-step walks include (a, b, c) and (b, a, d), etc.
  359. if walk == [b"a" * 40, b"c" * 40] or walk == [b"b" * 40, b"d" * 40]:
  360. gw.ack(walk[0])
  361. acked = True
  362. walk.append(next(gw))
  363. if not acked and walk[2] == b"c" * 40:
  364. gw.ack(b"a" * 40)
  365. elif not acked and walk[2] == b"d" * 40:
  366. gw.ack(b"b" * 40)
  367. walk.append(next(gw))
  368. self.assertIs(None, next(gw))
  369. self.assertEqual([b"a" * 40, b"b" * 40, b"c" * 40, b"d" * 40], sorted(walk))
  370. self.assertLess(walk.index(b"a" * 40), walk.index(b"c" * 40))
  371. self.assertLess(walk.index(b"b" * 40), walk.index(b"d" * 40))
  372. class CommitTreeChangesTests(TestCase):
  373. def setUp(self):
  374. super().setUp()
  375. self.store = MemoryObjectStore()
  376. self.blob_a = make_object(Blob, data=b"a")
  377. self.blob_b = make_object(Blob, data=b"b")
  378. self.blob_c = make_object(Blob, data=b"c")
  379. for blob in [self.blob_a, self.blob_b, self.blob_c]:
  380. self.store.add_object(blob)
  381. blobs = [
  382. (b"a", self.blob_a.id, 0o100644),
  383. (b"ad/b", self.blob_b.id, 0o100644),
  384. (b"ad/bd/c", self.blob_c.id, 0o100755),
  385. (b"ad/c", self.blob_c.id, 0o100644),
  386. (b"c", self.blob_c.id, 0o100644),
  387. ]
  388. self.tree_id = commit_tree(self.store, blobs)
  389. def test_no_changes(self):
  390. self.assertEqual(
  391. self.store[self.tree_id],
  392. commit_tree_changes(self.store, self.store[self.tree_id], []),
  393. )
  394. def test_add_blob(self):
  395. blob_d = make_object(Blob, data=b"d")
  396. new_tree = commit_tree_changes(
  397. self.store, self.store[self.tree_id], [(b"d", 0o100644, blob_d.id)]
  398. )
  399. self.assertEqual(
  400. new_tree[b"d"],
  401. (33188, b"c59d9b6344f1af00e504ba698129f07a34bbed8d"),
  402. )
  403. def test_add_blob_in_dir(self):
  404. blob_d = make_object(Blob, data=b"d")
  405. new_tree = commit_tree_changes(
  406. self.store,
  407. self.store[self.tree_id],
  408. [(b"e/f/d", 0o100644, blob_d.id)],
  409. )
  410. self.assertEqual(
  411. new_tree.items(),
  412. [
  413. TreeEntry(path=b"a", mode=stat.S_IFREG | 0o100644, sha=self.blob_a.id),
  414. TreeEntry(
  415. path=b"ad",
  416. mode=stat.S_IFDIR,
  417. sha=b"0e2ce2cd7725ff4817791be31ccd6e627e801f4a",
  418. ),
  419. TreeEntry(path=b"c", mode=stat.S_IFREG | 0o100644, sha=self.blob_c.id),
  420. TreeEntry(
  421. path=b"e",
  422. mode=stat.S_IFDIR,
  423. sha=b"6ab344e288724ac2fb38704728b8896e367ed108",
  424. ),
  425. ],
  426. )
  427. e_tree = self.store[new_tree[b"e"][1]]
  428. self.assertEqual(
  429. e_tree.items(),
  430. [
  431. TreeEntry(
  432. path=b"f",
  433. mode=stat.S_IFDIR,
  434. sha=b"24d2c94d8af232b15a0978c006bf61ef4479a0a5",
  435. )
  436. ],
  437. )
  438. f_tree = self.store[e_tree[b"f"][1]]
  439. self.assertEqual(
  440. f_tree.items(),
  441. [TreeEntry(path=b"d", mode=stat.S_IFREG | 0o100644, sha=blob_d.id)],
  442. )
  443. def test_delete_blob(self):
  444. new_tree = commit_tree_changes(
  445. self.store, self.store[self.tree_id], [(b"ad/bd/c", None, None)]
  446. )
  447. self.assertEqual(set(new_tree), {b"a", b"ad", b"c"})
  448. ad_tree = self.store[new_tree[b"ad"][1]]
  449. self.assertEqual(set(ad_tree), {b"b", b"c"})
  450. class TestReadPacksFile(TestCase):
  451. def test_read_packs(self):
  452. self.assertEqual(
  453. ["pack-1.pack"],
  454. list(
  455. read_packs_file(
  456. BytesIO(
  457. b"""P pack-1.pack
  458. """
  459. )
  460. )
  461. ),
  462. )