test_missing_obj_finder.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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.errors import (
  19. MissingCommitError,
  20. )
  21. from dulwich.object_store import (
  22. MemoryObjectStore,
  23. )
  24. from dulwich.objects import (
  25. Commit,
  26. Blob,
  27. )
  28. from dulwich.tests import TestCase
  29. from utils import (
  30. F,
  31. make_object,
  32. build_commit_graph,
  33. )
  34. class MissingObjectFinderTest(TestCase):
  35. def setUp(self):
  36. super(MissingObjectFinderTest, self).setUp()
  37. self.store = MemoryObjectStore()
  38. self.commits = []
  39. def __getitem__(self, n):
  40. # rename for brevity
  41. return self.commits[n-1]
  42. def cmt(self, n):
  43. return self[n]
  44. def assertMissingMatch(self, haves, wants, expected):
  45. for sha,path in self.store.find_missing_objects(haves, wants):
  46. self.assertTrue(sha in expected, "FAILURE: (%s,%s) erroneously reported as missing" % (sha,path))
  47. expected.remove(sha)
  48. self.assertFalse(len(expected) > 0, "FAILURE: some objects are not reported as missing: %s" % (expected))
  49. class MOFLinearRepoTest(MissingObjectFinderTest):
  50. def setUp(self):
  51. super(MOFLinearRepoTest, self).setUp()
  52. f1_1 = make_object(Blob, data='f1') # present in 1, removed in 3
  53. f2_1 = make_object(Blob, data='f2') # present in all revisions, changed in 2 and 3
  54. f2_2 = make_object(Blob, data='f2-changed')
  55. f2_3 = make_object(Blob, data='f2-changed-again')
  56. f3_2 = make_object(Blob, data='f3') # added in 2, left unmodified in 3
  57. commit_spec = [[1], [2,1], [3,2]]
  58. trees = {1: [('f1', f1_1), ('f2',f2_1)],
  59. 2: [('f1',f1_1), ('f2', f2_2), ('f3', f3_2)],
  60. 3: [('f2', f2_3), ('f3', f3_2)] }
  61. # commit 1: f1 and f2
  62. # commit 2: f3 added, f2 changed. Missing shall report commit id and a tree referenced by commit
  63. # commit 3: f1 removed, f2 changed. Commit sha and root tree sha shall be reported as modified
  64. self.commits = build_commit_graph(self.store, commit_spec, trees)
  65. self.missing_1_2 = [self.cmt(2).id, self.cmt(2).tree, f2_2.id, f3_2.id]
  66. self.missing_2_3 = [self.cmt(3).id, self.cmt(3).tree, f2_3.id]
  67. self.missing_1_3 = [
  68. self.cmt(2).id, self.cmt(3).id,
  69. self.cmt(2).tree, self.cmt(3).tree,
  70. f2_2.id, f3_2.id, f2_3.id]
  71. def test_1_to_2(self):
  72. self.assertMissingMatch([self.cmt(1).id], [self.cmt(2).id], self.missing_1_2)
  73. def test_2_to_3(self):
  74. self.assertMissingMatch([self.cmt(2).id], [self.cmt(3).id], self.missing_2_3)
  75. def test_1_to_3(self):
  76. self.assertMissingMatch([self.cmt(1).id], [self.cmt(3).id], self.missing_1_3)
  77. def test_bogus_haves_failure(self):
  78. """Ensure non-existent SHA in haves are not tolerated"""
  79. bogus_sha = self.cmt(2).id[::-1]
  80. haves = [self.cmt(1).id, bogus_sha]
  81. wants = [self.cmt(3).id]
  82. self.assertRaises(KeyError, self.store.find_missing_objects, self.store, haves, wants)
  83. def test_bogus_wants_failure(self):
  84. """Ensure non-existent SHA in wants are not tolerated"""
  85. bogus_sha = self.cmt(2).id[::-1]
  86. haves = [self.cmt(1).id]
  87. wants = [self.cmt(3).id, bogus_sha]
  88. self.assertRaises(KeyError, self.store.find_missing_objects, self.store, haves, wants)
  89. def test_no_changes(self):
  90. self.assertMissingMatch([self.cmt(3).id], [self.cmt(3).id], [])
  91. class MOFMergeForkRepoTest(MissingObjectFinderTest):
  92. """ 1 --- 2 --- 4 --- 6 --- 7
  93. \ /
  94. 3 ---
  95. \
  96. 5
  97. """
  98. def setUp(self):
  99. super(MOFMergeForkRepoTest, self).setUp()
  100. f1_1 = make_object(Blob, data='f1')
  101. f1_2 = make_object(Blob, data='f1-2')
  102. f1_4 = make_object(Blob, data='f1-4')
  103. f1_7 = make_object(Blob, data='f1-2') # same data as in rev 2
  104. f2_1 = make_object(Blob, data='f2')
  105. f2_3 = make_object(Blob, data='f2-3')
  106. f3_3 = make_object(Blob, data='f3')
  107. f3_5 = make_object(Blob, data='f3-5')
  108. commit_spec = [[1], [2,1], [3,2], [4,2], [5,3], [6,3,4], [7,6]]
  109. trees = {1: [('f1', f1_1), ('f2',f2_1)],
  110. 2: [('f1',f1_2), ('f2', f2_1)], # f1 changed
  111. 3: [('f1',f1_2), ('f2', f2_3), ('f3', f3_3)], # f3 added, f2 changed
  112. 4: [('f1',f1_4), ('f2',f2_1)], # f1 changed
  113. 5: [('f1',f1_2), ('f3', f3_5)], # f2 removed, f3 changed
  114. 6: [('f1',f1_4), ('f2',f2_3), ('f3', f3_3)], # merged 3 and 4
  115. 7: [('f1',f1_7), ('f2',f2_3)]} # f1 changed to match rev2. f3 removed
  116. self.commits = build_commit_graph(self.store, commit_spec, trees)
  117. self.f1_2_id = f1_2.id
  118. self.f1_4_id = f1_4.id
  119. self.f1_7_id = f1_7.id
  120. self.f2_3_id = f2_3.id
  121. self.f3_3_id = f3_3.id
  122. self.assertEquals(f1_2.id, f1_7.id, "[sanity]")
  123. def test_have6_want7(self):
  124. """
  125. have 6, want 7. Ideally, shall not report f1_7 as it's the same as f1_2,
  126. however, to do so, MissingObjectFinder shall not record trees of common commits
  127. only, but also all parent trees and tree items, which is an overkill
  128. (i.e. in sha_done it records f1_4 as known, and doesn't record f1_2 was known
  129. prior to that, hence can't detect f1_7 is in fact f1_2 and shall not be reported)
  130. """
  131. self.assertMissingMatch([self.cmt(6).id], [self.cmt(7).id], [self.cmt(7).id, self.cmt(7).tree, self.f1_7_id])
  132. def test_have4_want7(self):
  133. """
  134. have 4, want 7. Shall not include rev5 as it is not in the tree between 4 and 7
  135. (well, it is, but its SHA's are irrelevant for 4..7 commit hierarchy)
  136. """
  137. self.assertMissingMatch([self.cmt(4).id], [self.cmt(7).id], [
  138. self.cmt(7).id, self.cmt(6).id, self.cmt(3).id,
  139. self.cmt(7).tree, self.cmt(6).tree, self.cmt(3).tree,
  140. self.f2_3_id, self.f3_3_id])
  141. def test_have1_want6(self):
  142. """
  143. have 1, want 6. Shall not include rev5
  144. """
  145. self.assertMissingMatch([self.cmt(1).id], [self.cmt(6).id], [
  146. self.cmt(6).id, self.cmt(4).id, self.cmt(3).id, self.cmt(2).id,
  147. self.cmt(6).tree, self.cmt(4).tree, self.cmt(3).tree, self.cmt(2).tree,
  148. self.f1_2_id, self.f1_4_id, self.f2_3_id, self.f3_3_id])
  149. def test_have3_want6(self):
  150. """
  151. have 3, want 7. Shall not report rev2 and its tree, because
  152. haves(3) means has parents, i.e. rev2, too
  153. BUT shall report any changes descending rev2 (excluding rev3)
  154. Shall NOT report f1_7 as it's techically == f1_2
  155. """
  156. self.assertMissingMatch([self.cmt(3).id], [self.cmt(7).id], [
  157. self.cmt(7).id, self.cmt(6).id, self.cmt(4).id,
  158. self.cmt(7).tree, self.cmt(6).tree, self.cmt(4).tree,
  159. self.f1_4_id])
  160. def test_have5_want7(self):
  161. """
  162. have 5, want 7. Common parent is rev2, hence children of rev2 from
  163. a descent line other than rev5 shall be reported
  164. """
  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])