test_missing_obj_finder.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. # test_missing_obj_finder.py -- tests for MissingObjectFinder
  2. # Copyright (C) 2012 syntevo GmbH
  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. from dulwich.object_store import (
  21. MemoryObjectStore,
  22. )
  23. from dulwich.objects import (
  24. Blob,
  25. )
  26. from dulwich.tests import TestCase
  27. from dulwich.tests.utils import (
  28. make_object,
  29. make_tag,
  30. build_commit_graph,
  31. )
  32. class MissingObjectFinderTest(TestCase):
  33. def setUp(self):
  34. super(MissingObjectFinderTest, self).setUp()
  35. self.store = MemoryObjectStore()
  36. self.commits = []
  37. def cmt(self, n):
  38. return self.commits[n-1]
  39. def assertMissingMatch(self, haves, wants, expected):
  40. for sha, path in self.store.find_missing_objects(haves, wants):
  41. self.assertTrue(
  42. sha in expected,
  43. "(%s,%s) erroneously reported as missing" % (sha, path))
  44. expected.remove(sha)
  45. self.assertEqual(
  46. len(expected), 0,
  47. "some objects are not reported as missing: %s" % (expected, ))
  48. class MOFLinearRepoTest(MissingObjectFinderTest):
  49. def setUp(self):
  50. super(MOFLinearRepoTest, self).setUp()
  51. # present in 1, removed in 3
  52. f1_1 = make_object(Blob, data=b'f1')
  53. # present in all revisions, changed in 2 and 3
  54. f2_1 = make_object(Blob, data=b'f2')
  55. f2_2 = make_object(Blob, data=b'f2-changed')
  56. f2_3 = make_object(Blob, data=b'f2-changed-again')
  57. # added in 2, left unmodified in 3
  58. f3_2 = make_object(Blob, data=b'f3')
  59. commit_spec = [[1], [2, 1], [3, 2]]
  60. trees = {1: [(b'f1', f1_1), (b'f2', f2_1)],
  61. 2: [(b'f1', f1_1), (b'f2', f2_2), (b'f3', f3_2)],
  62. 3: [(b'f2', f2_3), (b'f3', f3_2)]}
  63. # commit 1: f1 and f2
  64. # commit 2: f3 added, f2 changed. Missing shall report commit id and a
  65. # tree referenced by commit
  66. # commit 3: f1 removed, f2 changed. Commit sha and root tree sha shall
  67. # be reported as modified
  68. self.commits = build_commit_graph(self.store, commit_spec, trees)
  69. self.missing_1_2 = [self.cmt(2).id, self.cmt(2).tree, f2_2.id, f3_2.id]
  70. self.missing_2_3 = [self.cmt(3).id, self.cmt(3).tree, f2_3.id]
  71. self.missing_1_3 = [
  72. self.cmt(2).id, self.cmt(3).id,
  73. self.cmt(2).tree, self.cmt(3).tree,
  74. f2_2.id, f3_2.id, f2_3.id]
  75. def test_1_to_2(self):
  76. self.assertMissingMatch(
  77. [self.cmt(1).id], [self.cmt(2).id],
  78. self.missing_1_2)
  79. def test_2_to_3(self):
  80. self.assertMissingMatch(
  81. [self.cmt(2).id], [self.cmt(3).id],
  82. self.missing_2_3)
  83. def test_1_to_3(self):
  84. self.assertMissingMatch(
  85. [self.cmt(1).id], [self.cmt(3).id],
  86. self.missing_1_3)
  87. def test_bogus_haves(self):
  88. """Ensure non-existent SHA in haves are tolerated"""
  89. bogus_sha = self.cmt(2).id[::-1]
  90. haves = [self.cmt(1).id, bogus_sha]
  91. wants = [self.cmt(3).id]
  92. self.assertMissingMatch(haves, wants, self.missing_1_3)
  93. def test_bogus_wants_failure(self):
  94. """Ensure non-existent SHA in wants are not tolerated"""
  95. bogus_sha = self.cmt(2).id[::-1]
  96. haves = [self.cmt(1).id]
  97. wants = [self.cmt(3).id, bogus_sha]
  98. self.assertRaises(
  99. KeyError, self.store.find_missing_objects, haves, wants)
  100. def test_no_changes(self):
  101. self.assertMissingMatch([self.cmt(3).id], [self.cmt(3).id], [])
  102. class MOFMergeForkRepoTest(MissingObjectFinderTest):
  103. # 1 --- 2 --- 4 --- 6 --- 7
  104. # \ /
  105. # 3 ---
  106. # \
  107. # 5
  108. def setUp(self):
  109. super(MOFMergeForkRepoTest, self).setUp()
  110. f1_1 = make_object(Blob, data=b'f1')
  111. f1_2 = make_object(Blob, data=b'f1-2')
  112. f1_4 = make_object(Blob, data=b'f1-4')
  113. f1_7 = make_object(Blob, data=b'f1-2') # same data as in rev 2
  114. f2_1 = make_object(Blob, data=b'f2')
  115. f2_3 = make_object(Blob, data=b'f2-3')
  116. f3_3 = make_object(Blob, data=b'f3')
  117. f3_5 = make_object(Blob, data=b'f3-5')
  118. commit_spec = [[1], [2, 1], [3, 2], [4, 2], [5, 3], [6, 3, 4], [7, 6]]
  119. trees = {1: [(b'f1', f1_1), (b'f2', f2_1)],
  120. 2: [(b'f1', f1_2), (b'f2', f2_1)], # f1 changed
  121. # f3 added, f2 changed
  122. 3: [(b'f1', f1_2), (b'f2', f2_3), (b'f3', f3_3)],
  123. 4: [(b'f1', f1_4), (b'f2', f2_1)], # f1 changed
  124. 5: [(b'f1', f1_2), (b'f3', f3_5)], # f2 removed, f3 changed
  125. # merged 3 and 4
  126. 6: [(b'f1', f1_4), (b'f2', f2_3), (b'f3', f3_3)],
  127. # f1 changed to match rev2. f3 removed
  128. 7: [(b'f1', f1_7), (b'f2', f2_3)]}
  129. self.commits = build_commit_graph(self.store, commit_spec, trees)
  130. self.f1_2_id = f1_2.id
  131. self.f1_4_id = f1_4.id
  132. self.f1_7_id = f1_7.id
  133. self.f2_3_id = f2_3.id
  134. self.f3_3_id = f3_3.id
  135. self.assertEqual(f1_2.id, f1_7.id, "[sanity]")
  136. def test_have6_want7(self):
  137. # have 6, want 7. Ideally, shall not report f1_7 as it's the same as
  138. # f1_2, however, to do so, MissingObjectFinder shall not record trees
  139. # of common commits only, but also all parent trees and tree items,
  140. # which is an overkill (i.e. in sha_done it records f1_4 as known, and
  141. # doesn't record f1_2 was known prior to that, hence can't detect f1_7
  142. # is in fact f1_2 and shall not be reported)
  143. self.assertMissingMatch(
  144. [self.cmt(6).id], [self.cmt(7).id],
  145. [self.cmt(7).id, self.cmt(7).tree, self.f1_7_id])
  146. def test_have4_want7(self):
  147. # have 4, want 7. Shall not include rev5 as it is not in the tree
  148. # between 4 and 7 (well, it is, but its SHA's are irrelevant for 4..7
  149. # commit hierarchy)
  150. self.assertMissingMatch([self.cmt(4).id], [self.cmt(7).id], [
  151. self.cmt(7).id, self.cmt(6).id, self.cmt(3).id,
  152. self.cmt(7).tree, self.cmt(6).tree, self.cmt(3).tree,
  153. self.f2_3_id, self.f3_3_id])
  154. def test_have1_want6(self):
  155. # have 1, want 6. Shall not include rev5
  156. self.assertMissingMatch([self.cmt(1).id], [self.cmt(6).id], [
  157. self.cmt(6).id, self.cmt(4).id, self.cmt(3).id, self.cmt(2).id,
  158. self.cmt(6).tree, self.cmt(4).tree, self.cmt(3).tree,
  159. self.cmt(2).tree, self.f1_2_id, self.f1_4_id, self.f2_3_id,
  160. self.f3_3_id])
  161. def test_have3_want6(self):
  162. # have 3, want 7. Shall not report rev2 and its tree, because
  163. # haves(3) means has parents, i.e. rev2, too
  164. # BUT shall report any changes descending rev2 (excluding rev3)
  165. # Shall NOT report f1_7 as it's techically == f1_2
  166. self.assertMissingMatch([self.cmt(3).id], [self.cmt(7).id], [
  167. self.cmt(7).id, self.cmt(6).id, self.cmt(4).id,
  168. self.cmt(7).tree, self.cmt(6).tree, self.cmt(4).tree,
  169. self.f1_4_id])
  170. def test_have5_want7(self):
  171. # have 5, want 7. Common parent is rev2, hence children of rev2 from
  172. # a descent line other than rev5 shall be reported
  173. # expects f1_4 from rev6. f3_5 is known in rev5;
  174. # f1_7 shall be the same as f1_2 (known, too)
  175. self.assertMissingMatch([self.cmt(5).id], [self.cmt(7).id], [
  176. self.cmt(7).id, self.cmt(6).id, self.cmt(4).id,
  177. self.cmt(7).tree, self.cmt(6).tree, self.cmt(4).tree,
  178. self.f1_4_id])
  179. class MOFTagsTest(MissingObjectFinderTest):
  180. def setUp(self):
  181. super(MOFTagsTest, self).setUp()
  182. f1_1 = make_object(Blob, data=b'f1')
  183. commit_spec = [[1]]
  184. trees = {1: [(b'f1', f1_1)]}
  185. self.commits = build_commit_graph(self.store, commit_spec, trees)
  186. self._normal_tag = make_tag(self.cmt(1))
  187. self.store.add_object(self._normal_tag)
  188. self._tag_of_tag = make_tag(self._normal_tag)
  189. self.store.add_object(self._tag_of_tag)
  190. self._tag_of_tree = make_tag(self.store[self.cmt(1).tree])
  191. self.store.add_object(self._tag_of_tree)
  192. self._tag_of_blob = make_tag(f1_1)
  193. self.store.add_object(self._tag_of_blob)
  194. self._tag_of_tag_of_blob = make_tag(self._tag_of_blob)
  195. self.store.add_object(self._tag_of_tag_of_blob)
  196. self.f1_1_id = f1_1.id
  197. def test_tagged_commit(self):
  198. # The user already has the tagged commit, all they want is the tag,
  199. # so send them only the tag object.
  200. self.assertMissingMatch([self.cmt(1).id], [self._normal_tag.id],
  201. [self._normal_tag.id])
  202. # The remaining cases are unusual, but do happen in the wild.
  203. def test_tagged_tag(self):
  204. # User already has tagged tag, send only tag of tag
  205. self.assertMissingMatch([self._normal_tag.id], [self._tag_of_tag.id],
  206. [self._tag_of_tag.id])
  207. # User needs both tags, but already has commit
  208. self.assertMissingMatch([self.cmt(1).id], [self._tag_of_tag.id],
  209. [self._normal_tag.id, self._tag_of_tag.id])
  210. def test_tagged_tree(self):
  211. self.assertMissingMatch(
  212. [], [self._tag_of_tree.id],
  213. [self._tag_of_tree.id, self.cmt(1).tree, self.f1_1_id])
  214. def test_tagged_blob(self):
  215. self.assertMissingMatch([], [self._tag_of_blob.id],
  216. [self._tag_of_blob.id, self.f1_1_id])
  217. def test_tagged_tagged_blob(self):
  218. self.assertMissingMatch([], [self._tag_of_tag_of_blob.id],
  219. [self._tag_of_tag_of_blob.id,
  220. self._tag_of_blob.id, self.f1_1_id])