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