2
0

test_missing_obj_finder.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. "(%s,%s) erroneously reported as missing" % (sha, path))
  40. expected.remove(sha)
  41. self.assertEquals(len(expected), 0,
  42. "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
  57. # tree referenced by commit
  58. # commit 3: f1 removed, f2 changed. Commit sha and root tree sha shall
  59. # be reported as modified
  60. self.commits = build_commit_graph(self.store, commit_spec, trees)
  61. self.missing_1_2 = [self.cmt(2).id, self.cmt(2).tree, f2_2.id, f3_2.id]
  62. self.missing_2_3 = [self.cmt(3).id, self.cmt(3).tree, f2_3.id]
  63. self.missing_1_3 = [
  64. self.cmt(2).id, self.cmt(3).id,
  65. self.cmt(2).tree, self.cmt(3).tree,
  66. f2_2.id, f3_2.id, f2_3.id]
  67. def test_1_to_2(self):
  68. self.assertMissingMatch([self.cmt(1).id], [self.cmt(2).id],
  69. self.missing_1_2)
  70. def test_2_to_3(self):
  71. self.assertMissingMatch([self.cmt(2).id], [self.cmt(3).id],
  72. self.missing_2_3)
  73. def test_1_to_3(self):
  74. self.assertMissingMatch([self.cmt(1).id], [self.cmt(3).id],
  75. self.missing_1_3)
  76. def test_bogus_haves_failure(self):
  77. """Ensure non-existent SHA in haves are not tolerated"""
  78. bogus_sha = self.cmt(2).id[::-1]
  79. haves = [self.cmt(1).id, bogus_sha]
  80. wants = [self.cmt(3).id]
  81. self.assertRaises(KeyError, self.store.find_missing_objects,
  82. 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,
  89. self.store, haves, wants)
  90. def test_no_changes(self):
  91. self.assertMissingMatch([self.cmt(3).id], [self.cmt(3).id], [])
  92. class MOFMergeForkRepoTest(MissingObjectFinderTest):
  93. """ 1 --- 2 --- 4 --- 6 --- 7
  94. \ /
  95. 3 ---
  96. \
  97. 5
  98. """
  99. def setUp(self):
  100. super(MOFMergeForkRepoTest, self).setUp()
  101. f1_1 = make_object(Blob, data='f1')
  102. f1_2 = make_object(Blob, data='f1-2')
  103. f1_4 = make_object(Blob, data='f1-4')
  104. f1_7 = make_object(Blob, data='f1-2') # same data as in rev 2
  105. f2_1 = make_object(Blob, data='f2')
  106. f2_3 = make_object(Blob, data='f2-3')
  107. f3_3 = make_object(Blob, data='f3')
  108. f3_5 = make_object(Blob, data='f3-5')
  109. commit_spec = [[1], [2, 1], [3, 2], [4, 2], [5, 3], [6, 3, 4], [7, 6]]
  110. trees = {1: [('f1', f1_1), ('f2', f2_1)],
  111. 2: [('f1', f1_2), ('f2', f2_1)], # f1 changed
  112. # f3 added, f2 changed
  113. 3: [('f1', f1_2), ('f2', f2_3), ('f3', f3_3)],
  114. 4: [('f1', f1_4), ('f2', f2_1)], # f1 changed
  115. 5: [('f1', f1_2), ('f3', f3_5)], # f2 removed, f3 changed
  116. 6: [('f1', f1_4), ('f2', f2_3), ('f3', f3_3)], # merged 3 and 4
  117. # f1 changed to match rev2. f3 removed
  118. 7: [('f1', f1_7), ('f2', f2_3)]}
  119. self.commits = build_commit_graph(self.store, commit_spec, trees)
  120. self.f1_2_id = f1_2.id
  121. self.f1_4_id = f1_4.id
  122. self.f1_7_id = f1_7.id
  123. self.f2_3_id = f2_3.id
  124. self.f3_3_id = f3_3.id
  125. self.assertEquals(f1_2.id, f1_7.id, "[sanity]")
  126. def test_have6_want7(self):
  127. # have 6, want 7. Ideally, shall not report f1_7 as it's the same as
  128. # f1_2, however, to do so, MissingObjectFinder shall not record trees
  129. # of common commits only, but also all parent trees and tree items,
  130. # which is an overkill (i.e. in sha_done it records f1_4 as known, and
  131. # doesn't record f1_2 was known prior to that, hence can't detect f1_7
  132. # is in fact f1_2 and shall not be reported)
  133. self.assertMissingMatch([self.cmt(6).id], [self.cmt(7).id],
  134. [self.cmt(7).id, self.cmt(7).tree, self.f1_7_id])
  135. def test_have4_want7(self):
  136. # have 4, want 7. Shall not include rev5 as it is not in the tree
  137. # between 4 and 7 (well, it is, but its SHA's are irrelevant for 4..7
  138. # commit hierarchy)
  139. self.assertMissingMatch([self.cmt(4).id], [self.cmt(7).id], [
  140. self.cmt(7).id, self.cmt(6).id, self.cmt(3).id,
  141. self.cmt(7).tree, self.cmt(6).tree, self.cmt(3).tree,
  142. self.f2_3_id, self.f3_3_id])
  143. def test_have1_want6(self):
  144. # have 1, want 6. Shall not include rev5
  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,
  148. self.cmt(2).tree, self.f1_2_id, self.f1_4_id, self.f2_3_id,
  149. self.f3_3_id])
  150. def test_have3_want6(self):
  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. self.assertMissingMatch([self.cmt(3).id], [self.cmt(7).id], [
  156. self.cmt(7).id, self.cmt(6).id, self.cmt(4).id,
  157. self.cmt(7).tree, self.cmt(6).tree, self.cmt(4).tree,
  158. self.f1_4_id])
  159. def test_have5_want7(self):
  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. # expects f1_4 from rev6. f3_5 is known in rev5;
  163. # f1_7 shall be the same as f1_2 (known, too)
  164. self.assertMissingMatch([self.cmt(5).id], [self.cmt(7).id], [
  165. self.cmt(7).id, self.cmt(6).id, self.cmt(4).id,
  166. self.cmt(7).tree, self.cmt(6).tree, self.cmt(4).tree,
  167. self.f1_4_id])