server_utils.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. # server_utils.py -- Git server compatibility utilities
  2. # Copyright (C) 2010 Google, Inc.
  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. """Utilities for testing git server compatibility."""
  21. import errno
  22. import os
  23. import shutil
  24. import socket
  25. import tempfile
  26. from dulwich.objects import hex_to_sha
  27. from dulwich.protocol import CAPABILITY_SIDE_BAND_64K
  28. from dulwich.repo import Repo
  29. from dulwich.server import ReceivePackHandler
  30. from dulwich.tests.utils import tear_down_repo
  31. from .utils import require_git_version, run_git_or_fail
  32. class _StubRepo:
  33. """A stub repo that just contains a path to tear down."""
  34. def __init__(self, name) -> None:
  35. temp_dir = tempfile.mkdtemp()
  36. self.path = os.path.join(temp_dir, name)
  37. os.mkdir(self.path)
  38. def close(self):
  39. pass
  40. def _get_shallow(repo):
  41. shallow_file = repo.get_named_file("shallow")
  42. if not shallow_file:
  43. return []
  44. shallows = []
  45. with shallow_file:
  46. for line in shallow_file:
  47. sha = line.strip()
  48. if not sha:
  49. continue
  50. hex_to_sha(sha)
  51. shallows.append(sha)
  52. return shallows
  53. class ServerTests:
  54. """Base tests for testing servers.
  55. Does not inherit from TestCase so tests are not automatically run.
  56. """
  57. min_single_branch_version = (
  58. 1,
  59. 7,
  60. 10,
  61. )
  62. def import_repos(self):
  63. self._old_repo = self.import_repo("server_old.export")
  64. self._new_repo = self.import_repo("server_new.export")
  65. def url(self, port):
  66. return f"{self.protocol}://localhost:{port}/"
  67. def branch_args(self, branches=None):
  68. if branches is None:
  69. branches = ["master", "branch"]
  70. return [f"{b}:{b}" for b in branches]
  71. def test_push_to_dulwich(self):
  72. self.import_repos()
  73. self.assertReposNotEqual(self._old_repo, self._new_repo)
  74. port = self._start_server(self._old_repo)
  75. run_git_or_fail(
  76. ["push", self.url(port), *self.branch_args()],
  77. cwd=self._new_repo.path,
  78. )
  79. self.assertReposEqual(self._old_repo, self._new_repo)
  80. def test_push_to_dulwich_no_op(self):
  81. self._old_repo = self.import_repo("server_old.export")
  82. self._new_repo = self.import_repo("server_old.export")
  83. self.assertReposEqual(self._old_repo, self._new_repo)
  84. port = self._start_server(self._old_repo)
  85. run_git_or_fail(
  86. ["push", self.url(port), *self.branch_args()],
  87. cwd=self._new_repo.path,
  88. )
  89. self.assertReposEqual(self._old_repo, self._new_repo)
  90. def test_push_to_dulwich_remove_branch(self):
  91. self._old_repo = self.import_repo("server_old.export")
  92. self._new_repo = self.import_repo("server_old.export")
  93. self.assertReposEqual(self._old_repo, self._new_repo)
  94. port = self._start_server(self._old_repo)
  95. run_git_or_fail(["push", self.url(port), ":master"], cwd=self._new_repo.path)
  96. self.assertEqual(list(self._old_repo.get_refs().keys()), [b"refs/heads/branch"])
  97. def test_fetch_from_dulwich(self):
  98. self.import_repos()
  99. self.assertReposNotEqual(self._old_repo, self._new_repo)
  100. port = self._start_server(self._new_repo)
  101. run_git_or_fail(
  102. ["fetch", self.url(port), *self.branch_args()],
  103. cwd=self._old_repo.path,
  104. )
  105. # flush the pack cache so any new packs are picked up
  106. self._old_repo.object_store._pack_cache_time = 0
  107. self.assertReposEqual(self._old_repo, self._new_repo)
  108. def test_fetch_from_dulwich_no_op(self):
  109. self._old_repo = self.import_repo("server_old.export")
  110. self._new_repo = self.import_repo("server_old.export")
  111. self.assertReposEqual(self._old_repo, self._new_repo)
  112. port = self._start_server(self._new_repo)
  113. run_git_or_fail(
  114. ["fetch", self.url(port), *self.branch_args()],
  115. cwd=self._old_repo.path,
  116. )
  117. # flush the pack cache so any new packs are picked up
  118. self._old_repo.object_store._pack_cache_time = 0
  119. self.assertReposEqual(self._old_repo, self._new_repo)
  120. def test_clone_from_dulwich_empty(self):
  121. old_repo_dir = tempfile.mkdtemp()
  122. self.addCleanup(shutil.rmtree, old_repo_dir)
  123. self._old_repo = Repo.init_bare(old_repo_dir)
  124. port = self._start_server(self._old_repo)
  125. new_repo_base_dir = tempfile.mkdtemp()
  126. self.addCleanup(shutil.rmtree, new_repo_base_dir)
  127. new_repo_dir = os.path.join(new_repo_base_dir, "empty_new")
  128. run_git_or_fail(["clone", self.url(port), new_repo_dir], cwd=new_repo_base_dir)
  129. new_repo = Repo(new_repo_dir)
  130. self.assertReposEqual(self._old_repo, new_repo)
  131. def test_lsremote_from_dulwich(self):
  132. self._repo = self.import_repo("server_old.export")
  133. port = self._start_server(self._repo)
  134. o = run_git_or_fail(["ls-remote", self.url(port)])
  135. self.assertEqual(len(o.split(b"\n")), 4)
  136. def test_new_shallow_clone_from_dulwich(self):
  137. require_git_version(self.min_single_branch_version)
  138. self._source_repo = self.import_repo("server_new.export")
  139. self._stub_repo = _StubRepo("shallow")
  140. self.addCleanup(tear_down_repo, self._stub_repo)
  141. port = self._start_server(self._source_repo)
  142. # Fetch at depth 1
  143. run_git_or_fail(
  144. [
  145. "clone",
  146. "--mirror",
  147. "--depth=1",
  148. "--no-single-branch",
  149. self.url(port),
  150. self._stub_repo.path,
  151. ]
  152. )
  153. clone = self._stub_repo = Repo(self._stub_repo.path)
  154. expected_shallow = [
  155. b"35e0b59e187dd72a0af294aedffc213eaa4d03ff",
  156. b"514dc6d3fbfe77361bcaef320c4d21b72bc10be9",
  157. ]
  158. self.assertEqual(expected_shallow, _get_shallow(clone))
  159. self.assertReposNotEqual(clone, self._source_repo)
  160. def test_shallow_clone_from_git_is_identical(self):
  161. require_git_version(self.min_single_branch_version)
  162. self._source_repo = self.import_repo("server_new.export")
  163. self._stub_repo_git = _StubRepo("shallow-git")
  164. self.addCleanup(tear_down_repo, self._stub_repo_git)
  165. self._stub_repo_dw = _StubRepo("shallow-dw")
  166. self.addCleanup(tear_down_repo, self._stub_repo_dw)
  167. # shallow clone using stock git, then using dulwich
  168. run_git_or_fail(
  169. [
  170. "clone",
  171. "--mirror",
  172. "--depth=1",
  173. "--no-single-branch",
  174. "file://" + self._source_repo.path,
  175. self._stub_repo_git.path,
  176. ]
  177. )
  178. port = self._start_server(self._source_repo)
  179. run_git_or_fail(
  180. [
  181. "clone",
  182. "--mirror",
  183. "--depth=1",
  184. "--no-single-branch",
  185. self.url(port),
  186. self._stub_repo_dw.path,
  187. ]
  188. )
  189. # compare the two clones; they should be equal
  190. self.assertReposEqual(
  191. Repo(self._stub_repo_git.path), Repo(self._stub_repo_dw.path)
  192. )
  193. def test_fetch_same_depth_into_shallow_clone_from_dulwich(self):
  194. require_git_version(self.min_single_branch_version)
  195. self._source_repo = self.import_repo("server_new.export")
  196. self._stub_repo = _StubRepo("shallow")
  197. self.addCleanup(tear_down_repo, self._stub_repo)
  198. port = self._start_server(self._source_repo)
  199. # Fetch at depth 2
  200. run_git_or_fail(
  201. [
  202. "clone",
  203. "--mirror",
  204. "--depth=2",
  205. "--no-single-branch",
  206. self.url(port),
  207. self._stub_repo.path,
  208. ]
  209. )
  210. clone = self._stub_repo = Repo(self._stub_repo.path)
  211. # Fetching at the same depth is a no-op.
  212. run_git_or_fail(
  213. ["fetch", "--depth=2", self.url(port), *self.branch_args()],
  214. cwd=self._stub_repo.path,
  215. )
  216. expected_shallow = [
  217. b"94de09a530df27ac3bb613aaecdd539e0a0655e1",
  218. b"da5cd81e1883c62a25bb37c4d1f8ad965b29bf8d",
  219. ]
  220. self.assertEqual(expected_shallow, _get_shallow(clone))
  221. self.assertReposNotEqual(clone, self._source_repo)
  222. def test_fetch_full_depth_into_shallow_clone_from_dulwich(self):
  223. require_git_version(self.min_single_branch_version)
  224. self._source_repo = self.import_repo("server_new.export")
  225. self._stub_repo = _StubRepo("shallow")
  226. self.addCleanup(tear_down_repo, self._stub_repo)
  227. port = self._start_server(self._source_repo)
  228. # Fetch at depth 2
  229. run_git_or_fail(
  230. [
  231. "clone",
  232. "--mirror",
  233. "--depth=2",
  234. "--no-single-branch",
  235. self.url(port),
  236. self._stub_repo.path,
  237. ]
  238. )
  239. clone = self._stub_repo = Repo(self._stub_repo.path)
  240. # Fetching at the same depth is a no-op.
  241. run_git_or_fail(
  242. ["fetch", "--depth=2", self.url(port), *self.branch_args()],
  243. cwd=self._stub_repo.path,
  244. )
  245. # The whole repo only has depth 4, so it should equal server_new.
  246. run_git_or_fail(
  247. ["fetch", "--depth=4", self.url(port), *self.branch_args()],
  248. cwd=self._stub_repo.path,
  249. )
  250. self.assertEqual([], _get_shallow(clone))
  251. self.assertReposEqual(clone, self._source_repo)
  252. def test_fetch_from_dulwich_issue_88_standard(self):
  253. # Basically an integration test to see that the ACK/NAK
  254. # generation works on repos with common head.
  255. self._source_repo = self.import_repo("issue88_expect_ack_nak_server.export")
  256. self._client_repo = self.import_repo("issue88_expect_ack_nak_client.export")
  257. port = self._start_server(self._source_repo)
  258. run_git_or_fail(["fetch", self.url(port), "master"], cwd=self._client_repo.path)
  259. self.assertObjectStoreEqual(
  260. self._source_repo.object_store, self._client_repo.object_store
  261. )
  262. def test_fetch_from_dulwich_issue_88_alternative(self):
  263. # likewise, but the case where the two repos have no common parent
  264. self._source_repo = self.import_repo("issue88_expect_ack_nak_other.export")
  265. self._client_repo = self.import_repo("issue88_expect_ack_nak_client.export")
  266. port = self._start_server(self._source_repo)
  267. self.assertRaises(
  268. KeyError,
  269. self._client_repo.get_object,
  270. b"02a14da1fc1fc13389bbf32f0af7d8899f2b2323",
  271. )
  272. run_git_or_fail(["fetch", self.url(port), "master"], cwd=self._client_repo.path)
  273. self.assertEqual(
  274. b"commit",
  275. self._client_repo.get_object(
  276. b"02a14da1fc1fc13389bbf32f0af7d8899f2b2323"
  277. ).type_name,
  278. )
  279. def test_push_to_dulwich_issue_88_standard(self):
  280. # Same thing, but we reverse the role of the server/client
  281. # and do a push instead.
  282. self._source_repo = self.import_repo("issue88_expect_ack_nak_client.export")
  283. self._client_repo = self.import_repo("issue88_expect_ack_nak_server.export")
  284. port = self._start_server(self._source_repo)
  285. run_git_or_fail(["push", self.url(port), "master"], cwd=self._client_repo.path)
  286. self.assertReposEqual(self._source_repo, self._client_repo)
  287. # TODO(dborowitz): Come up with a better way of testing various permutations of
  288. # capabilities. The only reason it is the way it is now is that side-band-64k
  289. # was only recently introduced into git-receive-pack.
  290. class NoSideBand64kReceivePackHandler(ReceivePackHandler):
  291. """ReceivePackHandler that does not support side-band-64k."""
  292. @classmethod
  293. def capabilities(cls):
  294. return [
  295. c
  296. for c in ReceivePackHandler.capabilities()
  297. if c != CAPABILITY_SIDE_BAND_64K
  298. ]
  299. def ignore_error(error):
  300. """Check whether this error is safe to ignore."""
  301. (e_type, e_value, e_tb) = error
  302. return issubclass(e_type, socket.error) and e_value[0] in (
  303. errno.ECONNRESET,
  304. errno.EPIPE,
  305. )