2
0

test_object_store.py 19 KB

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