test_missing_obj_finder.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. # test_missing_obj_finder.py -- tests for MissingObjectFinder
  2. # Copyright (C) 2012 syntevo GmbH
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; version 2
  7. # or (at your option) any later version of the License.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  17. # MA 02110-1301, USA.
  18. from dulwich.object_store import (
  19. MemoryObjectStore,
  20. )
  21. from dulwich.objects import (
  22. Blob,
  23. )
  24. from dulwich.tests import TestCase
  25. from dulwich.tests.utils import (
  26. make_object,
  27. build_commit_graph,
  28. skipIfPY3,
  29. )
  30. @skipIfPY3
  31. class MissingObjectFinderTest(TestCase):
  32. def setUp(self):
  33. super(MissingObjectFinderTest, self).setUp()
  34. self.store = MemoryObjectStore()
  35. self.commits = []
  36. def cmt(self, n):
  37. return self.commits[n-1]
  38. def assertMissingMatch(self, haves, wants, expected):
  39. for sha, path in self.store.find_missing_objects(haves, wants):
  40. self.assertTrue(sha in expected,
  41. "(%s,%s) erroneously reported as missing" % (sha, path))
  42. expected.remove(sha)
  43. self.assertEqual(len(expected), 0,
  44. "some objects are not reported as missing: %s" % (expected, ))
  45. @skipIfPY3
  46. class MOFLinearRepoTest(MissingObjectFinderTest):
  47. def setUp(self):
  48. super(MOFLinearRepoTest, self).setUp()
  49. f1_1 = make_object(Blob, data='f1') # present in 1, removed in 3
  50. f2_1 = make_object(Blob, data='f2') # present in all revisions, changed in 2 and 3
  51. f2_2 = make_object(Blob, data='f2-changed')
  52. f2_3 = make_object(Blob, data='f2-changed-again')
  53. f3_2 = make_object(Blob, data='f3') # added in 2, left unmodified in 3
  54. commit_spec = [[1], [2, 1], [3, 2]]
  55. trees = {1: [('f1', f1_1), ('f2', f2_1)],
  56. 2: [('f1', f1_1), ('f2', f2_2), ('f3', f3_2)],
  57. 3: [('f2', f2_3), ('f3', f3_2)] }
  58. # commit 1: f1 and f2
  59. # commit 2: f3 added, f2 changed. Missing shall report commit id and a
  60. # tree referenced by commit
  61. # commit 3: f1 removed, f2 changed. Commit sha and root tree sha shall
  62. # be reported as modified
  63. self.commits = build_commit_graph(self.store, commit_spec, trees)
  64. self.missing_1_2 = [self.cmt(2).id, self.cmt(2).tree, f2_2.id, f3_2.id]
  65. self.missing_2_3 = [self.cmt(3).id, self.cmt(3).tree, f2_3.id]
  66. self.missing_1_3 = [
  67. self.cmt(2).id, self.cmt(3).id,
  68. self.cmt(2).tree, self.cmt(3).tree,
  69. f2_2.id, f3_2.id, f2_3.id]
  70. def test_1_to_2(self):
  71. self.assertMissingMatch([self.cmt(1).id], [self.cmt(2).id],
  72. self.missing_1_2)
  73. def test_2_to_3(self):
  74. self.assertMissingMatch([self.cmt(2).id], [self.cmt(3).id],
  75. self.missing_2_3)
  76. def test_1_to_3(self):
  77. self.assertMissingMatch([self.cmt(1).id], [self.cmt(3).id],
  78. self.missing_1_3)
  79. def test_bogus_haves_failure(self):
  80. """Ensure non-existent SHA in haves are not tolerated"""
  81. bogus_sha = self.cmt(2).id[::-1]
  82. haves = [self.cmt(1).id, bogus_sha]
  83. wants = [self.cmt(3).id]
  84. self.assertRaises(KeyError, self.store.find_missing_objects,
  85. self.store, haves, wants)
  86. def test_bogus_wants_failure(self):
  87. """Ensure non-existent SHA in wants are not tolerated"""
  88. bogus_sha = self.cmt(2).id[::-1]
  89. haves = [self.cmt(1).id]
  90. wants = [self.cmt(3).id, bogus_sha]
  91. self.assertRaises(KeyError, self.store.find_missing_objects,
  92. self.store, haves, wants)
  93. def test_no_changes(self):
  94. self.assertMissingMatch([self.cmt(3).id], [self.cmt(3).id], [])
  95. @skipIfPY3
  96. class MOFMergeForkRepoTest(MissingObjectFinderTest):
  97. # 1 --- 2 --- 4 --- 6 --- 7
  98. # \ /
  99. # 3 ---
  100. # \
  101. # 5
  102. def setUp(self):
  103. super(MOFMergeForkRepoTest, self).setUp()
  104. f1_1 = make_object(Blob, data='f1')
  105. f1_2 = make_object(Blob, data='f1-2')
  106. f1_4 = make_object(Blob, data='f1-4')
  107. f1_7 = make_object(Blob, data='f1-2') # same data as in rev 2
  108. f2_1 = make_object(Blob, data='f2')
  109. f2_3 = make_object(Blob, data='f2-3')
  110. f3_3 = make_object(Blob, data='f3')
  111. f3_5 = make_object(Blob, data='f3-5')
  112. commit_spec = [[1], [2, 1], [3, 2], [4, 2], [5, 3], [6, 3, 4], [7, 6]]
  113. trees = {1: [('f1', f1_1), ('f2', f2_1)],
  114. 2: [('f1', f1_2), ('f2', f2_1)], # f1 changed
  115. # f3 added, f2 changed
  116. 3: [('f1', f1_2), ('f2', f2_3), ('f3', f3_3)],
  117. 4: [('f1', f1_4), ('f2', f2_1)], # f1 changed
  118. 5: [('f1', f1_2), ('f3', f3_5)], # f2 removed, f3 changed
  119. 6: [('f1', f1_4), ('f2', f2_3), ('f3', f3_3)], # merged 3 and 4
  120. # f1 changed to match rev2. f3 removed
  121. 7: [('f1', f1_7), ('f2', f2_3)]}
  122. self.commits = build_commit_graph(self.store, commit_spec, trees)
  123. self.f1_2_id = f1_2.id
  124. self.f1_4_id = f1_4.id
  125. self.f1_7_id = f1_7.id
  126. self.f2_3_id = f2_3.id
  127. self.f3_3_id = f3_3.id
  128. self.assertEqual(f1_2.id, f1_7.id, "[sanity]")
  129. def test_have6_want7(self):
  130. # have 6, want 7. Ideally, shall not report f1_7 as it's the same as
  131. # f1_2, however, to do so, MissingObjectFinder shall not record trees
  132. # of common commits only, but also all parent trees and tree items,
  133. # which is an overkill (i.e. in sha_done it records f1_4 as known, and
  134. # doesn't record f1_2 was known prior to that, hence can't detect f1_7
  135. # is in fact f1_2 and shall not be reported)
  136. self.assertMissingMatch([self.cmt(6).id], [self.cmt(7).id],
  137. [self.cmt(7).id, self.cmt(7).tree, self.f1_7_id])
  138. def test_have4_want7(self):
  139. # have 4, want 7. Shall not include rev5 as it is not in the tree
  140. # between 4 and 7 (well, it is, but its SHA's are irrelevant for 4..7
  141. # commit hierarchy)
  142. self.assertMissingMatch([self.cmt(4).id], [self.cmt(7).id], [
  143. self.cmt(7).id, self.cmt(6).id, self.cmt(3).id,
  144. self.cmt(7).tree, self.cmt(6).tree, self.cmt(3).tree,
  145. self.f2_3_id, self.f3_3_id])
  146. def test_have1_want6(self):
  147. # have 1, want 6. Shall not include rev5
  148. self.assertMissingMatch([self.cmt(1).id], [self.cmt(6).id], [
  149. self.cmt(6).id, self.cmt(4).id, self.cmt(3).id, self.cmt(2).id,
  150. self.cmt(6).tree, self.cmt(4).tree, self.cmt(3).tree,
  151. self.cmt(2).tree, self.f1_2_id, self.f1_4_id, self.f2_3_id,
  152. self.f3_3_id])
  153. def test_have3_want6(self):
  154. # have 3, want 7. Shall not report rev2 and its tree, because
  155. # haves(3) means has parents, i.e. rev2, too
  156. # BUT shall report any changes descending rev2 (excluding rev3)
  157. # Shall NOT report f1_7 as it's techically == f1_2
  158. self.assertMissingMatch([self.cmt(3).id], [self.cmt(7).id], [
  159. self.cmt(7).id, self.cmt(6).id, self.cmt(4).id,
  160. self.cmt(7).tree, self.cmt(6).tree, self.cmt(4).tree,
  161. self.f1_4_id])
  162. def test_have5_want7(self):
  163. # have 5, want 7. Common parent is rev2, hence children of rev2 from
  164. # a descent line other than rev5 shall be reported
  165. # expects f1_4 from rev6. f3_5 is known in rev5;
  166. # f1_7 shall be the same as f1_2 (known, too)
  167. self.assertMissingMatch([self.cmt(5).id], [self.cmt(7).id], [
  168. self.cmt(7).id, self.cmt(6).id, self.cmt(4).id,
  169. self.cmt(7).tree, self.cmt(6).tree, self.cmt(4).tree,
  170. self.f1_4_id])